--- lbbs/src/str_process.c 2025/05/15 13:02:33 1.7 +++ lbbs/src/str_process.c 2025/06/14 02:58:11 1.14 @@ -25,23 +25,17 @@ int split_line(const char *buffer, int m int i; *p_eol = 0; *p_display_len = 0; + char c; for (i = 0; buffer[i] != '\0'; i++) { - char c = buffer[i]; + c = buffer[i]; if (c == '\r' || c == '\7') // skip { continue; } - if (c == '\n') - { - i++; - *p_eol = 1; - break; - } - if (c == '\033' && buffer[i + 1] == '[') // Skip control sequence { i += 2; @@ -52,78 +46,94 @@ int split_line(const char *buffer, int m continue; } - if (c > 127 && c <= 255) // GBK chinese character + if (c < 0 || c > 127) // GBK chinese character { if (*p_display_len + 2 > max_display_len) { - *p_eol = 1; break; } i++; - *p_display_len += 2; + (*p_display_len) += 2; } else { if (*p_display_len + 1 > max_display_len) { - *p_eol = 1; break; } (*p_display_len)++; + + // \n is regarded as 1 character wide in terminal editor, which is different from Web version + if (c == '\n') + { + i++; + *p_eol = 1; + break; + } } } return i; } -int split_file_lines(FILE *fin, int max_display_len, long *p_line_offsets, int max_line_cnt) +long split_data_lines(const char *p_buf, int max_display_len, long *p_line_offsets, long line_offsets_count) { - char buffer[LINE_BUFFER_LEN]; - char *p_buf = buffer; int line_cnt = 0; - int len = 0; + int len; int end_of_line = 0; int display_len = 0; p_line_offsets[line_cnt] = 0L; - while (fgets(p_buf, (int)sizeof(buffer) - len, fin)) + do { - p_buf = buffer; - while (1) + len = split_line(p_buf, max_display_len, &end_of_line, &display_len); + + // Exceed max_line_cnt + if (line_cnt + 1 >= line_offsets_count) { - len = split_line(p_buf, max_display_len, &end_of_line, &display_len); + log_error("Line count %d reaches limit %d\n", line_cnt + 1, line_offsets_count); + return line_cnt; + } - if (len == 0 || !end_of_line) // !end_of_line == EOF - { - break; - } + p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len; + line_cnt++; + p_buf += len; + } while (p_buf[0] != '\0'); - // 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; - } + return line_cnt; +} + +int ctrl_seq_filter(char *buffer) +{ + int i; + int j; + char c; + + for (i = 0, j = 0; buffer[i] != '\0'; i++) + { + c = buffer[i]; - p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len; - line_cnt++; - p_buf += len; + if (c == '\r' || c == '\7') // skip + { + continue; } - // Move p_buf[0 .. len - 1] to head of buffer - for (int i = 0; i < len; i++) + if (c == '\033' && buffer[i + 1] == '[') // Skip control sequence { - buffer[i] = p_buf[i]; + i += 2; + while (buffer[i] != '\0' && buffer[i] != 'm') + { + i++; + } + continue; } - p_buf = buffer + len; - } - if (len > 0 && line_cnt + 1 < max_line_cnt) - { - p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len; - line_cnt++; + buffer[j] = buffer[i]; + j++; } - return line_cnt; + buffer[j] = '\0'; + + return j; }