~ubuntu-branches/ubuntu/hardy/expat/hardy-updates

« back to all changes in this revision

Viewing changes to examples/outline.c

  • Committer: Bazaar Package Importer
  • Author(s): Ardo van Rangelrooij
  • Date: 2001-12-09 12:19:40 UTC
  • Revision ID: james.westby@ubuntu.com-20011209121940-zu64acbvutfed4hh
Tags: upstream-1.95.2
ImportĀ upstreamĀ versionĀ 1.95.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 */
 
22
 
 
23
 
 
24
#include <stdio.h>
 
25
#include <expat.h>
 
26
 
 
27
#define BUFFSIZE        8192
 
28
 
 
29
char Buff[BUFFSIZE];
 
30
 
 
31
int Depth;
 
32
 
 
33
static void
 
34
start(void *data, const char *el, const char **attr) {
 
35
  int i;
 
36
 
 
37
  for (i = 0; i < Depth; i++)
 
38
    printf("  ");
 
39
 
 
40
  printf("%s", el);
 
41
 
 
42
  for (i = 0; attr[i]; i += 2) {
 
43
    printf(" %s='%s'", attr[i], attr[i + 1]);
 
44
  }
 
45
 
 
46
  printf("\n");
 
47
  Depth++;
 
48
}  /* End of start handler */
 
49
 
 
50
static void
 
51
end(void *data, const char *el) {
 
52
  Depth--;
 
53
}  /* End of end handler */
 
54
 
 
55
int
 
56
main(int argc, char *argv[]) {
 
57
  XML_Parser p = XML_ParserCreate(NULL);
 
58
  if (! p) {
 
59
    fprintf(stderr, "Couldn't allocate memory for parser\n");
 
60
    exit(-1);
 
61
  }
 
62
 
 
63
  XML_SetElementHandler(p, start, end);
 
64
 
 
65
  for (;;) {
 
66
    int done;
 
67
    int len;
 
68
 
 
69
    len = fread(Buff, 1, BUFFSIZE, stdin);
 
70
    if (ferror(stdin)) {
 
71
      fprintf(stderr, "Read error\n");
 
72
      exit(-1);
 
73
    }
 
74
    done = feof(stdin);
 
75
 
 
76
    if (! XML_Parse(p, Buff, len, done)) {
 
77
      fprintf(stderr, "Parse error at line %d:\n%s\n",
 
78
              XML_GetCurrentLineNumber(p),
 
79
              XML_ErrorString(XML_GetErrorCode(p)));
 
80
      exit(-1);
 
81
    }
 
82
 
 
83
    if (done)
 
84
      break;
 
85
  }
 
86
  return 0;
 
87
}  /* End of main */
 
88