Subversion Repositories wimsdev

Rev

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