Subversion Repositories wimsdev

Rev

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.  
  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;
  85.               p1=find_word_start(p3);
  86.           }
  87.           if(l>=expl1 && l<=expl2) {
  88.               got=1;
  89.               if(l>0 && commacnt<MAX_COMMAS-2) {
  90.                commas[commacnt++]=p2-p1;
  91.                commas[commacnt++]=p3-p1;
  92.               }
  93.           }
  94.           continue;
  95.          }
  96.          notdefined: p4=find_word_start(p3);
  97.          if(*p4=='(') {
  98.           p3=find_matching(p4+1,')');
  99.           if(p3==NULL) error("Unmatched parentheses.");
  100.           p4++; memmove(buf,p4,p3-p2); buf[p3-p4]=0;
  101.           _extract(buf,p2-p+dist);
  102.           p3++;
  103.           if(expl2==exp_fn) extrout(p2,p3,dist+p2-p,commas,&commacnt);
  104.          }
  105.          else if(expl2==exp_variable) extrout(p2,p3,dist+p2-p,commas,&commacnt);
  106.          continue;
  107.      }
  108.      for(i=0;i<oppunctno && strncmp(p2,oppunct[i].name,strlen(oppunct[i].name))!=0; i++);
  109.      if(i>=oppunctno) error("Unknown operator.");
  110.      p3=p2+strlen(oppunct[i].name); l=oppunct[i].lvl;
  111.      if(l>expl2) {
  112.          if(got) extrout(p1,p2,dist+p1-p,commas,&commacnt);
  113.          if(expl1>0) got=0;
  114.          p1=find_word_start(p3);
  115.      }
  116.      if(l>=expl1 && l<=expl2) {
  117.          got=1;
  118.          if(l>0 && commacnt<MAX_COMMAS-2) {
  119.           commas[commacnt++]=p2-p1;
  120.           commas[commacnt++]=p3-p1;
  121.          }
  122.      }
  123.   }
  124.   if(got) extrout(p1,p1+strlen(p1),dist+p1-p,commas,&commacnt);
  125. }
  126.  
  127. void req_extract(void)
  128. {
  129.     int i;
  130.     char *p;
  131.  
  132.     if(objlinecnt<2) return;
  133.     if(*reqtype==0) error("Missing extraction type.");
  134.     for(i=0;i<exptypeno && strcmp(reqtype,exptype[i].name)!=0; i++);
  135.     if(i>=exptypeno) error("Bad extraction type.");
  136.     if(logdir!=0) nopart=1;
  137.     while((p=wordchr(objline[0],"nopart"))!=NULL) {
  138.      nopart=1; ovlstrcpy(p,p+strlen("nopart"));
  139.     }
  140.     while((p=wordchr(objline[0],"nonesting"))!=NULL) {
  141.      nonesting=1; ovlstrcpy(p,p+strlen("nonesting"));
  142.     }
  143.     while((p=wordchr(objline[0],"nocomma"))!=NULL) {
  144.      nocomma=1; ovlstrcpy(p,p+strlen("nocomma"));
  145.     }
  146.     getregex(objline[0]);
  147.     expl1=exptype[i].lvl1; expl2=exptype[i].lvl2;
  148.     for(i=1;i<objlinecnt;i++) {
  149.      thisobjline=i; p=find_word_start(objline[i]);
  150.      linelogdir=0;
  151.      if(*p=='>') {
  152.          if(logdir<0) continue;
  153.          p=find_word_start(p+1); linelogdir=1;
  154.      }
  155.      else if(*p=='<') {
  156.          if(logdir>0) continue;
  157.          p=find_word_start(p+1); linelogdir=-1;
  158.      }
  159.      thislinelen=strlen(p); if(thislinelen<=0) continue;
  160.      _estart=p-objline[i]; _extract(p, 0);
  161.     }
  162. }
  163.  
  164. void req_cut(void)
  165. {
  166.     nocoord=1; req_extract();
  167. }
  168.  
  169.