Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > DoxygentestAutomata.c
Go to the documentation of this file.
00001 /* 00002 * testRegexp.c: simple module for testing regular expressions 00003 * 00004 * See Copyright for the status of this software. 00005 * 00006 * Daniel Veillard <veillard@redhat.com> 00007 */ 00008 00009 #include "libxml.h" 00010 #ifdef LIBXML_AUTOMATA_ENABLED 00011 #include <string.h> 00012 00013 #include <libxml/tree.h> 00014 #include <libxml/xmlautomata.h> 00015 00016 static int scanNumber(char **ptr) { 00017 int ret = 0; 00018 char *cur; 00019 00020 cur = *ptr; 00021 while ((*cur >= '0') && (*cur <= '9')) { 00022 ret = ret * 10 + (*cur - '0'); 00023 cur++; 00024 } 00025 *ptr = cur; 00026 return(ret); 00027 } 00028 00029 static void 00030 testRegexpFile(const char *filename) { 00031 FILE *input; 00032 char expr[5000]; 00033 int len; 00034 int ret; 00035 int i; 00036 xmlAutomataPtr am; 00037 xmlAutomataStatePtr states[1000]; 00038 xmlRegexpPtr regexp = NULL; 00039 xmlRegExecCtxtPtr exec = NULL; 00040 00041 for (i = 0;i<1000;i++) 00042 states[i] = NULL; 00043 00044 input = fopen(filename, "r"); 00045 if (input == NULL) { 00046 xmlGenericError(xmlGenericErrorContext, 00047 "Cannot open %s for reading\n", filename); 00048 return; 00049 } 00050 00051 am = xmlNewAutomata(); 00052 if (am == NULL) { 00053 xmlGenericError(xmlGenericErrorContext, 00054 "Cannot create automata\n"); 00055 fclose(input); 00056 return; 00057 } 00058 states[0] = xmlAutomataGetInitState(am); 00059 if (states[0] == NULL) { 00060 xmlGenericError(xmlGenericErrorContext, 00061 "Cannot get start state\n"); 00062 xmlFreeAutomata(am); 00063 fclose(input); 00064 return; 00065 } 00066 ret = 0; 00067 00068 while (fgets(expr, 4500, input) != NULL) { 00069 if (expr[0] == '#') 00070 continue; 00071 len = strlen(expr); 00072 len--; 00073 while ((len >= 0) && 00074 ((expr[len] == '\n') || (expr[len] == '\t') || 00075 (expr[len] == '\r') || (expr[len] == ' '))) len--; 00076 expr[len + 1] = 0; 00077 if (len >= 0) { 00078 if ((am != NULL) && (expr[0] == 't') && (expr[1] == ' ')) { 00079 char *ptr = &expr[2]; 00080 int from, to; 00081 00082 from = scanNumber(&ptr); 00083 if (*ptr != ' ') { 00084 xmlGenericError(xmlGenericErrorContext, 00085 "Bad line %s\n", expr); 00086 break; 00087 } 00088 if (states[from] == NULL) 00089 states[from] = xmlAutomataNewState(am); 00090 ptr++; 00091 to = scanNumber(&ptr); 00092 if (*ptr != ' ') { 00093 xmlGenericError(xmlGenericErrorContext, 00094 "Bad line %s\n", expr); 00095 break; 00096 } 00097 if (states[to] == NULL) 00098 states[to] = xmlAutomataNewState(am); 00099 ptr++; 00100 xmlAutomataNewTransition(am, states[from], states[to], 00101 BAD_CAST ptr, NULL); 00102 } else if ((am != NULL) && (expr[0] == 'e') && (expr[1] == ' ')) { 00103 char *ptr = &expr[2]; 00104 int from, to; 00105 00106 from = scanNumber(&ptr); 00107 if (*ptr != ' ') { 00108 xmlGenericError(xmlGenericErrorContext, 00109 "Bad line %s\n", expr); 00110 break; 00111 } 00112 if (states[from] == NULL) 00113 states[from] = xmlAutomataNewState(am); 00114 ptr++; 00115 to = scanNumber(&ptr); 00116 if (states[to] == NULL) 00117 states[to] = xmlAutomataNewState(am); 00118 xmlAutomataNewEpsilon(am, states[from], states[to]); 00119 } else if ((am != NULL) && (expr[0] == 'f') && (expr[1] == ' ')) { 00120 char *ptr = &expr[2]; 00121 int state; 00122 00123 state = scanNumber(&ptr); 00124 if (states[state] == NULL) { 00125 xmlGenericError(xmlGenericErrorContext, 00126 "Bad state %d : %s\n", state, expr); 00127 break; 00128 } 00129 xmlAutomataSetFinalState(am, states[state]); 00130 } else if ((am != NULL) && (expr[0] == 'c') && (expr[1] == ' ')) { 00131 char *ptr = &expr[2]; 00132 int from, to; 00133 int min, max; 00134 00135 from = scanNumber(&ptr); 00136 if (*ptr != ' ') { 00137 xmlGenericError(xmlGenericErrorContext, 00138 "Bad line %s\n", expr); 00139 break; 00140 } 00141 if (states[from] == NULL) 00142 states[from] = xmlAutomataNewState(am); 00143 ptr++; 00144 to = scanNumber(&ptr); 00145 if (*ptr != ' ') { 00146 xmlGenericError(xmlGenericErrorContext, 00147 "Bad line %s\n", expr); 00148 break; 00149 } 00150 if (states[to] == NULL) 00151 states[to] = xmlAutomataNewState(am); 00152 ptr++; 00153 min = scanNumber(&ptr); 00154 if (*ptr != ' ') { 00155 xmlGenericError(xmlGenericErrorContext, 00156 "Bad line %s\n", expr); 00157 break; 00158 } 00159 ptr++; 00160 max = scanNumber(&ptr); 00161 if (*ptr != ' ') { 00162 xmlGenericError(xmlGenericErrorContext, 00163 "Bad line %s\n", expr); 00164 break; 00165 } 00166 ptr++; 00167 xmlAutomataNewCountTrans(am, states[from], states[to], 00168 BAD_CAST ptr, min, max, NULL); 00169 } else if ((am != NULL) && (expr[0] == '-') && (expr[1] == '-')) { 00170 /* end of the automata */ 00171 regexp = xmlAutomataCompile(am); 00172 xmlFreeAutomata(am); 00173 am = NULL; 00174 if (regexp == NULL) { 00175 xmlGenericError(xmlGenericErrorContext, 00176 "Failed to compile the automata"); 00177 break; 00178 } 00179 } else if ((expr[0] == '=') && (expr[1] == '>')) { 00180 if (regexp == NULL) { 00181 printf("=> failed not compiled\n"); 00182 } else { 00183 if (exec == NULL) 00184 exec = xmlRegNewExecCtxt(regexp, NULL, NULL); 00185 if (ret == 0) { 00186 ret = xmlRegExecPushString(exec, NULL, NULL); 00187 } 00188 if (ret == 1) 00189 printf("=> Passed\n"); 00190 else if ((ret == 0) || (ret == -1)) 00191 printf("=> Failed\n"); 00192 else if (ret < 0) 00193 printf("=> Error\n"); 00194 xmlRegFreeExecCtxt(exec); 00195 exec = NULL; 00196 } 00197 ret = 0; 00198 } else if (regexp != NULL) { 00199 if (exec == NULL) 00200 exec = xmlRegNewExecCtxt(regexp, NULL, NULL); 00201 ret = xmlRegExecPushString(exec, BAD_CAST expr, NULL); 00202 } else { 00203 xmlGenericError(xmlGenericErrorContext, 00204 "Unexpected line %s\n", expr); 00205 } 00206 } 00207 } 00208 fclose(input); 00209 if (regexp != NULL) 00210 xmlRegFreeRegexp(regexp); 00211 if (exec != NULL) 00212 xmlRegFreeExecCtxt(exec); 00213 if (am != NULL) 00214 xmlFreeAutomata(am); 00215 } 00216 00217 int main(int argc, char **argv) { 00218 00219 xmlInitMemory(); 00220 00221 if (argc == 1) { 00222 int ret; 00223 xmlAutomataPtr am; 00224 xmlAutomataStatePtr start, cur; 00225 xmlRegexpPtr regexp; 00226 xmlRegExecCtxtPtr exec; 00227 00228 am = xmlNewAutomata(); 00229 start = xmlAutomataGetInitState(am); 00230 00231 /* generate a[ba]*a */ 00232 cur = xmlAutomataNewTransition(am, start, NULL, BAD_CAST"a", NULL); 00233 xmlAutomataNewTransition(am, cur, cur, BAD_CAST"b", NULL); 00234 xmlAutomataNewTransition(am, cur, cur, BAD_CAST"a", NULL); 00235 cur = xmlAutomataNewCountTrans(am, cur, NULL, BAD_CAST"a", 2, 3, NULL); 00236 xmlAutomataSetFinalState(am, cur); 00237 00238 /* compile it in a regexp and free the automata */ 00239 regexp = xmlAutomataCompile(am); 00240 xmlFreeAutomata(am); 00241 00242 /* test the regexp */ 00243 xmlRegexpPrint(stdout, regexp); 00244 exec = xmlRegNewExecCtxt(regexp, NULL, NULL); 00245 ret = xmlRegExecPushString(exec, BAD_CAST"a", NULL); 00246 if (ret == 1) 00247 printf("final\n"); 00248 else if (ret < 0) 00249 printf("error\n"); 00250 ret =xmlRegExecPushString(exec, BAD_CAST"a", NULL); 00251 if (ret == 1) 00252 printf("final\n"); 00253 else if (ret < 0) 00254 printf("error\n"); 00255 ret =xmlRegExecPushString(exec, BAD_CAST"b", NULL); 00256 if (ret == 1) 00257 printf("final\n"); 00258 else if (ret < 0) 00259 printf("error\n"); 00260 ret =xmlRegExecPushString(exec, BAD_CAST"a", NULL); 00261 if (ret == 1) 00262 printf("final\n"); 00263 else if (ret < 0) 00264 printf("error\n"); 00265 ret =xmlRegExecPushString(exec, BAD_CAST"a", NULL); 00266 if (ret == 1) 00267 printf("final\n"); 00268 else if (ret < 0) 00269 printf("error\n"); 00270 ret =xmlRegExecPushString(exec, BAD_CAST"a", NULL); 00271 if (ret == 1) 00272 printf("final\n"); 00273 else if (ret < 0) 00274 printf("error\n"); 00275 ret =xmlRegExecPushString(exec, BAD_CAST"a", NULL); 00276 if (ret == 1) 00277 printf("final\n"); 00278 else if (ret < 0) 00279 printf("error\n"); 00280 if (ret == 0) { 00281 ret = xmlRegExecPushString(exec, NULL, NULL); 00282 if (ret == 1) 00283 printf("final\n"); 00284 else if (ret < 0) 00285 printf("error\n"); 00286 } 00287 xmlRegFreeExecCtxt(exec); 00288 00289 /* free the regexp */ 00290 xmlRegFreeRegexp(regexp); 00291 } else { 00292 int i; 00293 00294 for (i = 1;i < argc;i++) 00295 testRegexpFile(argv[i]); 00296 } 00297 00298 xmlCleanupParser(); 00299 xmlMemoryDump(); 00300 return(0); 00301 } 00302 00303 #else 00304 #include <stdio.h> 00305 int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) { 00306 printf("%s : Automata support not compiled in\n", argv[0]); 00307 return(0); 00308 } 00309 #endif /* LIBXML_AUTOMATA_ENABLED */ Generated on Sun May 27 2012 04:34:42 for ReactOS by
1.7.6.1
|