Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 15 | int repeat = 0; |
| 16 | int debug = 0; |
| 17 | |
| 18 | static 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 | |
| 35 | static void |
| 36 | testRegexpFile(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 | |
| 89 | static void usage(const char *name) { |
| 90 | fprintf(stderr, "Usage: %s\n", name); |
| 91 | } |
| 92 | |
| 93 | int 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 | } |
| 143 | if (comp != NULL) |
| 144 | xmlRegFreeRegexp(comp); |
| 145 | } |
| 146 | xmlCleanupParser(); |
Daniel Veillard | 99c394d | 2005-07-14 12:58:49 +0000 | [diff] [blame^] | 147 | xmlMemoryDump(); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 148 | return(0); |
| 149 | } |
| 150 | |
| 151 | #else |
| 152 | #include <stdio.h> |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 153 | int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 154 | printf("%s : Regexp support not compiled in\n", argv[0]); |
| 155 | return(0); |
| 156 | } |
| 157 | #endif /* LIBXML_REGEXP_ENABLED */ |