/[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.10 by sysadm, Wed May 28 05:46:27 2025 UTC Revision 1.19 by sysadm, Wed Jul 2 03:08:10 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 <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;
27          *p_display_len = 0;          *p_display_len = 0;
28            char c;
29    
30          for (i = 0; buffer[i] != '\0'; i++)          for (i = 0; buffer[i] != '\0'; i++)
31          {          {
32                  char c = buffer[i];                  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' && buffer[i + 1] == '[') // Skip control sequence
                 {  
                         i++;  
                         *p_eol = 1;  
                         break;  
                 }  
   
                 if (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')
# Line 68  int split_line(const char *buffer, int m Line 62  int split_line(const char *buffer, int m
62                                  break;                                  break;
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
67                            if (c == '\n')
68                            {
69                                    i++;
70                                    *p_eol = 1;
71                                    break;
72                            }
73                  }                  }
74          }          }
75    
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,
80                                              int skip_ctrl_seq, int *p_line_widths)
81  {  {
82          int line_cnt = 0;          int line_cnt = 0;
83          int len = 0;          int len;
84          int end_of_line = 0;          int end_of_line = 0;
85          int display_len = 0;          int display_len = 0;
86    
87          p_line_offsets[line_cnt] = 0L;          p_line_offsets[line_cnt] = 0L;
88    
89          while (1)          do
90          {          {
91                  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);
92    
93                  if (len == 0) // EOF                  if (p_line_widths)
94                  {                  {
95                          break;                          p_line_widths[line_cnt] = display_len;
96                  }                  }
97    
98                  // Exceed max_line_cnt                  // Exceed max_line_cnt
99                  if (line_cnt + 1 >= line_offsets_count)                  if (line_cnt + 1 >= line_offsets_count)
100                  {                  {
101                          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);
102                          return line_cnt;                          return line_cnt;
103                  }                  }
104    
105                  p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len;                  p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len;
106                  line_cnt++;                  line_cnt++;
107                  p_buf += len;                  p_buf += len;
108          }          } while (p_buf[0] != '\0');
109    
110          return line_cnt;          return line_cnt;
111  }  }
112    
113    int str_filter(char *buffer, int skip_ctrl_seq)
114    {
115            int i;
116            int j;
117    
118            for (i = 0, j = 0; buffer[i] != '\0'; i++)
119            {
120                    if (buffer[i] == '\r' || buffer[i] == '\7') // skip
121                    {
122                            continue;
123                    }
124    
125                    if (skip_ctrl_seq && buffer[i] == '\033' && buffer[i + 1] == '[') // Skip control sequence
126                    {
127                            i += 2;
128                            while (buffer[i] != '\0' && buffer[i] != 'm')
129                            {
130                                    i++;
131                            }
132                            continue;
133                    }
134    
135                    buffer[j] = buffer[i];
136                    j++;
137            }
138    
139            buffer[j] = '\0';
140    
141            return j;
142    }


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

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