Subversion Repositories wimsdev

Rev

Rev 7673 | Rev 8185 | Go to most recent revision | Details | Compare with Previous | 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
char *bufprep(char *p)
19
{
20
/*  singlespace(p); strip_trailing_spaces(p); return find_word_start(p); */
21
    nospace(p); return p;
22
}
23
 
24
char *parend(char *p)
25
{
26
    char *pp; int t;
27
    t=0; for(pp=p;*pp;pp++) {
7673 bpr 28
     if(*pp=='(') t++;
29
     if(*pp==')') {t--; if(t==0) return pp; if(t<0) return NULL;}
10 reyssat 30
    }
31
    return NULL;
32
}
33
 
34
char *relation_type[]={
35
    "sametext","samecase",
36
    "in",  "wordof","itemof","lineof","varof","variableof"
37
};
38
#define total_relations (sizeof(relation_type)/sizeof(relation_type[0]))
39
 
7673 bpr 40
/* Compares two string. Returns 1 if yes, 0 if no, -1 if error.
41
 * In fact, -1 will occur only if module_error() is modified to
8155 bpr 42
 * return instead of abort.
43
 */
7673 bpr 44
/* TODO: quoted string. */
10 reyssat 45
int compare(char *p, int numeric, int lvl)
46
{
47
    char *p1, *p2, *pp, *pt;
48
    char *r1, *r2;
49
    int i, l, k, r, neg, gotl;
7673 bpr 50
 
51
/* Check global pairs of parentheses */
10 reyssat 52
    p2=strip_trailing_spaces(p);
53
    p1=find_word_start(p);
54
    if(lvl==0 && p2-p1>=MAX_LINELEN-1) module_error("parm_too_long");
55
    while(*p1=='(' && *p2==')' && p2==parend(p1)) {
7673 bpr 56
     lvl=0; p1=find_word_start(++p1);
57
     do p2--; while(p2>=p1 && myisspace(*p2));
58
     p2[1]=0;
10 reyssat 59
    }
60
    gotl=100; r1=r2=p1; r=-1; neg=0;
61
    for(pp=p1; *pp; pp++) {
7673 bpr 62
     if(*pp==')') {badpar: module_error("unmatched_parentheses"); return -1;}
63
     if(*pp=='(') {
64
         pp=parend(pp); if(pp==NULL) goto badpar;
65
         continue;
66
     }
67
     if(gotl>3) {
68
         switch(*pp) {
69
          case '<': {
70
              gotl=3; r1=pp; r2=r1+1; r=102; neg=0;
71
              if(*r2=='=') {r2++; r=103; neg=1;}
72
              if(*r2=='>') {
73
               r2++; neg=1;
74
               if(numeric) r=101; else r=0;
75
              }
76
              break;
77
          }
78
          case '>': {
79
              gotl=3; r1=pp; r2=r1+1; r=103; neg=0;
80
              if(*r2=='=') {r2++; r=102; neg=1;}
81
              break;
82
          }
83
          case '=': {
84
              gotl=3; neg=0; r1=pp; r2=r1+1; if(*r2=='=') r2++;
85
              if(numeric) r=101; else r=0;
86
              break;
87
          }
88
          case '!': {
89
              if(pp[1]=='=') {
90
               gotl=3; r1=pp; r2=pp+2; neg=1;
91
               if(numeric) r=101; else r=0;
92
              }
93
              break;
94
          }
95
         }
96
         if(r2>p1) {
97
          if(lvl==2) break;
98
          pp=r2-1; continue;
99
         }
100
     }
101
     if(!myisspace(*pp)) continue;
102
     pp=find_word_start(pp);
103
     if(gotl>3) {
104
         if(pp[0]=='i' && pp[1]=='s') {k=2; neg=0; goto isnot;}
105
         if(pp[0]=='n' && pp[1]=='o' && pp[2]=='t') {k=3; neg=1; goto isnot;}
106
         goto rel;
107
         isnot:
108
         if(strchr("siwlv",pp[k])==NULL) goto rel;
109
         pt=pp; pp+=k; l=0;
110
         for(i=0;i<total_relations;i++) {
111
          l=strlen(relation_type[i]);
112
          if(strncmp(pp, relation_type[i], l)==0 &&
113
             ((!myisalnum(pp[l]) && pp[l]!='_') || pp[l]==0)) break;
114
         }
115
         if(i>=total_relations) {pp--; continue;}
116
         gotl=3; r=i+1; pp+=l; r1=pt; r2=pp;
117
         if(lvl==2) break; else {pp--; continue;}
118
     }
119
     rel:
120
     if(*pp!='|' && *pp!='&' && *pp!='a' && *pp!='o')
121
         {pp--; continue;}
122
     if(gotl>2 &&
123
        ((myisspace(pp[3]) && strncmp(pp,"and",3)==0) ||
124
         (myisspace(pp[2]) && strncmp(pp,"&&",2)==0))) {
125
         gotl=2; r1=pp; pp=r2=find_word_end(r1);
126
         if(lvl==1) break; else {pp--;continue;}
127
     }
128
     if(gotl>1 && myisspace(pp[2]) &&
129
        (strncmp(pp,"or",2)==0 || strncmp(pp,"||",2)==0)) {
130
         gotl=1; r1=pp; r2=pp=r1+2; break;
131
     }
10 reyssat 132
    }
133
    if(gotl>20) {
7673 bpr 134
     setvar(error_data_string,"relation not defined");
135
     module_error("comp_syntax"); return -1;
10 reyssat 136
    }
7673 bpr 137
 
10 reyssat 138
    switch(gotl) {
7673 bpr 139
     case 1: { /* or */
140
         *r1=0; i=compare(p1,numeric,1); if(i) return i;
141
         else return compare(r2,numeric,0);
142
     }
143
     case 2: { /* and */
144
         *r1=0; i=compare(p1,numeric,2); if(i<=0) return i;
145
         else return compare(r2,numeric,1);
146
     }
147
     case 3: { /* atomic comparison */
148
         if(r<100) { /* textual comparison */
149
          char buf1[MAX_LINELEN+1], buf2[MAX_LINELEN+1];
150
          while(r1>p1 && myisspace(r1[-1])) r1--;
151
          memmove(buf1,p1,r1-p1); buf1[r1-p1]=0;
152
          r2=find_word_start(r2);
153
          while(p2>=r2 && myisspace(*p2)) p2--;
154
          memmove(buf2,r2,p2+1-r2); buf2[p2+1-r2]=0;
155
          substitute(buf1); substitute(buf2);
156
          switch(r) {
157
              case 0: { /* equal */
158
               if(strcmp(buf1,buf2)==0) return 1^neg; else return neg;
159
              }
160
              case 1: { /* sametext */
161
               deaccent(buf1); deaccent(buf2);
162
               if(strcasecmp(bufprep(buf1),bufprep(buf2))==0)
163
                 return 1^neg;
164
               else return neg;
165
              }
166
              case 2: { /* samecase */
167
               if(strcmp(bufprep(buf1),bufprep(buf2))==0)
168
                 return 1^neg;
169
               else return neg;
170
              }
171
              case 3: { /* in */
172
               if(strstr(buf2,buf1)!=NULL) return 1^neg; else return neg;
173
              }
174
              case 4: { /* wordof */
175
               if(wordchr(buf2,buf1)!=NULL) return 1^neg; else return neg;
176
              }
177
              case 5: { /* itemof */
178
               if(itemchr(buf2,buf1)!=NULL) return 1^neg; else return neg;
179
              }
180
              case 6: { /* lineof */
181
               if(linechr(buf2,buf1)!=NULL) return 1^neg; else return neg;
182
              }
183
              case 7:
184
              case 8: { /* varof */
185
               if(varchr(buf2,buf1)!=NULL) return 1^neg; else return neg;
186
              }
187
          }
188
         }
189
         else { /* numerical comparison */
190
          double d1, d2, sum, diff, prec;
191
          *r1=0;
192
          d1=evalue(p1); d2=evalue(r2);
193
          sum=d1+d2; if(sum<0) sum=-sum;
194
          diff=d1-d2; if(diff<0) diff=-diff;
195
          prec=evalue(getvar("wims_compare_precision"));  /* Move string name to header! */
196
          diff=diff*prec;
197
          if(prec>0 && prec<1E10) sum=sum+1/prec;
198
          switch(r) {
199
              case 101: { /* = */
200
               if(sum>=diff) return 1^neg; else return neg;
201
              }
202
              case 102: { /* < */
203
               if(d1<d2) return 1^neg; else return neg;
204
              }
205
              case 103: { /* > */
206
               if(d1>d2) return 1^neg; else return neg;
207
              }
208
              default: break; /* should never occur */
209
          }
210
         }
211
     }
10 reyssat 212
    }
213
    internal_error("compare(): this should never happen.");
214
    return -1;
215
}
216