1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
|
/* Copyright (c) 2002,2003,2004,2009 James M. Kretchmar
*
* This file is part of Owl.
*
* Owl is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Owl is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Owl. If not, see <http://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------
*
* As of Owl version 2.1.12 there are patches contributed by
* developers of the branched BarnOwl project, Copyright (c)
* 2006-2009 The BarnOwl Developers. All rights reserved.
*/
#include "owl.h"
#include <stdlib.h>
#include <string.h>
static const char fileIdent[] = "$Id: fmtext.c,v 1.17 2009/03/29 12:38:20 kretch Exp $";
/* initialize an fmtext with no data */
void owl_fmtext_init_null(owl_fmtext *f)
{
f->textlen=0;
f->textbuff=owl_strdup("");
f->fmbuff=owl_malloc(5);
f->colorbuff=owl_malloc(5);
f->fmbuff[0]=OWL_FMTEXT_ATTR_NONE;
f->colorbuff[0]=OWL_COLOR_DEFAULT;
}
/* Internal function. Set the attribute 'attr' from index 'first' to
* index 'last'
*/
void _owl_fmtext_set_attr(owl_fmtext *f, int attr, int first, int last)
{
int i;
for (i=first; i<=last; i++) {
f->fmbuff[i]=(unsigned char) attr;
}
}
/* Internal function. Add the attribute 'attr' to the existing
* attributes from index 'first' to index 'last'
*/
void _owl_fmtext_add_attr(owl_fmtext *f, int attr, int first, int last)
{
int i;
for (i=first; i<=last; i++) {
f->fmbuff[i]|=(unsigned char) attr;
}
}
/* Internal function. Set the color to be 'color' from index 'first'
* to index 'last
*/
void _owl_fmtext_set_color(owl_fmtext *f, int color, int first, int last)
{
int i;
for (i=first; i<=last; i++) {
f->colorbuff[i]=(unsigned char) color;
}
}
/* append text to the end of 'f' with attribute 'attr' and color
* 'color'
*/
void owl_fmtext_append_attr(owl_fmtext *f, char *text, int attr, int color)
{
int newlen;
newlen=strlen(f->textbuff)+strlen(text);
f->textbuff=owl_realloc(f->textbuff, newlen+2);
f->fmbuff=owl_realloc(f->fmbuff, newlen+2);
f->colorbuff=owl_realloc(f->colorbuff, newlen+2);
strcat(f->textbuff, text);
_owl_fmtext_set_attr(f, attr, f->textlen, newlen);
_owl_fmtext_set_color(f, color, f->textlen, newlen);
f->textlen=newlen;
}
/* Append normal, uncolored text 'text' to 'f' */
void owl_fmtext_append_normal(owl_fmtext *f, char *text)
{
owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_NONE, OWL_COLOR_DEFAULT);
}
/* Append normal text 'text' to 'f' with color 'color' */
void owl_fmtext_append_normal_color(owl_fmtext *f, char *text, int color)
{
owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_NONE, color);
}
/* Append bold text 'text' to 'f' */
void owl_fmtext_append_bold(owl_fmtext *f, char *text)
{
owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_BOLD, OWL_COLOR_DEFAULT);
}
/* Append reverse video text 'text' to 'f' */
void owl_fmtext_append_reverse(owl_fmtext *f, char *text)
{
owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_REVERSE, OWL_COLOR_DEFAULT);
}
/* Append reversed and bold, uncolored text 'text' to 'f' */
void owl_fmtext_append_reversebold(owl_fmtext *f, char *text)
{
owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_REVERSE | OWL_FMTEXT_ATTR_BOLD, OWL_COLOR_DEFAULT);
}
/* Add the attribute 'attr' to all text in 'f' */
void owl_fmtext_addattr(owl_fmtext *f, int attr)
{
/* add the attribute to all text */
int i, j;
j=f->textlen;
for (i=0; i<j; i++) {
f->fmbuff[i] |= attr;
}
}
/* Anywhere the color is NOT ALREDY SET, set the color to 'color'.
* Other colors are left unchanged
*/
void owl_fmtext_colorize(owl_fmtext *f, int color)
{
/* everywhere the color is OWL_COLOR_DEFAULT, change it to be 'color' */
int i, j;
j=f->textlen;
for(i=0; i<j; i++) {
if (f->colorbuff[i]==OWL_COLOR_DEFAULT) f->colorbuff[i] = color;
}
}
/* Internal function. Append text from 'in' between index 'start' and
* 'stop' to the end of 'f'
*/
void _owl_fmtext_append_fmtext(owl_fmtext *f, owl_fmtext *in, int start, int stop)
{
int newlen, i;
newlen=strlen(f->textbuff)+(stop-start+1);
f->textbuff=owl_realloc(f->textbuff, newlen+1);
f->fmbuff=owl_realloc(f->fmbuff, newlen+1);
f->colorbuff=owl_realloc(f->colorbuff, newlen+1);
strncat(f->textbuff, in->textbuff+start, stop-start+1);
f->textbuff[newlen]='\0';
for (i=start; i<=stop; i++) {
f->fmbuff[f->textlen+(i-start)]=in->fmbuff[i];
f->colorbuff[f->textlen+(i-start)]=in->colorbuff[i];
}
f->textlen=newlen;
}
/* append fmtext 'in' to 'f' */
void owl_fmtext_append_fmtext(owl_fmtext *f, owl_fmtext *in)
{
_owl_fmtext_append_fmtext(f, in, 0, in->textlen);
}
/* Append 'nspaces' number of spaces to the end of 'f' */
void owl_fmtext_append_spaces(owl_fmtext *f, int nspaces)
{
int i;
for (i=0; i<nspaces; i++) {
owl_fmtext_append_normal(f, " ");
}
}
/* Return a plain version of the fmtext. Caller is responsible for
* freeing the return
*/
char *owl_fmtext_print_plain(owl_fmtext *f)
{
return(owl_strdup(f->textbuff));
}
/* add the formatted text to the curses window 'w'. The window 'w'
* must already be initiatlized with curses
*/
void owl_fmtext_curs_waddstr(owl_fmtext *f, WINDOW *w)
{
char *tmpbuff;
int position, trans1, trans2, len, lastsame;
if (w==NULL) {
owl_function_debugmsg("Hit a null window in owl_fmtext_curs_waddstr.");
return;
}
tmpbuff=owl_malloc(f->textlen+10);
position=0;
len=f->textlen;
while (position<=len) {
/* find the last char with the current format and color */
trans1=owl_util_find_trans(f->fmbuff+position, len-position);
trans2=owl_util_find_trans(f->colorbuff+position, len-position);
if (trans1<trans2) {
lastsame=position+trans1;
} else {
lastsame=position+trans2;
}
/* set the format */
wattrset(w, A_NORMAL);
if (f->fmbuff[position] & OWL_FMTEXT_ATTR_BOLD) {
wattron(w, A_BOLD);
}
if (f->fmbuff[position] & OWL_FMTEXT_ATTR_REVERSE) {
wattron(w, A_REVERSE);
}
if (f->fmbuff[position] & OWL_FMTEXT_ATTR_UNDERLINE) {
wattron(w, A_UNDERLINE);
}
/* set the color */
/* warning, this is sort of a hack */
if (owl_global_get_hascolors(&g)) {
if (f->colorbuff[position]!=OWL_COLOR_DEFAULT) {
wattron(w, COLOR_PAIR(f->colorbuff[position]));
}
}
/* add the text */
strncpy(tmpbuff, f->textbuff + position, lastsame-position+1);
tmpbuff[lastsame-position+1]='\0';
waddstr(w, tmpbuff);
position=lastsame+1;
}
owl_free(tmpbuff);
}
/* start with line 'aline' (where the first line is 0) and print
* 'lines' number of lines into 'out'
*/
int owl_fmtext_truncate_lines(owl_fmtext *in, int aline, int lines, owl_fmtext *out)
{
char *ptr1, *ptr2;
int i, offset;
/* find the starting line */
ptr1=in->textbuff;
if (aline!=0) {
for (i=0; i<aline; i++) {
ptr1=strchr(ptr1, '\n');
if (!ptr1) return(-1);
ptr1++;
}
}
/* ptr1 now holds the starting point */
/* copy in the next 'lines' lines */
if (lines<1) return(-1);
for (i=0; i<lines; i++) {
offset=ptr1-in->textbuff;
ptr2=strchr(ptr1, '\n');
if (!ptr2) {
_owl_fmtext_append_fmtext(out, in, offset, (in->textlen)-1);
return(-1);
}
_owl_fmtext_append_fmtext(out, in, offset, (ptr2-ptr1)+offset);
ptr1=ptr2+1;
}
return(0);
}
/* Truncate the message so that each line begins at column 'acol' and
* ends at 'bcol' or sooner. The first column is number 0. The new
* message is placed in 'out'. The message is * expected to end in a
* new line for now
*/
void owl_fmtext_truncate_cols(owl_fmtext *in, int acol, int bcol, owl_fmtext *out)
{
char *ptr1, *ptr2, *last;
int len, offset;
last=in->textbuff+in->textlen-1;
ptr1=in->textbuff;
while (ptr1<=last) {
ptr2=strchr(ptr1, '\n');
if (!ptr2) {
/* but this shouldn't happen if we end in a \n */
break;
}
if (ptr2==ptr1) {
owl_fmtext_append_normal(out, "\n");
ptr1++;
continue;
}
/* we need to check that we won't run over here */
len=bcol-acol;
if (len > (ptr2-(ptr1+acol))) {
/* the whole line fits with room to spare, don't take a full 'len' */
len=ptr2-(ptr1+acol);
}
if (len>last-ptr1) {
/* the whole rest of the text fits with room to spare, adjust for it */
len-=(last-ptr1);
}
if (len<=0) {
/* saftey check */
owl_fmtext_append_normal(out, "\n");
ptr1=ptr2+1;
continue;
}
offset=ptr1-in->textbuff;
_owl_fmtext_append_fmtext(out, in, offset+acol, offset+acol+len);
ptr1=ptr2+1;
}
}
/* Return the number of lines in 'f' */
int owl_fmtext_num_lines(owl_fmtext *f)
{
int lines, i;
if (f->textlen==0) return(0);
lines=0;
for (i=0; i<f->textlen; i++) {
if (f->textbuff[i]=='\n') lines++;
}
/* if the last char wasn't a \n there's one more line */
if (f->textbuff[i-1]!='\n') lines++;
return(lines);
}
char *owl_fmtext_get_text(owl_fmtext *f)
{
return(f->textbuff);
}
/* set the charater at 'index' to be 'char'. If index is out of
* bounds don't do anything */
void owl_fmtext_set_char(owl_fmtext *f, int index, int ch)
{
if ((index < 0) || (index > f->textlen-1)) return;
f->textbuff[index]=ch;
}
/* Make a copy of the fmtext 'src' into 'dst' */
void owl_fmtext_copy(owl_fmtext *dst, owl_fmtext *src)
{
int mallocsize;
if (src->textlen==0) {
mallocsize=5;
} else {
mallocsize=src->textlen+2;
}
dst->textlen=src->textlen;
dst->textbuff=owl_malloc(mallocsize);
dst->fmbuff=owl_malloc(mallocsize);
dst->colorbuff=owl_malloc(mallocsize);
memcpy(dst->textbuff, src->textbuff, src->textlen+1);
memcpy(dst->fmbuff, src->fmbuff, src->textlen);
memcpy(dst->colorbuff, src->colorbuff, src->textlen);
}
/* highlight all instances of "string". Return the number of
* instances found. This is a case insensitive search.
*/
int owl_fmtext_search_and_highlight(owl_fmtext *f, char *string)
{
int found, len;
char *ptr1, *ptr2;
len=strlen(string);
found=0;
ptr1=f->textbuff;
while (ptr1-f->textbuff <= f->textlen) {
ptr2=stristr(ptr1, string);
if (!ptr2) return(found);
found++;
_owl_fmtext_add_attr(f, OWL_FMTEXT_ATTR_REVERSE,
ptr2 - f->textbuff,
ptr2 - f->textbuff + len - 1);
ptr1=ptr2+len;
}
return(found);
}
/* return 1 if the string is found, 0 if not. This is a case
* insensitive search.
*/
int owl_fmtext_search(owl_fmtext *f, char *string)
{
if (stristr(f->textbuff, string)) return(1);
return(0);
}
/* Append the text 'text' to 'f' and interpret the zephyr style
* formatting syntax to set appropriate attributes.
*/
void owl_fmtext_append_ztext(owl_fmtext *f, char *text)
{
int stacksize, curattrs, curcolor;
char *ptr, *txtptr, *buff, *tmpptr;
int attrstack[32], chrstack[32];
curattrs=OWL_FMTEXT_ATTR_NONE;
curcolor=OWL_COLOR_DEFAULT;
stacksize=0;
txtptr=text;
while (1) {
ptr=strpbrk(txtptr, "@{[<()>]}");
if (!ptr) {
/* add all the rest of the text and exit */
owl_fmtext_append_attr(f, txtptr, curattrs, curcolor);
return;
} else if (ptr[0]=='@') {
/* add the text up to this point then deal with the stack */
buff=owl_malloc(ptr-txtptr+20);
strncpy(buff, txtptr, ptr-txtptr);
buff[ptr-txtptr]='\0';
owl_fmtext_append_attr(f, buff, curattrs, curcolor);
owl_free(buff);
/* update pointer to point at the @ */
txtptr=ptr;
/* now the stack */
/* if we've hit our max stack depth, print the @ and move on */
if (stacksize==32) {
owl_fmtext_append_attr(f, "@", curattrs, curcolor);
txtptr++;
continue;
}
/* if it's an @@, print an @ and continue */
if (txtptr[1]=='@') {
owl_fmtext_append_attr(f, "@", curattrs, curcolor);
txtptr+=2;
continue;
}
/* if there's no opener, print the @ and continue */
tmpptr=strpbrk(txtptr, "(<[{ ");
if (!tmpptr || tmpptr[0]==' ') {
owl_fmtext_append_attr(f, "@", curattrs, curcolor);
txtptr++;
continue;
}
/* check what command we've got, push it on the stack, start
using it, and continue ... unless it's a color command */
buff=owl_malloc(tmpptr-ptr+20);
strncpy(buff, ptr, tmpptr-ptr);
buff[tmpptr-ptr]='\0';
if (!strcasecmp(buff, "@bold")) {
attrstack[stacksize]=OWL_FMTEXT_ATTR_BOLD;
chrstack[stacksize]=tmpptr[0];
stacksize++;
curattrs|=OWL_FMTEXT_ATTR_BOLD;
txtptr+=6;
owl_free(buff);
continue;
} else if (!strcasecmp(buff, "@b")) {
attrstack[stacksize]=OWL_FMTEXT_ATTR_BOLD;
chrstack[stacksize]=tmpptr[0];
stacksize++;
curattrs|=OWL_FMTEXT_ATTR_BOLD;
txtptr+=3;
owl_free(buff);
continue;
} else if (!strcasecmp(buff, "@i")) {
attrstack[stacksize]=OWL_FMTEXT_ATTR_UNDERLINE;
chrstack[stacksize]=tmpptr[0];
stacksize++;
curattrs|=OWL_FMTEXT_ATTR_UNDERLINE;
txtptr+=3;
owl_free(buff);
continue;
} else if (!strcasecmp(buff, "@italic")) {
attrstack[stacksize]=OWL_FMTEXT_ATTR_UNDERLINE;
chrstack[stacksize]=tmpptr[0];
stacksize++;
curattrs|=OWL_FMTEXT_ATTR_UNDERLINE;
txtptr+=8;
owl_free(buff);
continue;
/* if it's a color read the color, set the current color and
continue */
} else if (!strcasecmp(buff, "@color")
&& owl_global_get_hascolors(&g)
&& owl_global_is_colorztext(&g)) {
owl_free(buff);
txtptr+=7;
tmpptr=strpbrk(txtptr, "@{[<()>]}");
if (tmpptr &&
((txtptr[-1]=='(' && tmpptr[0]==')') ||
(txtptr[-1]=='<' && tmpptr[0]=='>') ||
(txtptr[-1]=='[' && tmpptr[0]==']') ||
(txtptr[-1]=='{' && tmpptr[0]=='}'))) {
/* grab the color name */
buff=owl_malloc(tmpptr-txtptr+20);
strncpy(buff, txtptr, tmpptr-txtptr);
buff[tmpptr-txtptr]='\0';
/* set it as the current color */
curcolor=owl_util_string_to_color(buff);
if (curcolor==-1) curcolor=OWL_COLOR_DEFAULT;
owl_free(buff);
txtptr=tmpptr+1;
continue;
} else {
}
} else {
/* if we didn't understand it, we'll print it. This is different from zwgc
* but zwgc seems to be smarter about some screw cases than I am
*/
owl_fmtext_append_attr(f, "@", curattrs, curcolor);
txtptr++;
continue;
}
} else if (ptr[0]=='}' || ptr[0]==']' || ptr[0]==')' || ptr[0]=='>') {
/* add the text up to this point first */
buff=owl_malloc(ptr-txtptr+20);
strncpy(buff, txtptr, ptr-txtptr);
buff[ptr-txtptr]='\0';
owl_fmtext_append_attr(f, buff, curattrs, curcolor);
owl_free(buff);
/* now deal with the closer */
txtptr=ptr;
/* first, if the stack is empty we must bail (just print and go) */
if (stacksize==0) {
buff=owl_malloc(5);
buff[0]=ptr[0];
buff[1]='\0';
owl_fmtext_append_attr(f, buff, curattrs, curcolor);
owl_free(buff);
txtptr++;
continue;
}
/* if the closing char is what's on the stack, turn off the
attribue and pop the stack */
if ((ptr[0]==')' && chrstack[stacksize-1]=='(') ||
(ptr[0]=='>' && chrstack[stacksize-1]=='<') ||
(ptr[0]==']' && chrstack[stacksize-1]=='[') ||
(ptr[0]=='}' && chrstack[stacksize-1]=='{')) {
int i;
stacksize--;
curattrs=OWL_FMTEXT_ATTR_NONE;
for (i=0; i<stacksize; i++) {
curattrs|=attrstack[i];
}
txtptr+=1;
continue;
} else {
/* otherwise print and continue */
buff=owl_malloc(5);
buff[0]=ptr[0];
buff[1]='\0';
owl_fmtext_append_attr(f, buff, curattrs, curcolor);
owl_free(buff);
txtptr++;
continue;
}
} else {
/* we've found an unattached opener, print everything and move on */
buff=owl_malloc(ptr-txtptr+20);
strncpy(buff, txtptr, ptr-txtptr+1);
buff[ptr-txtptr+1]='\0';
owl_fmtext_append_attr(f, buff, curattrs, curcolor);
owl_free(buff);
txtptr=ptr+1;
continue;
}
}
}
/* requires that the list values are strings or NULL.
* joins the elements together with join_with.
* If format_fn is specified, passes it the list element value
* and it will return a string which this needs to free. */
void owl_fmtext_append_list(owl_fmtext *f, owl_list *l, char *join_with, char *(format_fn)(void*))
{
int i, size;
void *elem;
char *text;
size = owl_list_get_size(l);
for (i=0; i<size; i++) {
elem = (char*)owl_list_get_element(l,i);
if (elem && format_fn) {
text = format_fn(elem);
if (text) {
owl_fmtext_append_normal(f, text);
owl_free(text);
}
} else if (elem) {
owl_fmtext_append_normal(f, elem);
}
if ((i < size-1) && join_with) {
owl_fmtext_append_normal(f, join_with);
}
}
}
/* Free all memory allocated by the object */
void owl_fmtext_free(owl_fmtext *f)
{
if (f->textbuff) owl_free(f->textbuff);
if (f->fmbuff) owl_free(f->fmbuff);
if (f->colorbuff) owl_free(f->colorbuff);
}
|