Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
10 | reyssat | 1 | /* Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis |
2 | * |
||
3 | * This program is free software; you can redistribute it and/or modify |
||
4 | * it under the terms of the GNU General Public License as published by |
||
5 | * the Free Software Foundation; either version 2 of the License, or |
||
6 | * (at your option) any later version. |
||
7 | * |
||
8 | * This program is distributed in the hope that it will be useful, |
||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
11 | * GNU General Public License for more details. |
||
12 | * |
||
13 | * You should have received a copy of the GNU General Public License |
||
14 | * along with this program; if not, write to the Free Software |
||
15 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
||
16 | */ |
||
17 | /* line input / output routines */ |
||
18 | |||
19 | #include <stdarg.h> |
||
3717 | reyssat | 20 | |
21 | #include "basicstr.c" |
||
22 | |||
23 | |||
10 | reyssat | 24 | #define int_buf_size 40 |
25 | |||
26 | /* this is rapid. Output string will be erased at the next call. */ |
||
27 | char *int2str(int i) |
||
28 | { |
||
29 | int neg,t; |
||
30 | static char int_buf[int_buf_size]; |
||
31 | int_buf[int_buf_size-1]=0; |
||
32 | neg=0; if(i<0) {neg=1;i=-i;} |
||
33 | for(t=int_buf_size-2;t>=0;) { |
||
34 | int_buf[t--]=i%10+'0'; i/=10; |
||
35 | if(i==0) break; |
||
36 | } |
||
37 | if(t<=0) {int_buf[0]=0; return int_buf;} /* should never occur. */ |
||
38 | if(neg) int_buf[t--]='-'; |
||
39 | t++; return int_buf+t; |
||
40 | } |
||
41 | |||
42 | void *xmalloc(size_t n) |
||
43 | { |
||
44 | void *p; |
||
45 | p=malloc(n); |
||
46 | if(p==NULL) { |
||
47 | fprintf(stderr,"Malloc failure."); exit(1); |
||
48 | } |
||
49 | return p; |
||
50 | } |
||
51 | |||
52 | int msleep(int ms) |
||
53 | { |
||
54 | struct timespec req, rem; |
||
55 | if(ms<=0) return 0; |
||
56 | req.tv_sec=ms/1000; req.tv_nsec=(ms%1000)*1000*1000; |
||
57 | return nanosleep(&req,&rem); |
||
58 | } |
||
59 | |||
60 | /* dos/mac to unix/linux translation */ |
||
61 | void _tolinux(char *p) |
||
62 | { |
||
63 | char *pp,*p1; |
||
64 | pp=strchr(p,13); if(pp==NULL) return; |
||
65 | for(p1=pp; *pp; pp++) { |
||
66 | if(*pp==13) { |
||
67 | if(*(pp+1)=='\n' || (pp>p && *(pp-1)=='\n') || |
||
68 | (*(pp+1)=='\\' && *(pp+2)=='\n')) continue; |
||
69 | else *pp='\n'; |
||
70 | } |
||
71 | *p1++=*pp; |
||
72 | } |
||
73 | *p1=0; |
||
74 | } |
||
75 | |||
3717 | reyssat | 76 | |
10 | reyssat | 77 | /* optimized and secure strcpy */ |
5456 | bpr | 78 | |
79 | /* copies src to dest, at most lim bytes. Error if more than |
||
80 | MAX_LINELEN chars would be copied, including final \0. */ |
||
81 | |||
82 | void mystrncpy(char *dest, const char *src, size_t lim) |
||
10 | reyssat | 83 | { |
5456 | bpr | 84 | if (lim) |
85 | { |
||
86 | size_t i = strlen(src); |
||
87 | if (i >= lim) i = lim-1; |
||
88 | if (i >= MAX_LINELEN) error1("cmd_output_too_long"); |
||
89 | memmove(dest,src,i); dest[i]=0; |
||
90 | } |
||
10 | reyssat | 91 | } |
92 | |||
93 | /* find matching parenthesis. |
||
94 | * The entrance point should be after the opening |
||
95 | * parenthesis. |
||
96 | * Returns NULL if unmatched. */ |
||
97 | char *find_matching(char *p, char c) |
||
98 | { |
||
99 | char *pp; |
||
100 | int parenth, brak, brace; |
||
101 | if(c=='|') { |
||
102 | for(pp=p;*pp!=0;pp++) { |
||
103 | switch(*pp) { |
||
104 | case '|': return pp; |
||
105 | case '(': { |
||
106 | pp=find_matching(pp+1,')'); |
||
107 | if(pp==NULL) return NULL; |
||
108 | break; |
||
109 | } |
||
110 | case '[': { |
||
111 | pp=find_matching(pp+1,']'); |
||
112 | if(pp==NULL) return NULL; |
||
113 | break; |
||
114 | } |
||
115 | case '{': { |
||
116 | pp=find_matching(pp+1,'}'); |
||
117 | if(pp==NULL) return NULL; |
||
118 | break; |
||
119 | } |
||
120 | case ')': |
||
121 | case ']': |
||
122 | case '}': return NULL; |
||
123 | |||
124 | default: break; |
||
125 | } |
||
126 | } |
||
127 | return NULL; |
||
128 | } |
||
129 | parenth=brak=brace=0; |
||
130 | for(pp=p; *pp!=0; pp++) { |
||
131 | switch(*pp) { |
||
132 | case '[': brak++; break; |
||
133 | case ']': brak--; break; |
||
134 | case '(': parenth++; break; |
||
135 | case ')': parenth--; break; |
||
136 | case '{': brace++; break; |
||
137 | case '}': brace--; break; |
||
138 | default: continue; |
||
139 | } |
||
140 | if(parenth<0 || brak<0 || brace<0) { |
||
141 | if(*pp!=c || parenth>0 || brak>0 || brace>0) return NULL; |
||
142 | else break; |
||
143 | } |
||
144 | } |
||
145 | if(*pp!=c) return NULL; |
||
146 | return pp; |
||
147 | } |
||
148 | |||
149 | /* Points to the end of the word */ |
||
150 | char *find_word_end(char *p) |
||
151 | { |
||
152 | while(!myisspace(*p) && *p!=0) p++; |
||
153 | return p; |
||
154 | } |
||
155 | |||
156 | /* Strips leading spaces */ |
||
157 | char *find_word_start(char *p) |
||
158 | { |
||
159 | while(myisspace(*p)) p++; |
||
160 | return p; |
||
161 | } |
||
162 | |||
163 | /* find a character in a string, but skipping parentheses. */ |
||
164 | char *strparchr(char *p, char c) |
||
165 | { |
||
166 | char *pp; |
||
167 | |||
168 | for(pp=p;*pp && *pp!=c && pp-p<MAX_LINELEN; pp++) { |
||
169 | switch(*pp) { |
||
170 | case '(': pp=find_matching(pp+1,')'); break; |
||
171 | case '[': pp=find_matching(pp+1,']'); break; |
||
172 | case '{': pp=find_matching(pp+1,'}'); break; |
||
173 | } |
||
174 | if(pp==NULL) return NULL; |
||
175 | } |
||
176 | if(*pp==c) return pp; else return NULL; |
||
177 | } |
||
178 | |||
179 | /* search for string, skipping parentheses */ |
||
180 | char *strparstr(char *p, char *fnd) |
||
181 | { |
||
182 | char *pp, c; |
||
183 | int n; |
||
184 | |||
185 | if(*fnd==0) return p+strlen(p); |
||
186 | c=*fnd; n=strlen(fnd); |
||
187 | for(pp=p;pp-1!=NULL && *pp!=0; pp++) { |
||
188 | if(*pp==c && (n==1 || strncmp(pp,fnd,n)==0)) return pp; |
||
189 | switch(*pp) { |
||
190 | case '(': pp=find_matching(pp+1,')'); break; |
||
191 | case '[': pp=find_matching(pp+1,']'); break; |
||
192 | case '{': pp=find_matching(pp+1,'}'); break; |
||
193 | } |
||
194 | } |
||
195 | if(pp-1==NULL) pp=strstr(p,fnd); |
||
196 | if(pp!=NULL) return pp; |
||
197 | else return p+strlen(p); |
||
198 | } |
||
199 | |||
200 | /* Points to the end of an item */ |
||
201 | char *find_item_end(char *p) |
||
202 | { |
||
203 | return strparstr(p,","); |
||
204 | } |
||
205 | |||
206 | /* Points to the end of an item */ |
||
207 | char *find_line_end(char *p) |
||
208 | { |
||
209 | char *pp=strstr(p,"\n"); |
||
210 | if(pp==NULL) pp=p+strlen(p); |
||
211 | return pp; |
||
212 | } |
||
213 | |||
214 | char *charchr(char *p,char *w) |
||
215 | { |
||
216 | return strchr(p,w[0]); |
||
217 | } |
||
218 | |||
219 | /* Find first occurrence of word */ |
||
220 | char *wordchr(char *p, char *w) |
||
221 | { |
||
222 | char *r; int n; |
||
223 | |||
224 | if(*w==0) return NULL; |
||
225 | n=strlen(w); |
||
226 | for(r=strstr(p,w);r!=NULL && |
||
227 | ( (r>p && !myisspace(*(r-1))) || (!myisspace(*(r+n)) && *(r+n)!=0) ); |
||
228 | r=strstr(r+1,w)); |
||
229 | return r; |
||
230 | } |
||
231 | |||
232 | /* Find first occurrence of item */ |
||
233 | char *itemchr(char *p, char *w) |
||
234 | { |
||
235 | char *r, *r1, *r2; int n; |
||
236 | |||
237 | if(*w==0) return NULL; |
||
238 | n=strlen(w); |
||
239 | for(r=strstr(p,w);r!=NULL; r=strstr(r+1,w)) { |
||
240 | r1=r-1;while(r1>=p && myisspace(*r1)) r1--; |
||
241 | r2=find_word_start(r+n); |
||
242 | if((r1<p || *r1==',') && (*r2==0 || *r2==',')) return r; |
||
243 | } |
||
244 | return r; |
||
245 | } |
||
246 | |||
247 | /* Find first occurrence of line */ |
||
248 | char *linechr(char *p, char *w) |
||
249 | { |
||
250 | char *r; |
||
251 | int n; |
||
252 | |||
253 | if(*w==0) return NULL; |
||
254 | n=strlen(w); |
||
255 | for(r=strstr(p,w);r!=NULL; r=strstr(r+1,w)) { |
||
256 | if((r<=p || *(r-1)=='\n') && (*(r+n)==0 || *(r+n)=='\n')) |
||
257 | break; |
||
258 | } |
||
259 | return r; |
||
260 | } |
||
261 | |||
262 | /* find a variable in a string (math expression). |
||
263 | * Returns the pointer or NULL. */ |
||
264 | char *varchr(char *p, char *v) |
||
265 | { |
||
266 | char *pp; int n; |
||
267 | if(*v==0) return NULL; |
||
268 | n=strlen(v); |
||
269 | for(pp=strstr(p,v); pp!=NULL; pp=strstr(pp+1,v)) { |
||
270 | if((pp==p || (!myisalnum(*(pp-1)) && *(pp-1)!='_')) && |
||
271 | ((!myisalnum(*(pp+n)) && *(pp+n)!='_' && *(pp+n)!='\'') |
||
272 | || *(pp+n)==0)) break; |
||
273 | } |
||
274 | return pp; |
||
275 | } |
||
276 | |||
277 | int _cutit_(char *p, char *list[], int max, char *end_finder(char *pt), int tag) |
||
278 | { |
||
279 | int i; |
||
280 | char *pp, *pe, *p0; |
||
281 | if(tag&1) pp=find_word_start(p); else pp=p; /* strip head space */ |
||
282 | for(i=0;i<max && *pp;i++) { |
||
283 | pe=end_finder(pp); |
||
895 | reyssat | 284 | if((tag&2) && myisspace(pe[-1])) { /* strip trailing space */ |
10 | reyssat | 285 | for(p0=pe; p0>pp && myisspace(p0[-1]); p0--); |
286 | if(p0<pe) *p0=0; |
||
287 | } |
||
288 | if(*pe) *pe++=0; |
||
289 | list[i]=pp; |
||
290 | if(tag&1) pp=find_word_start(pe); else pp=pe; |
||
291 | } |
||
292 | return i; |
||
293 | } |
||
294 | |||
295 | int cutitems(char *p, char *list[], int max) |
||
296 | { |
||
297 | return _cutit_(p,list,max,find_item_end,3); |
||
298 | } |
||
299 | |||
300 | int cutwords(char *p, char *list[], int max) |
||
301 | { |
||
302 | return _cutit_(find_word_start(p),list,max,find_word_end,1); |
||
303 | } |
||
304 | |||
305 | int cutlines(char *p, char *list[], int max) |
||
306 | { |
||
307 | return _cutit_(p,list,max,find_line_end,0); |
||
308 | } |
||
309 | |||
310 | int cutchars(char *p, char *list[], int max) |
||
311 | { |
||
312 | int i; char *pp; |
||
313 | for(i=0,pp=p;*pp && i<max;list[i++]=pp++); |
||
314 | return i; |
||
315 | } |
||
316 | |||
317 | /* strip trailing spaces; return string end. */ |
||
318 | char *strip_trailing_spaces(char *p) |
||
319 | { |
||
320 | char *pp; |
||
321 | if(*p==0) return p; |
||
322 | for(pp=p+strlen(p)-1; pp>=p && myisspace(*pp); pp--); |
||
323 | if(pp[1]) pp[1]=0; return pp; |
||
324 | } |
||
325 | |||
326 | /* Routines for quick search of item in a list. */ |
||
327 | |||
328 | /* Verify whether a list is well-ordered. For debugging uses. |
||
329 | * Returns 0 if order is OK, -1 otherwise. */ |
||
330 | int verify_order(void *list, int items, size_t item_size) |
||
331 | { |
||
332 | int i; char *old, **p; |
||
333 | p=list; old=*p; |
||
334 | for(i=item_size;i<items*item_size;i+=item_size) { |
||
335 | p=list+i; |
||
336 | if(strcmp(*p,old)<0) { |
||
337 | fprintf(stderr,"Table disorder: %s > %s",old,*p); |
||
338 | exit(1); |
||
339 | } |
||
340 | old=*p; |
||
341 | } |
||
342 | return 0; |
||
343 | } |
||
344 | |||
345 | /* searches a list. Returns index if found, -1 if nomatch. |
||
346 | * Uses binary search, list must be sorted. */ |
||
347 | int search_list(void *list, int items, size_t item_size, const char *str) |
||
348 | { |
||
349 | int i1,i2,j,k; |
||
350 | char **p; |
||
351 | char c; |
||
352 | |||
353 | if(items<=0) return -1; |
||
354 | j=0; c=*str; |
||
355 | p=list; |
||
356 | k=**p-c; if(k==0) k=strcmp(*p,str); |
||
357 | if(k==0) return k; if(k>0) return -1; |
||
358 | p=list+(items-1)*item_size; |
||
359 | k=**p-c; if(k==0) k=strcmp(*p,str); |
||
360 | if(k==0) return items-1; if(k<0) return ~items; |
||
361 | for(i1=0,i2=items-1;i2>i1+1;) { |
||
362 | j=(i2+i1)/2; |
||
363 | p=list+(j*item_size); |
||
364 | k=**p-c; if(k==0) k=strcmp(*p,str); |
||
365 | if(k==0) return j; |
||
366 | if(k>0) {i2=j; continue;} |
||
367 | if(k<0) {i1=j; continue;} |
||
368 | } |
||
369 | return ~i2; |
||
370 | } |
||
371 | |||
372 | /* Returns number of lines in string p */ |
||
373 | unsigned int linenum(char *p) |
||
374 | { |
||
375 | int i; char *pp; |
||
376 | |||
377 | /* Ending blank line will be thus removed. */ |
||
378 | i=strlen(p); if(i>1 && *(p+i-1)=='\n') *(p+i-1)=0; |
||
379 | if(*p=='\n') i=1; else i=0; |
||
380 | for(pp=p; pp!=NULL && *pp!=0; pp=strchr(pp+1,'\n'), i++); |
||
381 | return i; |
||
382 | } |
||
383 | |||
384 | /* Returns number of items in the list p, comma separated */ |
||
385 | unsigned int itemnum(char *p) |
||
386 | { |
||
387 | int i; char *pp; |
||
388 | |||
389 | if(*p==0) return 0; |
||
390 | for(i=0,pp=p; pp==p || *(pp-1)!=0; pp=find_item_end(pp)+1, i++); |
||
391 | return i; |
||
392 | } |
||
393 | |||
394 | /* Returns number of words in string p */ |
||
395 | unsigned int wordnum(char *p) |
||
396 | { |
||
397 | int i; char *pp; |
||
398 | |||
399 | for(i=0, pp=find_word_start(p); *pp!=0; i++) { |
||
400 | while(!myisspace(*pp) && *pp!=0) pp++; |
||
401 | while(myisspace(*pp)) pp++; |
||
402 | } |
||
403 | return i; |
||
404 | } |
||
405 | |||
406 | /* This is just to suppress an annoying compiler warning message. */ |
||
407 | unsigned int charnum(char *p) |
||
408 | { |
||
409 | return strlen(p); |
||
410 | } |
||
411 | |||
412 | /* find n-th line in string p */ |
||
413 | char *fnd_line(char *p, int n, char bf[]) |
||
414 | { |
||
415 | char *pp; |
||
416 | int i; |
||
417 | |||
418 | for(i=1,pp=p; pp-1!=NULL && *pp!=0 && i<n; pp=strchr(pp,'\n')+1, i++); |
||
419 | fnd_position=pp; if(pp-1==NULL) { |
||
420 | fnd_position=NULL; fnd_nextpos=""; *bf=0; return bf; |
||
421 | } |
||
422 | for(i=0; *(pp+i)!=0 && *(pp+i)!='\n'; i++) *(bf+i)=*(pp+i); |
||
423 | *(bf+i)=0; |
||
424 | if(pp[i]=='\n') i++; fnd_nextpos=pp+i; |
||
425 | return bf; |
||
426 | } |
||
427 | |||
428 | /* find n-th item in list p, comma separated */ |
||
429 | char *fnd_item(char *p, int n, char bf[]) |
||
430 | { |
||
431 | char *pp, *pe; |
||
432 | int i; |
||
433 | |||
434 | for(i=1,pp=p; i<n && (pp==p || *(pp-1)!=0); pp=find_item_end(pp)+1, i++); |
||
435 | fnd_position=pp; if(pp>p && *(pp-1)==0) { |
||
436 | fnd_position=NULL; *bf=0; return bf; |
||
437 | } |
||
438 | pp=find_word_start(pp); pe=find_item_end(pp); |
||
439 | if(*pe) fnd_nextpos=pe+1; else fnd_nextpos=pe; |
||
440 | while(pe>pp && myisspace(*(pe-1))) pe--; |
||
441 | memmove(bf,pp,pe-pp); bf[pe-pp]=0; |
||
442 | return bf; |
||
443 | } |
||
444 | |||
445 | /* find n-th word in string p */ |
||
446 | char *fnd_word(char *p, int n, char bf[]) |
||
447 | { |
||
448 | char *pp; |
||
449 | int i; |
||
450 | |||
451 | for(i=1, pp=find_word_start(p); *pp!=0 && i<n ; i++) { |
||
452 | while(!myisspace(*pp) && *pp!=0) pp++; |
||
453 | pp=find_word_start(pp); |
||
454 | } |
||
455 | if(*pp) fnd_position=pp; else fnd_position=NULL; |
||
456 | for(i=0; *(pp+i)!=0 && !myisspace(*(pp+i)); i++) *(bf+i)=*(pp+i); |
||
457 | fnd_nextpos=find_word_start(pp+i); |
||
458 | *(bf+i)=0; |
||
459 | return bf; |
||
460 | } |
||
461 | |||
462 | /* find n-th char in string p */ |
||
463 | char *fnd_char(char *p, int n, char bf[]) |
||
464 | { |
||
465 | int t; |
||
466 | |||
467 | t=strlen(p); |
||
468 | if(n>t || n<1) {*bf=0;fnd_position=NULL; fnd_nextpos="";} |
||
469 | else { |
||
470 | *bf=*(p+n-1); *(bf+1)=0; |
||
471 | fnd_position=p+n-1;fnd_nextpos=p+n; |
||
472 | } |
||
473 | return bf; |
||
474 | } |
||
475 | |||
476 | /* Returns 1 if semicolons changed to new lines */ |
||
477 | int rows2lines(char *p) |
||
478 | { |
||
479 | char *pp, *p2; |
||
480 | int t; |
||
481 | if(strchr(p,'\n')!=NULL) return 0; |
||
482 | for(t=0, pp=p; *pp; pp++) { |
||
483 | if(*pp=='(') {p2=find_matching(pp+1,')'); if(p2!=NULL) pp=p2; continue;} |
||
484 | if(*pp=='[') {p2=find_matching(pp+1,']'); if(p2!=NULL) pp=p2; continue;} |
||
485 | if(*pp=='{') {p2=find_matching(pp+1,'}'); if(p2!=NULL) pp=p2; continue;} |
||
486 | if(*pp==';') {*pp='\n'; t++; continue;} |
||
487 | if(*pp=='&' && myisalpha(*(pp+1))) { |
||
488 | for(p2=pp+1; myisalpha(*p2) && p2-pp<14; p2++); |
||
489 | pp=p2; continue; |
||
490 | } |
||
491 | if(*pp=='&' && *(pp+1)=='#') { |
||
492 | for(p2=pp+2; myisdigit(*p2) && p2-pp<6; p2++); |
||
493 | pp=p2; continue; |
||
494 | } |
||
495 | } |
||
496 | return t; |
||
497 | } |
||
498 | |||
499 | void lines2rows(char *p) |
||
500 | { |
||
501 | char *pp; |
||
502 | strip_trailing_spaces(p); |
||
503 | for(pp=strchr(find_word_start(p),'\n'); pp!=NULL; pp=strchr(pp+1,'\n')) |
||
504 | *pp=';'; |
||
505 | } |
||
506 | |||
507 | unsigned int rownum(char *p) |
||
508 | { |
||
509 | char buf[MAX_LINELEN+1]; |
||
510 | snprintf(buf,sizeof(buf),"%s",p); |
||
511 | rows2lines(buf); |
||
512 | return linenum(buf); |
||
513 | } |
||
514 | |||
515 | /* find n-th row in a matrix p */ |
||
516 | char *fnd_row(char *p, int n, char bf[]) |
||
517 | { |
||
518 | rows2lines(p); return fnd_line(p,n,bf); |
||
519 | } |
||
520 | |||
521 | /* strstr but may have embedde zeros. |
||
522 | * Returns memory end if not found. |
||
523 | * Supposes memory ends with 0. */ |
||
524 | char *memstr(char *s1, char *s2, int len) |
||
525 | { |
||
526 | char *p, *pp; |
||
527 | pp=s1; |
||
528 | for(p=s1; p<s1+len; p=pp) { |
||
529 | pp=strstr(p,s2); if(pp!=NULL) break; |
||
530 | pp=p+strlen(p); |
||
531 | while(pp<s1+len && *pp==0) pp++; |
||
532 | } |
||
533 | return pp; |
||
534 | } |
||
535 | |||
536 | /* Check whether parentheses are balanced in a given string. |
||
537 | * Returns 0 if OK. */ |
||
538 | /* style=0: simple check. style<>0: strong check. */ |
||
539 | int check_parentheses(char *p, int style) |
||
540 | { |
||
541 | int i,j,k; |
||
542 | j=strlen(p); |
||
543 | if(j>=MAX_LINELEN) return 65535; |
||
544 | if(style!=0) { |
||
545 | char buf[MAX_LINELEN+1]; |
||
546 | char *pp, *pe, c; |
||
547 | for(pp=p;pp<p+j;pp++) { |
||
548 | switch (*pp) { |
||
549 | case ')': |
||
550 | case ']': |
||
551 | case '}': return -1; |
||
552 | case '(': c=')'; goto find; |
||
553 | case '[': c=']'; goto find; |
||
554 | case '{': c='}'; |
||
555 | find: { |
||
556 | pe=find_matching(pp+1,c); |
||
557 | if(pe==NULL) return 1; |
||
558 | memcpy(buf,pp+1,pe-pp-1); |
||
559 | buf[pe-pp-1]=0; |
||
560 | if((k=check_parentheses(buf,1))!=0) return k; |
||
561 | else pp=pe; |
||
562 | } |
||
563 | default: break; |
||
564 | } |
||
565 | } |
||
566 | return 0; |
||
567 | } |
||
568 | for(i=k=0;i<j && k>=0;i++) { |
||
569 | if(*(p+i)=='(') k++; |
||
570 | if(*(p+i)==')') k--; |
||
571 | } |
||
572 | return k; |
||
573 | } |
||
574 | |||
575 | /* Strip enclosing pairs of parentheses */ |
||
576 | void strip_enclosing_par(char *p) |
||
577 | { |
||
578 | char *p1; |
||
579 | partest: strip_trailing_spaces(p); |
||
580 | if(*p=='(') { |
||
581 | p1=find_matching(p+1,')'); |
||
582 | if(p1 && *(p1+1)==0) { |
||
3717 | reyssat | 583 | *p1=0; ovlstrcpy(p,find_word_start(p+1)); |
10 | reyssat | 584 | goto partest; |
585 | } |
||
586 | } |
||
587 | if(*p=='[') { |
||
588 | p1=find_matching(p+1,']'); |
||
589 | if(p1 && *(p1+1)==0) { |
||
3717 | reyssat | 590 | *p1=0; ovlstrcpy(p,find_word_start(p+1)); |
10 | reyssat | 591 | goto partest; |
592 | } |
||
593 | } |
||
594 | if(*p=='{') { |
||
595 | p1=find_matching(p+1,'}'); |
||
596 | if(p1 && *(p1+1)==0) { |
||
3717 | reyssat | 597 | *p1=0; ovlstrcpy(p,find_word_start(p+1)); |
10 | reyssat | 598 | goto partest; |
599 | } |
||
600 | } |
||
601 | } |
||
602 | |||
7076 | obado | 603 | /* change all spaces into ' ', and collapse multiple occurences */ |
10 | reyssat | 604 | void singlespace(char *p) |
605 | { |
||
606 | char *pp, *pt, *p2; |
||
607 | for(pp=pt=p;*pp;pp++) { |
||
7076 | obado | 608 | if(!myisspace(*pp)) {*pt++=*pp; continue; } |
609 | *pt++=' '; |
||
610 | for(p2=pp+1;myisspace(*p2);p2++); |
||
611 | pp=--p2; |
||
10 | reyssat | 612 | } |
613 | *pt=0; |
||
614 | } |
||
615 | |||
7076 | obado | 616 | /* collapses all space characters in string. */ |
10 | reyssat | 617 | void nospace(char *p) |
618 | { |
||
619 | char *pp, *pt; |
||
620 | for(pp=pt=p;*pp;pp++) if(!myisspace(*pp)) *pt++=*pp; |
||
621 | *pt=0; |
||
622 | } |
||
623 | |||
624 | void _spaces2_(char *p, char c) |
||
625 | { |
||
626 | char *pp; int n; |
||
627 | singlespace(p); |
||
628 | n=strlen(p); if(*p==' ') {memmove(p,p+1,n);n--;} |
||
629 | if(n==0) return; if(p[n-1]==' ') p[n-1]=0; |
||
630 | for(pp=strchr(p,' '); pp; pp=strchr(pp,' ')) *pp++=c; |
||
631 | } |
||
632 | /* change words to items */ |
||
633 | void words2items(char *p) |
||
634 | { _spaces2_(p,','); } |
||
635 | |||
636 | /* change words to lines */ |
||
637 | void words2lines(char *p) |
||
638 | { _spaces2_(p,'\n'); } |
||
639 | |||
640 | /* change lines to items */ |
||
641 | void lines2items(char *p) |
||
642 | { |
||
643 | char *pp; |
||
644 | for(pp=strchr(p,'\n'); pp; pp=strchr(pp,'\n')) *pp++=','; |
||
645 | } |
||
646 | |||
647 | /* change lines to words */ |
||
648 | void lines2words(char *p) |
||
649 | { |
||
650 | char *pp; |
||
651 | for(pp=strchr(p,'\n'); pp; pp=strchr(pp,'\n')) *pp++=' '; |
||
652 | } |
||
653 | |||
654 | /* change items to words */ |
||
655 | void items2words(char *p) |
||
656 | { |
||
657 | char *pp; |
||
658 | for(pp=strparstr(p,",");*pp;pp=strparstr(pp+1,",")) *pp=' '; |
||
659 | } |
||
660 | |||
661 | /* change items to lines */ |
||
662 | void items2lines(char *p) |
||
663 | { |
||
664 | char *pp; |
||
665 | for(pp=strparstr(p,",");*pp;pp=strparstr(pp+1,",")) *pp='\n'; |
||
666 | } |
||
667 | |||
668 | char *acctab="çéèêëúùûüáàâäãóòôöõíìïîñýÿÇÉÈÊËÚÙÛÜÁÀÂÃÄÓÒÔÖÕÍÌÏÎÑÝ", |
||
669 | *deatab="ceeeeuuuuaaaaaoooooiiiinyyCEEEEUUUUAAAAAOOOOOIIIINY"; |
||
670 | |||
671 | /* fold accented letters to unaccented */ |
||
672 | void deaccent(char *p) |
||
673 | { |
||
3247 | bpr | 674 | char *sp; |
10 | reyssat | 675 | char *v; |
676 | for(sp=p;*sp;sp++) { |
||
677 | if(*sp<0 && (v=strchr(acctab,*sp))!=NULL) |
||
678 | *sp=*(deatab+(v-acctab)); |
||
679 | } |
||
680 | } |
||
681 | |||
682 | char *reaccents="'`\"^~"; |
||
683 | char *reaccentl="aeiouycnAEIOUYCN"; |
||
684 | char *reaccentab="\ |
||
685 | áàäâãéèëêeíìïîióòöôõúùüûuýyÿyyccccçnnnnñ\ |
||
686 | ÁÀÄÂÃÉÈËÊEÍÌÏÎIÓÒÖÔÕÚÙÜÛUÝYYYYCCCCÇNNNNÑ"; |
||
687 | |||
688 | /* compose accent using symbol keys */ |
||
689 | void reaccent(char *p) |
||
690 | { |
||
691 | char *sp, *ap, c; |
||
692 | int i, k; |
||
693 | if(*p==0) return; |
||
694 | for(sp=p+1; *sp; sp++) { |
||
695 | ap=strchr(reaccents,*sp); if(ap==NULL) continue; |
||
696 | i=ap-reaccents; |
||
697 | sp--; ap=strchr(reaccentl,*sp); if(ap==NULL) {sp++; continue;} |
||
698 | k=ap-reaccentl; |
||
699 | c=reaccentab[k*strlen(reaccents)+i]; |
||
700 | if(c!=*sp) {*sp=c; strcpy(sp+1,sp+2);} |
||
701 | else sp++; |
||
702 | } |
||
703 | } |
||
704 | |||
5456 | bpr | 705 | /* modify a string. Bufferlen must be at least MAX_LINELEN */ |
10 | reyssat | 706 | void string_modify(char *start, char *bad_beg, char *bad_end, char *good,...) |
707 | { |
||
708 | char buf[MAX_LINELEN+1]; |
||
709 | int ln, le; |
||
710 | va_list vp; |
||
711 | |||
712 | va_start(vp,good); |
||
713 | vsnprintf(buf,sizeof(buf),good,vp); va_end(vp); |
||
714 | ln=strlen(buf); le=strlen(bad_end); |
||
715 | if(ln+le+(bad_beg-start)>=MAX_LINELEN) { |
||
716 | error1("string_too_long"); return; |
||
717 | } |
||
718 | if(ln!=bad_end-bad_beg) memmove(bad_beg+ln,bad_end,le+1); |
||
719 | memmove(bad_beg,buf,ln); |
||
720 | } |
||
721 | |||
722 | /* returns number of bytes written */ |
||
723 | int catfile(FILE *outf, char *fn,...) |
||
724 | { |
||
725 | char buf[4096]; |
||
726 | va_list vp; |
||
727 | int l,tot,fd; |
||
728 | |||
729 | tot=0; |
||
730 | va_start(vp,fn); |
||
731 | vsnprintf(buf,sizeof(buf),fn,vp); va_end(vp); |
||
732 | fd=open(buf,O_RDONLY); if(fd==-1) return 0; |
||
733 | for(l=read(fd,buf,4096);l>0 && l<=4096; l=read(fd,buf,4096)) { |
||
734 | fwrite(buf,1,l,outf); tot+=l; |
||
735 | } |
||
736 | close(fd); |
||
737 | return tot; |
||
738 | } |
||
739 | |||
740 | /* returns -1 if error */ |
||
741 | long int filelength(char *fn,...) |
||
742 | { |
||
743 | char buf[MAX_FNAME+1]; |
||
744 | va_list vp; |
||
745 | struct stat st; |
||
746 | int l; |
||
747 | |||
748 | va_start(vp,fn); |
||
749 | vsnprintf(buf,sizeof(buf),fn,vp); va_end(vp); |
||
750 | l=stat(buf,&st); if(l) return -1; |
||
751 | return st.st_size; |
||
752 | } |
||
753 |