2017-08-12 14:56:14 -04:00
|
|
|
/*
|
|
|
|
__ __ _
|
|
|
|
___\ \/ /_ __ __ _| |_
|
|
|
|
/ _ \\ /| '_ \ / _` | __|
|
|
|
|
| __// \| |_) | (_| | |_
|
|
|
|
\___/_/\_\ .__/ \__,_|\__|
|
|
|
|
|_| XML parser
|
|
|
|
|
|
|
|
Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
|
|
|
|
Copyright (c) 2000-2017 Expat development team
|
|
|
|
Licensed under the MIT license:
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
a copy of this software and associated documentation files (the
|
|
|
|
"Software"), to deal in the Software without restriction, including
|
|
|
|
without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
persons to whom the Software is furnished to do so, subject to the
|
|
|
|
following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included
|
|
|
|
in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2003-09-04 12:01:54 -04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include "expat.h"
|
|
|
|
|
2006-01-01 16:17:41 -05:00
|
|
|
#ifdef XML_LARGE_SIZE
|
2019-08-03 14:34:54 -04:00
|
|
|
# define XML_FMT_INT_MOD "ll"
|
2006-01-01 16:17:41 -05:00
|
|
|
#else
|
2019-08-03 14:34:54 -04:00
|
|
|
# define XML_FMT_INT_MOD "l"
|
2006-01-01 16:17:41 -05:00
|
|
|
#endif
|
|
|
|
|
2017-08-24 09:19:03 -04:00
|
|
|
#ifdef XML_UNICODE_WCHAR_T
|
2019-08-03 14:34:54 -04:00
|
|
|
# define XML_FMT_STR "ls"
|
2017-08-24 09:19:03 -04:00
|
|
|
#else
|
2019-08-03 14:34:54 -04:00
|
|
|
# define XML_FMT_STR "s"
|
2017-08-24 09:19:03 -04:00
|
|
|
#endif
|
|
|
|
|
2003-09-04 12:01:54 -04:00
|
|
|
static void
|
2019-08-03 14:34:54 -04:00
|
|
|
usage(const char *prog, int rc) {
|
|
|
|
fprintf(stderr, "usage: %s [-n] filename bufferSize nr_of_loops\n", prog);
|
2003-09-04 12:01:54 -04:00
|
|
|
exit(rc);
|
|
|
|
}
|
|
|
|
|
2019-08-03 14:34:54 -04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[]) {
|
|
|
|
XML_Parser parser;
|
|
|
|
char *XMLBuf, *XMLBufEnd, *XMLBufPtr;
|
|
|
|
FILE *fd;
|
2003-09-04 12:01:54 -04:00
|
|
|
struct stat fileAttr;
|
2019-08-03 14:34:54 -04:00
|
|
|
int nrOfLoops, bufferSize, fileSize, i, isFinal;
|
|
|
|
int j = 0, ns = 0;
|
|
|
|
clock_t tstart, tend;
|
|
|
|
double cpuTime = 0.0;
|
2003-09-04 12:01:54 -04:00
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
if (argv[1][0] == '-') {
|
|
|
|
if (argv[1][1] == 'n' && argv[1][2] == '\0') {
|
|
|
|
ns = 1;
|
|
|
|
j = 1;
|
2019-08-03 14:34:54 -04:00
|
|
|
} else
|
2003-09-04 12:01:54 -04:00
|
|
|
usage(argv[0], 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc != j + 4)
|
|
|
|
usage(argv[0], 1);
|
|
|
|
|
2019-08-03 14:34:54 -04:00
|
|
|
if (stat(argv[j + 1], &fileAttr) != 0) {
|
|
|
|
fprintf(stderr, "could not access file '%s'\n", argv[j + 1]);
|
2003-09-04 12:01:54 -04:00
|
|
|
return 2;
|
|
|
|
}
|
2019-08-03 14:34:54 -04:00
|
|
|
|
|
|
|
fd = fopen(argv[j + 1], "r");
|
|
|
|
if (! fd) {
|
|
|
|
fprintf(stderr, "could not open file '%s'\n", argv[j + 1]);
|
2003-09-04 12:01:54 -04:00
|
|
|
exit(2);
|
|
|
|
}
|
2019-08-03 14:34:54 -04:00
|
|
|
|
|
|
|
bufferSize = atoi(argv[j + 2]);
|
|
|
|
nrOfLoops = atoi(argv[j + 3]);
|
2003-09-04 12:01:54 -04:00
|
|
|
if (bufferSize <= 0 || nrOfLoops <= 0) {
|
2019-08-03 14:34:54 -04:00
|
|
|
fprintf(stderr, "buffer size and nr of loops must be greater than zero.\n");
|
2003-09-04 12:01:54 -04:00
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
|
2019-08-03 14:34:54 -04:00
|
|
|
XMLBuf = malloc(fileAttr.st_size);
|
|
|
|
fileSize = fread(XMLBuf, sizeof(char), fileAttr.st_size, fd);
|
|
|
|
fclose(fd);
|
|
|
|
|
2006-04-07 16:25:43 -04:00
|
|
|
if (ns)
|
|
|
|
parser = XML_ParserCreateNS(NULL, '!');
|
|
|
|
else
|
|
|
|
parser = XML_ParserCreate(NULL);
|
|
|
|
|
2003-09-04 12:01:54 -04:00
|
|
|
i = 0;
|
|
|
|
XMLBufEnd = XMLBuf + fileSize;
|
|
|
|
while (i < nrOfLoops) {
|
|
|
|
XMLBufPtr = XMLBuf;
|
|
|
|
isFinal = 0;
|
|
|
|
tstart = clock();
|
|
|
|
do {
|
|
|
|
int parseBufferSize = XMLBufEnd - XMLBufPtr;
|
|
|
|
if (parseBufferSize <= bufferSize)
|
|
|
|
isFinal = 1;
|
|
|
|
else
|
|
|
|
parseBufferSize = bufferSize;
|
2019-08-03 14:34:54 -04:00
|
|
|
if (! XML_Parse(parser, XMLBufPtr, parseBufferSize, isFinal)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"error '%" XML_FMT_STR "' at line %" XML_FMT_INT_MOD
|
|
|
|
"u character %" XML_FMT_INT_MOD "u\n",
|
|
|
|
XML_ErrorString(XML_GetErrorCode(parser)),
|
|
|
|
XML_GetCurrentLineNumber(parser),
|
|
|
|
XML_GetCurrentColumnNumber(parser));
|
|
|
|
free(XMLBuf);
|
|
|
|
XML_ParserFree(parser);
|
|
|
|
exit(4);
|
2003-09-04 12:01:54 -04:00
|
|
|
}
|
|
|
|
XMLBufPtr += bufferSize;
|
2019-08-03 14:34:54 -04:00
|
|
|
} while (! isFinal);
|
2003-09-04 12:01:54 -04:00
|
|
|
tend = clock();
|
2019-08-03 14:34:54 -04:00
|
|
|
cpuTime += ((double)(tend - tstart)) / CLOCKS_PER_SEC;
|
2006-04-07 16:25:43 -04:00
|
|
|
XML_ParserReset(parser, NULL);
|
2003-09-04 12:01:54 -04:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2019-08-03 14:34:54 -04:00
|
|
|
XML_ParserFree(parser);
|
|
|
|
free(XMLBuf);
|
|
|
|
|
|
|
|
printf("%d loops, with buffer size %d. Average time per loop: %f\n",
|
|
|
|
nrOfLoops, bufferSize, cpuTime / (double)nrOfLoops);
|
2003-09-04 12:01:54 -04:00
|
|
|
return 0;
|
|
|
|
}
|