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; |
|
56 |
|
57 if (!text) return; |
|
58 |
|
59 hbuf_block *hbuf_block_elt = g_new0(hbuf_block, 1); |
75
|
60 if (prefix) |
|
61 strncpy(hbuf_block_elt->persist.prefix, prefix, PREFIX_LENGTH-1); |
71
|
62 if (!hbuf) { |
|
63 hbuf_block_elt->ptr = g_new(char, HBB_BLOCKSIZE); |
|
64 hbuf_block_elt->flags = HBB_FLAG_ALLOC | HBB_FLAG_PERSISTENT; |
|
65 hbuf_block_elt->persist.ptr_end_alloc = hbuf_block_elt->ptr + HBB_BLOCKSIZE; |
|
66 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt); |
|
67 } else { |
|
68 hbuf_block *hbuf_b_prev = g_list_last(hbuf)->data; |
|
69 hbuf_block_elt->ptr = hbuf_b_prev->ptr_end; |
|
70 hbuf_block_elt->flags = HBB_FLAG_PERSISTENT; |
|
71 hbuf_block_elt->persist.ptr_end_alloc = hbuf_b_prev->persist.ptr_end_alloc; |
|
72 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt); |
|
73 } |
|
74 |
|
75 if (strlen(text) >= HBB_BLOCKSIZE) { |
|
76 // Too long |
|
77 text = "[ERR:LINE_TOO_LONG]"; |
|
78 } |
|
79 if (hbuf_block_elt->ptr + strlen(text) >= hbuf_block_elt->persist.ptr_end_alloc) { |
|
80 // Too long for the current allocated bloc, we need another one |
|
81 hbuf_block_elt->ptr = g_new0(char, HBB_BLOCKSIZE); |
|
82 hbuf_block_elt->flags = HBB_FLAG_ALLOC | HBB_FLAG_PERSISTENT; |
|
83 hbuf_block_elt->persist.ptr_end_alloc = hbuf_block_elt->ptr + HBB_BLOCKSIZE; |
|
84 } |
|
85 |
|
86 line = hbuf_block_elt->ptr; |
|
87 // Ok, now we can copy the text.. |
|
88 strcpy(line, text); |
|
89 hbuf_block_elt->ptr_end = line + strlen(line) + 1; |
|
90 end = hbuf_block_elt->ptr_end; |
|
91 |
|
92 // Let's add non-persistent blocs if necessary |
|
93 // - If there are '\n' in the string |
|
94 // - If length > width (and width != 0) |
|
95 cr = strchr(line, '\n'); |
|
96 while (cr || (width && strlen(line) > width)) { |
|
97 hbuf_block *hbuf_b_prev = hbuf_block_elt; |
|
98 |
|
99 if (!width || (cr && (cr - line <= (int)width))) { |
|
100 // Carriage return |
|
101 *cr = 0; |
|
102 hbuf_block_elt->ptr_end = cr; |
|
103 // Create another persistent block |
|
104 hbuf_block_elt = g_new0(hbuf_block, 1); |
|
105 hbuf_block_elt->ptr = hbuf_b_prev->ptr_end + 1; // == cr+1 |
|
106 hbuf_block_elt->ptr_end = end; |
|
107 hbuf_block_elt->flags = HBB_FLAG_PERSISTENT; |
|
108 hbuf_block_elt->persist.ptr_end_alloc = hbuf_b_prev->persist.ptr_end_alloc; |
|
109 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt); |
|
110 line = hbuf_block_elt->ptr; |
|
111 } else { |
|
112 // We need to break where we can find a space char |
|
113 char *br; // break pointer |
|
114 for (br = line + width; br > line && *br != 32 && *br != 9; br--) |
|
115 ; |
|
116 if (br <= line) |
|
117 br = line + width; |
|
118 else |
|
119 br++; |
|
120 hbuf_block_elt->ptr_end = br; |
|
121 // Create another block, non-persistent |
|
122 hbuf_block_elt = g_new0(hbuf_block, 1); |
|
123 hbuf_block_elt->ptr = hbuf_b_prev->ptr_end; // == br |
|
124 hbuf_block_elt->ptr_end = end; |
|
125 hbuf_block_elt->flags = 0; |
|
126 hbuf_block_elt->persist.ptr_end_alloc = hbuf_b_prev->persist.ptr_end_alloc; |
|
127 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt); |
|
128 line = hbuf_block_elt->ptr; |
|
129 } |
|
130 cr = strchr(line, '\n'); |
|
131 } |
|
132 } |
|
133 |
|
134 // hbuf_free() |
|
135 // Destroys all hbuf list. |
|
136 void hbuf_free(GList **p_hbuf) |
|
137 { |
|
138 hbuf_block *hbuf_b_elt; |
|
139 GList *hbuf_elt; |
|
140 GList *first_elt = g_list_first(*p_hbuf); |
|
141 |
|
142 for (hbuf_elt = first_elt; hbuf_elt; hbuf_elt = g_list_next(hbuf_elt)) { |
|
143 hbuf_b_elt = (hbuf_block*)(hbuf_elt->data); |
|
144 if (hbuf_b_elt->flags & HBB_FLAG_ALLOC) { |
|
145 g_free(hbuf_b_elt->ptr); |
|
146 } |
|
147 } |
|
148 |
|
149 g_list_free(*p_hbuf); |
|
150 *p_hbuf = NULL; |
|
151 } |
|
152 |
|
153 // hbuf_rebuild() |
|
154 // Rebuild all hbuf list, with the new width. |
|
155 // If width == 0, lines are not wrapped. |
|
156 void hbuf_rebuild(GList **p_hbuf, unsigned int width) |
|
157 { |
|
158 GList *first_elt, *curr_elt, *next_elt; |
|
159 hbuf_block *hbuf_b_curr, *hbuf_b_next; |
|
160 |
|
161 first_elt = g_list_first(*p_hbuf); |
|
162 |
|
163 // #1 Remove non-persistent blocks (ptr_end should be updated!) |
|
164 curr_elt = first_elt; |
|
165 while (curr_elt) { |
|
166 next_elt = g_list_next(curr_elt); |
|
167 // Last element? |
|
168 if (!next_elt) |
|
169 break; |
|
170 hbuf_b_curr = (hbuf_block*)(curr_elt->data); |
|
171 hbuf_b_next = (hbuf_block*)(next_elt->data); |
|
172 // Is next line not-persistent? |
|
173 if (!(hbuf_b_next->flags & HBB_FLAG_PERSISTENT)) { |
|
174 hbuf_b_curr->ptr_end = hbuf_b_next->ptr_end; |
|
175 g_list_delete_link(curr_elt, next_elt); |
|
176 next_elt = g_list_next(curr_elt); |
|
177 } else |
|
178 curr_elt = next_elt; |
|
179 } |
|
180 // #2 Go back to head and create non-persistent blocks when needed |
|
181 if (width) { |
|
182 char *line, *end; |
|
183 curr_elt = first_elt; |
|
184 |
|
185 while (curr_elt) { |
|
186 hbuf_b_curr = (hbuf_block*)(curr_elt->data); |
|
187 line = hbuf_b_curr->ptr; |
|
188 if (strlen(line) > width) { |
|
189 hbuf_block *hbuf_b_prev = hbuf_b_curr; |
|
190 |
|
191 // We need to break where we can find a space char |
|
192 char *br; // break pointer |
|
193 for (br = line + width; br > line && *br != 32 && *br != 9; br--) |
|
194 ; |
|
195 if (br <= line) |
|
196 br = line + width; |
|
197 else |
|
198 br++; |
|
199 end = hbuf_b_curr->ptr_end; |
|
200 hbuf_b_curr->ptr_end = br; |
|
201 // Create another block, non-persistent |
|
202 hbuf_b_curr = g_new0(hbuf_block, 1); |
|
203 hbuf_b_curr->ptr = hbuf_b_prev->ptr_end; // == br |
|
204 hbuf_b_curr->ptr_end = end; |
|
205 hbuf_b_curr->flags = 0; |
|
206 hbuf_b_curr->persist.ptr_end_alloc = hbuf_b_prev->persist.ptr_end_alloc; |
|
207 /* |
|
208 // Is there a better way? |
|
209 if (g_list_next(curr_elt)) |
|
210 g_list_insert_before(*p_hbuf, curr_elt->next, hbuf_b_curr); |
|
211 else |
|
212 *p_hbuf = g_list_append(*p_hbuf, hbuf_b_curr); |
|
213 */ |
|
214 // This is OK because insert_before(NULL) <==> append() |
|
215 g_list_insert_before(*p_hbuf, curr_elt->next, hbuf_b_curr); |
|
216 } |
|
217 curr_elt = g_list_next(curr_elt); |
|
218 } |
|
219 } |
|
220 } |
|
221 |
|
222 // hbuf_get_lines(hbuf, n, where) |
75
|
223 // Returns an array of 2*n pointers (for n prefixes + n lines from hbuf) |
|
224 // (prefix line 1, line 1, prefix line 2, line 2, etc.) |
71
|
225 // (The first line will be the line currently pointed by hbuf) |
|
226 // Note:The caller should free the array after use. |
|
227 char **hbuf_get_lines(GList *hbuf, unsigned int n) |
|
228 { |
|
229 unsigned int i; |
|
230 |
75
|
231 char **array = g_new0(char*, n*2); |
71
|
232 char **array_elt = array; |
|
233 |
|
234 for (i=0 ; i < n ; i++) { |
|
235 if (hbuf) { |
|
236 hbuf_block *blk = (hbuf_block*)(hbuf->data); |
|
237 int maxlen; |
|
238 maxlen = blk->ptr_end - blk->ptr; |
75
|
239 *array_elt++ = blk->persist.prefix; |
71
|
240 *array_elt++ = g_strndup(blk->ptr, maxlen); |
|
241 hbuf = g_list_next(hbuf); |
|
242 } else |
|
243 *array_elt++ = NULL; |
|
244 } |
|
245 |
|
246 return array; |
|
247 } |
|
248 |