| 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 |
|
|
| 29 |
if (len == 0) |
for (i = 0; buffer[i] != '\0'; i++) |
|
{ |
|
|
return 0; |
|
|
} |
|
|
|
|
|
for (; i < len; i++) |
|
| 30 |
{ |
{ |
| 31 |
char c = buffer[i]; |
char c = buffer[i]; |
| 32 |
|
|
| 45 |
if (c == '\033' && buffer[i + 1] == '[') // Skip control sequence |
if (c == '\033' && buffer[i + 1] == '[') // Skip control sequence |
| 46 |
{ |
{ |
| 47 |
i += 2; |
i += 2; |
| 48 |
while (i < len && buffer[i] != 'm') |
while (buffer[i] != '\0' && buffer[i] != 'm') |
| 49 |
{ |
{ |
| 50 |
i++; |
i++; |
| 51 |
} |
} |
| 54 |
|
|
| 55 |
if (c > 127 && c <= 255) // GBK chinese character |
if (c > 127 && c <= 255) // GBK chinese character |
| 56 |
{ |
{ |
| 57 |
if (*p_display_len + 2 > max_len) |
if (*p_display_len + 2 > max_display_len) |
| 58 |
{ |
{ |
| 59 |
*p_eol = 1; |
*p_eol = 1; |
| 60 |
break; |
break; |
| 64 |
} |
} |
| 65 |
else |
else |
| 66 |
{ |
{ |
| 67 |
if (*p_display_len + 1 > max_len) |
if (*p_display_len + 1 > max_display_len) |
| 68 |
{ |
{ |
| 69 |
*p_eol = 1; |
*p_eol = 1; |
| 70 |
break; |
break; |
| 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) |
int split_file_lines(FILE *fin, int max_display_len, long *p_line_offsets, int max_line_cnt) |
| 80 |
{ |
{ |
| 81 |
char buffer[LINE_BUFFER_LEN]; |
char buffer[LINE_BUFFER_LEN]; |
| 82 |
char *p_buf = buffer; |
char *p_buf = buffer; |
| 83 |
unsigned int line_cnt = 0; |
int line_cnt = 0; |
| 84 |
unsigned int len = 0; |
int len = 0; |
| 85 |
int end_of_line = 0; |
int end_of_line = 0; |
| 86 |
int display_len = 0; |
int display_len = 0; |
| 87 |
|
|
| 88 |
p_line_offsets[line_cnt] = 0L; |
p_line_offsets[line_cnt] = 0L; |
| 89 |
|
|
| 90 |
while (fgets(p_buf, (int)(sizeof(buffer) - len), fin)) |
while (fgets(p_buf, (int)sizeof(buffer) - len, fin)) |
| 91 |
{ |
{ |
| 92 |
p_buf = buffer; |
p_buf = buffer; |
| 93 |
while (1) |
while (1) |
| 94 |
{ |
{ |
| 95 |
len = split_line(p_buf, max_len, &end_of_line, &display_len); |
len = split_line(p_buf, max_display_len, &end_of_line, &display_len); |
| 96 |
|
|
| 97 |
if (len == 0 || !end_of_line) |
if (len == 0 || !end_of_line) // !end_of_line == EOF |
| 98 |
{ |
{ |
| 99 |
break; |
break; |
| 100 |
} |
} |