comparison mcabber/src/hbuf.c @ 182:f7b03201877a

[/trunk] Changeset 194 by mikael * Move ptr_end_alloc outside of the "persist" structure, as it is used in non-persistent blocks too (for convenience).
author mikael
date Thu, 05 May 2005 14:34:14 +0000
parents ae0844311710
children b5aa2b9c425a
comparison
equal deleted inserted replaced
181:4a0bde661562 182:f7b03201877a
27 /* This is a private structure type */ 27 /* This is a private structure type */
28 28
29 #define PREFIX_LENGTH 32 29 #define PREFIX_LENGTH 32
30 typedef struct { 30 typedef struct {
31 char *ptr; 31 char *ptr;
32 char *ptr_end; 32 char *ptr_end; // beginning of the block
33 char *ptr_end_alloc; // end of the current persistent block
33 guchar flags; 34 guchar flags;
34 35
35 // XXX This should certainly be a pointer, and be allocated only when needed 36 // XXX This should certainly be a pointer, and be allocated only when needed
36 // (for ex. when HBB_FLAG_PERSISTENT is set). 37 // (for ex. when HBB_FLAG_PERSISTENT is set).
37 struct { // hbuf_line_info 38 struct { // hbuf_line_info
38 char *ptr_end_alloc;
39 char prefix[PREFIX_LENGTH]; 39 char prefix[PREFIX_LENGTH];
40 } persist; 40 } persist;
41 } hbuf_block; 41 } hbuf_block;
42 42
43 43
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;
68 hbuf_block_elt->persist.ptr_end_alloc = hbuf_block_elt->ptr + HBB_BLOCKSIZE; 68 hbuf_block_elt->ptr_end_alloc = hbuf_block_elt->ptr + HBB_BLOCKSIZE;
69 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt); 69 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt);
70 } else { 70 } else {
71 hbuf_block *hbuf_b_prev = g_list_last(hbuf)->data; 71 hbuf_block *hbuf_b_prev = g_list_last(hbuf)->data;
72 hbuf_block_elt->ptr = hbuf_b_prev->ptr_end; 72 hbuf_block_elt->ptr = hbuf_b_prev->ptr_end;
73 hbuf_block_elt->flags = HBB_FLAG_PERSISTENT; 73 hbuf_block_elt->flags = HBB_FLAG_PERSISTENT;
74 hbuf_block_elt->persist.ptr_end_alloc = hbuf_b_prev->persist.ptr_end_alloc; 74 hbuf_block_elt->ptr_end_alloc = hbuf_b_prev->ptr_end_alloc;
75 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt); 75 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt);
76 } 76 }
77 77
78 if (strlen(text) >= HBB_BLOCKSIZE) { 78 if (strlen(text) >= HBB_BLOCKSIZE) {
79 // Too long 79 // Too long
80 text = "[ERR:LINE_TOO_LONG]"; 80 text = "[ERR:LINE_TOO_LONG]";
81 } 81 }
82 if (hbuf_block_elt->ptr + strlen(text) >= hbuf_block_elt->persist.ptr_end_alloc) { 82 if (hbuf_block_elt->ptr + strlen(text) >= hbuf_block_elt->ptr_end_alloc) {
83 // Too long for the current allocated bloc, we need another one 83 // Too long for the current allocated bloc, we need another one
84 do { 84 do {
85 hbuf_block_elt->ptr = g_new0(char, HBB_BLOCKSIZE); 85 hbuf_block_elt->ptr = g_new0(char, HBB_BLOCKSIZE);
86 } while (!hbuf_block_elt->ptr); 86 } while (!hbuf_block_elt->ptr);
87 hbuf_block_elt->flags = HBB_FLAG_ALLOC | HBB_FLAG_PERSISTENT; 87 hbuf_block_elt->flags = HBB_FLAG_ALLOC | HBB_FLAG_PERSISTENT;
88 hbuf_block_elt->persist.ptr_end_alloc = hbuf_block_elt->ptr + HBB_BLOCKSIZE; 88 hbuf_block_elt->ptr_end_alloc = hbuf_block_elt->ptr + HBB_BLOCKSIZE;
89 } 89 }
90 90
91 line = hbuf_block_elt->ptr; 91 line = hbuf_block_elt->ptr;
92 // Ok, now we can copy the text.. 92 // Ok, now we can copy the text..
93 strcpy(line, text); 93 strcpy(line, text);
108 // Create another persistent block 108 // Create another persistent block
109 hbuf_block_elt = g_new0(hbuf_block, 1); 109 hbuf_block_elt = g_new0(hbuf_block, 1);
110 hbuf_block_elt->ptr = hbuf_b_prev->ptr_end + 1; // == cr+1 110 hbuf_block_elt->ptr = hbuf_b_prev->ptr_end + 1; // == cr+1
111 hbuf_block_elt->ptr_end = end; 111 hbuf_block_elt->ptr_end = end;
112 hbuf_block_elt->flags = HBB_FLAG_PERSISTENT; 112 hbuf_block_elt->flags = HBB_FLAG_PERSISTENT;
113 hbuf_block_elt->persist.ptr_end_alloc = hbuf_b_prev->persist.ptr_end_alloc; 113 hbuf_block_elt->ptr_end_alloc = hbuf_b_prev->ptr_end_alloc;
114 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt); 114 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt);
115 line = hbuf_block_elt->ptr; 115 line = hbuf_block_elt->ptr;
116 } else { 116 } else {
117 // We need to break where we can find a space char 117 // We need to break where we can find a space char
118 char *br; // break pointer 118 char *br; // break pointer
126 // Create another block, non-persistent 126 // Create another block, non-persistent
127 hbuf_block_elt = g_new0(hbuf_block, 1); 127 hbuf_block_elt = g_new0(hbuf_block, 1);
128 hbuf_block_elt->ptr = hbuf_b_prev->ptr_end; // == br 128 hbuf_block_elt->ptr = hbuf_b_prev->ptr_end; // == br
129 hbuf_block_elt->ptr_end = end; 129 hbuf_block_elt->ptr_end = end;
130 hbuf_block_elt->flags = 0; 130 hbuf_block_elt->flags = 0;
131 hbuf_block_elt->persist.ptr_end_alloc = hbuf_b_prev->persist.ptr_end_alloc; 131 hbuf_block_elt->ptr_end_alloc = hbuf_b_prev->ptr_end_alloc;
132 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt); 132 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt);
133 line = hbuf_block_elt->ptr; 133 line = hbuf_block_elt->ptr;
134 } 134 }
135 cr = strchr(line, '\n'); 135 cr = strchr(line, '\n');
136 } 136 }
206 // Create another block, non-persistent 206 // Create another block, non-persistent
207 hbuf_b_curr = g_new0(hbuf_block, 1); 207 hbuf_b_curr = g_new0(hbuf_block, 1);
208 hbuf_b_curr->ptr = hbuf_b_prev->ptr_end; // == br 208 hbuf_b_curr->ptr = hbuf_b_prev->ptr_end; // == br
209 hbuf_b_curr->ptr_end = end; 209 hbuf_b_curr->ptr_end = end;
210 hbuf_b_curr->flags = 0; 210 hbuf_b_curr->flags = 0;
211 hbuf_b_curr->persist.ptr_end_alloc = hbuf_b_prev->persist.ptr_end_alloc; 211 hbuf_b_curr->ptr_end_alloc = hbuf_b_prev->ptr_end_alloc;
212 // This is OK because insert_before(NULL) == append(): 212 // This is OK because insert_before(NULL) == append():
213 *p_hbuf = g_list_insert_before(*p_hbuf, curr_elt->next, hbuf_b_curr); 213 *p_hbuf = g_list_insert_before(*p_hbuf, curr_elt->next, hbuf_b_curr);
214 } 214 }
215 curr_elt = g_list_next(curr_elt); 215 curr_elt = g_list_next(curr_elt);
216 } 216 }