Subversion Repositories wimsdev

Rev

Rev 3840 | Rev 8185 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  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.  */
  17. #include "../../Lib/libwims.h"
  18. #include "mathexp.h"
  19.  
  20. int nopart=0, nonesting=0, nocomma=0, nocoord=0, _estart;
  21.  
  22. void extrout(char *pb, char *pe, int dist, int commas[], int *commacnt)
  23. {
  24.     int i;
  25.     if(pe-pb<thislinelen && nopart) return;
  26.     while(pe>pb && isspace(*(pe-1))) pe--;
  27.     if(pe<=pb) return;
  28.     if(regexcnt>0 && pe-pb<MAX_LINELEN) {
  29.      char buf[MAX_LINELEN+1];
  30.      memmove(buf,pb,pe-pb); buf[pe-pb]=0;
  31.      if(!checkregex(buf)) return;
  32.     }
  33.     if(!nocoord)
  34.       printf("%d %d %d, ",thisobjline,dist+_estart,(int)(pe-pb)+dist+_estart);
  35.     while(*commacnt>0 && commas[*commacnt-1]>pe-pb) (*commacnt)--;
  36.     if(*commacnt>0 && !nocomma) {
  37.      fwrite(pb,1,commas[0],stdout); putchar(',');
  38.      for(i=1;i<*commacnt;i++) {
  39.          fwrite(pb+commas[i-1],1,commas[i]-commas[i-1],stdout); putchar(',');
  40.      }
  41.      pb+=commas[*commacnt-1];
  42.     }
  43.     fwrite(pb,1,pe-pb,stdout); printf("\n");
  44.     *commacnt=0;
  45. }
  46.  
  47. void _extract(char *p, int dist)
  48. {
  49.     int i,l,got,lt;
  50.     char *p1, *p2, *p3, *p4;
  51.     char buf[MAX_LINELEN+1];
  52.     int commas[MAX_COMMAS];
  53.     int commacnt;
  54.  
  55.     if(dist>0 && (nonesting || nopart)) return;
  56.     commacnt=0;
  57.     if(expl1<=0) got=1; else got=0;
  58.     for(p1=find_word_start(p), p2=p1; *p2; p2=find_word_start(p3)) {
  59.      if(*p2=='.' || isdigit(*p2)) {
  60.          (void)strtod(p2,&p3); continue;
  61.      }
  62.      if(*p2=='(') {
  63.          p3=find_matching(p2+1,')'); lt=exp_paren;
  64.          paren: if(p3==NULL) error("Unmatched parentheses.");
  65.          p2++; memmove(buf,p2,p3-p2); buf[p3-p2]=0;
  66.          _extract(buf,p2-p+dist);
  67.          if(expl2==lt) extrout(p2,p3,dist+p2-p,commas,&commacnt);
  68.          p3++; continue;
  69.      }
  70.      if(*p2=='[') {
  71.          p3=find_matching(p2+1,']'); lt=exp_matrix; goto paren;
  72.      }
  73.      if(*p2=='{') {
  74.          p3=find_matching(p2+1,'}'); lt=exp_set; goto paren;
  75.      }
  76.      if(isalpha(*p2)) {
  77.          for(p3=p2; *p3=='_' || isalnum(*p3); p3++);
  78.          if(p3-p2>=16) goto notdefined;
  79.          memmove(buf,p2,p3-p2); buf[p3-p2]=0;
  80.          for(i=0;i<opalphano && strcmp(buf,opalpha[i].name)!=0; i++);
  81.          if(i<opalphano) {
  82.           l=opalpha[i].lvl; if(l>expl2) {
  83.               if(got) extrout(p1,p2,dist+p1-p,commas,&commacnt);
  84.               if(expl1>0) got=0; p1=find_word_start(p3);
  85.           }
  86.           if(l>=expl1 && l<=expl2) {
  87.               got=1;
  88.               if(l>0 && commacnt<MAX_COMMAS-2) {
  89.                commas[commacnt++]=p2-p1;
  90.                commas[commacnt++]=p3-p1;
  91.               }
  92.           }
  93.           continue;
  94.          }
  95.          notdefined: p4=find_word_start(p3);
  96.          if(*p4=='(') {
  97.           p3=find_matching(p4+1,')');
  98.           if(p3==NULL) error("Unmatched parentheses.");
  99.           p4++; memmove(buf,p4,p3-p2); buf[p3-p4]=0;
  100.           _extract(buf,p2-p+dist);
  101.           p3++;
  102.           if(expl2==exp_fn) extrout(p2,p3,dist+p2-p,commas,&commacnt);
  103.          }
  104.          else if(expl2==exp_variable) extrout(p2,p3,dist+p2-p,commas,&commacnt);
  105.          continue;
  106.      }
  107.      for(i=0;i<oppunctno && strncmp(p2,oppunct[i].name,strlen(oppunct[i].name))!=0; i++);
  108.      if(i>=oppunctno) error("Unknown operator.");
  109.      p3=p2+strlen(oppunct[i].name); l=oppunct[i].lvl;
  110.      if(l>expl2) {
  111.          if(got) extrout(p1,p2,dist+p1-p,commas,&commacnt);
  112.          if(expl1>0) got=0; p1=find_word_start(p3);
  113.      }
  114.      if(l>=expl1 && l<=expl2) {
  115.          got=1;
  116.          if(l>0 && commacnt<MAX_COMMAS-2) {
  117.           commas[commacnt++]=p2-p1;
  118.           commas[commacnt++]=p3-p1;
  119.          }
  120.      }
  121.   }
  122.   if(got) extrout(p1,p1+strlen(p1),dist+p1-p,commas,&commacnt);
  123. }
  124.  
  125. void req_extract(void)
  126. {
  127.     int i;
  128.     char *p;
  129.  
  130.     if(objlinecnt<2) return;
  131.     if(*reqtype==0) error("Missing extraction type.");
  132.     for(i=0;i<exptypeno && strcmp(reqtype,exptype[i].name)!=0; i++);
  133.     if(i>=exptypeno) error("Bad extraction type.");
  134.     if(logdir!=0) nopart=1;
  135.     while((p=wordchr(objline[0],"nopart"))!=NULL) {
  136.      nopart=1; ovlstrcpy(p,p+strlen("nopart"));
  137.     }
  138.     while((p=wordchr(objline[0],"nonesting"))!=NULL) {
  139.      nonesting=1; ovlstrcpy(p,p+strlen("nonesting"));
  140.     }
  141.     while((p=wordchr(objline[0],"nocomma"))!=NULL) {
  142.      nocomma=1; ovlstrcpy(p,p+strlen("nocomma"));
  143.     }
  144.     getregex(objline[0]);
  145.     expl1=exptype[i].lvl1; expl2=exptype[i].lvl2;
  146.     for(i=1;i<objlinecnt;i++) {
  147.      thisobjline=i; p=find_word_start(objline[i]);
  148.      linelogdir=0;
  149.      if(*p=='>') {
  150.          if(logdir<0) continue;
  151.          p=find_word_start(p+1); linelogdir=1;
  152.      }
  153.      else if(*p=='<') {
  154.          if(logdir>0) continue;
  155.          p=find_word_start(p+1); linelogdir=-1;
  156.      }
  157.      thislinelen=strlen(p); if(thislinelen<=0) continue;
  158.      _estart=p-objline[i]; _extract(p, 0);
  159.     }
  160. }
  161.  
  162. void req_cut(void)
  163. {
  164.     nocoord=1; req_extract();
  165. }
  166.  
  167.