/[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.1 by sysadm, Sat May 3 06:25:17 2025 UTC Revision 1.10 by sysadm, Wed May 28 05:46:27 2025 UTC
# Line 9  Line 9 
9   *                                                                         *   *                                                                         *
10   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
11   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
12   *   the Free Software Foundation; either version 2 of the License, or     *   *   the Free Software Foundation; either version 3 of the License, or     *
13   *   (at your option) any later version.                                   *   *   (at your option) any later version.                                   *
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
# Line 20  Line 20 
20  #include <stdio.h>  #include <stdio.h>
21  #include <string.h>  #include <string.h>
22    
23  int split_line(const char *buffer, int max_len, int *p_eol)  int split_line(const char *buffer, int max_display_len, int *p_eol, int *p_display_len)
24  {  {
25          int len = strnlen(buffer, LINE_BUFFER_LEN);          int i;
         int display_len = 0;  
         int i = 0;  
26          *p_eol = 0;          *p_eol = 0;
27            *p_display_len = 0;
28    
29          if (len == 0)          for (i = 0; buffer[i] != '\0'; i++)
         {  
                 return 0;  
         }  
   
         for (; i < len; i++)  
30          {          {
31                  char c = buffer[i];                  char c = buffer[i];
32    
# Line 47  int split_line(const char *buffer, int m Line 41  int split_line(const char *buffer, int m
41                          *p_eol = 1;                          *p_eol = 1;
42                          break;                          break;
43                  }                  }
44                    
45                  if (c == '\033' && buffer[i + 1] == '[') // Skip control sequence                  if (c == '\033' && buffer[i + 1] == '[') // Skip control sequence
46                  {                  {
47                          i += 2;                          i += 2;
48                          while(i < len && buffer[i] != 'm')                          while (buffer[i] != '\0' && buffer[i] != 'm')
49                          {                          {
50                                  i++;                                  i++;
51                          }                          }
52                          continue;                          continue;
53                  }                  }
54    
55                  if (c > 127 && c <= 255) // GBK chinese character                  if (c < 0 || c > 127) // GBK chinese character
56                  {                  {
57                          if (display_len + 2 > max_len)                          if (*p_display_len + 2 > max_display_len)
58                          {                          {
                                 *p_eol = 1;  
59                                  break;                                  break;
60                          }                          }
61                          i++;                          i++;
62                          display_len += 2;                          (*p_display_len) += 2;
63                  }                  }
64                  else                  else
65                  {                  {
66                          if (display_len >= max_len)                          if (*p_display_len + 1 > max_display_len)
67                          {                          {
                                 *p_eol = 1;  
68                                  break;                                  break;
69                          }                          }
70                          display_len++;                          (*p_display_len)++;
71                  }                  }
72          }          }
73    
74          return i;          return i;
75  }  }
76    
77  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)
78  {  {
         char buffer[LINE_BUFFER_LEN];  
         char *p_buf = buffer;  
79          int line_cnt = 0;          int line_cnt = 0;
80          int len = 0;          int len = 0;
81          int end_of_line = 0;          int end_of_line = 0;
82            int display_len = 0;
83    
84          p_line_offsets[line_cnt] = 0L;          p_line_offsets[line_cnt] = 0L;
85    
86          while (fgets(p_buf, sizeof(buffer) - len, fin))          while (1)
87          {          {
88                  p_buf = buffer;                  len = split_line(p_buf, max_display_len, &end_of_line, &display_len);
                 while (1)  
                 {  
                         len = split_line(p_buf, max_len, &end_of_line);  
89    
90                          if (len == 0 || !end_of_line)                  if (len == 0) // EOF
91                          {                  {
92                                  break;                          break;
                         }  
   
                         // Exceed max_line_cnt  
                         if (line_cnt + 1 >= max_line_cnt)  
                         {  
                                 log_error("File line count %d reaches limit\n", line_cnt + 1);  
                                 return line_cnt;  
                         }  
   
                         p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len;  
                         line_cnt++;  
                         p_buf += len;  
93                  }                  }
94    
95                  // Move p_buf[0 .. len - 1] to head of buffer                  // Exceed max_line_cnt
96                  for (int i = 0; i < len; i++)                  if (line_cnt + 1 >= line_offsets_count)
97                  {                  {
98                          buffer[i] = p_buf[i];                          log_error("Line count %d reaches limit %d\n", line_cnt + 1, line_offsets_count);
99                            return line_cnt;
100                  }                  }
                 p_buf = buffer + len;  
         }  
101    
         if (len > 0 && line_cnt + 1 < max_line_cnt)  
         {  
102                  p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len;                  p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len;
103                  line_cnt++;                  line_cnt++;
104                    p_buf += len;
105          }          }
106    
107          return line_cnt;          return line_cnt;


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

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