Subversion Repositories wimsdev

Rev

Rev 8100 | Rev 8195 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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