| 20 |
#include <stdio.h> |
#include <stdio.h> |
| 21 |
#include <string.h> |
#include <string.h> |
| 22 |
|
|
| 23 |
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) |
| 24 |
{ |
{ |
| 25 |
size_t len = strnlen(buffer, LINE_BUFFER_LEN); |
int i; |
|
unsigned int i = 0; |
|
| 26 |
*p_eol = 0; |
*p_eol = 0; |
| 27 |
*p_display_len = 0; |
*p_display_len = 0; |
| 28 |
|
char c; |
| 29 |
|
|
| 30 |
if (len == 0) |
for (i = 0; buffer[i] != '\0'; i++) |
| 31 |
{ |
{ |
| 32 |
return 0; |
c = buffer[i]; |
|
} |
|
|
|
|
|
for (; i < len; i++) |
|
|
{ |
|
|
char c = buffer[i]; |
|
| 33 |
|
|
| 34 |
if (c == '\r' || c == '\7') // skip |
if (c == '\r' || c == '\7') // skip |
| 35 |
{ |
{ |
| 36 |
continue; |
continue; |
| 37 |
} |
} |
| 38 |
|
|
|
if (c == '\n') |
|
|
{ |
|
|
i++; |
|
|
*p_eol = 1; |
|
|
break; |
|
|
} |
|
|
|
|
| 39 |
if (c == '\033' && buffer[i + 1] == '[') // Skip control sequence |
if (c == '\033' && buffer[i + 1] == '[') // Skip control sequence |
| 40 |
{ |
{ |
| 41 |
i += 2; |
i += 2; |
| 42 |
while (i < len && buffer[i] != 'm') |
while (buffer[i] != '\0' && buffer[i] != 'm') |
| 43 |
{ |
{ |
| 44 |
i++; |
i++; |
| 45 |
} |
} |
| 46 |
continue; |
continue; |
| 47 |
} |
} |
| 48 |
|
|
| 49 |
if (c > 127 && c <= 255) // GBK chinese character |
if (c < 0 || c > 127) // GBK chinese character |
| 50 |
{ |
{ |
| 51 |
if (*p_display_len + 2 > max_len) |
if (*p_display_len + 2 > max_display_len) |
| 52 |
{ |
{ |
|
*p_eol = 1; |
|
| 53 |
break; |
break; |
| 54 |
} |
} |
| 55 |
i++; |
i++; |
| 56 |
*p_display_len += 2; |
(*p_display_len) += 2; |
| 57 |
} |
} |
| 58 |
else |
else |
| 59 |
{ |
{ |
| 60 |
if (*p_display_len + 1 > max_len) |
if (*p_display_len + 1 > max_display_len) |
| 61 |
{ |
{ |
|
*p_eol = 1; |
|
| 62 |
break; |
break; |
| 63 |
} |
} |
| 64 |
(*p_display_len)++; |
(*p_display_len)++; |
| 65 |
|
|
| 66 |
|
// \n is regarded as 1 character wide in terminal editor, which is different from Web version |
| 67 |
|
if (c == '\n') |
| 68 |
|
{ |
| 69 |
|
i++; |
| 70 |
|
*p_eol = 1; |
| 71 |
|
break; |
| 72 |
|
} |
| 73 |
} |
} |
| 74 |
} |
} |
| 75 |
|
|
| 76 |
return i; |
return i; |
| 77 |
} |
} |
| 78 |
|
|
| 79 |
unsigned int split_file_lines(FILE *fin, int max_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) |
| 80 |
{ |
{ |
| 81 |
char buffer[LINE_BUFFER_LEN]; |
int line_cnt = 0; |
| 82 |
char *p_buf = buffer; |
int len; |
|
unsigned int line_cnt = 0; |
|
|
unsigned int len = 0; |
|
| 83 |
int end_of_line = 0; |
int end_of_line = 0; |
| 84 |
int display_len = 0; |
int display_len = 0; |
| 85 |
|
|
| 86 |
p_line_offsets[line_cnt] = 0L; |
p_line_offsets[line_cnt] = 0L; |
| 87 |
|
|
| 88 |
while (fgets(p_buf, (int)(sizeof(buffer) - len), fin)) |
do |
| 89 |
{ |
{ |
| 90 |
p_buf = buffer; |
len = split_line(p_buf, max_display_len, &end_of_line, &display_len); |
| 91 |
while (1) |
|
| 92 |
|
// Exceed max_line_cnt |
| 93 |
|
if (line_cnt + 1 >= line_offsets_count) |
| 94 |
{ |
{ |
| 95 |
len = split_line(p_buf, max_len, &end_of_line, &display_len); |
log_error("Line count %d reaches limit %d\n", line_cnt + 1, line_offsets_count); |
| 96 |
|
return line_cnt; |
| 97 |
|
} |
| 98 |
|
|
| 99 |
if (len == 0 || !end_of_line) // !end_of_line == EOF |
p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len; |
| 100 |
{ |
line_cnt++; |
| 101 |
break; |
p_buf += len; |
| 102 |
} |
} while (p_buf[0] != '\0'); |
| 103 |
|
|
| 104 |
// Exceed max_line_cnt |
return line_cnt; |
| 105 |
if (line_cnt + 1 >= max_line_cnt) |
} |
|
{ |
|
|
log_error("File line count %d reaches limit\n", line_cnt + 1); |
|
|
return line_cnt; |
|
|
} |
|
| 106 |
|
|
| 107 |
p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len; |
int ctrl_seq_filter(char *buffer) |
| 108 |
line_cnt++; |
{ |
| 109 |
p_buf += len; |
int i; |
| 110 |
|
int j; |
| 111 |
|
|
| 112 |
|
for (i = 0, j = 0; buffer[i] != '\0'; i++) |
| 113 |
|
{ |
| 114 |
|
if (buffer[i] == '\r' || buffer[i] == '\7') // skip |
| 115 |
|
{ |
| 116 |
|
continue; |
| 117 |
} |
} |
| 118 |
|
|
| 119 |
// Move p_buf[0 .. len - 1] to head of buffer |
if (buffer[i] == '\033' && buffer[i + 1] == '[') // Skip control sequence |
|
for (int i = 0; i < len; i++) |
|
| 120 |
{ |
{ |
| 121 |
buffer[i] = p_buf[i]; |
i += 2; |
| 122 |
|
while (buffer[i] != '\0' && buffer[i] != 'm') |
| 123 |
|
{ |
| 124 |
|
i++; |
| 125 |
|
} |
| 126 |
|
continue; |
| 127 |
} |
} |
|
p_buf = buffer + len; |
|
|
} |
|
| 128 |
|
|
| 129 |
if (len > 0 && line_cnt + 1 < max_line_cnt) |
buffer[j] = buffer[i]; |
| 130 |
{ |
j++; |
|
p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len; |
|
|
line_cnt++; |
|
| 131 |
} |
} |
| 132 |
|
|
| 133 |
return line_cnt; |
buffer[j] = '\0'; |
| 134 |
|
|
| 135 |
|
return j; |
| 136 |
} |
} |