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