bool read_host_cfg(char **data, size_t *line_count) { loff_t size_ = 0; if (!read_cfg(WL_NETWORK, data, &size_)) return false; tok_str(data, '\n'); // 根据 '\0' 确定行数更加准确 *line_count = get_line_count(*data, size_); return true; }
int main(int argc,char **argv) { if (argc != 9) { fprintf(stderr,"usage: %s <ITEMS> <CS> <p1> <p2> <p3> <p1> <p2> <p3>\n",argv[0]);exit(1); } itemFileName=argv[1]; cFileName=argv[2]; iP1=atoi(argv[3]); iP2=atoi(argv[4]); iP3=atoi(argv[5]); cP1=atoi(argv[6]); cP2=atoi(argv[7]); cP3=atoi(argv[8]); if (iP1 != -1) iP1--; if (iP2 != -1) iP2--; if (iP3 != -1) iP3--; if (cP1 != -1) cP1--; if (cP2 != -1) cP2--; if (cP3 != -1) cP3--; mType=get_type(cP1,cP2,cP3,iP1,iP2,iP3); //printf("mType=%d\n",mType); cMxCnt=get_line_count(cFileName); // needed to allocate memory cMxLgh=8; // for now, just to allocate memory and read the items cCnt=read_data(cFileName,cMxCnt,cMxLgh,maxPos(cP1,cP2,cP3),&cs); // read C items cMxLgh=strlen(cs[0]); // assume all C items are same length buildAlphabet(cs,cCnt,cMxLgh); // assign unique serial number to each letter used in C items buildIndex(&cIdx,cs,cCnt,mType,cP1,cP2,cP3); // calculate an index for each C items (unique at up to MLET letters) buildHash(mType); // build hash table with C items init_hits(&hits,cCnt); fprintf(stderr,"prep done\n"); match_items_cs(mType); // match GIGA file data to C items using hash table dump_hits(hits,cs,cCnt); }
BC_PHYSICSIMP_DLL core::bc_vector< bc_scene_debug_line > bc_platform_scene_debug<g_api_physx>::get_lines() const noexcept { core::bc_vector< bc_scene_debug_line > result; auto* l_px_lines = m_pack.m_px_debug->getLines(); auto l_line_count = get_line_count(); result.reserve(l_line_count); for (bcUINT32 i = 0; i < l_line_count; ++i) { result.push_back(bc_scene_debug_line ( bc_to_game_hand(l_px_lines->pos0), l_px_lines->color0, bc_to_game_hand(l_px_lines->pos1), l_px_lines->color1 )); } return result; }
static void get_log(struct evbuffer *rsps) { char *log = get_file_path("log"); FILE *fd = fopen(log, "r"); if (fd == NULL) return; int lines = get_line_count(fd); int start = lines - 50; if (start < 0) start = 0; int i; char buf[MAX_URL_LEN]; for (i = 0; i < start; i++) fgets(buf, MAX_URL_LEN, fd); while (fgets(buf, MAX_URL_LEN, fd) != NULL) evbuffer_add_printf(rsps, "%s", buf); fclose(fd); }
int main(int argc, char *argv[]) { char *filename, *buffer; int filelen, nlines, bufsize, tlines, nlen, fd; char opt; ssize_t n; nlines = DEFAULT_LINES; while ((opt = getopt(argc, argv, "n:")) != -1) { switch(opt) { case 'n': nlines = atoi(optarg); break; default: fprintf(stderr, "Usage: mtail [-n <lines>] <filename>\n"); exit(EXIT_FAILURE); } } if (optind < argc) { nlen = strlen(argv[optind]); filename = malloc(nlen + 1); strncpy(filename, argv[optind], nlen + 1); fd = open(filename, O_RDONLY); if (fd == -1) { perror("open"); exit(EXIT_FAILURE); } } else { fprintf(stderr, "Usage: mtail [-n <lines>] <filename>\n"); exit(EXIT_FAILURE); } if ((filelen = (int)lseek(fd, 0, SEEK_END)) == -1) { perror("lseek"); exit(EXIT_FAILURE); } bufsize = BUFSIZE; while (1) { bufsize = min(bufsize, filelen); if (lseek(fd, -bufsize, SEEK_END) == -1) { perror("lseek"); exit(EXIT_FAILURE); } buffer = alloca(bufsize + 1); n = read(fd, buffer, bufsize); buffer[n] = '\0'; tlines = get_line_count(buffer); if (tlines >= nlines || filelen == bufsize) { if (nlines < tlines) buffer = trim_lines(buffer, nlines, tlines); write(STDOUT_FILENO, buffer, strlen(buffer)); break; } bufsize *= 2; } exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { buffer_t* new_buffer; FILE *source; long buffer_start_offset; long cur_line_count; long initial_offset; long bytes_to_read; long offset; long requested_line_count = DEFAULT_N_LINES; int inotify_fd = -1; // parse the command line parse_options(argc, argv, &requested_line_count); requested_line_count++; if (optind + 1 != argc) { usage(EXIT_FAILURE); } // open the file source = fopen(argv[optind], "rb"); if (source == NULL) { printf("fopen %s failed %d\n", argv[optind], errno); goto error; } if (forever) { // set an inotify watch inotify_fd = inotify_init(); if (inotify_fd == -1) { printf("inotify_init failed %d", errno); goto error; } if (inotify_add_watch(inotify_fd, argv[optind], IN_MODIFY) == -1) { printf("inotify_add_watch failed %d", errno); goto error; } } // seek to the end if (fseek(source, 0, SEEK_END) == -1) { printf("fseek failed (1) %d\n", errno); goto error; } initial_offset = ftell(source); if (initial_offset == -1) { printf("ftell failed %d\n", errno); goto error; } offset = initial_offset; initialize_list_head(&buffer_queue); for (;;) { if (offset > CHUNK_SIZE_READ) { bytes_to_read = CHUNK_SIZE_READ; } else { bytes_to_read = offset; } offset -= bytes_to_read; if (offset + MEMORY_LIMIT < initial_offset) { printf("memory limit exceeded\n"); goto error; } // allocate a buffer new_buffer = (buffer_t*)malloc(sizeof(buffer_t)); if (new_buffer == NULL) { printf("malloc failed (1)\n"); goto error; } new_buffer->data = malloc(bytes_to_read); if (new_buffer->data == NULL) { printf("malloc failed (2)\n"); goto error; } insert_head_list(&buffer_queue, &new_buffer->node); new_buffer->offset = offset; new_buffer->size = bytes_to_read; // read from the file if (fseek(source, offset, SEEK_SET) == -1) { printf("fseek failed (2) %d\n", errno); goto error; } if (fread(new_buffer->data, 1, bytes_to_read, source) != bytes_to_read) { printf("fread failed %d\n", errno); goto error; } cur_line_count = get_line_count(new_buffer, &buffer_start_offset); if (cur_line_count >= requested_line_count) { // read enough data, seek to the initial offset and print the result if (fseek(source, initial_offset, SEEK_SET) == -1) { printf("fseek failed (3) %d\n", errno); goto error; } return print_lines(source, inotify_fd, new_buffer, buffer_start_offset, cur_line_count - requested_line_count + 1); } if (offset > 0) { continue; } if (cur_line_count <= 0) { goto error; } // reached the beginning of the file, seek to the initial offset and print the result if (fseek(source, initial_offset, SEEK_SET) == -1) { printf("fseek failed (4) %d\n", errno); goto error; } return print_lines(source, inotify_fd, new_buffer, buffer_start_offset, 0); } return 0; error: // not bothering to clean up anything since the process quits return 1; }