outline.c revision 1.2
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#ifdef XML_LARGE_SIZE
29#if defined(XML_USE_MSC_EXTENSIONS) && _MSC_VER < 1400
30#define XML_FMT_INT_MOD "I64"
31#else
32#define XML_FMT_INT_MOD "ll"
33#endif
34#else
35#define XML_FMT_INT_MOD "l"
36#endif
37
38#define BUFFSIZE        8192
39
40char Buff[BUFFSIZE];
41
42int Depth;
43
44static void XMLCALL
45start(void *data, const char *el, const char **attr)
46{
47  int i;
48
49  for (i = 0; i < Depth; i++)
50    printf("  ");
51
52  printf("%s", el);
53
54  for (i = 0; attr[i]; i += 2) {
55    printf(" %s='%s'", attr[i], attr[i + 1]);
56  }
57
58  printf("\n");
59  Depth++;
60}
61
62static void XMLCALL
63end(void *data, const char *el)
64{
65  Depth--;
66}
67
68#ifdef AMIGA_SHARED_LIB
69#include <proto/expat.h>
70int
71amiga_main(int argc, char *argv[])
72#else
73int
74main(int argc, char *argv[])
75#endif
76{
77  XML_Parser p = XML_ParserCreate(NULL);
78  if (! p) {
79    fprintf(stderr, "Couldn't allocate memory for parser\n");
80    exit(-1);
81  }
82
83  XML_SetElementHandler(p, start, end);
84
85  for (;;) {
86    int done;
87    int len;
88
89    len = fread(Buff, 1, BUFFSIZE, stdin);
90    if (ferror(stdin)) {
91      fprintf(stderr, "Read error\n");
92      exit(-1);
93    }
94    done = feof(stdin);
95
96    if (XML_Parse(p, Buff, len, done) == XML_STATUS_ERROR) {
97      fprintf(stderr, "Parse error at line %" XML_FMT_INT_MOD "u:\n%s\n",
98              XML_GetCurrentLineNumber(p),
99              XML_ErrorString(XML_GetErrorCode(p)));
100      exit(-1);
101    }
102
103    if (done)
104      break;
105  }
106  return 0;
107}
108