Apache HTTPD
elements.c
Go to the documentation of this file.
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) 2001-2003 Fred L. Drake, Jr. <[email protected]>
15 Copyright (c) 2004-2006 Karl Waclawek <[email protected]>
16 Copyright (c) 2005-2007 Steven Solie <[email protected]>
17 Copyright (c) 2016-2022 Sebastian Pipping <[email protected]>
18 Copyright (c) 2017 Rhodri James <[email protected]>
19 Copyright (c) 2019 Zhongyuan Zhou <[email protected]>
20 Licensed under the MIT license:
21
22 Permission is hereby granted, free of charge, to any person obtaining
23 a copy of this software and associated documentation files (the
24 "Software"), to deal in the Software without restriction, including
25 without limitation the rights to use, copy, modify, merge, publish,
26 distribute, sublicense, and/or sell copies of the Software, and to permit
27 persons to whom the Software is furnished to do so, subject to the
28 following conditions:
29
30 The above copyright notice and this permission notice shall be included
31 in all copies or substantial portions of the Software.
32
33 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
34 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
35 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
36 NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
37 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
38 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
39 USE OR OTHER DEALINGS IN THE SOFTWARE.
40*/
41
42#include <stdio.h>
43#include <expat.h>
44
45#ifdef XML_LARGE_SIZE
46# define XML_FMT_INT_MOD "ll"
47#else
48# define XML_FMT_INT_MOD "l"
49#endif
50
51#ifdef XML_UNICODE_WCHAR_T
52# define XML_FMT_STR "ls"
53#else
54# define XML_FMT_STR "s"
55#endif
56
57static void XMLCALL
58startElement(void *userData, const XML_Char *name, const XML_Char **atts) {
59 int i;
60 int *const depthPtr = (int *)userData;
61 (void)atts;
62
63 for (i = 0; i < *depthPtr; i++)
64 putchar('\t');
65 printf("%" XML_FMT_STR "\n", name);
66 *depthPtr += 1;
67}
68
69static void XMLCALL
70endElement(void *userData, const XML_Char *name) {
71 int *const depthPtr = (int *)userData;
72 (void)name;
73
74 *depthPtr -= 1;
75}
76
77int
78main(void) {
80 int done;
81 int depth = 0;
82
83 if (! parser) {
84 fprintf(stderr, "Couldn't allocate memory for parser\n");
85 return 1;
86 }
87
88 XML_SetUserData(parser, &depth);
90
91 do {
92 void *const buf = XML_GetBuffer(parser, BUFSIZ);
93 if (! buf) {
94 fprintf(stderr, "Couldn't allocate memory for buffer\n");
96 return 1;
97 }
98
99 const size_t len = fread(buf, 1, BUFSIZ, stdin);
100
101 if (ferror(stdin)) {
102 fprintf(stderr, "Read error\n");
104 return 1;
105 }
106
107 done = feof(stdin);
108
109 if (XML_ParseBuffer(parser, (int)len, done) == XML_STATUS_ERROR) {
111 "Parse error at line %" XML_FMT_INT_MOD "u:\n%" XML_FMT_STR "\n",
115 return 1;
116 }
117 } while (! done);
118
120 return 0;
121}
const char apr_size_t len
Definition ap_regex.h:187
#define XML_FMT_INT_MOD
Definition elements.c:48
static void XMLCALL endElement(void *userData, const XML_Char *name)
Definition elements.c:70
static void XMLCALL startElement(void *userData, const XML_Char *name, const XML_Char **atts)
Definition elements.c:58
#define XML_FMT_STR
Definition elements.c:54
int main(void)
Definition elements.c:78
const XML_LChar * XML_ErrorString(enum XML_Error code)
Definition xmlparse.c:2429
#define XML_STATUS_ERROR
Definition expat.h:76
void XML_SetElementHandler(XML_Parser parser, XML_StartElementHandler start, XML_EndElementHandler end)
Definition xmlparse.c:1691
enum XML_Error XML_GetErrorCode(XML_Parser parser)
Definition xmlparse.c:2318
XML_Parser XML_ParserCreate(const XML_Char *encoding)
Definition xmlparse.c:766
void XML_ParserFree(XML_Parser parser)
Definition xmlparse.c:1537
XML_Size XML_GetCurrentLineNumber(XML_Parser parser)
Definition xmlparse.c:2364
void XML_SetUserData(XML_Parser parser, void *userData)
Definition xmlparse.c:1637
enum XML_Status XML_ParseBuffer(XML_Parser parser, int len, int isFinal)
Definition xmlparse.c:2037
void * XML_GetBuffer(XML_Parser parser, int len)
Definition xmlparse.c:2108
char XML_Char
#define XMLCALL
const unsigned char * buf
Definition util_md5.h:50
apr_xml_parser ** parser
Definition apr_xml.h:228
apr_size_t size
return NULL
Definition mod_so.c:359
int i
Definition mod_so.c:347
char * name