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