blob: 7f344148d90fe660ba64231d9880293c8fec5bcd [file] [log] [blame]
Daniel Veillard4255d502002-04-16 15:50:10 +00001/*
2 * testRegexp.c: simple module for testing regular expressions
3 *
4 * See Copyright for the status of this software.
5 *
6 * Daniel Veillard <veillard@redhat.com>
7 */
8
9#include <string.h>
10#include "libxml.h"
11#ifdef LIBXML_REGEXP_ENABLED
12#include <libxml/tree.h>
13#include <libxml/xmlregexp.h>
14
15int repeat = 0;
16int debug = 0;
17
18static void testRegexp(xmlRegexpPtr comp, const char *value) {
19 int ret;
20
21 ret = xmlRegexpExec(comp, (const xmlChar *) value);
22 if (ret == 1)
23 printf("%s: Ok\n", value);
24 else if (ret == 0)
25 printf("%s: Fail\n", value);
26 else
27 printf("%s: Error: %d\n", value, ret);
28 if (repeat) {
29 int j;
30 for (j = 0;j < 999999;j++)
31 xmlRegexpExec(comp, (const xmlChar *) value);
32 }
33}
34
35static void
36testRegexpFile(const char *filename) {
37 xmlRegexpPtr comp = NULL;
38 FILE *input;
39 char expression[5000];
40 int len;
41
42 input = fopen(filename, "r");
43 if (input == NULL) {
44 xmlGenericError(xmlGenericErrorContext,
45 "Cannot open %s for reading\n", filename);
46 return;
47 }
48 while (fgets(expression, 4500, input) != NULL) {
49 len = strlen(expression);
50 len--;
51 while ((len >= 0) &&
52 ((expression[len] == '\n') || (expression[len] == '\t') ||
53 (expression[len] == '\r') || (expression[len] == ' '))) len--;
54 expression[len + 1] = 0;
55 if (len >= 0) {
56 if (expression[0] == '#')
57 continue;
58 if ((expression[0] == '=') && (expression[1] == '>')) {
59 char *pattern = &expression[2];
60
61 if (comp != NULL) {
62 xmlRegFreeRegexp(comp);
63 comp = NULL;
64 }
65 printf("Regexp: %s\n", pattern) ;
66 comp = xmlRegexpCompile((const xmlChar *) pattern);
67 if (comp == NULL) {
68 printf(" failed to compile\n");
69 break;
70 }
71 } else if (comp == NULL) {
72 printf("Regexp: %s\n", expression) ;
73 comp = xmlRegexpCompile((const xmlChar *) expression);
74 if (comp == NULL) {
75 printf(" failed to compile\n");
76 break;
77 }
78 } else if (comp != NULL) {
79 testRegexp(comp, expression);
80 }
81 }
82 }
83 fclose(input);
84 if (comp != NULL)
85 xmlRegFreeRegexp(comp);
86}
87
88
89static void usage(const char *name) {
90 fprintf(stderr, "Usage: %s\n", name);
91}
92
93int main(int argc, char **argv) {
94 xmlRegexpPtr comp = NULL;
95 const char *pattern = NULL;
96 char *filename = NULL;
97 int i;
98
99 xmlInitMemory();
100
101 if (argc <= 1) {
102 usage(argv[0]);
103 return(1);
104 }
105 for (i = 1; i < argc ; i++) {
106 if (!strcmp(argv[i], "-"))
107 break;
108
109 if (argv[i][0] != '-')
110 continue;
111 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) {
112 debug++;
113 } else if ((!strcmp(argv[i], "-repeat")) ||
114 (!strcmp(argv[i], "--repeat"))) {
115 repeat++;
116 } else if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input")))
117 filename = argv[++i];
118 else {
119 fprintf(stderr, "Unknown option %s\n", argv[i]);
120 usage(argv[0]);
121 }
122 }
123 if (filename != NULL) {
124 testRegexpFile(filename);
125 } else {
126 for (i = 1; i < argc ; i++) {
127 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
128 if (pattern == NULL) {
129 pattern = argv[i];
130 printf("Testing %s:\n", pattern);
131 comp = xmlRegexpCompile((const xmlChar *) pattern);
132 if (comp == NULL) {
133 printf(" failed to compile\n");
134 break;
135 }
136 if (debug)
137 xmlRegexpPrint(stdout, comp);
138 } else {
139 testRegexp(comp, argv[i]);
140 }
141 }
142 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000143 xmlMemoryDump();
Daniel Veillard4255d502002-04-16 15:50:10 +0000144 if (comp != NULL)
145 xmlRegFreeRegexp(comp);
146 }
147 xmlCleanupParser();
Daniel Veillard23e73572002-09-19 19:56:43 +0000148 /* xmlMemoryDump(); */
Daniel Veillard4255d502002-04-16 15:50:10 +0000149 return(0);
150}
151
152#else
153#include <stdio.h>
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000154int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
Daniel Veillard4255d502002-04-16 15:50:10 +0000155 printf("%s : Regexp support not compiled in\n", argv[0]);
156 return(0);
157}
158#endif /* LIBXML_REGEXP_ENABLED */