2 * testRegexp.c: simple module for testing regular expressions
4 * See Copyright for the status of this software.
6 * Daniel Veillard <veillard@redhat.com>
11 #ifdef LIBXML_REGEXP_ENABLED
12 #include <libxml/tree.h>
13 #include <libxml/xmlregexp.h>
18 static void testRegexp(xmlRegexpPtr comp, const char *value) {
21 ret = xmlRegexpExec(comp, (const xmlChar *) value);
23 printf("%s: Ok\n", value);
25 printf("%s: Fail\n", value);
27 printf("%s: Error: %d\n", value, ret);
30 for (j = 0;j < 999999;j++)
31 xmlRegexpExec(comp, (const xmlChar *) value);
36 testRegexpFile(const char *filename) {
37 xmlRegexpPtr comp = NULL;
39 char expression[5000];
42 input = fopen(filename, "r");
44 xmlGenericError(xmlGenericErrorContext,
45 "Cannot open %s for reading\n", filename);
48 while (fgets(expression, 4500, input) != NULL) {
49 len = strlen(expression);
52 ((expression[len] == '\n') || (expression[len] == '\t') ||
53 (expression[len] == '\r') || (expression[len] == ' '))) len--;
54 expression[len + 1] = 0;
56 if (expression[0] == '#')
58 if ((expression[0] == '=') && (expression[1] == '>')) {
59 char *pattern = &expression[2];
62 xmlRegFreeRegexp(comp);
65 printf("Regexp: %s\n", pattern) ;
66 comp = xmlRegexpCompile((const xmlChar *) pattern);
68 printf(" failed to compile\n");
71 } else if (comp == NULL) {
72 printf("Regexp: %s\n", expression) ;
73 comp = xmlRegexpCompile((const xmlChar *) expression);
75 printf(" failed to compile\n");
78 } else if (comp != NULL) {
79 testRegexp(comp, expression);
85 xmlRegFreeRegexp(comp);
89 static void usage(const char *name) {
90 fprintf(stderr, "Usage: %s\n", name);
93 int main(int argc, char **argv) {
94 xmlRegexpPtr comp = NULL;
95 const char *pattern = NULL;
96 char *filename = NULL;
105 for (i = 1; i < argc ; i++) {
106 if (!strcmp(argv[i], "-"))
109 if (argv[i][0] != '-')
111 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) {
113 } else if ((!strcmp(argv[i], "-repeat")) ||
114 (!strcmp(argv[i], "--repeat"))) {
116 } else if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input")))
117 filename = argv[++i];
119 fprintf(stderr, "Unknown option %s\n", argv[i]);
123 if (filename != NULL) {
124 testRegexpFile(filename);
126 for (i = 1; i < argc ; i++) {
127 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
128 if (pattern == NULL) {
130 printf("Testing %s:\n", pattern);
131 comp = xmlRegexpCompile((const xmlChar *) pattern);
133 printf(" failed to compile\n");
137 xmlRegexpPrint(stdout, comp);
139 testRegexp(comp, argv[i]);
145 xmlRegFreeRegexp(comp);
148 /* xmlMemoryDump(); */
154 int main(int argc, char **argv) {
155 printf("%s : Regexp support not compiled in\n", argv[0]);
158 #endif /* LIBXML_REGEXP_ENABLED */