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