Subversion Repositories wimsdev

Rev

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