source: fmtext.c @ 180cd15

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