1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2024-10-26 20:25:14 +03:00
libxml2/testRegexp.c
Daniel Veillard a9cce9cd0d Okay this is scary but it is just adding a configure option to disable
* HTMLtree.c SAX2.c c14n.c catalog.c configure.in debugXML.c
  encoding.c entities.c nanoftp.c nanohttp.c parser.c relaxng.c
  testAutomata.c testC14N.c testHTML.c testRegexp.c testRelax.c
  testSchemas.c testXPath.c threads.c tree.c valid.c xmlIO.c
  xmlcatalog.c xmllint.c xmlmemory.c xmlreader.c xmlschemas.c
  example/gjobread.c include/libxml/HTMLtree.h include/libxml/c14n.h
  include/libxml/catalog.h include/libxml/debugXML.h
  include/libxml/entities.h include/libxml/nanohttp.h
  include/libxml/relaxng.h include/libxml/tree.h
  include/libxml/valid.h include/libxml/xmlIO.h
  include/libxml/xmlschemas.h include/libxml/xmlversion.h.in
  include/libxml/xpathInternals.h python/libxml.c:
  Okay this is scary but it is just adding a configure option
  to disable output, this touches most of the files.
Daniel
2003-09-29 13:20:24 +00:00

159 lines
3.6 KiB
C

/*
* testRegexp.c: simple module for testing regular expressions
*
* See Copyright for the status of this software.
*
* Daniel Veillard <veillard@redhat.com>
*/
#include <string.h>
#include "libxml.h"
#ifdef LIBXML_REGEXP_ENABLED
#include <libxml/tree.h>
#include <libxml/xmlregexp.h>
int repeat = 0;
int debug = 0;
static void testRegexp(xmlRegexpPtr comp, const char *value) {
int ret;
ret = xmlRegexpExec(comp, (const xmlChar *) value);
if (ret == 1)
printf("%s: Ok\n", value);
else if (ret == 0)
printf("%s: Fail\n", value);
else
printf("%s: Error: %d\n", value, ret);
if (repeat) {
int j;
for (j = 0;j < 999999;j++)
xmlRegexpExec(comp, (const xmlChar *) value);
}
}
static void
testRegexpFile(const char *filename) {
xmlRegexpPtr comp = NULL;
FILE *input;
char expression[5000];
int len;
input = fopen(filename, "r");
if (input == NULL) {
xmlGenericError(xmlGenericErrorContext,
"Cannot open %s for reading\n", filename);
return;
}
while (fgets(expression, 4500, input) != NULL) {
len = strlen(expression);
len--;
while ((len >= 0) &&
((expression[len] == '\n') || (expression[len] == '\t') ||
(expression[len] == '\r') || (expression[len] == ' '))) len--;
expression[len + 1] = 0;
if (len >= 0) {
if (expression[0] == '#')
continue;
if ((expression[0] == '=') && (expression[1] == '>')) {
char *pattern = &expression[2];
if (comp != NULL) {
xmlRegFreeRegexp(comp);
comp = NULL;
}
printf("Regexp: %s\n", pattern) ;
comp = xmlRegexpCompile((const xmlChar *) pattern);
if (comp == NULL) {
printf(" failed to compile\n");
break;
}
} else if (comp == NULL) {
printf("Regexp: %s\n", expression) ;
comp = xmlRegexpCompile((const xmlChar *) expression);
if (comp == NULL) {
printf(" failed to compile\n");
break;
}
} else if (comp != NULL) {
testRegexp(comp, expression);
}
}
}
fclose(input);
if (comp != NULL)
xmlRegFreeRegexp(comp);
}
static void usage(const char *name) {
fprintf(stderr, "Usage: %s\n", name);
}
int main(int argc, char **argv) {
xmlRegexpPtr comp = NULL;
const char *pattern = NULL;
char *filename = NULL;
int i;
xmlInitMemory();
if (argc <= 1) {
usage(argv[0]);
return(1);
}
for (i = 1; i < argc ; i++) {
if (!strcmp(argv[i], "-"))
break;
if (argv[i][0] != '-')
continue;
if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) {
debug++;
} else if ((!strcmp(argv[i], "-repeat")) ||
(!strcmp(argv[i], "--repeat"))) {
repeat++;
} else if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input")))
filename = argv[++i];
else {
fprintf(stderr, "Unknown option %s\n", argv[i]);
usage(argv[0]);
}
}
if (filename != NULL) {
testRegexpFile(filename);
} else {
for (i = 1; i < argc ; i++) {
if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
if (pattern == NULL) {
pattern = argv[i];
printf("Testing %s:\n", pattern);
comp = xmlRegexpCompile((const xmlChar *) pattern);
if (comp == NULL) {
printf(" failed to compile\n");
break;
}
if (debug)
xmlRegexpPrint(stdout, comp);
} else {
testRegexp(comp, argv[i]);
}
}
}
xmlMemoryDump();
if (comp != NULL)
xmlRegFreeRegexp(comp);
}
xmlCleanupParser();
/* xmlMemoryDump(); */
return(0);
}
#else
#include <stdio.h>
int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
printf("%s : Regexp support not compiled in\n", argv[0]);
return(0);
}
#endif /* LIBXML_REGEXP_ENABLED */