Subversion Repositories wimsdev

Rev

Rev 8160 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 8160 Rev 12260
Line 19... Line 19...
19
#include "libwims.h"
19
#include "libwims.h"
20
 
20
 
21
/* Points to the start of a mathematics variable (or number) */
21
/* Points to the start of a mathematics variable (or number) */
22
char *find_mathvar_start(char *p)
22
char *find_mathvar_start(char *p)
23
{
23
{
24
    int i;
24
  int i;
25
    for(i=0;!isalnum(*p) && *p!='.' && *p!=0 && i<MAX_LINELEN; p++,i++);
25
  for(i=0;!isalnum(*p) && *p!='.' && *p!=0 && i<MAX_LINELEN; p++,i++);
26
    return p;
26
  return p;
27
}
27
}
28
 
28
 
29
/* Points to the end of a mathematics variable (or number) */
29
/* Points to the end of a mathematics variable (or number) */
30
char *find_mathvar_end(char *p)
30
char *find_mathvar_end(char *p)
31
{
31
{
32
    int i; char *pp;
32
  int i; char *pp;
33
    if(!isalnum(*p) && *p!='.') return p;
33
  if(!isalnum(*p) && *p!='.') return p;
34
    if(isalpha(*p)) {
34
  if(isalpha(*p)) {
35
      for(i=0; *p!=0 && (isalnum(*p) || *p=='.' || *p=='\'')
35
    for(i=0; *p!=0 && (isalnum(*p) || *p=='.' || *p=='\'')
36
          && i<MAX_LINELEN;p++,i++);
36
        && i<MAX_LINELEN;p++,i++);
37
      return p;
37
    return p;
38
    }
38
  }
39
    else {
39
  else {
40
      int t=0;
40
    int t=0;
41
      expon:
41
    expon:
42
      for(i=0; (isdigit(*p) || *p=='.') && i<MAX_LINELEN;p++,i++);
42
    for(i=0; (isdigit(*p) || *p=='.') && i<MAX_LINELEN;p++,i++);
43
      pp=p; if(t) return pp;
43
    pp=p; if(t) return pp;
44
      while(isspace(*p)) p++;
44
    while(isspace(*p)) p++;
45
      if(*p=='e' || *p=='E'){
45
    if(*p=='e' || *p=='E'){
46
          t=1; p++; while(isspace(*p)) p++;
46
      t=1; p++; while(isspace(*p)) p++;
47
          if(isdigit(*p)) goto expon;
47
      if(isdigit(*p)) goto expon;
48
          if((*p=='-' || *p=='+') && isdigit(*(p+1))) {
48
      if((*p=='-' || *p=='+') && isdigit(*(p+1))) {
49
            p++; goto expon;
49
        p++; goto expon;
50
          }
-
 
51
      }
50
      }
52
      return pp;
-
 
53
    }
51
    }
-
 
52
    return pp;
-
 
53
  }
54
}
54
}
55
 
55
 
56
/* list variable (function) names in an expression.
56
/* list variable (function) names in an expression.
57
 * buffer is modified to contain the list.
57
 * buffer is modified to contain the list.
58
 */
58
 */
59
void mathvarlist(char *p)
59
void mathvarlist(char *p)
60
{
60
{
61
    char buf[MAX_LINELEN+1], *pb, *pe, *pp;
61
  char buf[MAX_LINELEN+1], *pb, *pe, *pp;
62
    char *l[10240];
62
  char *l[10240];
63
    int i,j,len, nofn=0;
63
  int i,j,len, nofn=0;
64
 
64
 
65
    pb=find_word_start(p);pe=find_word_end(pb);
65
  pb=find_word_start(p);pe=find_word_end(pb);
66
    if(pe-pb==strlen("nofn") && memcmp(pb,"nofn",strlen("nofn"))==0) {
66
  if(pe-pb==strlen("nofn") && memcmp(pb,"nofn",strlen("nofn"))==0) {
67
      nofn=1; pb=find_word_start(pe);
67
    nofn=1; pb=find_word_start(pe);
-
 
68
  }
-
 
69
  snprintf(buf,sizeof(buf),"%s",pb);
-
 
70
  len=strlen(buf);
-
 
71
  for(i=0,pb=buf;pb<buf+len;pb++) {
-
 
72
    if(!isalpha(*pb)) continue;
-
 
73
    if(pb>buf && isalnum(*(pb-1))) {
-
 
74
      pb=find_mathvar_end(pb); continue;
68
    }
75
    }
69
    snprintf(buf,sizeof(buf),"%s",pb);
-
 
70
    len=strlen(buf);
-
 
71
    for(i=0,pb=buf;pb<buf+len;pb++) {
-
 
72
      if(!isalpha(*pb)) continue;
-
 
73
      if(pb>buf && isalnum(*(pb-1))) {
-
 
74
          pb=find_mathvar_end(pb); continue;
-
 
75
      }
-
 
76
      pe=find_mathvar_end(pb); pp=find_word_start(pe);
76
    pe=find_mathvar_end(pb); pp=find_word_start(pe);
77
      if(nofn && *pp=='(') {
77
    if(nofn && *pp=='(') {
78
          pb=pe; continue;
78
      pb=pe; continue;
79
      }
-
 
80
      *pe=0;
-
 
81
      if(i<10240) {
-
 
82
          for(j=0;j<i;j++) if(strcmp(pb,l[j])==0) goto nextp;
-
 
83
          l[i++]=pb;
-
 
84
      }
-
 
85
      nextp:
-
 
86
      pb=pe;
-
 
87
    }
79
    }
-
 
80
    *pe=0;
88
    for(*p=0,j=0;j<i;j++) {
81
    if(i<10240) {
89
      strcat(p,l[j]);
82
      for(j=0;j<i;j++) if(strcmp(pb,l[j])==0) goto nextp;
90
      if(j<i-1) strcat(p,",");
83
      l[i++]=pb;
91
    }
84
    }
-
 
85
    nextp:
-
 
86
    pb=pe;
-
 
87
  }
-
 
88
  for(*p=0,j=0;j<i;j++) {
-
 
89
    strcat(p,l[j]);
-
 
90
    if(j<i-1) strcat(p,",");
-
 
91
  }
92
}
92
}