Subversion Repositories wimsdev

Rev

Rev 11104 | Blame | Compare with Previous | Last modification | View Log | RSS feed

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