• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
5    It must be used with Expat compiled for UTF-8 output.
6 */
7 
8 #include <stdio.h>
9 #include "expat.h"
10 
11 #ifdef XML_LARGE_SIZE
12 #if defined(XML_USE_MSC_EXTENSIONS) && _MSC_VER < 1400
13 #define XML_FMT_INT_MOD "I64"
14 #else
15 #define XML_FMT_INT_MOD "ll"
16 #endif
17 #else
18 #define XML_FMT_INT_MOD "l"
19 #endif
20 
21 static void XMLCALL
startElement(void * userData,const char * name,const char ** atts)22 startElement(void *userData, const char *name, const char **atts)
23 {
24   int i;
25   int *depthPtr = (int *)userData;
26   for (i = 0; i < *depthPtr; i++)
27     putchar('\t');
28   puts(name);
29   *depthPtr += 1;
30 }
31 
32 static void XMLCALL
endElement(void * userData,const char * name)33 endElement(void *userData, const char *name)
34 {
35   int *depthPtr = (int *)userData;
36   *depthPtr -= 1;
37 }
38 
39 #ifdef AMIGA_SHARED_LIB
40 #include <proto/expat.h>
41 int
amiga_main(int argc,char * argv[])42 amiga_main(int argc, char *argv[])
43 #else
44 int
45 main(int argc, char *argv[])
46 #endif
47 {
48   char buf[BUFSIZ];
49   XML_Parser parser = XML_ParserCreate(NULL);
50   int done;
51   int depth = 0;
52   XML_SetUserData(parser, &depth);
53   XML_SetElementHandler(parser, startElement, endElement);
54   do {
55     size_t len = fread(buf, 1, sizeof(buf), stdin);
56     done = len < sizeof(buf);
57     if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR) {
58       fprintf(stderr,
59               "%s at line %" XML_FMT_INT_MOD "u\n",
60               XML_ErrorString(XML_GetErrorCode(parser)),
61               XML_GetCurrentLineNumber(parser));
62       return 1;
63     }
64   } while (!done);
65   XML_ParserFree(parser);
66   return 0;
67 }
68