1 /*
2 __ __ _
3 ___\ \/ /_ __ __ _| |_
4 / _ \\ /| '_ \ / _` | __|
5 | __// \| |_) | (_| | |_
6 \___/_/\_\ .__/ \__,_|\__|
7 |_| XML parser
8
9 Copyright (c) 2003-2006 Karl Waclawek <karl@waclawek.net>
10 Copyright (c) 2005-2007 Steven Solie <steven@solie.ca>
11 Copyright (c) 2017-2025 Sebastian Pipping <sebastian@pipping.org>
12 Copyright (c) 2017 Rhodri James <rhodri@wildebeest.org.uk>
13 Licensed under the MIT license:
14
15 Permission is hereby granted, free of charge, to any person obtaining
16 a copy of this software and associated documentation files (the
17 "Software"), to deal in the Software without restriction, including
18 without limitation the rights to use, copy, modify, merge, publish,
19 distribute, sublicense, and/or sell copies of the Software, and to permit
20 persons to whom the Software is furnished to do so, subject to the
21 following conditions:
22
23 The above copyright notice and this permission notice shall be included
24 in all copies or substantial portions of the Software.
25
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
29 NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
30 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
31 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
32 USE OR OTHER DEALINGS IN THE SOFTWARE.
33 */
34
35 #define _POSIX_C_SOURCE 1 // fdopen
36
37 #if defined(_MSC_VER)
38 # include <io.h> // _open, _close
39 #else
40 # include <unistd.h> // close
41 #endif
42
43 #include <fcntl.h> // open
44 #include <sys/stat.h>
45 #include <assert.h>
46 #include <stddef.h> // ptrdiff_t
47 #include <stdio.h>
48 #include <time.h>
49 #include "expat.h"
50
51 #ifdef XML_LARGE_SIZE
52 # define XML_FMT_INT_MOD "ll"
53 #else
54 # define XML_FMT_INT_MOD "l"
55 #endif
56
57 #ifdef XML_UNICODE_WCHAR_T
58 # define XML_FMT_STR "ls"
59 #else
60 # define XML_FMT_STR "s"
61 #endif
62
63 static int
usage(const char * prog,int rc)64 usage(const char *prog, int rc) {
65 fprintf(stderr, "usage: %s [-n] filename bufferSize nr_of_loops\n", prog);
66 return rc;
67 }
68
69 int
main(int argc,char * argv[])70 main(int argc, char *argv[]) {
71 XML_Parser parser;
72 char *XMLBuf, *XMLBufEnd, *XMLBufPtr;
73 int fd;
74 FILE *file;
75 struct stat fileAttr;
76 int nrOfLoops, bufferSize, i, isFinal;
77 size_t fileSize;
78 int j = 0, ns = 0;
79 clock_t tstart, tend;
80 double cpuTime = 0.0;
81
82 if (argc > 1) {
83 if (argv[1][0] == '-') {
84 if (argv[1][1] == 'n' && argv[1][2] == '\0') {
85 ns = 1;
86 j = 1;
87 } else
88 return usage(argv[0], 1);
89 }
90 }
91
92 if (argc != j + 4)
93 return usage(argv[0], 1);
94
95 fd = open(argv[j + 1], O_RDONLY);
96 if (fd == -1) {
97 fprintf(stderr, "could not open file '%s'\n", argv[j + 1]);
98 return 2;
99 }
100
101 if (fstat(fd, &fileAttr) != 0) {
102 close(fd);
103 fprintf(stderr, "could not fstat file '%s'\n", argv[j + 1]);
104 return 2;
105 }
106
107 file = fdopen(fd, "r");
108 if (! file) {
109 close(fd);
110 fprintf(stderr, "could not fdopen file '%s'\n", argv[j + 1]);
111 return 2;
112 }
113
114 bufferSize = atoi(argv[j + 2]);
115 nrOfLoops = atoi(argv[j + 3]);
116 if (bufferSize <= 0 || nrOfLoops <= 0) {
117 fclose(file); // NOTE: this closes fd as well
118 fprintf(stderr, "buffer size and nr of loops must be greater than zero.\n");
119 return 3;
120 }
121
122 XMLBuf = malloc(fileAttr.st_size);
123 if (XMLBuf == NULL) {
124 fclose(file); // NOTE: this closes fd as well
125 fprintf(stderr, "ouf of memory.\n");
126 return 5;
127 }
128 fileSize = fread(XMLBuf, sizeof(char), fileAttr.st_size, file);
129 fclose(file); // NOTE: this closes fd as well
130
131 if (ns)
132 parser = XML_ParserCreateNS(NULL, '!');
133 else
134 parser = XML_ParserCreate(NULL);
135
136 i = 0;
137 XMLBufEnd = XMLBuf + fileSize;
138 while (i < nrOfLoops) {
139 XMLBufPtr = XMLBuf;
140 isFinal = 0;
141 tstart = clock();
142 do {
143 ptrdiff_t parseBufferSize = XMLBufEnd - XMLBufPtr;
144 if (parseBufferSize <= (ptrdiff_t)bufferSize)
145 isFinal = 1;
146 else
147 parseBufferSize = bufferSize;
148 assert(parseBufferSize <= (ptrdiff_t)bufferSize);
149 if (! XML_Parse(parser, XMLBufPtr, (int)parseBufferSize, isFinal)) {
150 fprintf(stderr,
151 "error '%" XML_FMT_STR "' at line %" XML_FMT_INT_MOD
152 "u character %" XML_FMT_INT_MOD "u\n",
153 XML_ErrorString(XML_GetErrorCode(parser)),
154 XML_GetCurrentLineNumber(parser),
155 XML_GetCurrentColumnNumber(parser));
156 free(XMLBuf);
157 XML_ParserFree(parser);
158 return 4;
159 }
160 XMLBufPtr += bufferSize;
161 } while (! isFinal);
162 tend = clock();
163 cpuTime += ((double)(tend - tstart)) / CLOCKS_PER_SEC;
164 XML_ParserReset(parser, NULL);
165 i++;
166 }
167
168 XML_ParserFree(parser);
169 free(XMLBuf);
170
171 printf("%d loops, with buffer size %d. Average time per loop: %f\n",
172 nrOfLoops, bufferSize, cpuTime / (double)nrOfLoops);
173 return 0;
174 }
175