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