Subversion Repositories wimsdev

Rev

Rev 7840 | Go to most recent revision | Details | 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
 */
17
 
18
        /* Subroutines to manipulate math expressions */
19
 
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
void mathvarlist(char *p)
59
{
60
    char buf[MAX_LINELEN+1], *pb, *pe, *pp;
61
    char *l[10240];
62
    int i,j,len, nofn=0;
63
 
64
    pb=find_word_start(p);pe=find_word_end(pb);
65
    if(pe-pb==strlen("nofn") && memcmp(pb,"nofn",strlen("nofn"))==0) {
66
        nofn=1; pb=find_word_start(pe);
67
    }
68
    snprintf(buf,sizeof(buf),"%s",pb);
69
    len=strlen(buf);
70
    for(i=0,pb=buf;pb<buf+len;pb++) {
71
        if(!isalpha(*pb)) continue;
72
        if(pb>buf && isalnum(*(pb-1))) {
73
            pb=find_mathvar_end(pb); continue;
74
        }
75
        pe=find_mathvar_end(pb); pp=find_word_start(pe);
76
        if(nofn && *pp=='(') {
77
            pb=pe; continue;
78
        }
79
        *pe=0;
80
        if(i<10240) {
81
            for(j=0;j<i;j++) if(strcmp(pb,l[j])==0) goto nextp;
82
            l[i++]=pb;
83
        }
84
        nextp:
85
        pb=pe;
86
    }
87
    for(*p=0,j=0;j<i;j++) {
88
        strcat(p,l[j]);
89
        if(j<i-1) strcat(p,",");
90
    }
91
}
92