| 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 |
int split_line(const char *buffer, int max_display_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, int skip_ctrl_seq) |
| 24 |
{ |
{ |
| 25 |
int i; |
int i; |
| 26 |
*p_eol = 0; |
*p_eol = 0; |
| 36 |
continue; |
continue; |
| 37 |
} |
} |
| 38 |
|
|
| 39 |
if (c == '\033' && buffer[i + 1] == '[') // Skip control sequence |
if (skip_ctrl_seq && c == '\033' && buffer[i + 1] == '[') // Skip control sequence |
| 40 |
{ |
{ |
| 41 |
i += 2; |
i += 2; |
| 42 |
while (buffer[i] != '\0' && buffer[i] != 'm') |
while (buffer[i] != '\0' && buffer[i] != 'm') |
| 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 |
// \n is regarded as 1 character wide in terminal editor, which is different from Web version |
| 67 |
if (c == '\n') |
if (c == '\n') |
| 68 |
{ |
{ |
| 69 |
i++; |
i++; |
| 76 |
return i; |
return i; |
| 77 |
} |
} |
| 78 |
|
|
| 79 |
long split_data_lines(const char *p_buf, int max_display_len, long *p_line_offsets, long line_offsets_count) |
long split_data_lines(const char *p_buf, int max_display_len, long *p_line_offsets, long line_offsets_count, int skip_ctrl_seq) |
| 80 |
{ |
{ |
| 81 |
int line_cnt = 0; |
int line_cnt = 0; |
| 82 |
int len = 0; |
int len; |
| 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 (1) |
do |
| 89 |
{ |
{ |
| 90 |
len = split_line(p_buf, max_display_len, &end_of_line, &display_len); |
len = split_line(p_buf, max_display_len, &end_of_line, &display_len, skip_ctrl_seq); |
|
|
|
|
if (len == 0) // EOF |
|
|
{ |
|
|
break; |
|
|
} |
|
| 91 |
|
|
| 92 |
// Exceed max_line_cnt |
// Exceed max_line_cnt |
| 93 |
if (line_cnt + 1 >= line_offsets_count) |
if (line_cnt + 1 >= line_offsets_count) |
| 94 |
{ |
{ |
| 95 |
log_error("Line count %d reaches limit %d\n", line_cnt + 1, line_offsets_count); |
// log_error("Line count %d reaches limit %d\n", line_cnt + 1, line_offsets_count); |
| 96 |
return line_cnt; |
return line_cnt; |
| 97 |
} |
} |
| 98 |
|
|
| 99 |
p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len; |
p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len; |
| 100 |
line_cnt++; |
line_cnt++; |
| 101 |
p_buf += len; |
p_buf += len; |
| 102 |
} |
} while (p_buf[0] != '\0'); |
| 103 |
|
|
| 104 |
return line_cnt; |
return line_cnt; |
| 105 |
} |
} |
| 106 |
|
|
| 107 |
|
int str_filter(char *buffer, int skip_ctrl_seq) |
| 108 |
|
{ |
| 109 |
|
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 |
|
if (skip_ctrl_seq && buffer[i] == '\033' && buffer[i + 1] == '[') // Skip control sequence |
| 120 |
|
{ |
| 121 |
|
i += 2; |
| 122 |
|
while (buffer[i] != '\0' && buffer[i] != 'm') |
| 123 |
|
{ |
| 124 |
|
i++; |
| 125 |
|
} |
| 126 |
|
continue; |
| 127 |
|
} |
| 128 |
|
|
| 129 |
|
buffer[j] = buffer[i]; |
| 130 |
|
j++; |
| 131 |
|
} |
| 132 |
|
|
| 133 |
|
buffer[j] = '\0'; |
| 134 |
|
|
| 135 |
|
return j; |
| 136 |
|
} |