/[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.13 by sysadm, Fri Jun 13 10:52:07 2025 UTC Revision 1.29 by sysadm, Mon Nov 10 12:53:16 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     */
 /***************************************************************************  
  *                                                                         *  
  *   This program is free software; you can redistribute it and/or modify  *  
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
 #include "str_process.h"  
9  #include "common.h"  #include "common.h"
10  #include "log.h"  #include "log.h"
11    #include "str_process.h"
12    #include <ctype.h>
13  #include <stdio.h>  #include <stdio.h>
14    #include <stdlib.h>
15  #include <string.h>  #include <string.h>
16    #include <wchar.h>
17    
18    int UTF8_fixed_width = 1;
19    
20  int split_line(const char *buffer, int max_display_len, int *p_eol, int *p_display_len)  int str_length(const char *str, int skip_ctrl_seq)
21    {
22            int str_len;
23            char input_str[5];
24            wchar_t wcs[2];
25            int wc_len;
26            int i;
27            char c;
28            int ret = 0;
29    
30            for (i = 0; str[i] != '\0'; i++)
31            {
32                    c = str[i];
33    
34                    if (c == '\r' || c == '\7') // skip
35                    {
36                            continue;
37                    }
38    
39                    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                            str_len = 0;
64                            c = (char)(c & 0xf0);
65                            while (c & 0x80)
66                            {
67                                    input_str[str_len] = str[i + str_len];
68                                    str_len++;
69                                    c = (c & 0x7f) << 1;
70                            }
71                            input_str[str_len] = '\0';
72    
73                            if (mbstowcs(wcs, input_str, 1) == (size_t)-1)
74                            {
75    #ifdef _DEBUG
76                                    log_error("mbstowcs(%s) error\n", input_str);
77    #endif
78                                    wc_len = (UTF8_fixed_width ? 2 : 1); // Fallback
79                            }
80                            else
81                            {
82                                    wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
83                            }
84    
85                            i += (str_len - 1);
86                            ret += wc_len;
87                    }
88                    else
89                    {
90                            ret++;
91                    }
92            }
93    
94            return ret;
95    }
96    
97    int split_line(const char *buffer, int max_display_len, int *p_eol, int *p_display_len, int skip_ctrl_seq)
98  {  {
99          int i;          int i;
100          *p_eol = 0;          *p_eol = 0;
101          *p_display_len = 0;          *p_display_len = 0;
102          char c;          char c;
103            int str_len;
104            char input_str[5];
105            wchar_t wcs[2];
106            int wc_len;
107    
108          for (i = 0; buffer[i] != '\0'; i++)          for (i = 0; buffer[i] != '\0'; i++)
109          {          {
# Line 36  int split_line(const char *buffer, int m Line 114  int split_line(const char *buffer, int m
114                          continue;                          continue;
115                  }                  }
116    
117                  if (c == '\033' && buffer[i + 1] == '[') // Skip control sequence                  if (skip_ctrl_seq && c == '\033' && buffer[i + 1] == '[') // Skip control sequence
118                  {                  {
119                          i += 2;                          i += 2;
120                          while (buffer[i] != '\0' && buffer[i] != 'm')                          while (buffer[i] != '\0' && buffer[i] != 'm')
# Line 46  int split_line(const char *buffer, int m Line 124  int split_line(const char *buffer, int m
124                          continue;                          continue;
125                  }                  }
126    
127                  if (c < 0 || c > 127) // GBK chinese character                  if (c & 0x80) // head of multi-byte character
128                  {                  {
129                          if (*p_display_len + 2 > max_display_len)                          str_len = 0;
130                            c = (char)(c & 0xf0);
131                            while (c & 0x80)
132                            {
133                                    input_str[str_len] = buffer[i + str_len];
134                                    str_len++;
135                                    c = (c & 0x7f) << 1;
136                            }
137                            input_str[str_len] = '\0';
138    
139                            if (mbstowcs(wcs, input_str, 1) == (size_t)-1)
140                            {
141    #ifdef _DEBUG
142                                    log_error("mbstowcs(%s) error\n", input_str);
143    #endif
144                                    wc_len = (UTF8_fixed_width ? 2 : 1); // Fallback
145                            }
146                            else
147                            {
148                                    wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
149                            }
150    
151                            if (*p_display_len + wc_len > max_display_len)
152                          {                          {
153                                  break;                                  break;
154                          }                          }
155                          i++;  
156                          (*p_display_len) += 2;                          i += (str_len - 1);
157                            (*p_display_len) += wc_len;
158                  }                  }
159                  else                  else
160                  {                  {
# Line 63  int split_line(const char *buffer, int m Line 164  int split_line(const char *buffer, int m
164                          }                          }
165                          (*p_display_len)++;                          (*p_display_len)++;
166    
167                           // \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
168                          if (c == '\n')                          if (c == '\n')
169                          {                          {
170                                  i++;                                  i++;
# Line 76  int split_line(const char *buffer, int m Line 177  int split_line(const char *buffer, int m
177          return i;          return i;
178  }  }
179    
180  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,
181                                              int skip_ctrl_seq, int *p_line_widths)
182  {  {
183          int line_cnt = 0;          int line_cnt = 0;
184          int len;          int len;
# Line 87  long split_data_lines(const char *p_buf, Line 189  long split_data_lines(const char *p_buf,
189    
190          do          do
191          {          {
192                  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);
193    
194                    if (p_line_widths)
195                    {
196                            p_line_widths[line_cnt] = display_len;
197                    }
198    
199                  // Exceed max_line_cnt                  // Exceed max_line_cnt
200                  if (line_cnt + 1 >= line_offsets_count)                  if (line_cnt + 1 >= line_offsets_count)
201                  {                  {
202                          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);
203                          return line_cnt;                          return line_cnt;
204                  }                  }
205    
206                  p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len;                  p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len;
207                  line_cnt++;                  line_cnt++;
208                  p_buf += len;                  p_buf += len;
209          } while (p_buf[0] != '\0');          } while (p_buf[0] != '\0' || end_of_line);
210    
211          return line_cnt;          return line_cnt;
212  }  }
213    
214    int str_filter(char *buffer, int skip_ctrl_seq)
215    {
216            int i;
217            int j;
218    
219            for (i = 0, j = 0; buffer[i] != '\0'; i++)
220            {
221                    if (buffer[i] == '\r' || buffer[i] == '\7') // skip
222                    {
223                            continue;
224                    }
225    
226                    if (skip_ctrl_seq && buffer[i] == '\033' && buffer[i + 1] == '[') // Skip control sequence
227                    {
228                            i += 2;
229                            while (buffer[i] != '\0' && buffer[i] != 'm')
230                            {
231                                    i++;
232                            }
233                            continue;
234                    }
235    
236                    buffer[j] = buffer[i];
237                    j++;
238            }
239    
240            buffer[j] = '\0';
241    
242            return j;
243    }


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

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