/[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.6 - (hide annotations)
Thu May 15 12:05:32 2025 UTC (10 months ago) by sysadm
Branch: MAIN
Changes since 1.5: +5 -5 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.6 unsigned int split_line(const char *buffer, int max_display_len, int *p_eol, int *p_display_len)
24 sysadm 1.1 {
25 sysadm 1.2 size_t len = strnlen(buffer, LINE_BUFFER_LEN);
26     unsigned int i = 0;
27 sysadm 1.1 *p_eol = 0;
28 sysadm 1.4 *p_display_len = 0;
29 sysadm 1.1
30     if (len == 0)
31     {
32     return 0;
33     }
34    
35     for (; i < len; i++)
36     {
37     char c = buffer[i];
38    
39     if (c == '\r' || c == '\7') // skip
40     {
41     continue;
42     }
43    
44     if (c == '\n')
45     {
46     i++;
47     *p_eol = 1;
48     break;
49     }
50 sysadm 1.2
51 sysadm 1.1 if (c == '\033' && buffer[i + 1] == '[') // Skip control sequence
52     {
53     i += 2;
54 sysadm 1.2 while (i < len && buffer[i] != 'm')
55 sysadm 1.1 {
56     i++;
57     }
58     continue;
59     }
60    
61     if (c > 127 && c <= 255) // GBK chinese character
62     {
63 sysadm 1.6 if (*p_display_len + 2 > max_display_len)
64 sysadm 1.1 {
65     *p_eol = 1;
66     break;
67     }
68     i++;
69 sysadm 1.4 *p_display_len += 2;
70 sysadm 1.1 }
71     else
72     {
73 sysadm 1.6 if (*p_display_len + 1 > max_display_len)
74 sysadm 1.1 {
75     *p_eol = 1;
76     break;
77     }
78 sysadm 1.4 (*p_display_len)++;
79 sysadm 1.1 }
80     }
81    
82     return i;
83     }
84    
85 sysadm 1.6 unsigned int split_file_lines(FILE *fin, int max_display_len, long *p_line_offsets, int max_line_cnt)
86 sysadm 1.1 {
87     char buffer[LINE_BUFFER_LEN];
88     char *p_buf = buffer;
89 sysadm 1.2 unsigned int line_cnt = 0;
90     unsigned int len = 0;
91 sysadm 1.1 int end_of_line = 0;
92 sysadm 1.4 int display_len = 0;
93    
94 sysadm 1.1 p_line_offsets[line_cnt] = 0L;
95    
96 sysadm 1.2 while (fgets(p_buf, (int)(sizeof(buffer) - len), fin))
97 sysadm 1.1 {
98     p_buf = buffer;
99     while (1)
100     {
101 sysadm 1.6 len = split_line(p_buf, max_display_len, &end_of_line, &display_len);
102 sysadm 1.1
103 sysadm 1.5 if (len == 0 || !end_of_line) // !end_of_line == EOF
104 sysadm 1.1 {
105     break;
106     }
107    
108     // Exceed max_line_cnt
109     if (line_cnt + 1 >= max_line_cnt)
110     {
111     log_error("File line count %d reaches limit\n", line_cnt + 1);
112     return line_cnt;
113     }
114    
115     p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len;
116     line_cnt++;
117     p_buf += len;
118     }
119    
120     // Move p_buf[0 .. len - 1] to head of buffer
121     for (int i = 0; i < len; i++)
122     {
123     buffer[i] = p_buf[i];
124     }
125     p_buf = buffer + len;
126     }
127    
128     if (len > 0 && line_cnt + 1 < max_line_cnt)
129     {
130     p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len;
131     line_cnt++;
132     }
133    
134     return line_cnt;
135     }

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