/[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.31 by sysadm, Mon Nov 17 12:16:48 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                    str_process.c  -  description  /*
3                                                           -------------------   * str_process
4          Copyright            : (C) 2004-2025 by Leaflet   *   - common string processing features with UTF-8 support
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
 #include "str_process.h"  
13  #include "common.h"  #include "common.h"
14  #include "log.h"  #include "log.h"
15    #include "str_process.h"
16    #include <ctype.h>
17  #include <stdio.h>  #include <stdio.h>
18    #include <stdlib.h>
19  #include <string.h>  #include <string.h>
20    #include <wchar.h>
21    
22  unsigned int split_line(const char *buffer, int max_len, int *p_eol, int *p_display_len)  int UTF8_fixed_width = 1;
 {  
         size_t len = strnlen(buffer, LINE_BUFFER_LEN);  
         unsigned int i = 0;  
         *p_eol = 0;  
         *p_display_len = 0;  
23    
24          if (len == 0)  int str_length(const char *str, int skip_ctrl_seq)
25          {  {
26                  return 0;          int str_len;
27          }          char input_str[5];
28            wchar_t wcs[2];
29            int wc_len;
30            int i;
31            char c;
32            int ret = 0;
33    
34          for (; i < len; i++)          for (i = 0; str[i] != '\0'; i++)
35          {          {
36                  char c = buffer[i];                  c = str[i];
37    
38                  if (c == '\r' || c == '\7') // skip                  if (c == '\r' || c == '\7') // skip
39                  {                  {
40                          continue;                          continue;
41                  }                  }
42    
43                  if (c == '\n')                  if (skip_ctrl_seq && c == '\033' && str[i + 1] == '[') // Skip control sequence
44                    {
45                            for (i = i + 2; isdigit((int)str[i]) || str[i] == ';' || str[i] == '?'; i++)
46                                    ;
47    
48                            if (str[i] == 'm') // valid
49                            {
50                                    // skip
51                            }
52                            else if (isalpha((int)str[i]))
53                            {
54                                    // unsupported ANSI CSI command
55                            }
56                            else
57                            {
58                                    i--;
59                            }
60    
61                            continue;
62                    }
63    
64                    // Process UTF-8 Chinese characters
65                    if (c & 0x80) // head of multi-byte character
66                    {
67                            str_len = 0;
68                            c = (char)(c & 0xf0);
69                            while (c & 0x80)
70                            {
71                                    input_str[str_len] = str[i + str_len];
72                                    str_len++;
73                                    c = (c & 0x7f) << 1;
74                            }
75                            input_str[str_len] = '\0';
76    
77                            if (mbstowcs(wcs, input_str, 1) == (size_t)-1)
78                            {
79    #ifdef _DEBUG
80                                    log_error("mbstowcs(%s) error\n", input_str);
81    #endif
82                                    wc_len = (UTF8_fixed_width ? 2 : 1); // Fallback
83                            }
84                            else
85                            {
86                                    wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
87                            }
88    
89                            i += (str_len - 1);
90                            ret += wc_len;
91                    }
92                    else
93                    {
94                            ret++;
95                    }
96            }
97    
98            return ret;
99    }
100    
101    int split_line(const char *buffer, int max_display_len, int *p_eol, int *p_display_len, int skip_ctrl_seq)
102    {
103            int i;
104            *p_eol = 0;
105            *p_display_len = 0;
106            char c;
107            int str_len;
108            char input_str[5];
109            wchar_t wcs[2];
110            int wc_len;
111    
112            for (i = 0; buffer[i] != '\0'; i++)
113            {
114                    c = buffer[i];
115    
116                    if (c == '\r' || c == '\7') // skip
117                  {                  {
118                          i++;                          continue;
                         *p_eol = 1;  
                         break;  
119                  }                  }
120    
121                  if (c == '\033' && buffer[i + 1] == '[') // Skip control sequence                  if (skip_ctrl_seq && c == '\033' && buffer[i + 1] == '[') // Skip control sequence
122                  {                  {
123                          i += 2;                          i += 2;
124                          while (i < len && buffer[i] != 'm')                          while (buffer[i] != '\0' && buffer[i] != 'm')
125                          {                          {
126                                  i++;                                  i++;
127                          }                          }
128                          continue;                          continue;
129                  }                  }
130    
131                  if (c > 127 && c <= 255) // GBK chinese character                  if (c & 0x80) // head of multi-byte character
132                  {                  {
133                          if (*p_display_len + 2 > max_len)                          str_len = 0;
134                            c = (char)(c & 0xf0);
135                            while (c & 0x80)
136                            {
137                                    input_str[str_len] = buffer[i + str_len];
138                                    str_len++;
139                                    c = (c & 0x7f) << 1;
140                            }
141                            input_str[str_len] = '\0';
142    
143                            if (mbstowcs(wcs, input_str, 1) == (size_t)-1)
144                            {
145    #ifdef _DEBUG
146                                    log_error("mbstowcs(%s) error\n", input_str);
147    #endif
148                                    wc_len = (UTF8_fixed_width ? 2 : 1); // Fallback
149                            }
150                            else
151                            {
152                                    wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
153                            }
154    
155                            if (*p_display_len + wc_len > max_display_len)
156                          {                          {
                                 *p_eol = 1;  
157                                  break;                                  break;
158                          }                          }
159                          i++;  
160                          *p_display_len += 2;                          i += (str_len - 1);
161                            (*p_display_len) += wc_len;
162                  }                  }
163                  else                  else
164                  {                  {
165                          if (*p_display_len + 1 > max_len)                          if (*p_display_len + 1 > max_display_len)
166                          {                          {
                                 *p_eol = 1;  
167                                  break;                                  break;
168                          }                          }
169                          (*p_display_len)++;                          (*p_display_len)++;
170    
171                            // \n is regarded as 1 character wide in terminal editor, which is different from Web version
172                            if (c == '\n')
173                            {
174                                    i++;
175                                    *p_eol = 1;
176                                    break;
177                            }
178                  }                  }
179          }          }
180    
181          return i;          return i;
182  }  }
183    
184  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,
185                                              int skip_ctrl_seq, int *p_line_widths)
186  {  {
187          char buffer[LINE_BUFFER_LEN];          int line_cnt = 0;
188          char *p_buf = buffer;          int len;
         unsigned int line_cnt = 0;  
         unsigned int len = 0;  
189          int end_of_line = 0;          int end_of_line = 0;
190          int display_len = 0;          int display_len = 0;
191    
192          p_line_offsets[line_cnt] = 0L;          p_line_offsets[line_cnt] = 0L;
193    
194          while (fgets(p_buf, (int)(sizeof(buffer) - len), fin))          do
195          {          {
196                  p_buf = buffer;                  len = split_line(p_buf, max_display_len, &end_of_line, &display_len, skip_ctrl_seq);
197                  while (1)  
198                    if (p_line_widths)
199                  {                  {
200                          len = split_line(p_buf, max_len, &end_of_line, &display_len);                          p_line_widths[line_cnt] = display_len;
201                    }
202    
203                          if (len == 0 || !end_of_line) // !end_of_line == EOF                  // Exceed max_line_cnt
204                          {                  if (line_cnt + 1 >= line_offsets_count)
205                                  break;                  {
206                          }                          // log_error("Line count %d reaches limit %d\n", line_cnt + 1, line_offsets_count);
207                            return line_cnt;
208                    }
209    
210                          // Exceed max_line_cnt                  p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len;
211                          if (line_cnt + 1 >= max_line_cnt)                  line_cnt++;
212                          {                  p_buf += len;
213                                  log_error("File line count %d reaches limit\n", line_cnt + 1);          } while (p_buf[0] != '\0' || end_of_line);
214                                  return line_cnt;  
215                          }          return line_cnt;
216    }
217    
218    int str_filter(char *buffer, int skip_ctrl_seq)
219    {
220            int i;
221            int j;
222    
223                          p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len;          for (i = 0, j = 0; buffer[i] != '\0'; i++)
224                          line_cnt++;          {
225                          p_buf += len;                  if (buffer[i] == '\r' || buffer[i] == '\7') // skip
226                    {
227                            continue;
228                  }                  }
229    
230                  // 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++)  
231                  {                  {
232                          buffer[i] = p_buf[i];                          i += 2;
233                            while (buffer[i] != '\0' && buffer[i] != 'm')
234                            {
235                                    i++;
236                            }
237                            continue;
238                  }                  }
                 p_buf = buffer + len;  
         }  
239    
240          if (len > 0 && line_cnt + 1 < max_line_cnt)                  buffer[j] = buffer[i];
241          {                  j++;
                 p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len;  
                 line_cnt++;  
242          }          }
243    
244          return line_cnt;          buffer[j] = '\0';
245    
246            return j;
247  }  }


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

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