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

Annotation of /lbbs/src/str_process.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (hide annotations)
Thu May 15 13:02:33 2025 UTC (10 months ago) by sysadm
Branch: MAIN
Changes since 1.6: +8 -14 lines
Content type: text/x-csrc
Refine

1 sysadm 1.1 /***************************************************************************
2     str_process.c - description
3     -------------------
4     Copyright : (C) 2004-2025 by Leaflet
5     Email : leaflet@leafok.com
6     ***************************************************************************/
7    
8     /***************************************************************************
9     * *
10     * 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 *
12 sysadm 1.3 * the Free Software Foundation; either version 3 of the License, or *
13 sysadm 1.1 * (at your option) any later version. *
14     * *
15     ***************************************************************************/
16    
17     #include "str_process.h"
18     #include "common.h"
19     #include "log.h"
20     #include <stdio.h>
21     #include <string.h>
22    
23 sysadm 1.7 int split_line(const char *buffer, int max_display_len, int *p_eol, int *p_display_len)
24 sysadm 1.1 {
25 sysadm 1.7 int i;
26 sysadm 1.1 *p_eol = 0;
27 sysadm 1.4 *p_display_len = 0;
28 sysadm 1.1
29 sysadm 1.7 for (i = 0; buffer[i] != '\0'; i++)
30 sysadm 1.1 {
31     char c = buffer[i];
32    
33     if (c == '\r' || c == '\7') // skip
34     {
35     continue;
36     }
37    
38     if (c == '\n')
39     {
40     i++;
41     *p_eol = 1;
42     break;
43     }
44 sysadm 1.2
45 sysadm 1.1 if (c == '\033' && buffer[i + 1] == '[') // Skip control sequence
46     {
47     i += 2;
48 sysadm 1.7 while (buffer[i] != '\0' && buffer[i] != 'm')
49 sysadm 1.1 {
50     i++;
51     }
52     continue;
53     }
54    
55     if (c > 127 && c <= 255) // GBK chinese character
56     {
57 sysadm 1.6 if (*p_display_len + 2 > max_display_len)
58 sysadm 1.1 {
59     *p_eol = 1;
60     break;
61     }
62     i++;
63 sysadm 1.4 *p_display_len += 2;
64 sysadm 1.1 }
65     else
66     {
67 sysadm 1.6 if (*p_display_len + 1 > max_display_len)
68 sysadm 1.1 {
69     *p_eol = 1;
70     break;
71     }
72 sysadm 1.4 (*p_display_len)++;
73 sysadm 1.1 }
74     }
75    
76     return i;
77     }
78    
79 sysadm 1.7 int split_file_lines(FILE *fin, int max_display_len, long *p_line_offsets, int max_line_cnt)
80 sysadm 1.1 {
81     char buffer[LINE_BUFFER_LEN];
82     char *p_buf = buffer;
83 sysadm 1.7 int line_cnt = 0;
84     int len = 0;
85 sysadm 1.1 int end_of_line = 0;
86 sysadm 1.4 int display_len = 0;
87    
88 sysadm 1.1 p_line_offsets[line_cnt] = 0L;
89    
90 sysadm 1.7 while (fgets(p_buf, (int)sizeof(buffer) - len, fin))
91 sysadm 1.1 {
92     p_buf = buffer;
93     while (1)
94     {
95 sysadm 1.6 len = split_line(p_buf, max_display_len, &end_of_line, &display_len);
96 sysadm 1.1
97 sysadm 1.5 if (len == 0 || !end_of_line) // !end_of_line == EOF
98 sysadm 1.1 {
99     break;
100     }
101    
102     // Exceed max_line_cnt
103     if (line_cnt + 1 >= max_line_cnt)
104     {
105     log_error("File line count %d reaches limit\n", line_cnt + 1);
106     return line_cnt;
107     }
108    
109     p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len;
110     line_cnt++;
111     p_buf += len;
112     }
113    
114     // Move p_buf[0 .. len - 1] to head of buffer
115     for (int i = 0; i < len; i++)
116     {
117     buffer[i] = p_buf[i];
118     }
119     p_buf = buffer + len;
120     }
121    
122     if (len > 0 && line_cnt + 1 < max_line_cnt)
123     {
124     p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len;
125     line_cnt++;
126     }
127    
128     return line_cnt;
129     }

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