Rev 8863 | Rev 10200 | 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 | |||
8863 | bpr | 333 | /* strip trailing spaces; return string end. */ |
334 | char *strip_trailing_spaces2(char *p) |
||
335 | { |
||
336 | char *pp; |
||
337 | if(*p==0) return p; |
||
338 | for(pp=p+strlen(p)-1; pp>=p && isspace(*pp); *(pp--)=0); |
||
339 | return pp; |
||
340 | } |
||
10 | reyssat | 341 | |
8863 | bpr | 342 | /* Routines for quick search of item in a list. */ |
343 | |||
7840 | bpr | 344 | /* Verify whether a list is well-ordered. For debugging uses. |
345 | * Returns 0 if order is OK, -1 otherwise. */ |
||
10 | reyssat | 346 | int verify_order(void *list, int items, size_t item_size) |
347 | { |
||
348 | int i; char *old, **p; |
||
349 | p=list; old=*p; |
||
350 | for(i=item_size;i<items*item_size;i+=item_size) { |
||
7840 | bpr | 351 | p=list+i; |
352 | if(strcmp(*p,old)<0) { |
||
353 | fprintf(stderr,"Table disorder: %s > %s",old,*p); |
||
354 | exit(1); |
||
355 | } |
||
356 | old=*p; |
||
10 | reyssat | 357 | } |
358 | return 0; |
||
359 | } |
||
360 | |||
8082 | bpr | 361 | /* searches a list. Returns index if found, (-1-index of insertion) if nomatch. |
7840 | bpr | 362 | * Uses binary search, list must be sorted. */ |
8082 | bpr | 363 | |
10 | reyssat | 364 | int search_list(void *list, int items, size_t item_size, const char *str) |
365 | { |
||
8082 | bpr | 366 | int i = 0; |
367 | while (items > 0) |
||
368 | { |
||
369 | int m = items / 2, j = i + m; |
||
370 | int k = strcmp(*(char **)(list + j * item_size), str); |
||
371 | if (k == 0) return j; |
||
372 | if (k > 0) items = m; else {i = j + 1; items -= (m + 1);} |
||
373 | } |
||
374 | return ~i; |
||
10 | reyssat | 375 | } |
376 | |||
7840 | bpr | 377 | /* Returns number of lines in string p */ |
10 | reyssat | 378 | unsigned int linenum(char *p) |
379 | { |
||
380 | int i; char *pp; |
||
381 | |||
382 | /* Ending blank line will be thus removed. */ |
||
383 | i=strlen(p); if(i>1 && *(p+i-1)=='\n') *(p+i-1)=0; |
||
384 | if(*p=='\n') i=1; else i=0; |
||
385 | for(pp=p; pp!=NULL && *pp!=0; pp=strchr(pp+1,'\n'), i++); |
||
386 | return i; |
||
387 | } |
||
388 | |||
7840 | bpr | 389 | /* Returns number of items in the list p, comma separated */ |
10 | reyssat | 390 | unsigned int itemnum(char *p) |
391 | { |
||
392 | int i; char *pp; |
||
393 | |||
394 | if(*p==0) return 0; |
||
395 | for(i=0,pp=p; pp==p || *(pp-1)!=0; pp=find_item_end(pp)+1, i++); |
||
396 | return i; |
||
397 | } |
||
398 | |||
7840 | bpr | 399 | /* Returns number of words in string p */ |
10 | reyssat | 400 | unsigned int wordnum(char *p) |
401 | { |
||
402 | int i; char *pp; |
||
7079 | bpr | 403 | |
10 | reyssat | 404 | for(i=0, pp=find_word_start(p); *pp!=0; i++) { |
7840 | bpr | 405 | while(!myisspace(*pp) && *pp!=0) pp++; |
406 | while(myisspace(*pp)) pp++; |
||
10 | reyssat | 407 | } |
408 | return i; |
||
409 | } |
||
410 | |||
7840 | bpr | 411 | /* This is just to suppress an annoying compiler warning message. */ |
10 | reyssat | 412 | unsigned int charnum(char *p) |
413 | { |
||
414 | return strlen(p); |
||
415 | } |
||
416 | |||
7840 | bpr | 417 | /* find n-th line in string p */ |
10 | reyssat | 418 | char *fnd_line(char *p, int n, char bf[]) |
419 | { |
||
420 | char *pp; |
||
421 | int i; |
||
7079 | bpr | 422 | |
10 | reyssat | 423 | for(i=1,pp=p; pp-1!=NULL && *pp!=0 && i<n; pp=strchr(pp,'\n')+1, i++); |
424 | fnd_position=pp; if(pp-1==NULL) { |
||
7840 | bpr | 425 | fnd_position=NULL; fnd_nextpos=""; *bf=0; return bf; |
10 | reyssat | 426 | } |
427 | for(i=0; *(pp+i)!=0 && *(pp+i)!='\n'; i++) *(bf+i)=*(pp+i); |
||
428 | *(bf+i)=0; |
||
429 | if(pp[i]=='\n') i++; fnd_nextpos=pp+i; |
||
430 | return bf; |
||
431 | } |
||
432 | |||
7840 | bpr | 433 | /* find n-th item in list p, comma separated */ |
10 | reyssat | 434 | char *fnd_item(char *p, int n, char bf[]) |
435 | { |
||
436 | char *pp, *pe; |
||
437 | int i; |
||
438 | |||
439 | for(i=1,pp=p; i<n && (pp==p || *(pp-1)!=0); pp=find_item_end(pp)+1, i++); |
||
440 | fnd_position=pp; if(pp>p && *(pp-1)==0) { |
||
7840 | bpr | 441 | fnd_position=NULL; *bf=0; return bf; |
10 | reyssat | 442 | } |
443 | pp=find_word_start(pp); pe=find_item_end(pp); |
||
444 | if(*pe) fnd_nextpos=pe+1; else fnd_nextpos=pe; |
||
445 | while(pe>pp && myisspace(*(pe-1))) pe--; |
||
446 | memmove(bf,pp,pe-pp); bf[pe-pp]=0; |
||
447 | return bf; |
||
448 | } |
||
449 | |||
7840 | bpr | 450 | /* find n-th word in string p */ |
10 | reyssat | 451 | char *fnd_word(char *p, int n, char bf[]) |
452 | { |
||
453 | char *pp; |
||
454 | int i; |
||
7079 | bpr | 455 | |
10 | reyssat | 456 | for(i=1, pp=find_word_start(p); *pp!=0 && i<n ; i++) { |
7840 | bpr | 457 | while(!myisspace(*pp) && *pp!=0) pp++; |
458 | pp=find_word_start(pp); |
||
10 | reyssat | 459 | } |
460 | if(*pp) fnd_position=pp; else fnd_position=NULL; |
||
461 | for(i=0; *(pp+i)!=0 && !myisspace(*(pp+i)); i++) *(bf+i)=*(pp+i); |
||
462 | fnd_nextpos=find_word_start(pp+i); |
||
7079 | bpr | 463 | *(bf+i)=0; |
10 | reyssat | 464 | return bf; |
465 | } |
||
466 | |||
7840 | bpr | 467 | /* find n-th char in string p */ |
10 | reyssat | 468 | char *fnd_char(char *p, int n, char bf[]) |
469 | { |
||
470 | int t; |
||
7079 | bpr | 471 | |
10 | reyssat | 472 | t=strlen(p); |
473 | if(n>t || n<1) {*bf=0;fnd_position=NULL; fnd_nextpos="";} |
||
474 | else { |
||
7840 | bpr | 475 | *bf=*(p+n-1); *(bf+1)=0; |
476 | fnd_position=p+n-1;fnd_nextpos=p+n; |
||
10 | reyssat | 477 | } |
478 | return bf; |
||
479 | } |
||
480 | |||
7840 | bpr | 481 | /* Returns 1 if semicolons changed to new lines */ |
10 | reyssat | 482 | int rows2lines(char *p) |
483 | { |
||
484 | char *pp, *p2; |
||
485 | int t; |
||
486 | if(strchr(p,'\n')!=NULL) return 0; |
||
487 | for(t=0, pp=p; *pp; pp++) { |
||
7840 | bpr | 488 | if(*pp=='(') {p2=find_matching(pp+1,')'); if(p2!=NULL) pp=p2; continue;} |
489 | if(*pp=='[') {p2=find_matching(pp+1,']'); if(p2!=NULL) pp=p2; continue;} |
||
490 | if(*pp=='{') {p2=find_matching(pp+1,'}'); if(p2!=NULL) pp=p2; continue;} |
||
491 | if(*pp==';') {*pp='\n'; t++; continue;} |
||
492 | if(*pp=='&' && myisalpha(*(pp+1))) { |
||
493 | for(p2=pp+1; myisalpha(*p2) && p2-pp<14; p2++); |
||
494 | pp=p2; continue; |
||
495 | } |
||
496 | if(*pp=='&' && *(pp+1)=='#') { |
||
497 | for(p2=pp+2; myisdigit(*p2) && p2-pp<6; p2++); |
||
498 | pp=p2; continue; |
||
499 | } |
||
10 | reyssat | 500 | } |
501 | return t; |
||
502 | } |
||
503 | |||
504 | void lines2rows(char *p) |
||
505 | { |
||
506 | char *pp; |
||
507 | strip_trailing_spaces(p); |
||
508 | for(pp=strchr(find_word_start(p),'\n'); pp!=NULL; pp=strchr(pp+1,'\n')) |
||
509 | *pp=';'; |
||
510 | } |
||
511 | |||
512 | unsigned int rownum(char *p) |
||
513 | { |
||
514 | char buf[MAX_LINELEN+1]; |
||
515 | snprintf(buf,sizeof(buf),"%s",p); |
||
516 | rows2lines(buf); |
||
517 | return linenum(buf); |
||
518 | } |
||
519 | |||
7840 | bpr | 520 | /* find n-th row in a matrix p */ |
10 | reyssat | 521 | char *fnd_row(char *p, int n, char bf[]) |
522 | { |
||
523 | rows2lines(p); return fnd_line(p,n,bf); |
||
524 | } |
||
525 | |||
7840 | bpr | 526 | /* strstr but may have embedded zeros. |
527 | * Returns memory end if not found. |
||
528 | * Supposes memory ends with 0. |
||
529 | */ |
||
10 | reyssat | 530 | char *memstr(char *s1, char *s2, int len) |
531 | { |
||
532 | char *p, *pp; |
||
533 | pp=s1; |
||
534 | for(p=s1; p<s1+len; p=pp) { |
||
7840 | bpr | 535 | pp=strstr(p,s2); if(pp!=NULL) break; |
536 | pp=p+strlen(p); |
||
537 | while(pp<s1+len && *pp==0) pp++; |
||
10 | reyssat | 538 | } |
539 | return pp; |
||
540 | } |
||
541 | |||
7840 | bpr | 542 | /* Check whether parentheses are balanced in a given string. |
543 | * Returns 0 if OK. |
||
544 | */ |
||
545 | /* style=0: simple check. style<>0: strong check. */ |
||
10 | reyssat | 546 | int check_parentheses(char *p, int style) |
547 | { |
||
548 | int i,j,k; |
||
549 | j=strlen(p); |
||
550 | if(j>=MAX_LINELEN) return 65535; |
||
551 | if(style!=0) { |
||
7840 | bpr | 552 | char buf[MAX_LINELEN+1]; |
553 | char *pp, *pe, c; |
||
554 | for(pp=p;pp<p+j;pp++) { |
||
555 | switch (*pp) { |
||
556 | case ')': |
||
557 | case ']': |
||
558 | case '}': return -1; |
||
559 | case '(': c=')'; goto find; |
||
560 | case '[': c=']'; goto find; |
||
561 | case '{': c='}'; |
||
562 | find: { |
||
563 | pe=find_matching(pp+1,c); |
||
564 | if(pe==NULL) return 1; |
||
565 | memcpy(buf,pp+1,pe-pp-1); |
||
566 | buf[pe-pp-1]=0; |
||
567 | if((k=check_parentheses(buf,1))!=0) return k; |
||
568 | else pp=pe; |
||
569 | } |
||
570 | default: break; |
||
571 | } |
||
572 | } |
||
573 | return 0; |
||
10 | reyssat | 574 | } |
575 | for(i=k=0;i<j && k>=0;i++) { |
||
7840 | bpr | 576 | if(*(p+i)=='(') k++; |
577 | if(*(p+i)==')') k--; |
||
10 | reyssat | 578 | } |
579 | return k; |
||
580 | } |
||
581 | |||
7840 | bpr | 582 | /* Strip enclosing pairs of parentheses */ |
10 | reyssat | 583 | void strip_enclosing_par(char *p) |
584 | { |
||
585 | char *p1; |
||
586 | partest: strip_trailing_spaces(p); |
||
587 | if(*p=='(') { |
||
7840 | bpr | 588 | p1=find_matching(p+1,')'); |
589 | if(p1 && *(p1+1)==0) { |
||
590 | *p1=0; ovlstrcpy(p,find_word_start(p+1)); |
||
591 | goto partest; |
||
592 | } |
||
10 | reyssat | 593 | } |
594 | if(*p=='[') { |
||
7840 | bpr | 595 | p1=find_matching(p+1,']'); |
596 | if(p1 && *(p1+1)==0) { |
||
597 | *p1=0; ovlstrcpy(p,find_word_start(p+1)); |
||
598 | goto partest; |
||
599 | } |
||
10 | reyssat | 600 | } |
601 | if(*p=='{') { |
||
7840 | bpr | 602 | p1=find_matching(p+1,'}'); |
603 | if(p1 && *(p1+1)==0) { |
||
604 | *p1=0; ovlstrcpy(p,find_word_start(p+1)); |
||
605 | goto partest; |
||
606 | } |
||
10 | reyssat | 607 | } |
608 | } |
||
609 | |||
7076 | obado | 610 | /* change all spaces into ' ', and collapse multiple occurences */ |
10 | reyssat | 611 | void singlespace(char *p) |
612 | { |
||
613 | char *pp, *pt, *p2; |
||
614 | for(pp=pt=p;*pp;pp++) { |
||
7840 | bpr | 615 | if(!myisspace(*pp)) {*pt++=*pp; continue; } |
616 | *pt++=' '; |
||
617 | for(p2=pp+1;myisspace(*p2);p2++){}; |
||
618 | pp=--p2; |
||
10 | reyssat | 619 | } |
620 | *pt=0; |
||
621 | } |
||
622 | |||
7076 | obado | 623 | /* collapses all space characters in string. */ |
10 | reyssat | 624 | void nospace(char *p) |
625 | { |
||
626 | char *pp, *pt; |
||
8647 | bpr | 627 | for(pp=pt=p;*pp;pp++) if(!myismspace(*pp)) *pt++=*pp; |
10 | reyssat | 628 | *pt=0; |
629 | } |
||
630 | |||
631 | void _spaces2_(char *p, char c) |
||
632 | { |
||
633 | char *pp; int n; |
||
634 | singlespace(p); |
||
635 | n=strlen(p); if(*p==' ') {memmove(p,p+1,n);n--;} |
||
636 | if(n==0) return; if(p[n-1]==' ') p[n-1]=0; |
||
637 | for(pp=strchr(p,' '); pp; pp=strchr(pp,' ')) *pp++=c; |
||
638 | } |
||
7840 | bpr | 639 | /* change words to items */ |
10 | reyssat | 640 | void words2items(char *p) |
7840 | bpr | 641 | { _spaces2_(p,','); } |
10 | reyssat | 642 | |
7840 | bpr | 643 | /* change words to lines */ |
10 | reyssat | 644 | void words2lines(char *p) |
7840 | bpr | 645 | { _spaces2_(p,'\n'); } |
10 | reyssat | 646 | |
7840 | bpr | 647 | /* change lines to items */ |
10 | reyssat | 648 | void lines2items(char *p) |
649 | { |
||
650 | char *pp; |
||
651 | for(pp=strchr(p,'\n'); pp; pp=strchr(pp,'\n')) *pp++=','; |
||
652 | } |
||
653 | |||
7840 | bpr | 654 | /* change lines to words */ |
10 | reyssat | 655 | void lines2words(char *p) |
656 | { |
||
657 | char *pp; |
||
658 | for(pp=strchr(p,'\n'); pp; pp=strchr(pp,'\n')) *pp++=' '; |
||
659 | } |
||
660 | |||
7840 | bpr | 661 | /* change items to words */ |
10 | reyssat | 662 | void items2words(char *p) |
663 | { |
||
664 | char *pp; |
||
665 | for(pp=strparstr(p,",");*pp;pp=strparstr(pp+1,",")) *pp=' '; |
||
666 | } |
||
667 | |||
7840 | bpr | 668 | /* change items to lines */ |
10 | reyssat | 669 | void items2lines(char *p) |
670 | { |
||
671 | char *pp; |
||
672 | for(pp=strparstr(p,",");*pp;pp=strparstr(pp+1,",")) *pp='\n'; |
||
673 | } |
||
674 | |||
675 | char *acctab="çéèêëúùûüáàâäãóòôöõíìïîñýÿÇÉÈÊËÚÙÛÜÁÀÂÃÄÓÒÔÖÕÍÌÏÎÑÝ", |
||
676 | *deatab="ceeeeuuuuaaaaaoooooiiiinyyCEEEEUUUUAAAAAOOOOOIIIINY"; |
||
677 | |||
7840 | bpr | 678 | /* fold accented letters to unaccented */ |
10 | reyssat | 679 | void deaccent(char *p) |
680 | { |
||
3247 | bpr | 681 | char *sp; |
10 | reyssat | 682 | char *v; |
683 | for(sp=p;*sp;sp++) { |
||
7840 | bpr | 684 | if(*sp<0 && (v=strchr(acctab,*sp))!=NULL) |
685 | *sp=*(deatab+(v-acctab)); |
||
10 | reyssat | 686 | } |
687 | } |
||
688 | |||
689 | char *reaccents="'`\"^~"; |
||
690 | char *reaccentl="aeiouycnAEIOUYCN"; |
||
691 | char *reaccentab="\ |
||
692 | áàäâãéèëêeíìïîióòöôõúùüûuýyÿyyccccçnnnnñ\ |
||
693 | ÁÀÄÂÃÉÈËÊEÍÌÏÎIÓÒÖÔÕÚÙÜÛUÝYYYYCCCCÇNNNNÑ"; |
||
694 | |||
7840 | bpr | 695 | /* compose accent using symbol keys */ |
10 | reyssat | 696 | void reaccent(char *p) |
697 | { |
||
698 | char *sp, *ap, c; |
||
699 | int i, k; |
||
700 | if(*p==0) return; |
||
701 | for(sp=p+1; *sp; sp++) { |
||
7840 | bpr | 702 | ap=strchr(reaccents,*sp); if(ap==NULL) continue; |
703 | i=ap-reaccents; |
||
704 | sp--; ap=strchr(reaccentl,*sp); if(ap==NULL) {sp++; continue;} |
||
705 | k=ap-reaccentl; |
||
706 | c=reaccentab[k*strlen(reaccents)+i]; |
||
7843 | bpr | 707 | if(c!=*sp) {*sp=c; ovlstrcpy(sp+1,sp+2);} |
7840 | bpr | 708 | else sp++; |
10 | reyssat | 709 | } |
710 | } |
||
711 | |||
7840 | bpr | 712 | /* modify a string. Bufferlen must be at least MAX_LINELEN */ |
8086 | bpr | 713 | void string_modify1(char *start, char *bad_beg, char *bad_end, char *good,...) |
10 | reyssat | 714 | { |
715 | char buf[MAX_LINELEN+1]; |
||
716 | int ln, le; |
||
717 | va_list vp; |
||
7079 | bpr | 718 | |
10 | reyssat | 719 | va_start(vp,good); |
720 | vsnprintf(buf,sizeof(buf),good,vp); va_end(vp); |
||
721 | ln=strlen(buf); le=strlen(bad_end); |
||
722 | if(ln+le+(bad_beg-start)>=MAX_LINELEN) { |
||
8195 | bpr | 723 | error("string_too_long"); return; |
10 | reyssat | 724 | } |
725 | if(ln!=bad_end-bad_beg) memmove(bad_beg+ln,bad_end,le+1); |
||
726 | memmove(bad_beg,buf,ln); |
||
727 | } |
||
728 | |||
8861 | bpr | 729 | /* modify a string. Bufferlen must be at least MAX_LINELEN */ |
730 | void string_modify3(char *start, char *bad_beg, char *bad_end, char *good,...) |
||
731 | { |
||
732 | char buf[MAX_LINELEN+1]; |
||
733 | va_list vp; |
||
734 | |||
735 | va_start(vp,good); |
||
736 | vsnprintf(buf,sizeof(buf),good,vp); va_end(vp); |
||
737 | if(strlen(start)-(bad_end-bad_beg)+strlen(buf)>=MAX_LINELEN) |
||
738 | return; /* this is an error situation. */ |
||
739 | strcat(buf,bad_end); |
||
740 | ovlstrcpy(bad_beg,buf); |
||
741 | } |
||
742 | |||
7840 | bpr | 743 | /* returns number of bytes written */ |
10 | reyssat | 744 | int catfile(FILE *outf, char *fn,...) |
745 | { |
||
746 | char buf[4096]; |
||
747 | va_list vp; |
||
748 | int l,tot,fd; |
||
7079 | bpr | 749 | |
10 | reyssat | 750 | tot=0; |
751 | va_start(vp,fn); |
||
752 | vsnprintf(buf,sizeof(buf),fn,vp); va_end(vp); |
||
753 | fd=open(buf,O_RDONLY); if(fd==-1) return 0; |
||
754 | for(l=read(fd,buf,4096);l>0 && l<=4096; l=read(fd,buf,4096)) { |
||
7840 | bpr | 755 | fwrite(buf,1,l,outf); tot+=l; |
10 | reyssat | 756 | } |
757 | close(fd); |
||
758 | return tot; |
||
759 | } |
||
760 | |||
7840 | bpr | 761 | /* returns -1 if error */ |
10 | reyssat | 762 | long int filelength(char *fn,...) |
763 | { |
||
764 | char buf[MAX_FNAME+1]; |
||
765 | va_list vp; |
||
766 | struct stat st; |
||
767 | int l; |
||
7079 | bpr | 768 | |
10 | reyssat | 769 | va_start(vp,fn); |
770 | vsnprintf(buf,sizeof(buf),fn,vp); va_end(vp); |
||
771 | l=stat(buf,&st); if(l) return -1; |
||
772 | return st.st_size; |
||
773 | } |
||
8880 | bpr | 774 | |
775 | char *parend(char *p) |
||
776 | { |
||
777 | char *pp; int t; |
||
778 | t=0; for(pp=p;*pp;pp++) { |
||
779 | if(*pp=='(') t++; |
||
780 | if(*pp==')') {t--; if(t==0) return pp; if(t<0) return NULL;} |
||
781 | } |
||
782 | return NULL; |
||
783 | } |
||
784 | |||
785 | char *bufprep(char *p) |
||
786 | { |
||
787 | /* singlespace(p); strip_trailing_spaces(p); return find_word_start(p); */ |
||
788 | nospace(p); return p; |
||
789 | } |