2 textbuffer.c : Text buffer handling
4 Copyright (C) 1999-2001 Timo Sirainen
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
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
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 USA
25 #include "textbuffer.h"
31 #define TEXT_CHUNK_USABLE_SIZE (LINE_TEXT_CHUNK_SIZE-2-(int)sizeof(char*))
33 static GMemChunk *buffer_chunk, *line_chunk, *text_chunk;
35 TEXT_BUFFER_REC *textbuffer_create(void)
37 TEXT_BUFFER_REC *buffer;
39 buffer = g_mem_chunk_alloc0(buffer_chunk);
40 buffer->last_eol = TRUE;
44 void textbuffer_destroy(TEXT_BUFFER_REC *buffer)
46 g_return_if_fail(buffer != NULL);
48 textbuffer_remove_all_lines(buffer);
49 g_mem_chunk_free(buffer_chunk, buffer);
52 static TEXT_CHUNK_REC *text_chunk_find(TEXT_BUFFER_REC *buffer,
53 const unsigned char *data)
57 for (tmp = buffer->text_chunks; tmp != NULL; tmp = tmp->next) {
58 TEXT_CHUNK_REC *rec = tmp->data;
60 if (data >= rec->buffer &&
61 data < rec->buffer+sizeof(rec->buffer))
68 #define mark_temp_eol(chunk) G_STMT_START { \
69 (chunk)->buffer[(chunk)->pos] = 0; \
70 (chunk)->buffer[(chunk)->pos+1] = LINE_CMD_EOL; \
73 static TEXT_CHUNK_REC *text_chunk_create(TEXT_BUFFER_REC *buffer)
76 unsigned char *buf, *ptr, **pptr;
78 rec = g_mem_chunk_alloc(text_chunk);
82 if (buffer->cur_line != NULL && buffer->cur_line->text != NULL) {
83 /* create a link to new block from the old block */
84 buf = buffer->cur_text->buffer + buffer->cur_text->pos;
85 *buf++ = 0; *buf++ = (char) LINE_CMD_CONTINUE;
87 /* we want to store pointer to beginning of the new text
88 block to char* buffer. this probably isn't ANSI-C
89 compatible, and trying this without the pptr variable
90 breaks at least NetBSD/Alpha, so don't go "optimize"
92 ptr = rec->buffer; pptr = &ptr;
93 memcpy(buf, pptr, sizeof(unsigned char *));
99 buffer->cur_text = rec;
100 buffer->text_chunks = g_slist_append(buffer->text_chunks, rec);
104 static void text_chunk_destroy(TEXT_BUFFER_REC *buffer, TEXT_CHUNK_REC *chunk)
106 buffer->text_chunks = g_slist_remove(buffer->text_chunks, chunk);
107 g_mem_chunk_free(text_chunk, chunk);
110 static void text_chunk_line_free(TEXT_BUFFER_REC *buffer, LINE_REC *line)
112 TEXT_CHUNK_REC *chunk;
113 const unsigned char *text;
114 unsigned char *tmp = NULL;
116 for (text = line->text;; text++) {
121 if (*text == LINE_CMD_CONTINUE || *text == LINE_CMD_EOL) {
122 if (*text == LINE_CMD_CONTINUE)
123 memcpy(&tmp, text+1, sizeof(char *));
125 /* free the previous block */
126 chunk = text_chunk_find(buffer, text);
127 if (--chunk->refcount == 0) {
128 if (buffer->cur_text == chunk)
131 text_chunk_destroy(buffer, chunk);
134 if (*text == LINE_CMD_EOL)
142 static void text_chunk_append(TEXT_BUFFER_REC *buffer,
143 const unsigned char *data, int len)
145 TEXT_CHUNK_REC *chunk;
151 chunk = buffer->cur_text;
152 while (chunk->pos + len >= TEXT_CHUNK_USABLE_SIZE) {
153 left = TEXT_CHUNK_USABLE_SIZE - chunk->pos;
154 if (left > 0 && data[left-1] == 0)
155 left--; /* don't split the commands */
157 memcpy(chunk->buffer + chunk->pos, data, left);
160 chunk = text_chunk_create(buffer);
162 len -= left; data += left;
165 memcpy(chunk->buffer + chunk->pos, data, len);
168 mark_temp_eol(chunk);
171 static LINE_REC *textbuffer_line_create(TEXT_BUFFER_REC *buffer)
175 if (buffer->cur_text == NULL)
176 text_chunk_create(buffer);
178 rec = g_mem_chunk_alloc(line_chunk);
180 rec->text = buffer->cur_text->buffer + buffer->cur_text->pos;
182 buffer->cur_text->refcount++;
186 static LINE_REC *textbuffer_line_insert(TEXT_BUFFER_REC *buffer,
191 line = textbuffer_line_create(buffer);
194 line->next = buffer->first_line;
195 if (buffer->first_line != NULL)
196 buffer->first_line->prev = line;
197 buffer->first_line = line;
199 line->next = prev->next;
200 if (line->next != NULL)
201 line->next->prev = line;
205 if (prev == buffer->cur_line)
206 buffer->cur_line = line;
207 buffer->lines_count++;
212 void textbuffer_line_ref(LINE_REC *line)
214 g_return_if_fail(line != NULL);
216 if (++line->refcount == 255)
217 g_error("line reference counter wrapped - shouldn't happen");
220 void textbuffer_line_unref(TEXT_BUFFER_REC *buffer, LINE_REC *line)
222 g_return_if_fail(buffer != NULL);
223 g_return_if_fail(line != NULL);
225 if (--line->refcount == 0) {
226 text_chunk_line_free(buffer, line);
227 g_mem_chunk_free(line_chunk, line);
231 void textbuffer_line_unref_list(TEXT_BUFFER_REC *buffer, GList *list)
233 g_return_if_fail(buffer != NULL);
235 while (list != NULL) {
236 if (list->data != NULL)
237 textbuffer_line_unref(buffer, list->data);
242 LINE_REC *textbuffer_line_last(TEXT_BUFFER_REC *buffer)
246 line = buffer->cur_line;
248 while (line->next != NULL)
254 int textbuffer_line_exists_after(LINE_REC *line, LINE_REC *search)
256 while (line != NULL) {
264 LINE_REC *textbuffer_append(TEXT_BUFFER_REC *buffer,
265 const unsigned char *data, int len,
268 return textbuffer_insert(buffer, buffer->cur_line, data, len, info);
271 LINE_REC *textbuffer_insert(TEXT_BUFFER_REC *buffer, LINE_REC *insert_after,
272 const unsigned char *data, int len,
277 g_return_val_if_fail(buffer != NULL, NULL);
278 g_return_val_if_fail(data != NULL, NULL);
283 line = !buffer->last_eol ? insert_after :
284 textbuffer_line_insert(buffer, insert_after);
287 memcpy(&line->info, info, sizeof(line->info));
289 text_chunk_append(buffer, data, len);
291 buffer->last_eol = len >= 2 &&
292 data[len-2] == 0 && data[len-1] == LINE_CMD_EOL;
297 void textbuffer_remove(TEXT_BUFFER_REC *buffer, LINE_REC *line)
299 g_return_if_fail(buffer != NULL);
300 g_return_if_fail(line != NULL);
302 if (buffer->first_line == line)
303 buffer->first_line = line->next;
304 if (line->prev != NULL)
305 line->prev->next = line->next;
306 if (line->next != NULL)
307 line->next->prev = line->prev;
309 if (buffer->cur_line == line) {
310 buffer->cur_line = line->next != NULL ?
311 line->next : line->prev;
314 line->prev = line->next = NULL;
316 buffer->lines_count--;
317 textbuffer_line_unref(buffer, line);
320 /* Removes all lines from buffer, ignoring reference counters */
321 void textbuffer_remove_all_lines(TEXT_BUFFER_REC *buffer)
326 g_return_if_fail(buffer != NULL);
328 for (tmp = buffer->text_chunks; tmp != NULL; tmp = tmp->next)
329 g_mem_chunk_free(text_chunk, tmp->data);
330 g_slist_free(buffer->text_chunks);
331 buffer->text_chunks = NULL;
333 while (buffer->first_line != NULL) {
334 line = buffer->first_line->next;
335 g_mem_chunk_free(line_chunk, buffer->first_line);
336 buffer->first_line = line;
338 buffer->lines_count = 0;
340 buffer->cur_line = NULL;
341 buffer->cur_text = NULL;
343 buffer->last_eol = TRUE;
346 static void set_color(GString *str, int cmd, int *last_fg, int *last_bg)
348 if (cmd & LINE_COLOR_DEFAULT) {
349 g_string_sprintfa(str, "\004%c", FORMAT_STYLE_DEFAULTS);
351 /* need to reset the fg/bg color */
352 if (cmd & LINE_COLOR_BG) {
354 if (*last_fg != -1) {
355 g_string_sprintfa(str, "\004%c%c",
357 FORMAT_COLOR_NOCHANGE);
361 if (*last_bg != -1) {
362 g_string_sprintfa(str, "\004%c%c",
363 FORMAT_COLOR_NOCHANGE,
370 if ((cmd & LINE_COLOR_BG) == 0) {
371 /* change foreground color */
372 *last_fg = (cmd & 0x0f)+'0';
373 g_string_sprintfa(str, "\004%c%c", *last_fg,
374 FORMAT_COLOR_NOCHANGE);
376 /* change background color */
377 *last_bg = (cmd & 0x0f)+'0';
378 g_string_sprintfa(str, "\004%c%c",
379 FORMAT_COLOR_NOCHANGE, *last_bg);
383 void textbuffer_line2text(LINE_REC *line, int coloring, GString *str)
385 unsigned char cmd, *ptr, *tmp;
386 int last_fg, last_bg;
388 g_return_if_fail(line != NULL);
389 g_return_if_fail(str != NULL);
391 g_string_truncate(str, 0);
393 last_fg = last_bg = -1;
394 for (ptr = line->text;;) {
396 g_string_append_c(str, (char) *ptr);
405 if (cmd == LINE_CMD_EOL || cmd == LINE_CMD_FORMAT) {
410 if (cmd == LINE_CMD_CONTINUE) {
411 /* line continues in another address.. */
412 memcpy(&tmp, ptr, sizeof(unsigned char *));
418 /* no colors, skip coloring commands */
422 if ((cmd & 0x80) == 0) {
424 set_color(str, cmd, &last_fg, &last_bg);
425 } else switch (cmd) {
426 case LINE_CMD_UNDERLINE:
427 g_string_append_c(str, 31);
429 case LINE_CMD_REVERSE:
430 g_string_append_c(str, 22);
432 case LINE_CMD_COLOR0:
433 g_string_sprintfa(str, "\004%c%c",
434 '0', FORMAT_COLOR_NOCHANGE);
436 case LINE_CMD_INDENT:
438 case LINE_CMD_INDENT_FUNC:
439 ptr += sizeof(void *);
445 GList *textbuffer_find_text(TEXT_BUFFER_REC *buffer, LINE_REC *startline,
446 int level, int nolevel, const char *text,
447 int before, int after,
448 int regexp, int fullword, int case_sensitive)
453 LINE_REC *line, *pre_line;
456 int i, match_after, line_matched;
458 g_return_val_if_fail(buffer != NULL, NULL);
459 g_return_val_if_fail(text != NULL, NULL);
463 int flags = REG_EXTENDED | REG_NOSUB |
464 (case_sensitive ? 0 : REG_ICASE);
465 if (regcomp(&preg, text, flags) != 0)
472 matches = NULL; match_after = 0;
473 str = g_string_new(NULL);
475 line = startline != NULL ? startline : buffer->first_line;
477 for (; line != NULL; line = line->next) {
478 if ((line->info.level & level) == 0 ||
479 (line->info.level & nolevel) != 0)
483 /* no search word, everything matches */
484 textbuffer_line_ref(line);
485 matches = g_list_append(matches, line);
489 textbuffer_line2text(line, FALSE, str);
493 regexp ? regexec(&preg, str->str, 0, NULL, 0) == 0 :
495 fullword ? strstr_full_case(str->str, text, !case_sensitive) != NULL :
496 case_sensitive ? strstr(str->str, text) != NULL :
497 stristr(str->str, text) != NULL;
499 /* add the -before lines */
501 for (i = 0; i < before; i++) {
502 if (pre_line->prev == NULL ||
503 g_list_find(matches, pre_line->prev) != NULL)
505 pre_line = pre_line->prev;
508 for (; pre_line != line; pre_line = pre_line->next) {
509 textbuffer_line_ref(pre_line);
510 matches = g_list_append(matches, pre_line);
516 if (line_matched || match_after > 0) {
518 textbuffer_line_ref(line);
519 matches = g_list_append(matches, line);
521 if (!line_matched && --match_after == 0)
522 matches = g_list_append(matches, NULL);
526 if (regexp) regfree(&preg);
528 g_string_free(str, TRUE);
532 void textbuffer_init(void)
534 buffer_chunk = g_mem_chunk_new("text buffer chunk",
535 sizeof(TEXT_BUFFER_REC),
536 sizeof(TEXT_BUFFER_REC)*32, G_ALLOC_AND_FREE);
537 line_chunk = g_mem_chunk_new("line chunk", sizeof(LINE_REC),
538 sizeof(LINE_REC)*1024, G_ALLOC_AND_FREE);
539 text_chunk = g_mem_chunk_new("text chunk", sizeof(TEXT_CHUNK_REC),
540 sizeof(TEXT_CHUNK_REC)*32, G_ALLOC_AND_FREE);
543 void textbuffer_deinit(void)
545 g_mem_chunk_destroy(buffer_chunk);
546 g_mem_chunk_destroy(line_chunk);
547 g_mem_chunk_destroy(text_chunk);