source: fmtext.c @ 7d4fbcd

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 7d4fbcd was 7d4fbcd, checked in by James M. Kretchmar <kretch@mit.edu>, 22 years ago
Initial check in
  • Property mode set to 100644
File size: 12.6 KB
Line 
1#include "owl.h"
2#include <stdlib.h>
3#include <string.h>
4
5void owl_fmtext_init_null(owl_fmtext *f) {
6  f->textlen=0;
7  f->textbuff=owl_strdup("");
8  f->fmbuff=owl_malloc(5);
9  f->colorbuff=owl_malloc(5);
10  f->fmbuff[0]=OWL_FMTEXT_ATTR_NONE;
11  f->colorbuff[0]=OWL_COLOR_DEFAULT;
12}
13
14
15void _owl_fmtext_set_attr(owl_fmtext *f, int attr, int first, int last) {
16  int i;
17  for (i=first; i<=last; i++) {
18    f->fmbuff[i]=(unsigned char) attr;
19  }
20}
21
22void _owl_fmtext_set_color(owl_fmtext *f, int color, int first, int last) {
23  int i;
24  for (i=first; i<=last; i++) {
25    f->colorbuff[i]=(unsigned char) color;
26  }
27}
28
29
30void owl_fmtext_append_attr(owl_fmtext *f, char *text, int attr, int color) {
31  int newlen;
32
33  newlen=strlen(f->textbuff)+strlen(text);
34  f->textbuff=owl_realloc(f->textbuff, newlen+2);
35  f->fmbuff=owl_realloc(f->fmbuff, newlen+2);
36  f->colorbuff=owl_realloc(f->colorbuff, newlen+2);
37
38  strcat(f->textbuff, text);
39  _owl_fmtext_set_attr(f, attr, f->textlen, newlen);
40  _owl_fmtext_set_color(f, color, f->textlen, newlen);
41  f->textlen=newlen;
42}
43
44
45void owl_fmtext_append_normal(owl_fmtext *f, char *text) {
46  owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_NONE, OWL_COLOR_DEFAULT);
47}
48
49void owl_fmtext_append_normal_color(owl_fmtext *f, char *text, int color) {
50  owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_NONE, color);
51}
52
53
54void owl_fmtext_append_bold(owl_fmtext *f, char *text) {
55  owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_BOLD, OWL_COLOR_DEFAULT);
56}
57
58
59void owl_fmtext_append_reverse(owl_fmtext *f, char *text) {
60  owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_REVERSE, OWL_COLOR_DEFAULT);
61}
62
63
64void owl_fmtext_append_reversebold(owl_fmtext *f, char *text) {
65  owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_REVERSE | OWL_FMTEXT_ATTR_BOLD, OWL_COLOR_DEFAULT);
66}
67
68
69void owl_fmtext_addattr(owl_fmtext *f, int attr) {
70  /* add the attribute to all text */
71  int i, j;
72
73  j=f->textlen;
74  for (i=0; i<j; i++) {
75    f->fmbuff[i] |= attr;
76  }
77}
78
79void owl_fmtext_colorize(owl_fmtext *f, int color) {
80  /* everywhere the color is OWL_COLOR_DEFAULT, change it to be 'color' */
81  int i, j;
82
83  j=f->textlen;
84  for(i=0; i<j; i++) {
85    if (f->colorbuff[i]==OWL_COLOR_DEFAULT) f->colorbuff[i] = color;
86  }
87}
88
89
90void owl_fmtext_append_ztext(owl_fmtext *f, char *text) {
91  int stacksize, curattrs, curcolor;
92  char *ptr, *txtptr, *buff, *tmpptr;
93  int attrstack[32], chrstack[32];
94
95  curattrs=OWL_FMTEXT_ATTR_NONE;
96  curcolor=OWL_COLOR_DEFAULT;
97  stacksize=0;
98  txtptr=text;
99  while (1) {
100    ptr=strpbrk(txtptr, "@{[<()>]}");
101    if (!ptr) {
102      /* add all the rest of the text and exit */
103      owl_fmtext_append_attr(f, txtptr, curattrs, curcolor);
104      return;
105    } else if (ptr[0]=='@') {
106      /* add the text up to this point then deal with the stack */
107      buff=owl_malloc(ptr-txtptr+20);
108      strncpy(buff, txtptr, ptr-txtptr);
109      buff[ptr-txtptr]='\0';
110      owl_fmtext_append_attr(f, buff, curattrs, curcolor);
111      owl_free(buff);
112
113      /* update pointer to point at the @ */
114      txtptr=ptr;
115
116      /* now the stack */
117
118      /* if we've hit our max stack depth, print the @ and move on */
119      if (stacksize==32) {
120        owl_fmtext_append_attr(f, "@", curattrs, curcolor);
121        txtptr++;
122        continue;
123      }
124
125      /* if it's an @@, print an @ and continue */
126      if (txtptr[1]=='@') {
127        owl_fmtext_append_attr(f, "@", curattrs, curcolor);
128        txtptr+=2;
129        continue;
130      }
131       
132      /* if there's no opener, print the @ and continue */
133      tmpptr=strpbrk(txtptr, "(<[{ ");
134      if (!tmpptr || tmpptr[0]==' ') {
135        owl_fmtext_append_attr(f, "@", curattrs, curcolor);
136        txtptr++;
137        continue;
138      }
139
140      /* check what command we've got, push it on the stack, start
141         using it, and continue ... unless it's a color command */
142      buff=owl_malloc(tmpptr-ptr+20);
143      strncpy(buff, ptr, tmpptr-ptr);
144      buff[tmpptr-ptr]='\0';
145      if (!strcasecmp(buff, "@bold")) {
146        attrstack[stacksize]=OWL_FMTEXT_ATTR_BOLD;
147        chrstack[stacksize]=tmpptr[0];
148        stacksize++;
149        curattrs|=OWL_FMTEXT_ATTR_BOLD;
150        txtptr+=6;
151        owl_free(buff);
152        continue;
153      } else if (!strcasecmp(buff, "@b")) {
154        attrstack[stacksize]=OWL_FMTEXT_ATTR_BOLD;
155        chrstack[stacksize]=tmpptr[0];
156        stacksize++;
157        curattrs|=OWL_FMTEXT_ATTR_BOLD;
158        txtptr+=3;
159        owl_free(buff);
160        continue;
161      } else if (!strcasecmp(buff, "@i")) {
162        attrstack[stacksize]=OWL_FMTEXT_ATTR_UNDERLINE;
163        chrstack[stacksize]=tmpptr[0];
164        stacksize++;
165        curattrs|=OWL_FMTEXT_ATTR_UNDERLINE;
166        txtptr+=3;
167        owl_free(buff);
168        continue;
169      } else if (!strcasecmp(buff, "@italic")) {
170        attrstack[stacksize]=OWL_FMTEXT_ATTR_UNDERLINE;
171        chrstack[stacksize]=tmpptr[0];
172        stacksize++;
173        curattrs|=OWL_FMTEXT_ATTR_UNDERLINE;
174        txtptr+=8;
175        owl_free(buff);
176        continue;
177
178        /* if it's a color read the color, set the current color and
179           continue */
180      } else if (!strcasecmp(buff, "@color") && owl_global_get_hascolors(&g)) {
181        owl_free(buff);
182        txtptr+=7;
183        tmpptr=strpbrk(txtptr, "@{[<()>]}");
184        if (tmpptr &&
185            ((txtptr[-1]=='(' && tmpptr[0]==')') ||
186             (txtptr[-1]=='<' && tmpptr[0]=='>') ||
187             (txtptr[-1]=='[' && tmpptr[0]==']') ||
188             (txtptr[-1]=='{' && tmpptr[0]=='}'))) {
189
190          /* grab the color name */
191          buff=owl_malloc(tmpptr-txtptr+20);
192          strncpy(buff, txtptr, tmpptr-txtptr);
193          buff[tmpptr-txtptr]='\0';
194
195          /* set it as the current color */
196          curcolor=owl_util_string_to_color(buff);
197          owl_free(buff);
198          txtptr=tmpptr+1;
199          continue;
200
201        } else {
202
203        }
204
205      } else {
206        /* if we didn't understand it, we'll print it.  This is different from zwgc
207           but zwgc seems to be smarter about some screw cases than I am */
208        owl_fmtext_append_attr(f, "@", curattrs, curcolor);
209        txtptr++;
210        continue;
211      }
212
213    } else if (ptr[0]=='}' || ptr[0]==']' || ptr[0]==')' || ptr[0]=='>') {
214      /* add the text up to this point first */
215      buff=owl_malloc(ptr-txtptr+20);
216      strncpy(buff, txtptr, ptr-txtptr);
217      buff[ptr-txtptr]='\0';
218      owl_fmtext_append_attr(f, buff, curattrs, curcolor);
219      owl_free(buff);
220
221      /* now deal with the closer */
222      txtptr=ptr;
223
224      /* first, if the stack is empty we must bail (just print and go) */
225      if (stacksize==0) {
226        buff=owl_malloc(5);
227        buff[0]=ptr[0];
228        buff[1]='\0';
229        owl_fmtext_append_attr(f, buff, curattrs, curcolor);
230        owl_free(buff);
231        txtptr++;
232        continue;
233      }
234
235      /* if the closing char is what's on the stack, turn off the
236         attribue and pop the stack */
237      if ((ptr[0]==')' && chrstack[stacksize-1]=='(') ||
238          (ptr[0]=='>' && chrstack[stacksize-1]=='<') ||
239          (ptr[0]==']' && chrstack[stacksize-1]=='[') ||
240          (ptr[0]=='}' && chrstack[stacksize-1]=='{')) {
241        int i;
242        stacksize--;
243        curattrs=OWL_FMTEXT_ATTR_NONE;
244        for (i=0; i<stacksize; i++) {
245          curattrs|=attrstack[i];
246        }
247        txtptr+=1;
248        continue;
249      } else {
250        /* otherwise print and continue */
251        buff=owl_malloc(5);
252        buff[0]=ptr[0];
253        buff[1]='\0';
254        owl_fmtext_append_attr(f, buff, curattrs, curcolor);
255        owl_free(buff);
256        txtptr++;
257        continue;
258      }
259    } else {
260      /* we've found an unattached opener, print everything and move on */
261      buff=owl_malloc(ptr-txtptr+20);
262      strncpy(buff, txtptr, ptr-txtptr+1);
263      buff[ptr-txtptr+1]='\0';
264      owl_fmtext_append_attr(f, buff, curattrs, curcolor);
265      owl_free(buff);
266      txtptr=ptr+1;
267      continue;
268    }
269  }
270
271}
272
273void owl_fmtext_append_fmtext(owl_fmtext *f, owl_fmtext *in, int start, int stop) {
274  int newlen, i;
275
276  newlen=strlen(f->textbuff)+(stop-start+1);
277  f->textbuff=owl_realloc(f->textbuff, newlen+1);
278  f->fmbuff=owl_realloc(f->fmbuff, newlen+1);
279  f->colorbuff=owl_realloc(f->colorbuff, newlen+1);
280
281  strncat(f->textbuff, in->textbuff+start, stop-start+1);
282  f->textbuff[newlen]='\0';
283  for (i=start; i<=stop; i++) {
284    f->fmbuff[f->textlen+(i-start)]=in->fmbuff[i];
285    f->colorbuff[f->textlen+(i-start)]=in->colorbuff[i];
286  }
287  f->textlen=newlen;
288}
289
290void owl_fmtext_append_spaces(owl_fmtext *f, int nspaces) {
291  int i;
292  for (i=0; i<nspaces; i++) {
293    owl_fmtext_append_normal(f, " ");
294  }
295}
296
297/* requires that the list values are strings or NULL.
298 * joins the elements together with join_with.
299 * If format_fn is specified, passes it the list element value
300 * and it will return a string which this needs to free. */
301void owl_fmtext_append_list(owl_fmtext *f, owl_list *l, char *join_with, char *(format_fn)(void*)) {
302  int i, size;
303  void *elem;
304  char *text;
305
306  size = owl_list_get_size(l);
307  for (i=0; i<size; i++) {
308    elem = (char*)owl_list_get_element(l,i);
309    if (elem && format_fn) {
310      text = format_fn(elem);
311      if (text) {
312        owl_fmtext_append_normal(f, text);
313        owl_free(text);
314      }
315    } else if (elem) {
316      owl_fmtext_append_normal(f, elem);
317    }
318    if ((i < size-1) && join_with) {
319      owl_fmtext_append_normal(f, join_with);
320    }
321  }
322}
323
324
325void owl_fmtext_print_plain(owl_fmtext *f, char *buff) {
326  strcpy(buff, f->textbuff);
327}
328
329
330void owl_fmtext_curs_waddstr(owl_fmtext *f, WINDOW *w) {
331  char *tmpbuff;
332  int position, trans1, trans2, len, lastsame;
333
334  tmpbuff=owl_malloc(f->textlen+10);
335
336  position=0;
337  len=f->textlen;
338  while (position<=len) {
339    /* find the last char with the current format and color */
340    trans1=owl_util_find_trans(f->fmbuff+position, len-position);
341    trans2=owl_util_find_trans(f->colorbuff+position, len-position);
342
343    if (trans1<trans2) {
344      lastsame=position+trans1;
345    } else {
346      lastsame=position+trans2;
347    }
348
349    /* set the format */
350    wattrset(w, A_NORMAL);
351    if (f->fmbuff[position] & OWL_FMTEXT_ATTR_BOLD) {
352      wattron(w, A_BOLD);
353    }
354    if (f->fmbuff[position] & OWL_FMTEXT_ATTR_REVERSE) {
355      wattron(w, A_REVERSE);
356    }
357    if (f->fmbuff[position] & OWL_FMTEXT_ATTR_UNDERLINE) {
358      wattron(w, A_UNDERLINE);
359    }
360
361    /* set the color */
362    /* warning, this is sort of a hack */
363    if (owl_global_get_hascolors(&g)) {
364      if (f->colorbuff[position]!=OWL_COLOR_DEFAULT) {
365        wattron(w, COLOR_PAIR(f->colorbuff[position]));
366      }
367    }
368
369    /* add the text */
370    strncpy(tmpbuff, f->textbuff + position, lastsame-position+1);
371    tmpbuff[lastsame-position+1]='\0';
372    waddstr(w, tmpbuff);
373
374    position=lastsame+1;
375  }
376  owl_free(tmpbuff);
377}
378
379
380int owl_fmtext_truncate_lines(owl_fmtext *in, int aline, int lines, owl_fmtext *out) {
381  /* start with line aline (where the first line is 0) and print
382   *  'lines' lines
383   */
384  char *ptr1, *ptr2;
385  int i, offset;
386
387  /* initialize out */
388  owl_fmtext_init_null(out);
389 
390  /* find the starting line */
391  ptr1=in->textbuff;
392  if (aline!=0) {
393    for (i=0; i<aline; i++) {
394      ptr1=strchr(ptr1, '\n');
395      if (!ptr1) return(-1);
396      ptr1++;
397    }
398  }
399  /* ptr1 now holds the starting point */
400
401  /* copy in the next 'lines' lines */
402  if (lines<1) return(-1);
403
404  for (i=0; i<lines; i++) {
405    ptr2=strchr(ptr1, '\n');
406    offset=ptr1-in->textbuff;
407    if (!ptr2) {
408      owl_fmtext_append_fmtext(out, in, offset, in->textlen-1);
409      return(-1);
410    }
411    owl_fmtext_append_fmtext(out, in, offset, (ptr2-ptr1)+offset);
412    ptr1=ptr2+1;
413  }
414  return(0);
415}
416
417
418void owl_fmtext_truncate_cols(owl_fmtext *in, int acol, int bcol, owl_fmtext *out) {
419  char *ptr1, *ptr2, *last;
420  int len, offset;
421 
422  /* the first column is column 0 */
423
424  /* the message is expected to end in a new line for now */
425
426  owl_fmtext_init_null(out);
427
428  last=in->textbuff+in->textlen-1;
429  ptr1=in->textbuff;
430  while (ptr1<=last) {
431    ptr2=strchr(ptr1, '\n');
432    if (!ptr2) {
433      /* but this shouldn't happen if we end in a \n */
434      break;
435    }
436   
437    if (ptr2==ptr1) {
438      owl_fmtext_append_normal(out, "\n");
439      ptr1++;
440      continue;
441    }
442
443    /* we need to check that we won't run over here */
444    len=bcol-acol;
445    if (len > (ptr2-(ptr1+acol))) {
446      len=ptr2-(ptr1+acol);
447    }
448    if (len>last-ptr1) {
449      len-=(last-ptr1);
450    }
451    if (len<=0) {
452      owl_fmtext_append_normal(out, "\n");
453      ptr1=ptr2+1;
454      continue;
455    }
456
457    offset=ptr1-in->textbuff;
458    owl_fmtext_append_fmtext(out, in, offset+acol, offset+acol+len);
459
460    ptr1=ptr2+1;
461  }
462}
463
464
465int owl_fmtext_num_lines(owl_fmtext *f) {
466  int lines, i;
467
468  lines=0;
469  for (i=0; f->textbuff[i]!='\0'; i++) {
470    if (f->textbuff[i]=='\n') lines++;
471  }
472
473  /* if the last char wasn't a \n there's one more line */
474  if (f->textbuff[i-1]!='\n') lines++;
475
476  return(lines);
477}
478
479
480char *owl_fmtext_get_text(owl_fmtext *f) {
481  return(f->textbuff);
482}
483
484void owl_fmtext_free(owl_fmtext *f) {
485  if (f->textbuff) owl_free(f->textbuff);
486  if (f->fmbuff) owl_free(f->fmbuff);
487  if (f->colorbuff) owl_free(f->colorbuff);
488}
489
490
491void owl_fmtext_copy(owl_fmtext *dst, owl_fmtext *src) {
492  dst->textlen=src->textlen;
493  dst->textbuff=owl_malloc(src->textlen+5);
494  dst->fmbuff=owl_malloc(src->textlen+5);
495  dst->colorbuff=owl_malloc(src->textlen+5);
496  memcpy(dst->textbuff, src->textbuff, src->textlen);
497  memcpy(dst->fmbuff, src->fmbuff, src->textlen);
498  memcpy(dst->colorbuff, src->colorbuff, src->textlen);
499}
Note: See TracBrowser for help on using the repository browser.