71
|
1 /*
|
|
2 * hbuf.c -- History buffer implementation
|
|
3 *
|
|
4 * Copyright (C) 2005 Mikael Berthe <bmikael@lists.lilotux.net>
|
|
5 *
|
|
6 * This program is free software; you can redistribute it and/or modify
|
|
7 * it under the terms of the GNU General Public License as published by
|
|
8 * the Free Software Foundation; either version 2 of the License, or (at
|
|
9 * your option) any later version.
|
|
10 *
|
|
11 * This program is distributed in the hope that it will be useful, but
|
|
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14 * General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License
|
|
17 * along with this program; if not, write to the Free Software
|
|
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
19 * USA
|
|
20 */
|
|
21
|
|
22 #include <string.h>
|
|
23
|
|
24 #include "hbuf.h"
|
|
25
|
|
26
|
|
27 /* This is a private structure type */
|
|
28
|
75
|
29 #define PREFIX_LENGTH 32
|
71
|
30 typedef struct {
|
|
31 char *ptr;
|
|
32 char *ptr_end;
|
|
33 guchar flags;
|
|
34
|
|
35 // XXX This should certainly be a pointer, and be allocated only when needed
|
|
36 // (for ex. when HBB_FLAG_PERSISTENT is set).
|
|
37 struct { // hbuf_line_info
|
|
38 char *ptr_end_alloc;
|
75
|
39 char prefix[PREFIX_LENGTH];
|
71
|
40 } persist;
|
|
41 } hbuf_block;
|
|
42
|
|
43
|
75
|
44 // hbuf_add_line(p_hbuf, text, prefix, width)
|
71
|
45 // Add a line to the given buffer. If width is not null, then lines are
|
|
46 // wrapped at this length.
|
|
47 //
|
|
48 // Note 1: Splitting according to width won't work if there are tabs; they
|
|
49 // should be expanded before.
|
|
50 // Note 2: width does not include the ending \0.
|
75
|
51 void hbuf_add_line(GList **p_hbuf, const char *text, const char *prefix,
|
|
52 unsigned int width)
|
71
|
53 {
|
|
54 GList *hbuf = *p_hbuf;
|
|
55 char *line, *cr, *end;
|
83
|
56 hbuf_block *hbuf_block_elt;
|
71
|
57
|
|
58 if (!text) return;
|
|
59
|
83
|
60 hbuf_block_elt = g_new0(hbuf_block, 1);
|
75
|
61 if (prefix)
|
|
62 strncpy(hbuf_block_elt->persist.prefix, prefix, PREFIX_LENGTH-1);
|
71
|
63 if (!hbuf) {
|
|
64 hbuf_block_elt->ptr = g_new(char, HBB_BLOCKSIZE);
|
|
65 hbuf_block_elt->flags = HBB_FLAG_ALLOC | HBB_FLAG_PERSISTENT;
|
|
66 hbuf_block_elt->persist.ptr_end_alloc = hbuf_block_elt->ptr + HBB_BLOCKSIZE;
|
|
67 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt);
|
|
68 } else {
|
|
69 hbuf_block *hbuf_b_prev = g_list_last(hbuf)->data;
|
|
70 hbuf_block_elt->ptr = hbuf_b_prev->ptr_end;
|
|
71 hbuf_block_elt->flags = HBB_FLAG_PERSISTENT;
|
|
72 hbuf_block_elt->persist.ptr_end_alloc = hbuf_b_prev->persist.ptr_end_alloc;
|
|
73 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt);
|
|
74 }
|
|
75
|
|
76 if (strlen(text) >= HBB_BLOCKSIZE) {
|
|
77 // Too long
|
|
78 text = "[ERR:LINE_TOO_LONG]";
|
|
79 }
|
|
80 if (hbuf_block_elt->ptr + strlen(text) >= hbuf_block_elt->persist.ptr_end_alloc) {
|
|
81 // Too long for the current allocated bloc, we need another one
|
|
82 hbuf_block_elt->ptr = g_new0(char, HBB_BLOCKSIZE);
|
|
83 hbuf_block_elt->flags = HBB_FLAG_ALLOC | HBB_FLAG_PERSISTENT;
|
|
84 hbuf_block_elt->persist.ptr_end_alloc = hbuf_block_elt->ptr + HBB_BLOCKSIZE;
|
|
85 }
|
|
86
|
|
87 line = hbuf_block_elt->ptr;
|
|
88 // Ok, now we can copy the text..
|
|
89 strcpy(line, text);
|
|
90 hbuf_block_elt->ptr_end = line + strlen(line) + 1;
|
|
91 end = hbuf_block_elt->ptr_end;
|
|
92
|
|
93 // Let's add non-persistent blocs if necessary
|
|
94 // - If there are '\n' in the string
|
|
95 // - If length > width (and width != 0)
|
|
96 cr = strchr(line, '\n');
|
|
97 while (cr || (width && strlen(line) > width)) {
|
|
98 hbuf_block *hbuf_b_prev = hbuf_block_elt;
|
|
99
|
|
100 if (!width || (cr && (cr - line <= (int)width))) {
|
|
101 // Carriage return
|
|
102 *cr = 0;
|
|
103 hbuf_block_elt->ptr_end = cr;
|
|
104 // Create another persistent block
|
|
105 hbuf_block_elt = g_new0(hbuf_block, 1);
|
|
106 hbuf_block_elt->ptr = hbuf_b_prev->ptr_end + 1; // == cr+1
|
|
107 hbuf_block_elt->ptr_end = end;
|
|
108 hbuf_block_elt->flags = HBB_FLAG_PERSISTENT;
|
|
109 hbuf_block_elt->persist.ptr_end_alloc = hbuf_b_prev->persist.ptr_end_alloc;
|
|
110 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt);
|
|
111 line = hbuf_block_elt->ptr;
|
|
112 } else {
|
|
113 // We need to break where we can find a space char
|
|
114 char *br; // break pointer
|
|
115 for (br = line + width; br > line && *br != 32 && *br != 9; br--)
|
|
116 ;
|
|
117 if (br <= line)
|
|
118 br = line + width;
|
|
119 else
|
|
120 br++;
|
|
121 hbuf_block_elt->ptr_end = br;
|
|
122 // Create another block, non-persistent
|
|
123 hbuf_block_elt = g_new0(hbuf_block, 1);
|
|
124 hbuf_block_elt->ptr = hbuf_b_prev->ptr_end; // == br
|
|
125 hbuf_block_elt->ptr_end = end;
|
|
126 hbuf_block_elt->flags = 0;
|
|
127 hbuf_block_elt->persist.ptr_end_alloc = hbuf_b_prev->persist.ptr_end_alloc;
|
|
128 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt);
|
|
129 line = hbuf_block_elt->ptr;
|
|
130 }
|
|
131 cr = strchr(line, '\n');
|
|
132 }
|
|
133 }
|
|
134
|
|
135 // hbuf_free()
|
|
136 // Destroys all hbuf list.
|
|
137 void hbuf_free(GList **p_hbuf)
|
|
138 {
|
|
139 hbuf_block *hbuf_b_elt;
|
|
140 GList *hbuf_elt;
|
|
141 GList *first_elt = g_list_first(*p_hbuf);
|
|
142
|
|
143 for (hbuf_elt = first_elt; hbuf_elt; hbuf_elt = g_list_next(hbuf_elt)) {
|
|
144 hbuf_b_elt = (hbuf_block*)(hbuf_elt->data);
|
|
145 if (hbuf_b_elt->flags & HBB_FLAG_ALLOC) {
|
|
146 g_free(hbuf_b_elt->ptr);
|
|
147 }
|
|
148 }
|
|
149
|
|
150 g_list_free(*p_hbuf);
|
|
151 *p_hbuf = NULL;
|
|
152 }
|
|
153
|
|
154 // hbuf_rebuild()
|
|
155 // Rebuild all hbuf list, with the new width.
|
|
156 // If width == 0, lines are not wrapped.
|
|
157 void hbuf_rebuild(GList **p_hbuf, unsigned int width)
|
|
158 {
|
|
159 GList *first_elt, *curr_elt, *next_elt;
|
|
160 hbuf_block *hbuf_b_curr, *hbuf_b_next;
|
|
161
|
150
|
162 // *p_hbuf needs to be the head of the list
|
|
163 first_elt = *p_hbuf = g_list_first(*p_hbuf);
|
71
|
164
|
|
165 // #1 Remove non-persistent blocks (ptr_end should be updated!)
|
|
166 curr_elt = first_elt;
|
|
167 while (curr_elt) {
|
|
168 next_elt = g_list_next(curr_elt);
|
|
169 // Last element?
|
|
170 if (!next_elt)
|
|
171 break;
|
|
172 hbuf_b_curr = (hbuf_block*)(curr_elt->data);
|
|
173 hbuf_b_next = (hbuf_block*)(next_elt->data);
|
|
174 // Is next line not-persistent?
|
|
175 if (!(hbuf_b_next->flags & HBB_FLAG_PERSISTENT)) {
|
|
176 hbuf_b_curr->ptr_end = hbuf_b_next->ptr_end;
|
|
177 g_list_delete_link(curr_elt, next_elt);
|
|
178 } else
|
|
179 curr_elt = next_elt;
|
|
180 }
|
|
181 // #2 Go back to head and create non-persistent blocks when needed
|
|
182 if (width) {
|
|
183 char *line, *end;
|
|
184 curr_elt = first_elt;
|
|
185
|
|
186 while (curr_elt) {
|
|
187 hbuf_b_curr = (hbuf_block*)(curr_elt->data);
|
|
188 line = hbuf_b_curr->ptr;
|
|
189 if (strlen(line) > width) {
|
|
190 hbuf_block *hbuf_b_prev = hbuf_b_curr;
|
|
191
|
|
192 // We need to break where we can find a space char
|
|
193 char *br; // break pointer
|
|
194 for (br = line + width; br > line && *br != 32 && *br != 9; br--)
|
|
195 ;
|
|
196 if (br <= line)
|
|
197 br = line + width;
|
|
198 else
|
|
199 br++;
|
|
200 end = hbuf_b_curr->ptr_end;
|
|
201 hbuf_b_curr->ptr_end = br;
|
|
202 // Create another block, non-persistent
|
|
203 hbuf_b_curr = g_new0(hbuf_block, 1);
|
|
204 hbuf_b_curr->ptr = hbuf_b_prev->ptr_end; // == br
|
|
205 hbuf_b_curr->ptr_end = end;
|
|
206 hbuf_b_curr->flags = 0;
|
|
207 hbuf_b_curr->persist.ptr_end_alloc = hbuf_b_prev->persist.ptr_end_alloc;
|
150
|
208 // This is OK because insert_before(NULL) == append():
|
|
209 *p_hbuf = g_list_insert_before(*p_hbuf, curr_elt->next, hbuf_b_curr);
|
71
|
210 }
|
|
211 curr_elt = g_list_next(curr_elt);
|
|
212 }
|
|
213 }
|
|
214 }
|
|
215
|
|
216 // hbuf_get_lines(hbuf, n, where)
|
75
|
217 // Returns an array of 2*n pointers (for n prefixes + n lines from hbuf)
|
|
218 // (prefix line 1, line 1, prefix line 2, line 2, etc.)
|
71
|
219 // (The first line will be the line currently pointed by hbuf)
|
|
220 // Note:The caller should free the array after use.
|
|
221 char **hbuf_get_lines(GList *hbuf, unsigned int n)
|
|
222 {
|
|
223 unsigned int i;
|
|
224
|
75
|
225 char **array = g_new0(char*, n*2);
|
71
|
226 char **array_elt = array;
|
|
227
|
|
228 for (i=0 ; i < n ; i++) {
|
|
229 if (hbuf) {
|
|
230 hbuf_block *blk = (hbuf_block*)(hbuf->data);
|
|
231 int maxlen;
|
|
232 maxlen = blk->ptr_end - blk->ptr;
|
75
|
233 *array_elt++ = blk->persist.prefix;
|
71
|
234 *array_elt++ = g_strndup(blk->ptr, maxlen);
|
|
235 hbuf = g_list_next(hbuf);
|
|
236 } else
|
|
237 *array_elt++ = NULL;
|
|
238 }
|
|
239
|
|
240 return array;
|
|
241 }
|
|
242
|