comparison mcabber/src/hbuf.c @ 1011:b5bcc223cf51

Fix a bug in hbuf_add_line() The first line of a buffer was never wrapped, because "curr_elt" was NULL. This patch also removes the useless variable "hbuf".
author Mikael Berthe <mikael@lilotux.net>
date Sun, 12 Nov 2006 13:11:07 +0100
parents 9ac0d166a85b
children 5be2408a6534
comparison
equal deleted inserted replaced
1010:54405d09b15a 1011:b5bcc223cf51
115 // should be expanded before. 115 // should be expanded before.
116 // Note 2: width does not include the ending \0. 116 // Note 2: width does not include the ending \0.
117 void hbuf_add_line(GList **p_hbuf, const char *text, time_t timestamp, 117 void hbuf_add_line(GList **p_hbuf, const char *text, time_t timestamp,
118 guint prefix_flags, guint width) 118 guint prefix_flags, guint width)
119 { 119 {
120 GList *hbuf = *p_hbuf;
121 GList *curr_elt; 120 GList *curr_elt;
122 char *line, *end; 121 char *line, *end;
123 hbuf_block *hbuf_block_elt; 122 hbuf_block *hbuf_block_elt;
124 123
125 if (!text) return; 124 if (!text) return;
126 125
127 hbuf_block_elt = g_new0(hbuf_block, 1); 126 hbuf_block_elt = g_new0(hbuf_block, 1);
128 hbuf_block_elt->prefix.timestamp = timestamp; 127 hbuf_block_elt->prefix.timestamp = timestamp;
129 hbuf_block_elt->prefix.flags = prefix_flags; 128 hbuf_block_elt->prefix.flags = prefix_flags;
130 if (!hbuf) { 129 if (!*p_hbuf) {
131 hbuf_block_elt->ptr = g_new(char, HBB_BLOCKSIZE); 130 hbuf_block_elt->ptr = g_new(char, HBB_BLOCKSIZE);
132 hbuf_block_elt->flags = HBB_FLAG_ALLOC | HBB_FLAG_PERSISTENT; 131 hbuf_block_elt->flags = HBB_FLAG_ALLOC | HBB_FLAG_PERSISTENT;
133 hbuf_block_elt->ptr_end_alloc = hbuf_block_elt->ptr + HBB_BLOCKSIZE; 132 hbuf_block_elt->ptr_end_alloc = hbuf_block_elt->ptr + HBB_BLOCKSIZE;
134 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt);
135 } else { 133 } else {
136 hbuf_block *hbuf_b_prev; 134 hbuf_block *hbuf_b_prev;
137 // Set p_hbuf to the end of the list, to speed up history loading 135 // Set p_hbuf to the end of the list, to speed up history loading
138 // (or CPU time will be used by g_list_last() for each line) 136 // (or CPU time will be used by g_list_last() for each line)
139 hbuf = *p_hbuf = g_list_last(*p_hbuf); 137 *p_hbuf = g_list_last(*p_hbuf);
140 hbuf_b_prev = hbuf->data; 138 hbuf_b_prev = (*p_hbuf)->data;
141 hbuf_block_elt->ptr = hbuf_b_prev->ptr_end; 139 hbuf_block_elt->ptr = hbuf_b_prev->ptr_end;
142 hbuf_block_elt->flags = HBB_FLAG_PERSISTENT; 140 hbuf_block_elt->flags = HBB_FLAG_PERSISTENT;
143 hbuf_block_elt->ptr_end_alloc = hbuf_b_prev->ptr_end_alloc; 141 hbuf_block_elt->ptr_end_alloc = hbuf_b_prev->ptr_end_alloc;
144 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt); 142 }
145 } 143 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt);
146 144
147 if (strlen(text) >= HBB_BLOCKSIZE) { 145 if (strlen(text) >= HBB_BLOCKSIZE) {
148 // Too long 146 // Too long
149 text = "[ERR:LINE_TOO_LONG]"; 147 text = "[ERR:LINE_TOO_LONG]";
150 hbuf_block_elt->prefix.flags |= HBB_PREFIX_INFO; 148 hbuf_block_elt->prefix.flags |= HBB_PREFIX_INFO;
160 // Ok, now we can copy the text.. 158 // Ok, now we can copy the text..
161 strcpy(line, text); 159 strcpy(line, text);
162 hbuf_block_elt->ptr_end = line + strlen(line) + 1; 160 hbuf_block_elt->ptr_end = line + strlen(line) + 1;
163 end = hbuf_block_elt->ptr_end; 161 end = hbuf_block_elt->ptr_end;
164 162
165 curr_elt = g_list_last(hbuf); 163 curr_elt = g_list_last(*p_hbuf);
166 164
167 // Wrap lines and handle CRs ('\n') 165 // Wrap lines and handle CRs ('\n')
168 do_wrap(p_hbuf, curr_elt, width); 166 do_wrap(p_hbuf, curr_elt, width);
169 } 167 }
170 168