|
| 1 | +#include<stdio.h> |
| 2 | +#include<stdlib.h> |
| 3 | +#include<string.h> |
| 4 | +#include<ctype.h> |
| 5 | + |
| 6 | +#include"regis.h" |
| 7 | +#include"common.h" |
| 8 | + |
| 9 | +int |
| 10 | +RS_isRegis(constchar*str) { |
| 11 | +unsignedchar*ptr=(unsignedchar*)str; |
| 12 | + |
| 13 | +while(ptr&&*ptr) |
| 14 | +if (isalpha(*ptr)||*ptr=='['||*ptr==']'||*ptr=='^') |
| 15 | +ptr++; |
| 16 | +else |
| 17 | +return0; |
| 18 | +return1; |
| 19 | +} |
| 20 | + |
| 21 | +#defineRS_IN_ONEOF1 |
| 22 | +#defineRS_IN_ONEOF_IN2 |
| 23 | +#defineRS_IN_NONEOF3 |
| 24 | +#defineRS_IN_WAIT4 |
| 25 | + |
| 26 | +staticRegisNode* |
| 27 | +newRegisNode(RegisNode*prev,intlen) { |
| 28 | +RegisNode*ptr; |
| 29 | +ptr= (RegisNode*)malloc(RNHDRSZ+len+1); |
| 30 | +if (!ptr) |
| 31 | +ts_error(ERROR,"No memory"); |
| 32 | +memset(ptr,0,RNHDRSZ+len+1); |
| 33 | +if (prev) |
| 34 | +prev->next=ptr; |
| 35 | +returnptr; |
| 36 | +} |
| 37 | + |
| 38 | +int |
| 39 | +RS_compile(Regis*r,intissuffix,constchar*str) { |
| 40 | +inti,len=strlen(str); |
| 41 | +intstate=RS_IN_WAIT; |
| 42 | +RegisNode*ptr=NULL; |
| 43 | + |
| 44 | +memset(r,0,sizeof(Regis)); |
| 45 | +r->issuffix= (issuffix) ?1 :0; |
| 46 | + |
| 47 | +for(i=0;i<len;i++) { |
| 48 | +unsignedcharc=*( ( (unsignedchar*)str )+i ); |
| 49 | +if (state==RS_IN_WAIT ) { |
| 50 | +if (isalpha(c) ) { |
| 51 | +if (ptr ) |
| 52 | +ptr=newRegisNode(ptr,len); |
| 53 | +else |
| 54 | +ptr=r->node=newRegisNode(NULL,len); |
| 55 | +ptr->data[0 ]=c; |
| 56 | +ptr->type=RSF_ONEOF; |
| 57 | +ptr->len=1; |
| 58 | +}elseif (c=='[' ) { |
| 59 | +if (ptr ) |
| 60 | +ptr=newRegisNode(ptr,len); |
| 61 | +else |
| 62 | +ptr=r->node=newRegisNode(NULL,len); |
| 63 | +ptr->type=RSF_ONEOF; |
| 64 | +state=RS_IN_ONEOF; |
| 65 | +}else |
| 66 | +ts_error(ERROR,"Error in regis: %s at pos %d\n",str,i+1); |
| 67 | +}elseif (state==RS_IN_ONEOF ) { |
| 68 | +if (c=='^' ) { |
| 69 | +ptr->type=RSF_NONEOF; |
| 70 | +state=RS_IN_NONEOF; |
| 71 | +}elseif (isalpha(c) ) { |
| 72 | +ptr->data[0 ]=c; |
| 73 | +ptr->len=1; |
| 74 | +state=RS_IN_ONEOF_IN; |
| 75 | +}else |
| 76 | +ts_error(ERROR,"Error in regis: %s at pos %d\n",str,i+1); |
| 77 | +}elseif (state==RS_IN_ONEOF_IN||state==RS_IN_NONEOF ) { |
| 78 | +if (isalpha(c) ) { |
| 79 | +ptr->data[ptr->len ]=c; |
| 80 | +ptr->len++; |
| 81 | +}elseif (c==']' ) { |
| 82 | +state=RS_IN_WAIT; |
| 83 | +}else |
| 84 | +ts_error(ERROR,"Error in regis: %s at pos %d\n",str,i+1); |
| 85 | +}else |
| 86 | +ts_error(ERROR,"Internal error in RS_compile: %d\n",state); |
| 87 | +} |
| 88 | + |
| 89 | +ptr=r->node; |
| 90 | +while(ptr) { |
| 91 | +r->nchar++; |
| 92 | +ptr=ptr->next; |
| 93 | +} |
| 94 | + |
| 95 | +return0; |
| 96 | +} |
| 97 | + |
| 98 | +void |
| 99 | +RS_free(Regis*r) { |
| 100 | +RegisNode*ptr=r->node,*tmp; |
| 101 | + |
| 102 | +while(ptr) { |
| 103 | +tmp=ptr->next; |
| 104 | +free(ptr); |
| 105 | +ptr=tmp; |
| 106 | +} |
| 107 | + |
| 108 | +r->node=NULL; |
| 109 | +} |
| 110 | + |
| 111 | +int |
| 112 | +RS_execute(Regis*r,constchar*str,intlen) { |
| 113 | +RegisNode*ptr=r->node; |
| 114 | +unsignedchar*c; |
| 115 | + |
| 116 | +if (len<0) |
| 117 | +len=strlen(str); |
| 118 | + |
| 119 | +if (len<r->nchar) |
| 120 | +return0; |
| 121 | + |
| 122 | +if (r->issuffix ) |
| 123 | +c= ((unsignedchar*)str)+len-r->nchar; |
| 124 | +else |
| 125 | +c= (unsignedchar*)str; |
| 126 | + |
| 127 | +while(ptr) { |
| 128 | +switch(ptr->type) { |
| 129 | +caseRSF_ONEOF: |
| 130 | +if (ptr->len==0 ) { |
| 131 | +if (*c!=*(ptr->data) ) |
| 132 | +return0; |
| 133 | +}elseif (strchr((char*)ptr->data,*c)==NULL ) |
| 134 | +return0; |
| 135 | +break; |
| 136 | +caseRSF_NONEOF: |
| 137 | +if (ptr->len==0 ) { |
| 138 | +if (*c==*(ptr->data) ) |
| 139 | +return0; |
| 140 | +}elseif (strchr((char*)ptr->data,*c)!=NULL ) |
| 141 | +return0; |
| 142 | +break; |
| 143 | +default: |
| 144 | +ts_error(ERROR,"RS_execute: Unknown type node: %d\n",ptr->type); |
| 145 | +} |
| 146 | +ptr=ptr->next; |
| 147 | +c++; |
| 148 | +} |
| 149 | + |
| 150 | +return1; |
| 151 | +} |