source: text.c @ faf71b5

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since faf71b5 was 995eb4b, checked in by James M. Kretchmar <kretch@mit.edu>, 20 years ago
Smart filters now work with weird regex characters
  • Property mode set to 100644
File size: 7.9 KB
Line 
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <ctype.h>
5#include "owl.h"
6
7static const char fileIdent[] = "$Id$";
8
9int owl_text_truncate_lines(char *out, char *in, int aline, int lines)
10{
11  /* start with line aline (where the first line is 1) and print
12   *  'lines' lines
13   */
14  char *ptr1, *ptr2;
15  int i;
16
17  strcpy(out, "");
18 
19  if (aline==0) aline=1; /* really illegal use */
20
21  /* find the starting line */
22  ptr1=in;
23  if (aline!=1) {
24     for (i=0; i<aline-1; i++) {
25      ptr1=strchr(ptr1, '\n');
26      if (!ptr1) return(-1);
27      ptr1++;
28    }
29  }
30  /* ptr1 now holds the starting point */
31
32  /* copy in the next 'lines' lines */
33  if (lines<1) return(-1);
34 
35  for (i=0; i<lines; i++) {
36    ptr2=strchr(ptr1, '\n');
37    if (!ptr2) {
38      strcat(out, ptr1);
39      return(-1);
40    }
41    strncat(out, ptr1, ptr2-ptr1+1);
42    ptr1=ptr2+1;
43  }
44  return(0);
45}
46
47void owl_text_truncate_cols(char *out, char *in, int acol, int bcol)
48{
49  char *ptr1, *ptr2, *tmpbuff, *last;
50  int len;
51 
52  /* the first column is column 0 */
53
54  /* the message is expected to end in a new line for now */
55
56  tmpbuff=owl_malloc(strlen(in)+20);
57
58  strcpy(tmpbuff, "");
59  last=in+strlen(in)-1;
60  ptr1=in;
61  while (ptr1<last) {
62    ptr2=strchr(ptr1, '\n');
63    if (!ptr2) {
64      /* but this shouldn't happen if we end in a \n */
65      break;
66    }
67   
68    if (ptr2==ptr1) {
69      strcat(tmpbuff, "\n");
70      ptr1++;
71      continue;
72    }
73
74    /* we need to check that we won't run over here */
75    if ( (ptr2-ptr1) < (bcol-acol) ) {
76      len=ptr2-(ptr1+acol);
77    } else {
78      len=bcol-acol;
79    }
80    if ((ptr1+len)>=last) {
81      len-=last-(ptr1+len);
82    }
83
84    strncat(tmpbuff, ptr1+acol, len);
85    strcat(tmpbuff, "\n");
86
87    ptr1=ptr2+1;
88  }
89  strcpy(out, tmpbuff);
90  owl_free(tmpbuff);
91}
92
93
94void owl_text_indent(char *out, char *in, int n)
95{
96  char *ptr1, *ptr2, *last;
97  int i;
98
99  strcpy(out, "");
100
101  last=in+strlen(in)-1;
102  ptr1=in;
103  while (ptr1<=last) {
104    for (i=0; i<n; i++) {
105      strcat(out, " ");
106    }
107    ptr2=strchr(ptr1, '\n');
108    if (!ptr2) {
109      strcat(out, ptr1);
110      break;
111    } else {
112      strncat(out, ptr1, ptr2-ptr1+1);
113    }
114    ptr1=ptr2+1;
115  }
116}
117
118int owl_text_num_lines(char *in)
119{
120  int lines, i;
121
122  lines=0;
123  for (i=0; in[i]!='\0'; i++) {
124    if (in[i]=='\n') lines++;
125  }
126
127  /* if the last char wasn't a \n there's one more line */
128  if (in[i-1]!='\n') lines++;
129
130  return(lines);
131}
132
133
134/* caller must free the return */
135char *owl_text_htmlstrip(char *in)
136{
137  char *ptr1, *end, *ptr2, *ptr3, *out, *out2;
138
139  out=owl_malloc(strlen(in)+30);
140  strcpy(out, "");
141
142  ptr1=in;
143  end=in+strlen(in);
144 
145  while(ptr1<end) {
146    /* look for an open bracket */
147    ptr2=strchr(ptr1, '<');
148
149    /* if none, copy in from here to end and exit */
150    if (ptr2==NULL) {
151      strcat(out, ptr1);
152      break;
153    }
154
155    /* otherwise copy in everything before the open bracket */
156    if (ptr2>ptr1) {
157      strncat(out, ptr1, ptr2-ptr1);
158    }
159
160    /* find the close bracket */
161    ptr3=strchr(ptr2, '>');
162   
163    /* if there is no close, copy as you are and exit */
164    if (!ptr3) {
165      strcat(out, ptr2);
166      break;
167    }
168
169    /* look for things we know */
170    if (!strncasecmp(ptr2, "<BODY ", 6) ||
171        !strncasecmp(ptr2, "<FONT", 5) ||
172        !strncasecmp(ptr2, "<HTML", 5) ||
173        !strncasecmp(ptr2, "</FONT", 6) ||
174        !strncasecmp(ptr2, "</HTML", 6) ||
175        !strncasecmp(ptr2, "</BODY", 6)) {
176
177      /* advance to beyond the angle brakcet and go again */
178      ptr1=ptr3+1;
179      continue;
180    }
181    if (!strncasecmp(ptr2, "<BR>", 4)) {
182      strcat(out, "\n");
183      ptr1=ptr3+1;
184      continue;
185    }
186
187    /* if it wasn't something we know, copy to the > and  go again */
188    strncat(out, ptr2, ptr3-ptr2+1);
189    ptr1=ptr3+1;
190  }
191
192  out2=owl_text_substitute(out, "&lt;", "<");
193  owl_free(out);
194  out=owl_text_substitute(out2, "&gt;", ">");
195  owl_free(out2);
196  out2=owl_text_substitute(out, "&amp;", "&");
197  owl_free(out);
198  out=owl_text_substitute(out2, "&quot;", "\"");
199  owl_free(out2);
200  out2=owl_text_substitute(out, "&nbsp;", " ");
201  owl_free(out);
202  out=owl_text_substitute(out2, "&ensp;", "  ");
203  owl_free(out2);
204  out2=owl_text_substitute(out, "&emsp;", "   ");
205  owl_free(out);
206  out=owl_text_substitute(out2, "&endash;", "--");
207  owl_free(out2);
208  out2=owl_text_substitute(out, "&emdash;", "---");
209  owl_free(out);
210
211  return(out2);
212}
213
214/* caller must free the return */
215char *owl_text_wordwrap(char *in, int col)
216{
217  char *out;
218  int cur, lastspace, len, lastnewline;
219
220  out=owl_strdup(in);
221  len=strlen(in);
222  cur=0;
223  lastspace=-1;
224  lastnewline=-1;
225
226  while (cur<(len-1)) {
227    if (out[cur]==' ') {
228      lastspace=cur;
229      cur++;
230      continue;
231    } else if (out[cur]=='\n') {
232      lastnewline=cur;
233      cur++;
234      continue;
235    }
236
237    /* do we need to wrap? */
238    if ( (cur-(lastnewline+1)) > col ) {
239      if (lastspace==-1 ||
240          (lastnewline>0 && (lastspace<=lastnewline))) {
241        /* we can't help, sorry */
242        cur++;
243        continue;
244      }
245
246      /* turn the last space into a newline */
247      out[lastspace]='\n';
248      lastnewline=lastspace;
249      lastspace=-1;
250      cur++;
251      continue;
252    }
253
254    cur++;
255    continue;
256  }
257  return(out);
258}
259
260/* exactly like strstr but case insensitive */
261char *stristr(char *a, char *b)
262{
263  char *x, *y, *ret;
264
265  if ((x=owl_strdup(a))==NULL) return(NULL);
266  if ((y=owl_strdup(b))==NULL) return(NULL);
267  downstr(x);
268  downstr(y);
269  ret=strstr(x, y);
270  if (ret==NULL) {
271    owl_free(x);
272    owl_free(y);
273    return(NULL);
274  }
275  ret=ret-x+a;
276  owl_free(x);
277  owl_free(y);
278  return(ret);
279}
280
281/* return 1 if a string is only whitespace, otherwise 0 */
282int only_whitespace(char *s)
283{
284  int i;
285  for (i=0; s[i]; i++) {
286    if (!isspace((int) s[i])) return(0);
287  }
288  return(1);
289}
290
291char *owl_getquoting(char *line)
292{
293  if (line[0]=='\0') return("'");
294  if (strchr(line, '\'')) return("\"");
295  if (strchr(line, '"')) return("'");
296  if (strchr(line, ' ')) return("'");
297  return("");
298}
299
300/* Return a string with any occurances of 'from' replaced with 'to'.
301 * Does not currently handle backslash quoting, but may in the future.
302 * Caller must free returned string.
303 */
304char *owl_text_substitute(char *in, char *from, char *to)
305{
306 
307  char *out;
308  int   outlen, tolen, fromlen, inpos=0, outpos=0;
309
310  if (!*from) return owl_strdup(in);
311
312  outlen = strlen(in)+1;
313  tolen  = strlen(to);
314  fromlen  = strlen(from);
315  out = malloc(outlen);
316
317  while (in[inpos]) {
318    if (!strncmp(in+inpos, from, fromlen)) {
319      outlen += tolen;
320      out = owl_realloc(out, outlen);
321      strcpy(out+outpos, to);
322      inpos += fromlen;
323      outpos += tolen;
324    } else {
325      out[outpos] = in[inpos];
326      inpos++; outpos++;
327    }
328  }
329  out[outpos] = '\0';
330  return(out);
331}
332
333/* replace all instances of character a in buff with the character
334 * b.  buff must be null terminated.
335 */
336void owl_text_tr(char *buff, char a, char b)
337{
338  int i;
339
340  owl_function_debugmsg("In: %s", buff);
341  for (i=0; buff[i]!='\0'; i++) {
342    if (buff[i]==a) buff[i]=b;
343  }
344  owl_function_debugmsg("Out: %s", buff);
345}
346
347/* Return a string which is like 'in' except that every instance of
348 * any character in 'toquote' found in 'in' is preceeded by the string
349 * 'quotestr'.  For example, owl_text_quote(in, "+*.", "\") would
350 * place a backslash before every '+', '*' or '.' in 'in'.  It is
351 * permissable for a character in 'quotestr' to be in 'toquote'.
352 * On success returns the string, on error returns NULL.
353 */
354char *owl_text_quote(char *in, char *toquote, char *quotestr)
355{
356  int i, x, r, place;
357  int in_len, toquote_len, quotestr_len;
358  char *out;
359
360  in_len=strlen(in);
361  toquote_len=strlen(toquote);
362  quotestr_len=strlen(quotestr);
363  out=owl_malloc((in_len*quotestr_len)+30);
364  place=0;
365  for (i=0; i<in_len; i++) {
366
367    /* check if it's a character that needs quoting */
368    for (x=0; x<toquote_len; x++) {
369      if (in[i]==toquote[x]) {
370        /* quote it */
371        for (r=0; r<quotestr_len; r++) {
372          out[place+r]=quotestr[r];
373        }
374        place+=quotestr_len;
375        break;
376      }
377    }
378
379    /* either way, we now copy over the character */
380    out[place]=in[i];
381    place++;
382  }
383  out[place]='\0';
384  return(out);
385}
Note: See TracBrowser for help on using the repository browser.