/[LeafOK_CVS]/lbbs/src/str_process.c
ViewVC logotype

Diff of /lbbs/src/str_process.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.5 by sysadm, Tue May 13 02:19:49 2025 UTC Revision 1.22 by sysadm, Fri Oct 31 06:04:17 2025 UTC
# Line 14  Line 14 
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 <ctype.h>
21  #include <stdio.h>  #include <stdio.h>
22  #include <string.h>  #include <string.h>
23    
24  unsigned int split_line(const char *buffer, int max_len, int *p_eol, int *p_display_len)  int str_length(const char *str, int skip_ctrl_seq)
25  {  {
26          size_t len = strnlen(buffer, LINE_BUFFER_LEN);          int i;
27          unsigned int i = 0;          char c;
28          *p_eol = 0;          int ret = 0;
         *p_display_len = 0;  
29    
30          if (len == 0)          for (i = 0; str[i] != '\0'; i++)
31          {          {
32                  return 0;                  c = str[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    
39                  if (c == '\n')                  if (skip_ctrl_seq && c == '\033' && str[i + 1] == '[') // Skip control sequence
40                    {
41                            for (i = i + 2; isdigit(str[i]) || str[i] == ';' || str[i] == '?'; i++)
42                                    ;
43    
44                            if (str[i] == 'm') // valid
45                            {
46                                    // skip
47                            }
48                            else if (isalpha(str[i]))
49                            {
50                                    // unsupported ANSI CSI command
51                            }
52                            else
53                            {
54                                    i--;
55                            }
56    
57                            continue;
58                    }
59    
60                    // Process UTF-8 Chinese characters
61                    if (c & 0x80) // head of multi-byte character
62                    {
63                            c = (c & 0x70) << 1;
64                            while (c & 0x80)
65                            {
66                                    i++;
67                                    c = (c & 0x7f) << 1;
68                            }
69    
70                            ret += 2;
71                    }
72                    else
73                    {
74                            ret++;
75                    }
76            }
77    
78            return ret;
79    }
80    
81    int split_line(const char *buffer, int max_display_len, int *p_eol, int *p_display_len, int skip_ctrl_seq)
82    {
83            int i;
84            *p_eol = 0;
85            *p_display_len = 0;
86            char c;
87    
88            for (i = 0; buffer[i] != '\0'; i++)
89            {
90                    c = buffer[i];
91    
92                    if (c == '\r' || c == '\7') // skip
93                  {                  {
94                          i++;                          continue;
                         *p_eol = 1;  
                         break;  
95                  }                  }
96    
97                  if (c == '\033' && buffer[i + 1] == '[') // Skip control sequence                  if (skip_ctrl_seq && c == '\033' && buffer[i + 1] == '[') // Skip control sequence
98                  {                  {
99                          i += 2;                          i += 2;
100                          while (i < len && buffer[i] != 'm')                          while (buffer[i] != '\0' && buffer[i] != 'm')
101                          {                          {
102                                  i++;                                  i++;
103                          }                          }
104                          continue;                          continue;
105                  }                  }
106    
107                  if (c > 127 && c <= 255) // GBK chinese character                  if (c & 0x80) // head of multi-byte character
108                  {                  {
109                          if (*p_display_len + 2 > max_len)                          if (*p_display_len + 2 > max_display_len)
110                          {                          {
                                 *p_eol = 1;  
111                                  break;                                  break;
112                          }                          }
113                          i++;  
114                          *p_display_len += 2;                          c = (c & 0x70) << 1;
115                            while (c & 0x80)
116                            {
117                                    i++;
118                                    c = (c & 0x7f) << 1;
119                            }
120    
121                            (*p_display_len) += 2;
122                  }                  }
123                  else                  else
124                  {                  {
125                          if (*p_display_len + 1 > max_len)                          if (*p_display_len + 1 > max_display_len)
126                          {                          {
                                 *p_eol = 1;  
127                                  break;                                  break;
128                          }                          }
129                          (*p_display_len)++;                          (*p_display_len)++;
130    
131                            // \n is regarded as 1 character wide in terminal editor, which is different from Web version
132                            if (c == '\n')
133                            {
134                                    i++;
135                                    *p_eol = 1;
136                                    break;
137                            }
138                  }                  }
139          }          }
140    
141          return i;          return i;
142  }  }
143    
144  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,
145                                              int skip_ctrl_seq, int *p_line_widths)
146  {  {
147          char buffer[LINE_BUFFER_LEN];          int line_cnt = 0;
148          char *p_buf = buffer;          int len;
         unsigned int line_cnt = 0;  
         unsigned int len = 0;  
149          int end_of_line = 0;          int end_of_line = 0;
150          int display_len = 0;          int display_len = 0;
151    
152          p_line_offsets[line_cnt] = 0L;          p_line_offsets[line_cnt] = 0L;
153    
154          while (fgets(p_buf, (int)(sizeof(buffer) - len), fin))          do
155          {          {
156                  p_buf = buffer;                  len = split_line(p_buf, max_display_len, &end_of_line, &display_len, skip_ctrl_seq);
157                  while (1)  
158                    if (p_line_widths)
159                  {                  {
160                          len = split_line(p_buf, max_len, &end_of_line, &display_len);                          p_line_widths[line_cnt] = display_len;
161                    }
162    
163                          if (len == 0 || !end_of_line) // !end_of_line == EOF                  // Exceed max_line_cnt
164                          {                  if (line_cnt + 1 >= line_offsets_count)
165                                  break;                  {
166                          }                          // log_error("Line count %d reaches limit %d\n", line_cnt + 1, line_offsets_count);
167                            return line_cnt;
168                    }
169    
170                          // Exceed max_line_cnt                  p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len;
171                          if (line_cnt + 1 >= max_line_cnt)                  line_cnt++;
172                          {                  p_buf += len;
173                                  log_error("File line count %d reaches limit\n", line_cnt + 1);          } while (p_buf[0] != '\0');
174                                  return line_cnt;  
175                          }          return line_cnt;
176    }
177    
178    int str_filter(char *buffer, int skip_ctrl_seq)
179    {
180            int i;
181            int j;
182    
183                          p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len;          for (i = 0, j = 0; buffer[i] != '\0'; i++)
184                          line_cnt++;          {
185                          p_buf += len;                  if (buffer[i] == '\r' || buffer[i] == '\7') // skip
186                    {
187                            continue;
188                  }                  }
189    
190                  // 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++)  
191                  {                  {
192                          buffer[i] = p_buf[i];                          i += 2;
193                            while (buffer[i] != '\0' && buffer[i] != 'm')
194                            {
195                                    i++;
196                            }
197                            continue;
198                  }                  }
                 p_buf = buffer + len;  
         }  
199    
200          if (len > 0 && line_cnt + 1 < max_line_cnt)                  buffer[j] = buffer[i];
201          {                  j++;
                 p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len;  
                 line_cnt++;  
202          }          }
203    
204          return line_cnt;          buffer[j] = '\0';
205    
206            return j;
207  }  }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1