xref: /openbsd-src/lib/libexpat/examples/elements.c (revision c0dd97bfcad3dab6c31ec12b9de1274fd2d2f993)
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                         / _ \\  /| '_ \ / _` | __|
9                        |  __//  \| |_) | (_| | |_
10                         \___/_/\_\ .__/ \__,_|\__|
11                                  |_| XML parser
12 
13    Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
14    Copyright (c) 2000-2017 Expat development team
15    Licensed under the MIT license:
16 
17    Permission is  hereby granted,  free of charge,  to any  person obtaining
18    a  copy  of  this  software   and  associated  documentation  files  (the
19    "Software"),  to  deal in  the  Software  without restriction,  including
20    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
21    distribute, sublicense, and/or sell copies of the Software, and to permit
22    persons  to whom  the Software  is  furnished to  do so,  subject to  the
23    following conditions:
24 
25    The above copyright  notice and this permission notice  shall be included
26    in all copies or substantial portions of the Software.
27 
28    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
29    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
30    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
31    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
32    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
33    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
34    USE OR OTHER DEALINGS IN THE SOFTWARE.
35 */
36 
37 #include <stdio.h>
38 #include <expat.h>
39 
40 #ifdef XML_LARGE_SIZE
41 #if defined(XML_USE_MSC_EXTENSIONS) && _MSC_VER < 1400
42 #define XML_FMT_INT_MOD "I64"
43 #else
44 #define XML_FMT_INT_MOD "ll"
45 #endif
46 #else
47 #define XML_FMT_INT_MOD "l"
48 #endif
49 
50 static void XMLCALL
51 startElement(void *userData, const char *name, const char **atts)
52 {
53   int i;
54   int *depthPtr = (int *)userData;
55   (void)atts;
56 
57   for (i = 0; i < *depthPtr; i++)
58     putchar('\t');
59   puts(name);
60   *depthPtr += 1;
61 }
62 
63 static void XMLCALL
64 endElement(void *userData, const char *name)
65 {
66   int *depthPtr = (int *)userData;
67   (void)name;
68 
69   *depthPtr -= 1;
70 }
71 
72 int
73 main(int argc, char *argv[])
74 {
75   char buf[BUFSIZ];
76   XML_Parser parser = XML_ParserCreate(NULL);
77   int done;
78   int depth = 0;
79   (void)argc;
80   (void)argv;
81 
82   XML_SetUserData(parser, &depth);
83   XML_SetElementHandler(parser, startElement, endElement);
84   do {
85     size_t len = fread(buf, 1, sizeof(buf), stdin);
86     done = len < sizeof(buf);
87     if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR) {
88       fprintf(stderr,
89               "%s at line %" XML_FMT_INT_MOD "u\n",
90               XML_ErrorString(XML_GetErrorCode(parser)),
91               XML_GetCurrentLineNumber(parser));
92       return 1;
93     }
94   } while (!done);
95   XML_ParserFree(parser);
96   return 0;
97 }
98