comparison mcabber/src/hbuf.c @ 75:ff119bb11563

[/trunk] Changeset 89 by mikael * Fix wrapping (prefix) issue.
author mikael
date Sat, 16 Apr 2005 17:14:55 +0000
parents 1e9d4949bcfd
children a95e2fc9ea6b
comparison
equal deleted inserted replaced
74:b392112ab995 75:ff119bb11563
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
29 typedef struct { 30 typedef struct {
30 char *ptr; 31 char *ptr;
31 char *ptr_end; 32 char *ptr_end;
32 guchar flags; 33 guchar flags;
33 34
34 // 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
35 // (for ex. when HBB_FLAG_PERSISTENT is set). 36 // (for ex. when HBB_FLAG_PERSISTENT is set).
36 struct { // hbuf_line_info 37 struct { // hbuf_line_info
37 char *ptr_end_alloc; 38 char *ptr_end_alloc;
38 char prefix[32]; 39 char prefix[PREFIX_LENGTH];
39 } persist; 40 } persist;
40 } hbuf_block; 41 } hbuf_block;
41 42
42 43
43 // hbuf_add_line(p_hbuf, text, width) 44 // hbuf_add_line(p_hbuf, text, prefix, width)
44 // 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
45 // wrapped at this length. 46 // wrapped at this length.
46 // 47 //
47 // 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
48 // should be expanded before. 49 // should be expanded before.
49 // Note 2: width does not include the ending \0. 50 // Note 2: width does not include the ending \0.
50 void hbuf_add_line(GList **p_hbuf, char *text, unsigned int width) 51 void hbuf_add_line(GList **p_hbuf, const char *text, const char *prefix,
52 unsigned int width)
51 { 53 {
52 GList *hbuf = *p_hbuf; 54 GList *hbuf = *p_hbuf;
53 char *line, *cr, *end; 55 char *line, *cr, *end;
54 56
55 if (!text) return; 57 if (!text) return;
56 58
57 hbuf_block *hbuf_block_elt = g_new0(hbuf_block, 1); 59 hbuf_block *hbuf_block_elt = g_new0(hbuf_block, 1);
60 if (prefix)
61 strncpy(hbuf_block_elt->persist.prefix, prefix, PREFIX_LENGTH-1);
58 if (!hbuf) { 62 if (!hbuf) {
59 hbuf_block_elt->ptr = g_new(char, HBB_BLOCKSIZE); 63 hbuf_block_elt->ptr = g_new(char, HBB_BLOCKSIZE);
60 hbuf_block_elt->flags = HBB_FLAG_ALLOC | HBB_FLAG_PERSISTENT; 64 hbuf_block_elt->flags = HBB_FLAG_ALLOC | HBB_FLAG_PERSISTENT;
61 hbuf_block_elt->persist.ptr_end_alloc = hbuf_block_elt->ptr + HBB_BLOCKSIZE; 65 hbuf_block_elt->persist.ptr_end_alloc = hbuf_block_elt->ptr + HBB_BLOCKSIZE;
62 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt); 66 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt);
214 } 218 }
215 } 219 }
216 } 220 }
217 221
218 // hbuf_get_lines(hbuf, n, where) 222 // hbuf_get_lines(hbuf, n, where)
219 // Returns an array of n pointers (for n lines from hbuf) 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.)
220 // (The first line will be the line currently pointed by hbuf) 225 // (The first line will be the line currently pointed by hbuf)
221 // Note:The caller should free the array after use. 226 // Note:The caller should free the array after use.
222 char **hbuf_get_lines(GList *hbuf, unsigned int n) 227 char **hbuf_get_lines(GList *hbuf, unsigned int n)
223 { 228 {
224 unsigned int i; 229 unsigned int i;
225 230
226 char **array = g_new0(char*, n); 231 char **array = g_new0(char*, n*2);
227 char **array_elt = array; 232 char **array_elt = array;
228 233
229 for (i=0 ; i < n ; i++) { 234 for (i=0 ; i < n ; i++) {
230 if (hbuf) { 235 if (hbuf) {
231 hbuf_block *blk = (hbuf_block*)(hbuf->data); 236 hbuf_block *blk = (hbuf_block*)(hbuf->data);
232 int maxlen; 237 int maxlen;
233 maxlen = blk->ptr_end - blk->ptr; 238 maxlen = blk->ptr_end - blk->ptr;
239 *array_elt++ = blk->persist.prefix;
234 *array_elt++ = g_strndup(blk->ptr, maxlen); 240 *array_elt++ = g_strndup(blk->ptr, maxlen);
235 hbuf = g_list_next(hbuf); 241 hbuf = g_list_next(hbuf);
236 } else 242 } else
237 *array_elt++ = NULL; 243 *array_elt++ = NULL;
238 } 244 }