]> git.saurik.com Git - wxWidgets.git/blame - src/expat/examples/elements.c
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / src / expat / examples / elements.c
CommitLineData
5e9f2524
VS
1/* This is simple demonstration of how to use expat. This program
2 reads an XML document from standard input and writes a line with
3 the name of each element to standard output indenting child
4 elements by one tab stop more than their parent element.
11a3e7b6 5 It must be used with Expat compiled for UTF-8 output.
5e9f2524
VS
6*/
7
8#include <stdio.h>
9#include "expat.h"
10
11a3e7b6
VZ
11#if defined(__amigaos__) && defined(__USE_INLINE__)
12#include <proto/expat.h>
13#endif
14
15#ifdef XML_LARGE_SIZE
16#if defined(XML_USE_MSC_EXTENSIONS) && _MSC_VER < 1400
17#define XML_FMT_INT_MOD "I64"
18#else
19#define XML_FMT_INT_MOD "ll"
20#endif
21#else
22#define XML_FMT_INT_MOD "l"
23#endif
24
25static void XMLCALL
5e9f2524
VS
26startElement(void *userData, const char *name, const char **atts)
27{
28 int i;
11a3e7b6 29 int *depthPtr = (int *)userData;
5e9f2524
VS
30 for (i = 0; i < *depthPtr; i++)
31 putchar('\t');
32 puts(name);
33 *depthPtr += 1;
34}
35
11a3e7b6 36static void XMLCALL
5e9f2524
VS
37endElement(void *userData, const char *name)
38{
11a3e7b6 39 int *depthPtr = (int *)userData;
5e9f2524
VS
40 *depthPtr -= 1;
41}
42
43int
44main(int argc, char *argv[])
45{
46 char buf[BUFSIZ];
47 XML_Parser parser = XML_ParserCreate(NULL);
48 int done;
49 int depth = 0;
50 XML_SetUserData(parser, &depth);
51 XML_SetElementHandler(parser, startElement, endElement);
52 do {
11a3e7b6 53 int len = (int)fread(buf, 1, sizeof(buf), stdin);
5e9f2524
VS
54 done = len < sizeof(buf);
55 if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR) {
56 fprintf(stderr,
11a3e7b6 57 "%s at line %" XML_FMT_INT_MOD "u\n",
5e9f2524
VS
58 XML_ErrorString(XML_GetErrorCode(parser)),
59 XML_GetCurrentLineNumber(parser));
60 return 1;
61 }
62 } while (!done);
63 XML_ParserFree(parser);
64 return 0;
65}