--- lbbs/src/str_process.c 2025/05/13 02:19:49 1.5 +++ lbbs/src/str_process.c 2025/05/15 14:17:53 1.8 @@ -20,19 +20,13 @@ #include #include -unsigned int split_line(const char *buffer, int max_len, int *p_eol, int *p_display_len) +int split_line(const char *buffer, int max_display_len, int *p_eol, int *p_display_len) { - size_t len = strnlen(buffer, LINE_BUFFER_LEN); - unsigned int i = 0; + int i; *p_eol = 0; *p_display_len = 0; - if (len == 0) - { - return 0; - } - - for (; i < len; i++) + for (i = 0; buffer[i] != '\0'; i++) { char c = buffer[i]; @@ -51,7 +45,7 @@ unsigned int split_line(const char *buff if (c == '\033' && buffer[i + 1] == '[') // Skip control sequence { i += 2; - while (i < len && buffer[i] != 'm') + while (buffer[i] != '\0' && buffer[i] != 'm') { i++; } @@ -60,7 +54,7 @@ unsigned int split_line(const char *buff if (c > 127 && c <= 255) // GBK chinese character { - if (*p_display_len + 2 > max_len) + if (*p_display_len + 2 > max_display_len) { *p_eol = 1; break; @@ -70,7 +64,7 @@ unsigned int split_line(const char *buff } else { - if (*p_display_len + 1 > max_len) + if (*p_display_len + 1 > max_display_len) { *p_eol = 1; break; @@ -82,47 +76,34 @@ unsigned int split_line(const char *buff return i; } -unsigned int split_file_lines(FILE *fin, int max_len, long *p_line_offsets, int max_line_cnt) +int split_data_lines(const char *p_buf, int max_display_len, long *p_line_offsets, int max_line_cnt) { - char buffer[LINE_BUFFER_LEN]; - char *p_buf = buffer; - unsigned int line_cnt = 0; - unsigned int len = 0; + int line_cnt = 0; + int len = 0; int end_of_line = 0; int display_len = 0; p_line_offsets[line_cnt] = 0L; - while (fgets(p_buf, (int)(sizeof(buffer) - len), fin)) + while (1) { - p_buf = buffer; - while (1) - { - len = split_line(p_buf, max_len, &end_of_line, &display_len); - - if (len == 0 || !end_of_line) // !end_of_line == EOF - { - break; - } - - // Exceed max_line_cnt - if (line_cnt + 1 >= max_line_cnt) - { - log_error("File line count %d reaches limit\n", line_cnt + 1); - return line_cnt; - } + len = split_line(p_buf, max_display_len, &end_of_line, &display_len); - p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len; - line_cnt++; - p_buf += len; + if (len == 0 || !end_of_line) // !end_of_line == EOF + { + break; } - // Move p_buf[0 .. len - 1] to head of buffer - for (int i = 0; i < len; i++) + // Exceed max_line_cnt + if (line_cnt + 1 >= max_line_cnt) { - buffer[i] = p_buf[i]; + log_error("File line count %d reaches limit\n", line_cnt + 1); + return line_cnt; } - p_buf = buffer + len; + + p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len; + line_cnt++; + p_buf += len; } if (len > 0 && line_cnt + 1 < max_line_cnt)