comparison mcabber/src/hbuf.c @ 184:b5aa2b9c425a

[/trunk] Changeset 196 by mikael * Don't use a char* as prefix, but split it to timestamp + flags. * Don't use a boolean for timestamp, use a time_t (actually we always use a timestamp so the bool made no sense...).
author mikael
date Thu, 05 May 2005 19:38:44 +0000
parents f7b03201877a
children 4f3975f1b852
comparison
equal deleted inserted replaced
183:c658c131ea10 184:b5aa2b9c425a
24 #include "hbuf.h" 24 #include "hbuf.h"
25 25
26 26
27 /* This is a private structure type */ 27 /* This is a private structure type */
28 28
29 #define PREFIX_LENGTH 32
30 typedef struct { 29 typedef struct {
31 char *ptr; 30 char *ptr;
32 char *ptr_end; // beginning of the block 31 char *ptr_end; // beginning of the block
33 char *ptr_end_alloc; // end of the current persistent block 32 char *ptr_end_alloc; // end of the current persistent block
34 guchar flags; 33 guchar flags;
35 34
36 // XXX This should certainly be a pointer, and be allocated only when needed 35 // XXX This should certainly be a pointer, and be allocated only when needed
37 // (for ex. when HBB_FLAG_PERSISTENT is set). 36 // (for ex. when HBB_FLAG_PERSISTENT is set).
38 struct { // hbuf_line_info 37 struct { // hbuf_line_info
39 char prefix[PREFIX_LENGTH]; 38 time_t timestamp;
40 } persist; 39 guchar flags;
40 } prefix;
41 } hbuf_block; 41 } hbuf_block;
42 42
43 43
44 // hbuf_add_line(p_hbuf, text, prefix, width) 44 // hbuf_add_line(p_hbuf, text, prefix_flags, width)
45 // Add a line to the given buffer. If width is not null, then lines are 45 // Add a line to the given buffer. If width is not null, then lines are
46 // wrapped at this length. 46 // wrapped at this length.
47 // 47 //
48 // Note 1: Splitting according to width won't work if there are tabs; they 48 // Note 1: Splitting according to width won't work if there are tabs; they
49 // should be expanded before. 49 // should be expanded before.
50 // Note 2: width does not include the ending \0. 50 // Note 2: width does not include the ending \0.
51 void hbuf_add_line(GList **p_hbuf, const char *text, const char *prefix, 51 void hbuf_add_line(GList **p_hbuf, const char *text, time_t timestamp,
52 unsigned int width) 52 guint prefix_flags, guint width)
53 { 53 {
54 GList *hbuf = *p_hbuf; 54 GList *hbuf = *p_hbuf;
55 char *line, *cr, *end; 55 char *line, *cr, *end;
56 hbuf_block *hbuf_block_elt; 56 hbuf_block *hbuf_block_elt;
57 57
58 if (!text) return; 58 if (!text) return;
59 59
60 hbuf_block_elt = g_new0(hbuf_block, 1); 60 hbuf_block_elt = g_new0(hbuf_block, 1);
61 if (prefix) 61 hbuf_block_elt->prefix.timestamp = timestamp;
62 strncpy(hbuf_block_elt->persist.prefix, prefix, PREFIX_LENGTH-1); 62 hbuf_block_elt->prefix.flags = prefix_flags;
63 if (!hbuf) { 63 if (!hbuf) {
64 do { 64 do {
65 hbuf_block_elt->ptr = g_new(char, HBB_BLOCKSIZE); 65 hbuf_block_elt->ptr = g_new(char, HBB_BLOCKSIZE);
66 } while (!hbuf_block_elt->ptr); 66 } while (!hbuf_block_elt->ptr);
67 hbuf_block_elt->flags = HBB_FLAG_ALLOC | HBB_FLAG_PERSISTENT; 67 hbuf_block_elt->flags = HBB_FLAG_ALLOC | HBB_FLAG_PERSISTENT;
215 curr_elt = g_list_next(curr_elt); 215 curr_elt = g_list_next(curr_elt);
216 } 216 }
217 } 217 }
218 } 218 }
219 219
220 // hbuf_get_lines(hbuf, n, where) 220 // hbuf_get_lines(hbuf, n, where) FIXME bad comments XXX
221 // Returns an array of 2*n pointers (for n prefixes + n lines from hbuf) 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.) 222 // (prefix line 1, line 1, prefix line 2, line 2, etc.)
223 // (The first line will be the line currently pointed by hbuf) 223 // (The first line will be the line currently pointed by hbuf)
224 // Note:The caller should free the array after use. 224 // Note:The caller should free the array after use.
225 char **hbuf_get_lines(GList *hbuf, unsigned int n) 225 hbb_line **hbuf_get_lines(GList *hbuf, unsigned int n)
226 { 226 {
227 unsigned int i; 227 unsigned int i;
228 228
229 char **array = g_new0(char*, n*2); 229 hbb_line **array = g_new0(hbb_line*, n);
230 char **array_elt = array; 230 hbb_line **array_elt = array;
231 231
232 for (i=0 ; i < n ; i++) { 232 for (i=0 ; i < n ; i++) {
233 if (hbuf) { 233 if (hbuf) {
234 hbuf_block *blk = (hbuf_block*)(hbuf->data); 234 hbuf_block *blk = (hbuf_block*)(hbuf->data);
235 int maxlen; 235 int maxlen;
236 maxlen = blk->ptr_end - blk->ptr; 236 maxlen = blk->ptr_end - blk->ptr;
237 *array_elt++ = blk->persist.prefix; 237 *array_elt = (hbb_line*)g_new(hbb_line, 1);
238 *array_elt++ = g_strndup(blk->ptr, maxlen); 238 (*array_elt)->timestamp = blk->prefix.timestamp;
239 (*array_elt)->flags = blk->prefix.flags;
240 (*array_elt)->text = g_strndup(blk->ptr, maxlen);
241
239 hbuf = g_list_next(hbuf); 242 hbuf = g_list_next(hbuf);
240 } else 243 } else
241 *array_elt++ = NULL; 244 break;
245
246 array_elt++;
242 } 247 }
243 248
244 return array; 249 return array;
245 } 250 }
246 251