Subversion Repositories wimsdev

Rev

Rev 10 | Rev 8160 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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