Subversion Repositories wimsdev

Rev

Rev 7840 | 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.  
  18. /* Subroutines to manipulate math expressions */
  19. #include "libwims.h"
  20.  
  21. /* Points to the start of a mathematics variable (or number) */
  22. char *find_mathvar_start(char *p)
  23. {
  24.     int i;
  25.     for(i=0;!isalnum(*p) && *p!='.' && *p!=0 && i<MAX_LINELEN; p++,i++);
  26.     return p;
  27. }
  28.  
  29. /* Points to the end of a mathematics variable (or number) */
  30. char *find_mathvar_end(char *p)
  31. {
  32.     int i; char *pp;
  33.     if(!isalnum(*p) && *p!='.') return p;
  34.     if(isalpha(*p)) {
  35.       for(i=0; *p!=0 && (isalnum(*p) || *p=='.' || *p=='\'')
  36.           && i<MAX_LINELEN;p++,i++);
  37.       return p;
  38.     }
  39.     else {
  40.       int t=0;
  41.       expon:
  42.       for(i=0; (isdigit(*p) || *p=='.') && i<MAX_LINELEN;p++,i++);
  43.       pp=p; if(t) return pp;
  44.       while(isspace(*p)) p++;
  45.       if(*p=='e' || *p=='E'){
  46.           t=1; p++; while(isspace(*p)) p++;
  47.           if(isdigit(*p)) goto expon;
  48.           if((*p=='-' || *p=='+') && isdigit(*(p+1))) {
  49.             p++; goto expon;
  50.           }
  51.       }
  52.       return pp;
  53.     }
  54. }
  55.  
  56. /* list variable (function) names in an expression.
  57.  * buffer is modified to contain the list.
  58.  */
  59. void mathvarlist(char *p)
  60. {
  61.     char buf[MAX_LINELEN+1], *pb, *pe, *pp;
  62.     char *l[10240];
  63.     int i,j,len, nofn=0;
  64.  
  65.     pb=find_word_start(p);pe=find_word_end(pb);
  66.     if(pe-pb==strlen("nofn") && memcmp(pb,"nofn",strlen("nofn"))==0) {
  67.       nofn=1; pb=find_word_start(pe);
  68.     }
  69.     snprintf(buf,sizeof(buf),"%s",pb);
  70.     len=strlen(buf);
  71.     for(i=0,pb=buf;pb<buf+len;pb++) {
  72.       if(!isalpha(*pb)) continue;
  73.       if(pb>buf && isalnum(*(pb-1))) {
  74.           pb=find_mathvar_end(pb); continue;
  75.       }
  76.       pe=find_mathvar_end(pb); pp=find_word_start(pe);
  77.       if(nofn && *pp=='(') {
  78.           pb=pe; continue;
  79.       }
  80.       *pe=0;
  81.       if(i<10240) {
  82.           for(j=0;j<i;j++) if(strcmp(pb,l[j])==0) goto nextp;
  83.           l[i++]=pb;
  84.       }
  85.       nextp:
  86.       pb=pe;
  87.     }
  88.     for(*p=0,j=0;j<i;j++) {
  89.       strcat(p,l[j]);
  90.       if(j<i-1) strcat(p,",");
  91.     }
  92. }
  93.