| 9 |
* * |
* * |
| 10 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
| 11 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
| 12 |
* the Free Software Foundation; either version 2 of the License, or * |
* the Free Software Foundation; either version 3 of the License, or * |
| 13 |
* (at your option) any later version. * |
* (at your option) any later version. * |
| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
|
#include "str_process.h" |
|
| 17 |
#include "common.h" |
#include "common.h" |
| 18 |
#include "log.h" |
#include "log.h" |
| 19 |
|
#include "str_process.h" |
| 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 str_length(const char *str, int skip_ctrl_seq) |
| 24 |
{ |
{ |
| 25 |
size_t len = strnlen(buffer, LINE_BUFFER_LEN); |
int i; |
| 26 |
int display_len = 0; |
char c; |
| 27 |
unsigned int i = 0; |
int ret = 0; |
|
*p_eol = 0; |
|
|
|
|
|
if (len == 0) |
|
|
{ |
|
|
return 0; |
|
|
} |
|
| 28 |
|
|
| 29 |
for (; i < len; i++) |
for (i = 0; str[i] != '\0'; i++) |
| 30 |
{ |
{ |
| 31 |
char c = buffer[i]; |
c = str[i]; |
| 32 |
|
|
| 33 |
if (c == '\r' || c == '\7') // skip |
if (c == '\r' || c == '\7') // skip |
| 34 |
{ |
{ |
| 35 |
continue; |
continue; |
| 36 |
} |
} |
| 37 |
|
|
| 38 |
if (c == '\n') |
if (skip_ctrl_seq && c == '\033' && str[i + 1] == '[') // Skip control sequence |
| 39 |
{ |
{ |
| 40 |
i++; |
i += 2; |
| 41 |
*p_eol = 1; |
while (str[i] != '\0' && str[i] != 'm') |
| 42 |
break; |
{ |
| 43 |
|
i++; |
| 44 |
|
} |
| 45 |
|
continue; |
| 46 |
} |
} |
| 47 |
|
|
| 48 |
if (c == '\033' && buffer[i + 1] == '[') // Skip control sequence |
// Process UTF-8 Chinese characters |
| 49 |
|
if (c & 0x80) // head of multi-byte character |
| 50 |
|
{ |
| 51 |
|
c = (c & 0x70) << 1; |
| 52 |
|
while (c & 0x80) |
| 53 |
|
{ |
| 54 |
|
i++; |
| 55 |
|
c = (c & 0x7f) << 1; |
| 56 |
|
} |
| 57 |
|
|
| 58 |
|
ret += 2; |
| 59 |
|
} |
| 60 |
|
else |
| 61 |
|
{ |
| 62 |
|
ret++; |
| 63 |
|
} |
| 64 |
|
} |
| 65 |
|
|
| 66 |
|
return ret; |
| 67 |
|
} |
| 68 |
|
|
| 69 |
|
int split_line(const char *buffer, int max_display_len, int *p_eol, int *p_display_len, int skip_ctrl_seq) |
| 70 |
|
{ |
| 71 |
|
int i; |
| 72 |
|
*p_eol = 0; |
| 73 |
|
*p_display_len = 0; |
| 74 |
|
char c; |
| 75 |
|
|
| 76 |
|
for (i = 0; buffer[i] != '\0'; i++) |
| 77 |
|
{ |
| 78 |
|
c = buffer[i]; |
| 79 |
|
|
| 80 |
|
if (c == '\r' || c == '\7') // skip |
| 81 |
|
{ |
| 82 |
|
continue; |
| 83 |
|
} |
| 84 |
|
|
| 85 |
|
if (skip_ctrl_seq && c == '\033' && buffer[i + 1] == '[') // Skip control sequence |
| 86 |
{ |
{ |
| 87 |
i += 2; |
i += 2; |
| 88 |
while (i < len && buffer[i] != 'm') |
while (buffer[i] != '\0' && buffer[i] != 'm') |
| 89 |
{ |
{ |
| 90 |
i++; |
i++; |
| 91 |
} |
} |
| 92 |
continue; |
continue; |
| 93 |
} |
} |
| 94 |
|
|
| 95 |
if (c > 127 && c <= 255) // GBK chinese character |
if (c & 0x80) // head of multi-byte character |
| 96 |
{ |
{ |
| 97 |
if (display_len + 2 > max_len) |
if (*p_display_len + 2 > max_display_len) |
| 98 |
{ |
{ |
|
*p_eol = 1; |
|
| 99 |
break; |
break; |
| 100 |
} |
} |
| 101 |
i++; |
|
| 102 |
display_len += 2; |
c = (c & 0x70) << 1; |
| 103 |
|
while (c & 0x80) |
| 104 |
|
{ |
| 105 |
|
i++; |
| 106 |
|
c = (c & 0x7f) << 1; |
| 107 |
|
} |
| 108 |
|
|
| 109 |
|
(*p_display_len) += 2; |
| 110 |
} |
} |
| 111 |
else |
else |
| 112 |
{ |
{ |
| 113 |
if (display_len >= max_len) |
if (*p_display_len + 1 > max_display_len) |
| 114 |
|
{ |
| 115 |
|
break; |
| 116 |
|
} |
| 117 |
|
(*p_display_len)++; |
| 118 |
|
|
| 119 |
|
// \n is regarded as 1 character wide in terminal editor, which is different from Web version |
| 120 |
|
if (c == '\n') |
| 121 |
{ |
{ |
| 122 |
|
i++; |
| 123 |
*p_eol = 1; |
*p_eol = 1; |
| 124 |
break; |
break; |
| 125 |
} |
} |
|
display_len++; |
|
| 126 |
} |
} |
| 127 |
} |
} |
| 128 |
|
|
| 129 |
return i; |
return i; |
| 130 |
} |
} |
| 131 |
|
|
| 132 |
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, |
| 133 |
|
int skip_ctrl_seq, int *p_line_widths) |
| 134 |
{ |
{ |
| 135 |
char buffer[LINE_BUFFER_LEN]; |
int line_cnt = 0; |
| 136 |
char *p_buf = buffer; |
int len; |
|
unsigned int line_cnt = 0; |
|
|
unsigned int len = 0; |
|
| 137 |
int end_of_line = 0; |
int end_of_line = 0; |
| 138 |
|
int display_len = 0; |
| 139 |
|
|
| 140 |
p_line_offsets[line_cnt] = 0L; |
p_line_offsets[line_cnt] = 0L; |
| 141 |
|
|
| 142 |
while (fgets(p_buf, (int)(sizeof(buffer) - len), fin)) |
do |
| 143 |
{ |
{ |
| 144 |
p_buf = buffer; |
len = split_line(p_buf, max_display_len, &end_of_line, &display_len, skip_ctrl_seq); |
| 145 |
while (1) |
|
| 146 |
|
if (p_line_widths) |
| 147 |
{ |
{ |
| 148 |
len = split_line(p_buf, max_len, &end_of_line); |
p_line_widths[line_cnt] = display_len; |
| 149 |
|
} |
| 150 |
|
|
| 151 |
if (len == 0 || !end_of_line) |
// Exceed max_line_cnt |
| 152 |
{ |
if (line_cnt + 1 >= line_offsets_count) |
| 153 |
break; |
{ |
| 154 |
} |
// log_error("Line count %d reaches limit %d\n", line_cnt + 1, line_offsets_count); |
| 155 |
|
return line_cnt; |
| 156 |
|
} |
| 157 |
|
|
| 158 |
// Exceed max_line_cnt |
p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len; |
| 159 |
if (line_cnt + 1 >= max_line_cnt) |
line_cnt++; |
| 160 |
{ |
p_buf += len; |
| 161 |
log_error("File line count %d reaches limit\n", line_cnt + 1); |
} while (p_buf[0] != '\0'); |
|
return line_cnt; |
|
|
} |
|
| 162 |
|
|
| 163 |
p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len; |
return line_cnt; |
| 164 |
line_cnt++; |
} |
| 165 |
p_buf += len; |
|
| 166 |
|
int str_filter(char *buffer, int skip_ctrl_seq) |
| 167 |
|
{ |
| 168 |
|
int i; |
| 169 |
|
int j; |
| 170 |
|
|
| 171 |
|
for (i = 0, j = 0; buffer[i] != '\0'; i++) |
| 172 |
|
{ |
| 173 |
|
if (buffer[i] == '\r' || buffer[i] == '\7') // skip |
| 174 |
|
{ |
| 175 |
|
continue; |
| 176 |
} |
} |
| 177 |
|
|
| 178 |
// Move p_buf[0 .. len - 1] to head of buffer |
if (skip_ctrl_seq && buffer[i] == '\033' && buffer[i + 1] == '[') // Skip control sequence |
|
for (int i = 0; i < len; i++) |
|
| 179 |
{ |
{ |
| 180 |
buffer[i] = p_buf[i]; |
i += 2; |
| 181 |
|
while (buffer[i] != '\0' && buffer[i] != 'm') |
| 182 |
|
{ |
| 183 |
|
i++; |
| 184 |
|
} |
| 185 |
|
continue; |
| 186 |
} |
} |
|
p_buf = buffer + len; |
|
|
} |
|
| 187 |
|
|
| 188 |
if (len > 0 && line_cnt + 1 < max_line_cnt) |
buffer[j] = buffer[i]; |
| 189 |
{ |
j++; |
|
p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len; |
|
|
line_cnt++; |
|
| 190 |
} |
} |
| 191 |
|
|
| 192 |
return line_cnt; |
buffer[j] = '\0'; |
| 193 |
|
|
| 194 |
|
return j; |
| 195 |
} |
} |