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