1178848Scokane#include <sys/stat.h>
2178848Scokane#include <stdlib.h>
3178848Scokane#include <stdio.h>
4178848Scokane#include <time.h>
5178848Scokane#include "expat.h"
6178848Scokane
7178848Scokane#if defined(__amigaos__) && defined(__USE_INLINE__)
8178848Scokane#include <proto/expat.h>
9178848Scokane#endif
10178848Scokane
11178848Scokane#ifdef XML_LARGE_SIZE
12178848Scokane#define XML_FMT_INT_MOD "ll"
13178848Scokane#else
14178848Scokane#define XML_FMT_INT_MOD "l"
15178848Scokane#endif
16178848Scokane
17178848Scokanestatic void
18178848Scokaneusage(const char *prog, int rc)
19178848Scokane{
20178848Scokane  fprintf(stderr,
21178848Scokane          "usage: %s [-n] filename bufferSize nr_of_loops\n", prog);
22178848Scokane  exit(rc);
23178848Scokane}
24178848Scokane
25178848Scokaneint main (int argc, char *argv[])
26178848Scokane{
27178848Scokane  XML_Parser  parser;
28178848Scokane  char        *XMLBuf, *XMLBufEnd, *XMLBufPtr;
29178848Scokane  FILE        *fd;
30178848Scokane  struct stat fileAttr;
31178848Scokane  int         nrOfLoops, bufferSize, fileSize, i, isFinal;
32178848Scokane  int         j = 0, ns = 0;
33178848Scokane  clock_t     tstart, tend;
34178848Scokane  double      cpuTime = 0.0;
35178848Scokane
36178848Scokane  if (argc > 1) {
37178848Scokane    if (argv[1][0] == '-') {
38178848Scokane      if (argv[1][1] == 'n' && argv[1][2] == '\0') {
39178848Scokane        ns = 1;
40178848Scokane        j = 1;
41178848Scokane      }
42178848Scokane      else
43178848Scokane        usage(argv[0], 1);
44178848Scokane    }
45178848Scokane  }
46178848Scokane
47178848Scokane  if (argc != j + 4)
48178848Scokane    usage(argv[0], 1);
49178848Scokane
50178848Scokane  if (stat (argv[j + 1], &fileAttr) != 0) {
51178848Scokane    fprintf (stderr, "could not access file '%s'\n", argv[j + 1]);
52178848Scokane    return 2;
53178848Scokane  }
54178848Scokane
55178848Scokane  fd = fopen (argv[j + 1], "r");
56178848Scokane  if (!fd) {
57178848Scokane    fprintf (stderr, "could not open file '%s'\n", argv[j + 1]);
58178848Scokane    exit(2);
59178848Scokane  }
60178848Scokane
61178848Scokane  bufferSize = atoi (argv[j + 2]);
62178848Scokane  nrOfLoops = atoi (argv[j + 3]);
63178848Scokane  if (bufferSize <= 0 || nrOfLoops <= 0) {
64178848Scokane    fprintf (stderr,
65178848Scokane             "buffer size and nr of loops must be greater than zero.\n");
66178848Scokane    exit(3);
67178848Scokane  }
68178848Scokane
69178848Scokane  XMLBuf = malloc (fileAttr.st_size);
70178848Scokane  fileSize = fread (XMLBuf, sizeof (char), fileAttr.st_size, fd);
71178848Scokane  fclose (fd);
72178848Scokane
73178848Scokane  if (ns)
74178848Scokane    parser = XML_ParserCreateNS(NULL, '!');
75178848Scokane  else
76178848Scokane    parser = XML_ParserCreate(NULL);
77178848Scokane
78178848Scokane  i = 0;
79178848Scokane  XMLBufEnd = XMLBuf + fileSize;
80178848Scokane  while (i < nrOfLoops) {
81178848Scokane    XMLBufPtr = XMLBuf;
82178848Scokane    isFinal = 0;
83178848Scokane    tstart = clock();
84178848Scokane    do {
85178848Scokane      int parseBufferSize = XMLBufEnd - XMLBufPtr;
86178848Scokane      if (parseBufferSize <= bufferSize)
87178848Scokane        isFinal = 1;
88178848Scokane      else
89178848Scokane        parseBufferSize = bufferSize;
90178848Scokane      if (!XML_Parse (parser, XMLBufPtr, parseBufferSize, isFinal)) {
91178848Scokane        fprintf (stderr, "error '%s' at line %" XML_FMT_INT_MOD \
92178848Scokane                     "u character %" XML_FMT_INT_MOD "u\n",
93178848Scokane                 XML_ErrorString (XML_GetErrorCode (parser)),
94178848Scokane                 XML_GetCurrentLineNumber (parser),
95178848Scokane                 XML_GetCurrentColumnNumber (parser));
96178848Scokane        free (XMLBuf);
97178848Scokane        XML_ParserFree (parser);
98178848Scokane        exit (4);
99178848Scokane      }
100178848Scokane      XMLBufPtr += bufferSize;
101178848Scokane    } while (!isFinal);
102178848Scokane    tend = clock();
103178848Scokane    cpuTime += ((double) (tend - tstart)) / CLOCKS_PER_SEC;
104178848Scokane    XML_ParserReset(parser, NULL);
105178848Scokane    i++;
106178848Scokane  }
107178848Scokane
108178848Scokane  XML_ParserFree (parser);
109178848Scokane  free (XMLBuf);
110178848Scokane
111178848Scokane  printf ("%d loops, with buffer size %d. Average time per loop: %f\n",
112178848Scokane          nrOfLoops, bufferSize, cpuTime / (double) nrOfLoops);
113178848Scokane  return 0;
114178848Scokane}
115