- Timestamp:
- Feb 4, 2011, 3:49:44 PM (14 years ago)
- Branches:
- master, release-1.10, release-1.8, release-1.9
- Children:
- 9de316d1
- Parents:
- d3c318b
- git-author:
- David Benjamin <davidben@mit.edu> (01/24/11 20:58:41)
- git-committer:
- David Benjamin <davidben@mit.edu> (02/04/11 15:49:44)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
fmtext.c
rd3c318b r7e111f4 6 6 void owl_fmtext_init_null(owl_fmtext *f) 7 7 { 8 f->textlen = 0; 9 f->bufflen = 5; 10 f->textbuff = owl_malloc(5); 11 f->textbuff[0] = 0; 8 f->buff = g_string_new(""); 12 9 f->default_attrs = OWL_FMTEXT_ATTR_NONE; 13 10 f->default_fgcolor = OWL_COLOR_DEFAULT; … … 19 16 void owl_fmtext_clear(owl_fmtext *f) 20 17 { 21 f->textlen = 0; 22 f->textbuff[0] = 0; 18 g_string_truncate(f->buff, 0); 23 19 f->default_attrs = OWL_FMTEXT_ATTR_NONE; 24 20 f->default_fgcolor = OWL_COLOR_DEFAULT; 25 21 f->default_bgcolor = OWL_COLOR_DEFAULT; 26 }27 28 static void _owl_fmtext_realloc(owl_fmtext *f, int newlen)29 {30 if(newlen + 1 > f->bufflen) {31 f->textbuff = owl_realloc(f->textbuff, newlen + 1);32 f->bufflen = newlen+1;33 }34 22 } 35 23 … … 45 33 void owl_fmtext_append_attr(owl_fmtext *f, const char *text, char attr, short fgcolor, short bgcolor) 46 34 { 47 char attrbuff[6]; 48 int newlen, a = 0, fg = 0, bg = 0; 35 int a = 0, fg = 0, bg = 0; 49 36 50 37 if (attr != OWL_FMTEXT_ATTR_NONE) a=1; … … 52 39 if (bgcolor != OWL_COLOR_DEFAULT) bg=1; 53 40 54 /* Plane-16 characters in UTF-8 are 4 bytes long. */55 newlen = strlen(f->textbuff) + strlen(text) + (8 * (a + fg + bg));56 _owl_fmtext_realloc(f, newlen);57 58 41 /* Set attributes */ 59 42 if (a) 60 strncat(f->textbuff, attrbuff, 61 g_unichar_to_utf8(OWL_FMTEXT_UC_ATTR | attr, attrbuff)); 43 g_string_append_unichar(f->buff, OWL_FMTEXT_UC_ATTR | attr); 62 44 if (fg) 63 strncat(f->textbuff, attrbuff, 64 g_unichar_to_utf8(OWL_FMTEXT_UC_FGCOLOR | fgcolor, attrbuff)); 45 g_string_append_unichar(f->buff, OWL_FMTEXT_UC_FGCOLOR | fgcolor); 65 46 if (bg) 66 strncat(f->textbuff, attrbuff, 67 g_unichar_to_utf8(OWL_FMTEXT_UC_BGCOLOR | bgcolor, attrbuff)); 68 69 strcat(f->textbuff, text); 47 g_string_append_unichar(f->buff, OWL_FMTEXT_UC_BGCOLOR | bgcolor); 48 49 g_string_append(f->buff, text); 70 50 71 51 /* Reset attributes */ 72 if (bg) strcat(f->textbuff, OWL_FMTEXT_UTF8_BGDEFAULT); 73 if (fg) strcat(f->textbuff, OWL_FMTEXT_UTF8_FGDEFAULT); 74 if (a) strcat(f->textbuff, OWL_FMTEXT_UTF8_ATTR_NONE); 75 f->textlen=newlen; 52 if (bg) g_string_append(f->buff, OWL_FMTEXT_UTF8_BGDEFAULT); 53 if (fg) g_string_append(f->buff, OWL_FMTEXT_UTF8_FGDEFAULT); 54 if (a) g_string_append(f->buff, OWL_FMTEXT_UTF8_ATTR_NONE); 76 55 } 77 56 … … 168 147 { 169 148 const char *p; 170 p = strchr(f-> textbuff, OWL_FMTEXT_UC_STARTBYTE_UTF8);171 while (p && p < f-> textbuff+ start) {149 p = strchr(f->buff->str, OWL_FMTEXT_UC_STARTBYTE_UTF8); 150 while (p && p < f->buff->str + start) { 172 151 _owl_fmtext_update_attributes(g_utf8_get_char(p), attr, fgcolor, bgcolor); 173 152 p = strchr(p+1, OWL_FMTEXT_UC_STARTBYTE_UTF8); … … 181 160 static void _owl_fmtext_append_fmtext(owl_fmtext *f, const owl_fmtext *in, int start, int stop) 182 161 { 183 char attrbuff[6]; 184 int newlen, a = 0, fg = 0, bg = 0; 162 int a = 0, fg = 0, bg = 0; 185 163 char attr = 0; 186 164 short fgcolor = OWL_COLOR_DEFAULT; … … 192 170 if (bgcolor != OWL_COLOR_DEFAULT) bg=1; 193 171 194 /* We will reset to defaults after appending the text. We may need195 to set initial attributes. */196 newlen=strlen(f->textbuff)+(stop-start) + (4 * (a + fg + bg)) + 12;197 _owl_fmtext_realloc(f, newlen);198 199 172 if (a) 200 strncat(f->textbuff, attrbuff, 201 g_unichar_to_utf8(OWL_FMTEXT_UC_ATTR | attr, attrbuff)); 173 g_string_append_unichar(f->buff, OWL_FMTEXT_UC_ATTR | attr); 202 174 if (fg) 203 strncat(f->textbuff, attrbuff, 204 g_unichar_to_utf8(OWL_FMTEXT_UC_FGCOLOR | fgcolor, attrbuff)); 175 g_string_append_unichar(f->buff, OWL_FMTEXT_UC_FGCOLOR | fgcolor); 205 176 if (bg) 206 strncat(f->textbuff, attrbuff, 207 g_unichar_to_utf8(OWL_FMTEXT_UC_BGCOLOR | bgcolor, attrbuff)); 208 209 strncat(f->textbuff, in->textbuff+start, stop-start); 177 g_string_append_unichar(f->buff, OWL_FMTEXT_UC_BGCOLOR | bgcolor); 178 179 g_string_append_len(f->buff, in->buff->str+start, stop-start); 210 180 211 181 /* Reset attributes */ 212 strcat(f->textbuff, OWL_FMTEXT_UTF8_BGDEFAULT); 213 strcat(f->textbuff, OWL_FMTEXT_UTF8_FGDEFAULT); 214 strcat(f->textbuff, OWL_FMTEXT_UTF8_ATTR_NONE); 215 216 f->textbuff[newlen]='\0'; 217 f->textlen=newlen; 182 g_string_append(f->buff, OWL_FMTEXT_UTF8_BGDEFAULT); 183 g_string_append(f->buff, OWL_FMTEXT_UTF8_FGDEFAULT); 184 g_string_append(f->buff, OWL_FMTEXT_UTF8_ATTR_NONE); 218 185 } 219 186 … … 221 188 void owl_fmtext_append_fmtext(owl_fmtext *f, const owl_fmtext *in) 222 189 { 223 _owl_fmtext_append_fmtext(f, in, 0, in-> textlen);190 _owl_fmtext_append_fmtext(f, in, 0, in->buff->len); 224 191 225 192 } … … 239 206 char *owl_fmtext_print_plain(const owl_fmtext *f) 240 207 { 241 return owl_strip_format_chars(f-> textbuff);208 return owl_strip_format_chars(f->buff->str); 242 209 } 243 210 … … 281 248 } 282 249 283 s = f-> textbuff;250 s = f->buff->str; 284 251 /* Set default attributes. */ 285 252 attr = f->default_attrs; … … 375 342 out->default_bgcolor = in->default_bgcolor; 376 343 377 for (ptr = in-> textbuff;378 ptr < in-> textbuff + in->textlen;344 for (ptr = in->buff->str; 345 ptr < in->buff->str + in->buff->len; 379 346 ptr = g_utf8_next_char(ptr)) { 380 347 gunichar c = g_utf8_get_char(ptr); … … 382 349 if (c == '\t') { 383 350 /* Copy up to this tab */ 384 _owl_fmtext_append_fmtext(out, in, numcopied, ptr - in-> textbuff);351 _owl_fmtext_append_fmtext(out, in, numcopied, ptr - in->buff->str); 385 352 /* and then copy spaces for the tab. */ 386 353 chwidth = OWL_TAB_WIDTH - (col % OWL_TAB_WIDTH); 387 354 owl_fmtext_append_spaces(out, chwidth); 388 355 col += chwidth; 389 numcopied = g_utf8_next_char(ptr) - in-> textbuff;356 numcopied = g_utf8_next_char(ptr) - in->buff->str; 390 357 } else { 391 358 /* Just update col. We'll append later. */ … … 398 365 } 399 366 /* Append anything we've missed. */ 400 if (numcopied < in-> textlen)401 _owl_fmtext_append_fmtext(out, in, numcopied, in-> textlen);367 if (numcopied < in->buff->len) 368 _owl_fmtext_append_fmtext(out, in, numcopied, in->buff->len); 402 369 } 403 370 … … 411 378 412 379 /* find the starting line */ 413 ptr1 = in-> textbuff;380 ptr1 = in->buff->str; 414 381 for (i = 0; i < aline; i++) { 415 382 ptr1 = strchr(ptr1, '\n'); … … 429 396 430 397 for (i = 0; i < lines; i++) { 431 offset = ptr1 - in-> textbuff;398 offset = ptr1 - in->buff->str; 432 399 ptr2 = strchr(ptr1, '\n'); 433 400 if (!ptr2) { 434 401 /* Copy to the end of the buffer. */ 435 _owl_fmtext_append_fmtext(out, in, offset, in-> textlen);402 _owl_fmtext_append_fmtext(out, in, offset, in->buff->len); 436 403 return(-1); 437 404 } … … 454 421 out->default_bgcolor = in->default_bgcolor; 455 422 456 last =in->textbuff+in->textlen-1;457 ptr_s =in->textbuff;423 last = in->buff->str + in->buff->len - 1; 424 ptr_s = in->buff->str; 458 425 while (ptr_s <= last) { 459 426 ptr_e=strchr(ptr_s, '\n'); … … 497 464 if (ptr_c == ptr_e) { 498 465 /* We made it to the newline. Append up to, and including it. */ 499 _owl_fmtext_append_fmtext(out, in, ptr_s - in-> textbuff, ptr_c - in->textbuff+ 1);466 _owl_fmtext_append_fmtext(out, in, ptr_s - in->buff->str, ptr_c - in->buff->str + 1); 500 467 } 501 468 else if (chwidth > 1) { 502 469 /* Last char is wide, truncate. */ 503 _owl_fmtext_append_fmtext(out, in, ptr_s - in-> textbuff, ptr_c - in->textbuff);470 _owl_fmtext_append_fmtext(out, in, ptr_s - in->buff->str, ptr_c - in->buff->str); 504 471 owl_fmtext_append_normal(out, "\n"); 505 472 } … … 508 475 * sure we get it all. */ 509 476 ptr_c = g_utf8_next_char(ptr_c); 510 _owl_fmtext_append_fmtext(out, in, ptr_s - in-> textbuff, ptr_c - in->textbuff);477 _owl_fmtext_append_fmtext(out, in, ptr_s - in->buff->str, ptr_c - in->buff->str); 511 478 } 512 479 } … … 535 502 536 503 /* _owl_fmtext_truncate_cols_internal cannot handle tabs. */ 537 if (strchr(in-> textbuff, '\t')) {504 if (strchr(in->buff->str, '\t')) { 538 505 owl_fmtext_init_null(¬abs); 539 506 owl_fmtext_expand_tabs(in, ¬abs, 0); … … 552 519 553 520 lines=0; 554 lastbreak = f-> textbuff;555 for (i =0; i<f->textlen; i++) {556 if (f-> textbuff[i]=='\n') {557 lastbreak = f-> textbuff+ i;521 lastbreak = f->buff->str; 522 for (i = 0; i < f->buff->len; i++) { 523 if (f->buff->str[i]=='\n') { 524 lastbreak = f->buff->str + i; 558 525 lines++; 559 526 } … … 562 529 /* Check if there's a trailing line; formatting characters don't count. */ 563 530 for (p = g_utf8_next_char(lastbreak); 564 p < f-> textbuff + f->textlen;531 p < f->buff->str + f->buff->len; 565 532 p = g_utf8_next_char(p)) { 566 533 if (!owl_fmtext_is_format_char(g_utf8_get_char(p))) { … … 581 548 { 582 549 int i, lineno = 0; 583 if (offset >= f-> textlen)584 offset = f-> textlen - 1;550 if (offset >= f->buff->len) 551 offset = f->buff->len - 1; 585 552 for (i = 0; i < offset; i++) { 586 if (f-> textbuff[i] == '\n')553 if (f->buff->str[i] == '\n') 587 554 lineno++; 588 555 } … … 596 563 int start, end; 597 564 char *newline; 598 for (start = 0; lineno > 0 && start < f-> textlen; start++) {599 if (f-> textbuff[start] == '\n')565 for (start = 0; lineno > 0 && start < f->buff->len; start++) { 566 if (f->buff->str[start] == '\n') 600 567 lineno--; 601 568 } 602 newline = strchr(f-> textbuff+ start, '\n');569 newline = strchr(f->buff->str + start, '\n'); 603 570 /* Include the newline, if it is there. */ 604 end = newline ? newline - f-> textbuff + 1 : f->textlen;571 end = newline ? newline - f->buff->str + 1 : f->buff->len; 605 572 if (o_start) *o_start = start; 606 573 if (o_end) *o_end = end; … … 609 576 const char *owl_fmtext_get_text(const owl_fmtext *f) 610 577 { 611 return (f->textbuff);578 return f->buff->str; 612 579 } 613 580 614 581 int owl_fmtext_num_bytes(const owl_fmtext *f) 615 582 { 616 return (f->textlen);583 return f->buff->len; 617 584 } 618 585 … … 622 589 void owl_fmtext_set_char(owl_fmtext *f, int index, char ch) 623 590 { 624 if ((index < 0) || (index > f-> textlen-1)) return;591 if ((index < 0) || (index > f->buff->len - 1)) return; 625 592 /* NOT ASCII*/ 626 if (f-> textbuff[index] & 0x80 || ch & 0x80) return;627 f-> textbuff[index]=ch;593 if (f->buff->str[index] & 0x80 || ch & 0x80) return; 594 f->buff->str[index] = ch; 628 595 } 629 596 … … 631 598 void owl_fmtext_copy(owl_fmtext *dst, const owl_fmtext *src) 632 599 { 633 int mallocsize; 634 635 if (src->textlen==0) { 636 mallocsize=5; 637 } else { 638 mallocsize=src->textlen+2; 639 } 640 dst->textlen=src->textlen; 641 dst->bufflen=mallocsize; 642 dst->textbuff=owl_malloc(mallocsize); 643 memcpy(dst->textbuff, src->textbuff, src->textlen+1); 600 dst->buff = g_string_new(src->buff->str); 644 601 dst->default_attrs = src->default_attrs; 645 602 dst->default_fgcolor = src->default_fgcolor; … … 654 611 { 655 612 int offset; 656 if (start > f-> textlen ||657 owl_regex_compare(re, f-> textbuff+ start, &offset, NULL) != 0)613 if (start > f->buff->len || 614 owl_regex_compare(re, f->buff->str + start, &offset, NULL) != 0) 658 615 return -1; 659 616 return offset + start; … … 897 854 void owl_fmtext_cleanup(owl_fmtext *f) 898 855 { 899 if (f->textbuff) owl_free(f->textbuff); 856 if (f->buff) g_string_free(f->buff, true); 857 f->buff = NULL; 900 858 } 901 859
Note: See TracChangeset
for help on using the changeset viewer.