source: text.c @ eef5b52

debianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since eef5b52 was f34dd65, checked in by Nelson Elhage <nelhage@mit.edu>, 16 years ago
Kill a whole bunch of unused code. I generated a list of dead functions by building with -ffunction-sections and linking with -Wl,--gc-sections -Wl,--print-gc-sections I kept a number of functions that seemed to be logical parts of an existing API, as well as stuff in varstubs.c, since that file is autogenerated.
  • Property mode set to 100644
File size: 6.9 KB
RevLine 
[7d4fbcd]1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
[995eb4b]4#include <ctype.h>
[7d4fbcd]5#include "owl.h"
6
[1aee7d9]7static const char fileIdent[] = "$Id$";
8
[ab31454]9void owl_text_indent(char *out, char *in, int n)
10{
[7d4fbcd]11  char *ptr1, *ptr2, *last;
12  int i;
13
14  strcpy(out, "");
15
16  last=in+strlen(in)-1;
17  ptr1=in;
18  while (ptr1<=last) {
19    for (i=0; i<n; i++) {
20      strcat(out, " ");
21    }
22    ptr2=strchr(ptr1, '\n');
23    if (!ptr2) {
24      strcat(out, ptr1);
25      break;
26    } else {
27      strncat(out, ptr1, ptr2-ptr1+1);
28    }
29    ptr1=ptr2+1;
30  }
31}
32
[ab31454]33int owl_text_num_lines(char *in)
34{
[7d4fbcd]35  int lines, i;
36
37  lines=0;
38  for (i=0; in[i]!='\0'; i++) {
39    if (in[i]=='\n') lines++;
40  }
41
42  /* if the last char wasn't a \n there's one more line */
[1bb1e67]43  if (i>0 && in[i-1]!='\n') lines++;
[7d4fbcd]44
45  return(lines);
46}
47
[3abf28b]48
49/* caller must free the return */
[ab31454]50char *owl_text_htmlstrip(char *in)
51{
[8d24696]52  char *ptr1, *end, *ptr2, *ptr3, *out, *out2;
[3abf28b]53
54  out=owl_malloc(strlen(in)+30);
55  strcpy(out, "");
56
57  ptr1=in;
58  end=in+strlen(in);
59 
60  while(ptr1<end) {
61    /* look for an open bracket */
62    ptr2=strchr(ptr1, '<');
63
[dafd919]64    /* if none, copy in from here to end and exit */
[3abf28b]65    if (ptr2==NULL) {
66      strcat(out, ptr1);
[8d24696]67      break;
[3abf28b]68    }
69
70    /* otherwise copy in everything before the open bracket */
71    if (ptr2>ptr1) {
72      strncat(out, ptr1, ptr2-ptr1);
73    }
74
75    /* find the close bracket */
76    ptr3=strchr(ptr2, '>');
77   
78    /* if there is no close, copy as you are and exit */
79    if (!ptr3) {
80      strcat(out, ptr2);
[8d24696]81      break;
[3abf28b]82    }
83
84    /* look for things we know */
[3bcf125]85    if (!strncasecmp(ptr2, "<BODY", 5) ||
[dafd919]86        !strncasecmp(ptr2, "<FONT", 5) ||
[3abf28b]87        !strncasecmp(ptr2, "<HTML", 5) ||
88        !strncasecmp(ptr2, "</FONT", 6) ||
89        !strncasecmp(ptr2, "</HTML", 6) ||
90        !strncasecmp(ptr2, "</BODY", 6)) {
91
92      /* advance to beyond the angle brakcet and go again */
93      ptr1=ptr3+1;
94      continue;
95    }
[75e3879]96    if (!strncasecmp(ptr2, "<BR>", 4)) {
97      strcat(out, "\n");
98      ptr1=ptr3+1;
99      continue;
100    }
[3abf28b]101
102    /* if it wasn't something we know, copy to the > and  go again */
[75e3879]103    strncat(out, ptr2, ptr3-ptr2+1);
[3abf28b]104    ptr1=ptr3+1;
105  }
[8d24696]106
[e3d9c77]107  out2=owl_text_substitute(out, "&lt;", "<");
[8d24696]108  owl_free(out);
[e3d9c77]109  out=owl_text_substitute(out2, "&gt;", ">");
[8d24696]110  owl_free(out2);
[e3d9c77]111  out2=owl_text_substitute(out, "&amp;", "&");
[8d24696]112  owl_free(out);
[e3d9c77]113  out=owl_text_substitute(out2, "&quot;", "\"");
[8d24696]114  owl_free(out2);
[e3d9c77]115  out2=owl_text_substitute(out, "&nbsp;", " ");
[8d24696]116  owl_free(out);
[e3d9c77]117  out=owl_text_substitute(out2, "&ensp;", "  ");
[8d24696]118  owl_free(out2);
[e3d9c77]119  out2=owl_text_substitute(out, "&emsp;", "   ");
[8d24696]120  owl_free(out);
[e3d9c77]121  out=owl_text_substitute(out2, "&endash;", "--");
[8d24696]122  owl_free(out2);
[e3d9c77]123  out2=owl_text_substitute(out, "&emdash;", "---");
[8d24696]124  owl_free(out);
125
126  return(out2);
[3abf28b]127}
[ab31454]128
129/* caller must free the return */
130char *owl_text_wordwrap(char *in, int col)
131{
132  char *out;
133  int cur, lastspace, len, lastnewline;
134
135  out=owl_strdup(in);
136  len=strlen(in);
137  cur=0;
138  lastspace=-1;
139  lastnewline=-1;
140
141  while (cur<(len-1)) {
142    if (out[cur]==' ') {
143      lastspace=cur;
144      cur++;
145      continue;
146    } else if (out[cur]=='\n') {
147      lastnewline=cur;
148      cur++;
149      continue;
150    }
151
152    /* do we need to wrap? */
153    if ( (cur-(lastnewline+1)) > col ) {
154      if (lastspace==-1 ||
155          (lastnewline>0 && (lastspace<=lastnewline))) {
156        /* we can't help, sorry */
157        cur++;
158        continue;
159      }
160
161      /* turn the last space into a newline */
162      out[lastspace]='\n';
163      lastnewline=lastspace;
164      lastspace=-1;
165      cur++;
166      continue;
167    }
168
169    cur++;
170    continue;
171  }
172  return(out);
173}
[e3d9c77]174
[f82e233]175/* this modifies 'in' */
176void owl_text_wordunwrap(char *in)
177{
178  int i, j;
179
180  j=strlen(in);
181  for (i=0; i<j; i++) {
182    if ( (in[i]=='\n') &&
183         ((i>0) && (i<(j-1))) &&
184         (in[i-1]!='\n') &&
185         (in[i+1]!='\n') )
186      in[i]=' ';
187  }
188}
189
[e3d9c77]190/* exactly like strstr but case insensitive */
191char *stristr(char *a, char *b)
192{
[28ee32b]193  char *x, *y;
194  char *ret = NULL;
195  if ((x = g_utf8_casefold(a, -1)) != NULL) {
196    if ((y = g_utf8_casefold(b, -1)) != NULL) {
197      ret = strstr(x, y);
198      if (ret != NULL) {
199        ret = ret - x + a;
200      }
201      g_free(y);
202    }
203    g_free(x);
[e3d9c77]204  }
205  return(ret);
206}
207
208/* return 1 if a string is only whitespace, otherwise 0 */
209int only_whitespace(char *s)
210{
[28ee32b]211  if (g_utf8_validate(s,-1,NULL)) {
212    char *p;
213    for(p = s; p[0]; p=g_utf8_next_char(p)) {
214      if (!g_unichar_isspace(g_utf8_get_char(p))) return 0;
215    }
216  }
217  else {
218    int i;
219    for (i=0; s[i]; i++) {
220      if (!isspace((int) s[i])) return(0);
221    }
[e3d9c77]222  }
223  return(1);
224}
225
226char *owl_getquoting(char *line)
227{
228  if (line[0]=='\0') return("'");
229  if (strchr(line, '\'')) return("\"");
230  if (strchr(line, '"')) return("'");
231  if (strchr(line, ' ')) return("'");
232  return("");
233}
234
235/* Return a string with any occurances of 'from' replaced with 'to'.
236 * Does not currently handle backslash quoting, but may in the future.
237 * Caller must free returned string.
238 */
239char *owl_text_substitute(char *in, char *from, char *to)
240{
241 
242  char *out;
243  int   outlen, tolen, fromlen, inpos=0, outpos=0;
244
245  if (!*from) return owl_strdup(in);
246
247  outlen = strlen(in)+1;
248  tolen  = strlen(to);
249  fromlen  = strlen(from);
[34509d5]250  out = owl_malloc(outlen);
[e3d9c77]251
252  while (in[inpos]) {
253    if (!strncmp(in+inpos, from, fromlen)) {
254      outlen += tolen;
255      out = owl_realloc(out, outlen);
256      strcpy(out+outpos, to);
257      inpos += fromlen;
258      outpos += tolen;
259    } else {
260      out[outpos] = in[inpos];
261      inpos++; outpos++;
262    }
263  }
264  out[outpos] = '\0';
265  return(out);
266}
267
268/* replace all instances of character a in buff with the character
269 * b.  buff must be null terminated.
270 */
271void owl_text_tr(char *buff, char a, char b)
272{
273  int i;
274
275  owl_function_debugmsg("In: %s", buff);
276  for (i=0; buff[i]!='\0'; i++) {
277    if (buff[i]==a) buff[i]=b;
278  }
279  owl_function_debugmsg("Out: %s", buff);
280}
281
282/* Return a string which is like 'in' except that every instance of
283 * any character in 'toquote' found in 'in' is preceeded by the string
284 * 'quotestr'.  For example, owl_text_quote(in, "+*.", "\") would
285 * place a backslash before every '+', '*' or '.' in 'in'.  It is
286 * permissable for a character in 'quotestr' to be in 'toquote'.
287 * On success returns the string, on error returns NULL.
288 */
289char *owl_text_quote(char *in, char *toquote, char *quotestr)
290{
[72db971]291  int i, x, r, place, escape;
[e3d9c77]292  int in_len, toquote_len, quotestr_len;
293  char *out;
294
295  in_len=strlen(in);
296  toquote_len=strlen(toquote);
297  quotestr_len=strlen(quotestr);
298  out=owl_malloc((in_len*quotestr_len)+30);
299  place=0;
[72db971]300  escape = 0;
301  for (i=0; i<in_len; i++) {
302    if(strchr(toquote, in[i]) != NULL)
303      escape++;
304  }
305  out = owl_malloc(in_len + quotestr_len*escape+1);
[e3d9c77]306  for (i=0; i<in_len; i++) {
307
308    /* check if it's a character that needs quoting */
309    for (x=0; x<toquote_len; x++) {
310      if (in[i]==toquote[x]) {
311        /* quote it */
312        for (r=0; r<quotestr_len; r++) {
313          out[place+r]=quotestr[r];
314        }
315        place+=quotestr_len;
316        break;
317      }
318    }
319
320    /* either way, we now copy over the character */
321    out[place]=in[i];
322    place++;
323  }
324  out[place]='\0';
325  return(out);
326}
Note: See TracBrowser for help on using the repository browser.