source: trunk/third/libxml2/testReader.c @ 20735

Revision 20735, 2.9 KB checked in by ghudson, 20 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r20734, which included commits to RCS files with non-trunk default branches.
Line 
1/*
2 * testSAX.c : a small tester program for parsing using the SAX API.
3 *
4 * See Copyright for the status of this software.
5 *
6 * daniel@veillard.com
7 */
8
9#include "libxml.h"
10
11#ifdef LIBXML_READER_ENABLED
12#include <string.h>
13#include <stdarg.h>
14
15#ifdef HAVE_SYS_TYPES_H
16#include <sys/types.h>
17#endif
18#ifdef HAVE_SYS_STAT_H
19#include <sys/stat.h>
20#endif
21#ifdef HAVE_FCNTL_H
22#include <fcntl.h>
23#endif
24#ifdef HAVE_UNISTD_H
25#include <unistd.h>
26#endif
27#ifdef HAVE_STDLIB_H
28#include <stdlib.h>
29#endif
30#ifdef HAVE_STRING_H
31#include <string.h>
32#endif
33
34
35#include <libxml/xmlreader.h>
36
37int debug = 0;
38int dump = 0;
39int noent = 0;
40int count = 0;
41int valid = 0;
42
43static void usage(const char *progname) {
44    printf("Usage : %s [options] XMLfiles ...\n", progname);
45    printf("\tParse the XML files using the xmlTextReader API\n");
46    printf("\t --count: count the number of attribute and elements\n");
47    printf("\t --valid: validate the document\n");
48    exit(1);
49}
50static int elem, attrs;
51
52static void processNode(xmlTextReaderPtr reader) {
53    int type;
54
55    type = xmlTextReaderNodeType(reader);
56    if (count) {
57        if (type == 1) {
58            elem++;
59            attrs += xmlTextReaderAttributeCount(reader);
60        }
61    }
62}
63
64static void handleFile(const char *filename) {
65    xmlTextReaderPtr reader;
66    int ret;
67
68    if (count) {
69        elem = 0;
70        attrs = 0;
71    }
72
73    reader = xmlNewTextReaderFilename(filename);
74    if (reader != NULL) {
75        if (valid)
76            xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1);
77
78        /*
79         * Process all nodes in sequence
80         */
81        ret = xmlTextReaderRead(reader);
82        while (ret == 1) {
83            processNode(reader);
84            ret = xmlTextReaderRead(reader);
85        }
86
87        /*
88         * Done, cleanup and status
89         */
90        xmlFreeTextReader(reader);
91        if (ret != 0) {
92            printf("%s : failed to parse\n", filename);
93        } else if (count)
94            printf("%s : %d elements, %d attributes\n", filename, elem, attrs);
95    } else {
96        fprintf(stderr, "Unable to open %s\n", filename);
97    }
98}
99
100int main(int argc, char **argv) {
101    int i;
102    int files = 0;
103
104    if (argc <= 1) {
105        usage(argv[0]);
106        return(1);
107    }
108    LIBXML_TEST_VERSION
109    for (i = 1; i < argc ; i++) {
110        if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
111            debug++;
112        else if ((!strcmp(argv[i], "-dump")) || (!strcmp(argv[i], "--dump")))
113            dump++;
114        else if ((!strcmp(argv[i], "-count")) || (!strcmp(argv[i], "--count")))
115            count++;
116        else if ((!strcmp(argv[i], "-valid")) || (!strcmp(argv[i], "--valid")))
117            valid++;
118        else if ((!strcmp(argv[i], "-noent")) ||
119                 (!strcmp(argv[i], "--noent")))
120            noent++;
121    }
122    if (noent != 0) xmlSubstituteEntitiesDefault(1);
123    for (i = 1; i < argc ; i++) {
124        if (argv[i][0] != '-') {
125            handleFile(argv[i]);
126            files ++;
127        }
128    }
129    xmlCleanupParser();
130    xmlMemoryDump();
131
132    return(0);
133}
134#else
135int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
136    printf("%s : xmlReader parser support not compiled in\n", argv[0]);
137    return(0);
138}
139#endif /* LIBXML_READER_ENABLED */
Note: See TracBrowser for help on using the repository browser.