Back to index

wims  3.65+svn20090927
compare.c
Go to the documentation of this file.
00001 /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
00002  *
00003  *  This program is free software; you can redistribute it and/or modify
00004  *  it under the terms of the GNU General Public License as published by
00005  *  the Free Software Foundation; either version 2 of the License, or
00006  *  (at your option) any later version.
00007  *
00008  *  This program is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *  GNU General Public License for more details.
00012  *
00013  *  You should have received a copy of the GNU General Public License
00014  *  along with this program; if not, write to the Free Software
00015  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00016  */
00017 
00018 char *bufprep(char *p)
00019 {
00020 /*  singlespace(p); strip_trailing_spaces(p); return find_word_start(p); */
00021     nospace(p); return p;
00022 }
00023 
00024 char *parend(char *p)
00025 {
00026     char *pp; int t;
00027     t=0; for(pp=p;*pp;pp++) {
00028        if(*pp=='(') t++;
00029        if(*pp==')') {t--; if(t==0) return pp; if(t<0) return NULL;}
00030     }
00031     return NULL;
00032 }
00033 
00034 char *relation_type[]={
00035     "sametext","samecase",
00036     "in",  "wordof","itemof","lineof","varof","variableof"
00037 };
00038 #define total_relations (sizeof(relation_type)/sizeof(relation_type[0]))
00039 
00040        /* Compares two string. Returns 1 if yes, 0 if no, -1 if error. 
00041         * In fact, -1 will occur only if module_error() is modified to 
00042         * return instead of abort. */
00043        /* TODO: quoted string. */
00044 int compare(char *p, int numeric, int lvl)
00045 {
00046     char *p1, *p2, *pp, *pt;
00047     char *r1, *r2;
00048     int i, l, k, r, neg, gotl;
00049     
00050        /* Check global pairs of parentheses */
00051     p2=strip_trailing_spaces(p);
00052     p1=find_word_start(p);
00053     if(lvl==0 && p2-p1>=MAX_LINELEN-1) module_error("parm_too_long");
00054     while(*p1=='(' && *p2==')' && p2==parend(p1)) {
00055        lvl=0; p1=find_word_start(++p1);
00056        do p2--; while(p2>=p1 && myisspace(*p2));
00057        p2[1]=0;
00058     }
00059     gotl=100; r1=r2=p1; r=-1; neg=0;
00060     for(pp=p1; *pp; pp++) {
00061        if(*pp==')') {badpar: module_error("unmatched_parentheses"); return -1;}
00062        if(*pp=='(') {
00063            pp=parend(pp); if(pp==NULL) goto badpar;
00064            continue;
00065        }
00066        if(gotl>3) {
00067            switch(*pp) {
00068               case '<': {
00069                   gotl=3; r1=pp; r2=r1+1; r=102; neg=0;
00070                   if(*r2=='=') {r2++; r=103; neg=1;}
00071                   if(*r2=='>') {
00072                      r2++; neg=1;
00073                      if(numeric) r=101; else r=0;
00074                   }
00075                   break;
00076               }
00077               case '>': {
00078                   gotl=3; r1=pp; r2=r1+1; r=103; neg=0;
00079                   if(*r2=='=') {r2++; r=102; neg=1;}
00080                   break;
00081               }
00082               case '=': {
00083                   gotl=3; neg=0; r1=pp; r2=r1+1; if(*r2=='=') r2++;
00084                   if(numeric) r=101; else r=0;
00085                   break;
00086               }
00087               case '!': {
00088                   if(pp[1]=='=') {
00089                      gotl=3; r1=pp; r2=pp+2; neg=1;
00090                      if(numeric) r=101; else r=0;
00091                   }
00092                   break;
00093               }
00094            }
00095            if(r2>p1) {
00096               if(lvl==2) break;
00097               pp=r2-1; continue;
00098            }
00099        }
00100        if(!myisspace(*pp)) continue;
00101        pp=find_word_start(pp);
00102        if(gotl>3) {
00103            if(pp[0]=='i' && pp[1]=='s') {k=2; neg=0; goto isnot;}
00104            if(pp[0]=='n' && pp[1]=='o' && pp[2]=='t') {k=3; neg=1; goto isnot;}
00105            goto rel;
00106            isnot:
00107            if(strchr("siwlv",pp[k])==NULL) goto rel;
00108            pt=pp; pp+=k; l=0;
00109            for(i=0;i<total_relations;i++) {
00110               l=strlen(relation_type[i]);
00111               if(strncmp(pp, relation_type[i], l)==0 && 
00112                  ((!myisalnum(pp[l]) && pp[l]!='_') || pp[l]==0)) break;
00113            }
00114            if(i>=total_relations) {pp--; continue;}
00115            gotl=3; r=i+1; pp+=l; r1=pt; r2=pp;
00116            if(lvl==2) break; else {pp--; continue;}
00117        }
00118        rel:
00119        if(*pp!='|' && *pp!='&' && *pp!='a' && *pp!='o')
00120            {pp--; continue;}
00121        if(gotl>2 && 
00122           ((myisspace(pp[3]) && strncmp(pp,"and",3)==0) || 
00123            (myisspace(pp[2]) && strncmp(pp,"&&",2)==0))) {
00124            gotl=2; r1=pp; pp=r2=find_word_end(r1);
00125            if(lvl==1) break; else {pp--;continue;}
00126        }
00127        if(gotl>1 && myisspace(pp[2]) && 
00128           (strncmp(pp,"or",2)==0 || strncmp(pp,"||",2)==0)) {
00129            gotl=1; r1=pp; r2=pp=r1+2; break;
00130        }
00131     }
00132     if(gotl>20) {
00133        setvar(error_data_string,"relation not defined");
00134        module_error("comp_syntax"); return -1;
00135     }
00136     
00137     switch(gotl) {
00138        case 1: {     /* or */
00139            *r1=0; i=compare(p1,numeric,1); if(i) return i;
00140            else return compare(r2,numeric,0);
00141        }
00142        case 2:       {      /* and */
00143            *r1=0; i=compare(p1,numeric,2); if(i<=0) return i;
00144            else return compare(r2,numeric,1);
00145        }
00146        case 3: {     /* atomic comparison */
00147            if(r<100) {      /* textual comparison */
00148               char buf1[MAX_LINELEN+1], buf2[MAX_LINELEN+1];
00149               while(r1>p1 && myisspace(r1[-1])) r1--;
00150               memmove(buf1,p1,r1-p1); buf1[r1-p1]=0;
00151               r2=find_word_start(r2);
00152               while(p2>=r2 && myisspace(*p2)) p2--;
00153               memmove(buf2,r2,p2+1-r2); buf2[p2+1-r2]=0;
00154               substitute(buf1); substitute(buf2);
00155               switch(r) {
00156                   case 0: { /* equal */
00157                      if(strcmp(buf1,buf2)==0) return 1^neg; else return neg;
00158                   }
00159                   case 1: { /* sametext */
00160                      deaccent(buf1); deaccent(buf2);
00161                      if(strcasecmp(bufprep(buf1),bufprep(buf2))==0)
00162                        return 1^neg;
00163                      else return neg;
00164                   }
00165                   case 2: { /* samecase */
00166                      if(strcmp(bufprep(buf1),bufprep(buf2))==0)
00167                        return 1^neg;
00168                      else return neg;
00169                   }
00170                   case 3: { /* in */
00171                      if(strstr(buf2,buf1)!=NULL) return 1^neg; else return neg;
00172                   }
00173                   case 4: { /* wordof */
00174                      if(wordchr(buf2,buf1)!=NULL) return 1^neg; else return neg;
00175                   }
00176                   case 5: { /* itemof */
00177                      if(itemchr(buf2,buf1)!=NULL) return 1^neg; else return neg;
00178                   }
00179                   case 6: { /* lineof */
00180                      if(linechr(buf2,buf1)!=NULL) return 1^neg; else return neg;
00181                   }
00182                   case 7:
00183                   case 8: { /* varof */
00184                      if(varchr(buf2,buf1)!=NULL) return 1^neg; else return neg;
00185                   }
00186               }
00187            }
00188            else {    /* numerical comparison */
00189               double d1, d2, sum, diff, prec;
00190               *r1=0;
00191               d1=evalue(p1); d2=evalue(r2);
00192               sum=d1+d2; if(sum<0) sum=-sum;
00193               diff=d1-d2; if(diff<0) diff=-diff;
00194               prec=evalue(getvar("wims_compare_precision"));  /* Move string name to header! */
00195               diff=diff*prec;
00196               if(prec>0 && prec<1E10) sum=sum+1/prec;
00197               switch(r) {
00198                   case 101: {      /* = */
00199                      if(sum>=diff) return 1^neg; else return neg;
00200                   }
00201                   case 102: { /* < */
00202                      if(d1<d2) return 1^neg; else return neg;
00203                   }
00204                   case 103: { /* > */
00205                      if(d1>d2) return 1^neg; else return neg;
00206                   }
00207                   default: break;  /* should never occur */
00208               }
00209            }
00210        }
00211     }
00212     internal_error("compare(): this should never happen.");
00213     return -1;
00214 }
00215