Subversion Repositories wimsdev

Rev

Rev 8185 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10 reyssat 1
/*    Copyright (C) 2002-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
 */
8185 bpr 17
 
8136 bpr 18
#include "mathexp.h"
8185 bpr 19
 
10 reyssat 20
void strcompress(char *src, char *dest)
21
{
22
    char *p1, *p2;
23
    char lastnospc, lastspc;
24
    p1=find_word_start(src); p2=dest;
25
    lastnospc=lastspc=0;
26
    for(;*p1 && p2-dest<MAX_LINELEN;p1++) {
8136 bpr 27
     if(isspace(*p1)) {lastspc=' '; continue;}
28
     if(lastspc!=0) {
29
         if((isalnum(lastnospc) || lastnospc=='_') &&
30
            (isalnum(*p1) || *p1=='_')) *p2++=' ';
31
     }
32
     lastspc=0; *p2++=*p1; lastnospc=*p1;
10 reyssat 33
    }
34
    *p2=0;
35
}
36
 
8136 bpr 37
/* the type of an expression, with cutpoints */
10 reyssat 38
int _type(char *p, int commas[], int *commacnt)
39
{
40
    int i,l,lvl;
41
    char *p1, *p2, *p3, *p4;
42
    char buf[MAX_LINELEN+1];
43
 
44
    lvl=-1; *commacnt=0;
45
    for(p1=find_word_start(p), p2=p1; *p2; p2=find_word_start(p3)) {
8136 bpr 46
     if(*p2=='.' || isdigit(*p2)) {
47
         if(lvl<exp_number) lvl=exp_number;
48
         (void)strtod(p2,&p3); continue;
49
     }
50
     if(*p2=='(') {
51
         p3=find_matching(p2+1,')');
52
         if(lvl<exp_paren) lvl=exp_paren;
53
         paren:
54
         if(p3==NULL) error("Unmatched parentheses");
55
         p3++; continue;
56
     }
57
     if(*p2=='[') {
58
         if(lvl<exp_matrix) lvl=exp_matrix;
59
         p3=find_matching(p2+1,']'); goto paren;
60
     }
61
     if(*p2=='{') {
62
         if(lvl<exp_set) lvl=exp_set;
63
         p3=find_matching(p2+1,'}'); goto paren;
64
     }
65
     if(isalpha(*p2)) {
66
         for(p3=p2; *p3=='_' || isalnum(*p3); p3++);
67
         if(p3-p2>=16) goto notdefined;
68
         memmove(buf,p2,p3-p2); buf[p3-p2]=0;
69
         for(i=0;i<opalphano && strcmp(buf,opalpha[i].name)!=0; i++);
70
         if(i<opalphano) {
71
          l=opalpha[i].lvl;
72
          if(l>lvl) {*commacnt=0; lvl=l;}
73
          if(l==lvl && *commacnt<MAX_COMMAS-2) {
74
              commas[(*commacnt)++]=p2-p;
75
              commas[(*commacnt)++]=p3-p;
76
          }
77
          continue;
78
         }
79
         notdefined: p4=find_word_start(p3);
80
         if(*p4=='(') {
81
          if(lvl<exp_fn) lvl=exp_fn;
82
          p3=find_matching(p4+1,')');
83
          if(p3==NULL) error("Unmatched parentheses.");
84
          p4++; memmove(buf,p4,p3-p2); buf[p3-p4]=0;
85
          p3++;
86
         }
87
         else {
88
          if(lvl<exp_variable) lvl=exp_variable;
89
         }
90
         continue;
91
     }
92
     for(i=0;i<oppunctno && strncmp(p2,oppunct[i].name,strlen(oppunct[i].name))!=0; i++);
93
     if(i>=oppunctno) error("Unknown operator.");
94
     p3=p2+strlen(oppunct[i].name); l=oppunct[i].lvl;
95
     if(l>lvl) {*commacnt=0; lvl=l;}
96
     if(l==lvl && *commacnt<MAX_COMMAS-2) {
97
         commas[(*commacnt)++]=p2-p;
98
         commas[(*commacnt)++]=p3-p;
99
     }
10 reyssat 100
    }
101
    return lvl;
102
}
103
 
104
void getregex(char *p)
105
{
106
    char *p1, *p2, *p3;
107
 
108
    p1=find_word_start(p);
109
    for(regexcnt=0; regexcnt<MAX_REGEX && *p1!=0; p1=find_word_start(p2)) {
8136 bpr 110
     p2=find_word_end(p1); if(*p2) *p2++=0;
111
     regexchk[regexcnt].srcreg=p1;
112
     for(p3=p1; *p3 && (isalnum(*p3) || *p3=='.'); p3++);
113
     if(*p3==0) {
114
         regexchk[regexcnt].isvar=1; regexcnt++;
115
     }
116
     else {
117
         regexchk[regexcnt].isvar=0;
118
         if(regcomp(&(regexchk[regexcnt].cmpreg),p1,REG_EXTENDED|REG_ICASE)==0)
119
           regexcnt++;
120
     }
10 reyssat 121
    }
122
}
123
 
8136 bpr 124
/* returns 1 if yes, 0 if no. */
10 reyssat 125
int checkregex(char *p)
126
{
127
    int i;
128
    char buf[MAX_LINELEN+1];
129
    regmatch_t matchbuf[100];
8136 bpr 130
 
10 reyssat 131
    if(regexcnt<1) return 1; /* nothing to check; always true. */
132
    strcompress(p,buf);
8136 bpr 133
    for(i=0;i<regexcnt;i++) {     /* all regex words are ANDed. */
134
     if(regexchk[i].isvar) {
135
         if(varchr(buf,regexchk[i].srcreg)==NULL) return 0;
136
     }
137
     else if(regexec(&(regexchk[i].cmpreg),buf,80,matchbuf,0)!=0) return 0;
10 reyssat 138
    }
139
    return 1;
140
}