]> matita.cs.unibo.it Git - helm.git/blob - helm/papers/use_case/stats/parse/parse_xmlsax.c
ocaml 3.09 transition
[helm.git] / helm / papers / use_case / stats / parse / parse_xmlsax.c
1 #include <stdio.h>
2 #include <sys/time.h>
3 #include <libxml/xmlreader.h>
4
5 void characters_cb
6 (void * ctx, const xmlChar * ch, int len) { return; }
7
8 void whitespace_cb
9 (void *ctx, const xmlChar *ch, int len) { return; }
10
11 void cdata_cb
12 (void *ctx, const xmlChar *value, int len) { return; }
13
14 void start_element_cb
15 (void *ctx, const xmlChar *name, const xmlChar **atts) { return; }
16
17 void start_element_ns_cb
18 (void *ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI,
19  int nb_namespaces, const xmlChar **namespaces, int nb_attributes,
20  int nb_defaulted, const xmlChar **attributes)
21 { return; }
22
23 void end_element_ns_cb
24 (void *ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI)
25 { return; }
26
27 void error_cb
28 (void *ctx, const char *msg, ...) {
29         fprintf(stderr, "Error: %s\n", msg);
30         return;
31 }
32
33 int
34 main(int argc, char *argv[])
35 {
36         char buf[BUFSIZ];
37         int res;
38         int done;
39         FILE *xmlfile = stdin;
40         struct timeval timing1, timing2;
41         xmlParserCtxtPtr ctxt;
42         xmlSAXHandler sax = {
43                 NULL, /* internalSubsetSAXFunc */
44                 NULL, /* isStandaloneSAXFunc */
45                 NULL, /* hasInternalSubsetSAXFunc */
46                 NULL, /* hasExternalSubsetSAXFunc */
47                 NULL, /* resolveEntitySAXFunc */
48                 NULL, /* getEntitySAXFunc */
49                 NULL, /* entityDeclSAXFunc */
50                 NULL, /* notationDeclSAXFunc */
51                 NULL, /* attributeDeclSAXFunc */
52                 NULL, /* elementDeclSAXFunc */
53                 NULL, /* unparsedEntityDeclSAXFunc */
54                 NULL, /* setDocumentLocatorSAXFunc */
55                 NULL, /* startDocumentSAXFunc */
56                 NULL, /* endDocumentSAXFunc */
57                 start_element_cb, /* startElementSAXFunc */
58                 NULL, /* endElementSAXFunc */
59                 NULL, /* referenceSAXFunc */
60                 characters_cb, /* charactersSAXFunc */
61                 whitespace_cb, /* ignorableWhitespaceSAXFunc */
62                 NULL, /* processingInstructionSAXFunc */
63                 NULL, /* commentSAXFunc */
64                 NULL, /* warningSAXFunc */
65                 error_cb, /* errorSAXFunc */
66                 NULL, /* fatalErrorSAXFunc */
67                 NULL, /* getParameterEntitySAXFunc */
68                 cdata_cb, /* cdataBlockSAXFunc */
69                 NULL, /* externalSubsetSAXFunc */
70                 XML_SAX2_MAGIC, /* initialized */
71                 NULL, /* _private */
72                 start_element_ns_cb, /* startElementNsSAX2Func */
73                 end_element_ns_cb, /* startElementNsSAX2Func */
74                 NULL /* xmlStructuredErrorFunc */
75         };
76         ctxt = xmlCreatePushParserCtxt(&sax, NULL, NULL, 0, NULL);
77         if (ctxt == NULL) {
78                 fprintf(stderr, "Can't instantiate parser\n");
79                 return 1;
80         }
81         gettimeofday(&timing1, NULL);
82         do {
83                 size_t len = fread(buf, 1, sizeof(buf), xmlfile);
84                 done = len < sizeof(buf);
85                 res = xmlParseChunk(ctxt, buf, len, 0);
86                 if (res != 0) {
87                         fprintf(stderr, "Parse error\n");
88                         return 1;
89                 }
90         } while (!done);
91         gettimeofday(&timing2, NULL);
92         xmlFreeParserCtxt(ctxt);
93         fprintf(stdout, "%d\n",
94                         ((timing2.tv_sec * 1000000 + timing2.tv_usec) -
95                          (timing1.tv_sec * 1000000 + timing1.tv_usec)) / 1000);
96         return 0;
97 }
98