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