Mercurial > ~mikael > mcabber > hg
annotate mcabber/src/hbuf.c @ 390:468c9cac2798
Minor changes to hbuf.c
- remove useless do {} while with g_new()
- some g_free() were missing
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Mon, 01 Aug 2005 21:43:01 +0100 |
parents | dd9e2eb52916 |
children | 6329c9601704 |
rev | line source |
---|---|
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 | |
370
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
22 #define _GNU_SOURCE /* We need glibc for strptime */ |
71 | 23 #include <string.h> |
24 | |
25 #include "hbuf.h" | |
26 | |
27 | |
28 /* This is a private structure type */ | |
29 | |
30 typedef struct { | |
31 char *ptr; | |
182 | 32 char *ptr_end; // beginning of the block |
33 char *ptr_end_alloc; // end of the current persistent block | |
71 | 34 guchar flags; |
35 | |
36 // XXX This should certainly be a pointer, and be allocated only when needed | |
37 // (for ex. when HBB_FLAG_PERSISTENT is set). | |
38 struct { // hbuf_line_info | |
184 | 39 time_t timestamp; |
40 guchar flags; | |
41 } prefix; | |
71 | 42 } hbuf_block; |
43 | |
44 | |
184 | 45 // hbuf_add_line(p_hbuf, text, prefix_flags, width) |
71 | 46 // Add a line to the given buffer. If width is not null, then lines are |
47 // wrapped at this length. | |
48 // | |
49 // Note 1: Splitting according to width won't work if there are tabs; they | |
50 // should be expanded before. | |
51 // Note 2: width does not include the ending \0. | |
184 | 52 void hbuf_add_line(GList **p_hbuf, const char *text, time_t timestamp, |
53 guint prefix_flags, guint width) | |
71 | 54 { |
55 GList *hbuf = *p_hbuf; | |
56 char *line, *cr, *end; | |
83 | 57 hbuf_block *hbuf_block_elt; |
71 | 58 |
59 if (!text) return; | |
60 | |
83 | 61 hbuf_block_elt = g_new0(hbuf_block, 1); |
184 | 62 hbuf_block_elt->prefix.timestamp = timestamp; |
63 hbuf_block_elt->prefix.flags = prefix_flags; | |
71 | 64 if (!hbuf) { |
390 | 65 hbuf_block_elt->ptr = g_new(char, HBB_BLOCKSIZE); |
71 | 66 hbuf_block_elt->flags = HBB_FLAG_ALLOC | HBB_FLAG_PERSISTENT; |
182 | 67 hbuf_block_elt->ptr_end_alloc = hbuf_block_elt->ptr + HBB_BLOCKSIZE; |
71 | 68 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt); |
69 } else { | |
70 hbuf_block *hbuf_b_prev = g_list_last(hbuf)->data; | |
71 hbuf_block_elt->ptr = hbuf_b_prev->ptr_end; | |
72 hbuf_block_elt->flags = HBB_FLAG_PERSISTENT; | |
182 | 73 hbuf_block_elt->ptr_end_alloc = hbuf_b_prev->ptr_end_alloc; |
71 | 74 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt); |
75 } | |
76 | |
77 if (strlen(text) >= HBB_BLOCKSIZE) { | |
78 // Too long | |
79 text = "[ERR:LINE_TOO_LONG]"; | |
197 | 80 hbuf_block_elt->prefix.flags |= HBB_PREFIX_INFO; |
71 | 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 |
390 | 84 hbuf_block_elt->ptr = g_new0(char, HBB_BLOCKSIZE); |
71 | 85 hbuf_block_elt->flags = HBB_FLAG_ALLOC | HBB_FLAG_PERSISTENT; |
182 | 86 hbuf_block_elt->ptr_end_alloc = hbuf_block_elt->ptr + HBB_BLOCKSIZE; |
71 | 87 } |
88 | |
89 line = hbuf_block_elt->ptr; | |
90 // Ok, now we can copy the text.. | |
91 strcpy(line, text); | |
92 hbuf_block_elt->ptr_end = line + strlen(line) + 1; | |
93 end = hbuf_block_elt->ptr_end; | |
94 | |
95 // Let's add non-persistent blocs if necessary | |
96 // - If there are '\n' in the string | |
97 // - If length > width (and width != 0) | |
98 cr = strchr(line, '\n'); | |
99 while (cr || (width && strlen(line) > width)) { | |
100 hbuf_block *hbuf_b_prev = hbuf_block_elt; | |
101 | |
102 if (!width || (cr && (cr - line <= (int)width))) { | |
103 // Carriage return | |
104 *cr = 0; | |
105 hbuf_block_elt->ptr_end = cr; | |
106 // Create another persistent block | |
107 hbuf_block_elt = g_new0(hbuf_block, 1); | |
108 hbuf_block_elt->ptr = hbuf_b_prev->ptr_end + 1; // == cr+1 | |
109 hbuf_block_elt->ptr_end = end; | |
110 hbuf_block_elt->flags = HBB_FLAG_PERSISTENT; | |
182 | 111 hbuf_block_elt->ptr_end_alloc = hbuf_b_prev->ptr_end_alloc; |
71 | 112 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt); |
113 line = hbuf_block_elt->ptr; | |
114 } else { | |
115 // We need to break where we can find a space char | |
116 char *br; // break pointer | |
117 for (br = line + width; br > line && *br != 32 && *br != 9; br--) | |
118 ; | |
119 if (br <= line) | |
120 br = line + width; | |
121 else | |
122 br++; | |
123 hbuf_block_elt->ptr_end = br; | |
124 // Create another block, non-persistent | |
125 hbuf_block_elt = g_new0(hbuf_block, 1); | |
126 hbuf_block_elt->ptr = hbuf_b_prev->ptr_end; // == br | |
127 hbuf_block_elt->ptr_end = end; | |
128 hbuf_block_elt->flags = 0; | |
182 | 129 hbuf_block_elt->ptr_end_alloc = hbuf_b_prev->ptr_end_alloc; |
71 | 130 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt); |
131 line = hbuf_block_elt->ptr; | |
132 } | |
133 cr = strchr(line, '\n'); | |
134 } | |
135 } | |
136 | |
137 // hbuf_free() | |
138 // Destroys all hbuf list. | |
139 void hbuf_free(GList **p_hbuf) | |
140 { | |
141 hbuf_block *hbuf_b_elt; | |
142 GList *hbuf_elt; | |
143 GList *first_elt = g_list_first(*p_hbuf); | |
144 | |
145 for (hbuf_elt = first_elt; hbuf_elt; hbuf_elt = g_list_next(hbuf_elt)) { | |
146 hbuf_b_elt = (hbuf_block*)(hbuf_elt->data); | |
147 if (hbuf_b_elt->flags & HBB_FLAG_ALLOC) { | |
148 g_free(hbuf_b_elt->ptr); | |
149 } | |
390 | 150 g_free(hbuf_b_elt); |
71 | 151 } |
152 | |
153 g_list_free(*p_hbuf); | |
154 *p_hbuf = NULL; | |
155 } | |
156 | |
157 // hbuf_rebuild() | |
158 // Rebuild all hbuf list, with the new width. | |
159 // If width == 0, lines are not wrapped. | |
160 void hbuf_rebuild(GList **p_hbuf, unsigned int width) | |
161 { | |
162 GList *first_elt, *curr_elt, *next_elt; | |
163 hbuf_block *hbuf_b_curr, *hbuf_b_next; | |
164 | |
150 | 165 // *p_hbuf needs to be the head of the list |
166 first_elt = *p_hbuf = g_list_first(*p_hbuf); | |
71 | 167 |
168 // #1 Remove non-persistent blocks (ptr_end should be updated!) | |
169 curr_elt = first_elt; | |
170 while (curr_elt) { | |
171 next_elt = g_list_next(curr_elt); | |
172 // Last element? | |
173 if (!next_elt) | |
174 break; | |
175 hbuf_b_curr = (hbuf_block*)(curr_elt->data); | |
176 hbuf_b_next = (hbuf_block*)(next_elt->data); | |
177 // Is next line not-persistent? | |
178 if (!(hbuf_b_next->flags & HBB_FLAG_PERSISTENT)) { | |
179 hbuf_b_curr->ptr_end = hbuf_b_next->ptr_end; | |
390 | 180 g_free(hbuf_b_next); |
71 | 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 | |
189 | 220 // hbuf_previous_persistent() |
221 // Returns the previous persistent block (line). If the given line is | |
222 // persistent, then it is returned. | |
223 // This function is used for example when resizing a buffer. If the top of the | |
224 // screen is on a non-persistent block, then a screen resize could destroy this | |
225 // line... | |
226 GList *hbuf_previous_persistent(GList *l_line) | |
227 { | |
228 hbuf_block *hbuf_b_elt; | |
229 | |
230 while (l_line) { | |
231 hbuf_b_elt = (hbuf_block*)l_line->data; | |
232 if (hbuf_b_elt->flags & HBB_FLAG_PERSISTENT) | |
233 return l_line; | |
234 l_line = g_list_previous(l_line); | |
235 } | |
236 | |
237 return NULL; | |
238 } | |
239 | |
368
da50f08ea058
Update hbuf_get_lines() comment
Mikael Berthe <mikael@lilotux.net>
parents:
197
diff
changeset
|
240 // hbuf_get_lines(hbuf, n) |
370
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
241 // Returns an array of n hbb_line pointers |
71 | 242 // (The first line will be the line currently pointed by hbuf) |
368
da50f08ea058
Update hbuf_get_lines() comment
Mikael Berthe <mikael@lilotux.net>
parents:
197
diff
changeset
|
243 // Note: The caller should free the array and the text pointers after use. |
184 | 244 hbb_line **hbuf_get_lines(GList *hbuf, unsigned int n) |
71 | 245 { |
246 unsigned int i; | |
247 | |
184 | 248 hbb_line **array = g_new0(hbb_line*, n); |
249 hbb_line **array_elt = array; | |
71 | 250 |
251 for (i=0 ; i < n ; i++) { | |
252 if (hbuf) { | |
253 hbuf_block *blk = (hbuf_block*)(hbuf->data); | |
254 int maxlen; | |
255 maxlen = blk->ptr_end - blk->ptr; | |
184 | 256 *array_elt = (hbb_line*)g_new(hbb_line, 1); |
257 (*array_elt)->timestamp = blk->prefix.timestamp; | |
258 (*array_elt)->flags = blk->prefix.flags; | |
259 (*array_elt)->text = g_strndup(blk->ptr, maxlen); | |
260 | |
71 | 261 hbuf = g_list_next(hbuf); |
262 } else | |
184 | 263 break; |
264 | |
265 array_elt++; | |
71 | 266 } |
267 | |
268 return array; | |
269 } | |
270 | |
370
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
271 // hbuf_search(hbuf, direction, string) |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
272 // Look backward/forward for a line containing string in the history buffer |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
273 // Search starts at hbuf, and goes forward if direction == 1, backward if -1 |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
274 GList *hbuf_search(GList *hbuf, int direction, const char *string) |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
275 { |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
276 hbuf_block *blk; |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
277 |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
278 for (;;) { |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
279 if (direction > 0) |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
280 hbuf = g_list_next(hbuf); |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
281 else |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
282 hbuf = g_list_previous(hbuf); |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
283 |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
284 if (!hbuf) break; |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
285 |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
286 blk = (hbuf_block*)(hbuf->data); |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
287 // XXX blk->ptr is (maybe) not really correct, because the match should |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
288 // not be after ptr_end. We should check that... |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
289 if (strcasestr(blk->ptr, string)) |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
290 break; |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
291 } |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
292 |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
293 return hbuf; |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
294 } |