xref: /freebsd/contrib/expat/examples/outline.c (revision 0f7f3352c8bc463607912e2463d13e52d44a4cae)
1 /*****************************************************************
2  * outline.c
3  *
4  * Copyright 1999, Clark Cooper
5  * All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the license contained in the
9  * COPYING file that comes with the expat distribution.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
12  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
13  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
14  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
15  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
16  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
17  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18  *
19  * Read an XML document from standard input and print an element
20  * outline on standard output.
21  * Must be used with Expat compiled for UTF-8 output.
22  */
23 
24 
25 #include <stdio.h>
26 #include <expat.h>
27 
28 #if defined(__amigaos__) && defined(__USE_INLINE__)
29 #include <proto/expat.h>
30 #endif
31 
32 #ifdef XML_LARGE_SIZE
33 #if defined(XML_USE_MSC_EXTENSIONS) && _MSC_VER < 1400
34 #define XML_FMT_INT_MOD "I64"
35 #else
36 #define XML_FMT_INT_MOD "ll"
37 #endif
38 #else
39 #define XML_FMT_INT_MOD "l"
40 #endif
41 
42 #define BUFFSIZE        8192
43 
44 char Buff[BUFFSIZE];
45 
46 int Depth;
47 
48 static void XMLCALL
49 start(void *data, const char *el, const char **attr)
50 {
51   int i;
52   (void)data;
53 
54   for (i = 0; i < Depth; i++)
55     printf("  ");
56 
57   printf("%s", el);
58 
59   for (i = 0; attr[i]; i += 2) {
60     printf(" %s='%s'", attr[i], attr[i + 1]);
61   }
62 
63   printf("\n");
64   Depth++;
65 }
66 
67 static void XMLCALL
68 end(void *data, const char *el)
69 {
70   (void)data;
71   (void)el;
72 
73   Depth--;
74 }
75 
76 int
77 main(int argc, char *argv[])
78 {
79   XML_Parser p = XML_ParserCreate(NULL);
80   (void)argc;
81   (void)argv;
82 
83   if (! p) {
84     fprintf(stderr, "Couldn't allocate memory for parser\n");
85     exit(-1);
86   }
87 
88   XML_SetElementHandler(p, start, end);
89 
90   for (;;) {
91     int done;
92     int len;
93 
94     len = (int)fread(Buff, 1, BUFFSIZE, stdin);
95     if (ferror(stdin)) {
96       fprintf(stderr, "Read error\n");
97       exit(-1);
98     }
99     done = feof(stdin);
100 
101     if (XML_Parse(p, Buff, len, done) == XML_STATUS_ERROR) {
102       fprintf(stderr, "Parse error at line %" XML_FMT_INT_MOD "u:\n%s\n",
103               XML_GetCurrentLineNumber(p),
104               XML_ErrorString(XML_GetErrorCode(p)));
105       exit(-1);
106     }
107 
108     if (done)
109       break;
110   }
111   XML_ParserFree(p);
112   return 0;
113 }
114