~mok0/+junk/buffer

« back to all changes in this revision

Viewing changes to xml/testexpatpp.cpp

  • Committer: Morten Kjeldgaard
  • Date: 2011-02-24 09:37:40 UTC
  • Revision ID: mok@bioxray.dk-20110224093740-dorklgmhuxrydhf8
establish repositorya

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 the
 
3
name of each element to standard output indenting child elements by
 
4
one tab stop more than their parent element. 
 
5
 
 
6
Copied from elements.c 98/08/31 AD to open specified file rather than stdin
 
7
and added a CharacterDataHandler
 
8
*/
 
9
 
 
10
#include <stdio.h>
 
11
#include "expatpp.h"
 
12
#include <string.h>
 
13
 
 
14
class myParser : public expatpp {
 
15
public:
 
16
        myParser() : mDepth(0) {};
 
17
        
 
18
        virtual void startElement(const XML_Char *name, const XML_Char **atts);
 
19
        virtual void endElement(const XML_Char* name);
 
20
        virtual void charData(const XML_Char *s, int len);
 
21
        
 
22
private:
 
23
        void WriteIndent();
 
24
        int     mDepth; 
 
25
};
 
26
 
 
27
 
 
28
void 
 
29
myParser::WriteIndent()
 
30
{
 
31
  for (int i = 0; i < mDepth; i++)
 
32
    putchar('\t');
 
33
}
 
34
 
 
35
 
 
36
void 
 
37
myParser::startElement(const char* name, const char** atts)
 
38
{
 
39
  WriteIndent();
 
40
  puts(name);
 
41
  if (atts) { /* write list of attributes indented below element */
 
42
    int i;
 
43
    for (i=0; atts[i]; i++) {
 
44
      WriteIndent();
 
45
      putchar('-'); putchar(' ');
 
46
      puts(atts[i]);
 
47
    }
 
48
  }
 
49
  mDepth++;
 
50
}
 
51
 
 
52
 
 
53
void 
 
54
myParser::endElement(const char*)
 
55
{
 
56
  mDepth--;
 
57
}
 
58
 
 
59
 
 
60
void 
 
61
myParser::charData(const XML_Char *s, int len)
 
62
{
 
63
  const int leadingSpace = skipWhiteSpace(s);
 
64
  if (len==0 || len==leadingSpace)
 
65
        return;  // called with whitespace between elements
 
66
        
 
67
  WriteIndent();
 
68
 
 
69
/* write out the user data bracketed by ()*/
 
70
  putchar('(');
 
71
  fwrite(s, len, 1, stdout);
 
72
  puts(")");
 
73
}
 
74
 
 
75
 
 
76
int main()
 
77
{       
 
78
  myParser parser;
 
79
  char filename[80];
 
80
  FILE* xmlFile;
 
81
  char buf[BUFSIZ];
 
82
  for (;;) {      
 
83
          int done;
 
84
          int depth = 0;
 
85
 
 
86
          puts("\n\nXML test: enter filename");
 
87
          fgets(filename,80,stdin);
 
88
          if (strlen(filename)==0)
 
89
                break;
 
90
                
 
91
          xmlFile = fopen(filename, "r");
 
92
          if (!xmlFile)
 
93
                break;
 
94
 
 
95
          do {
 
96
            size_t len = fread(buf, 1, sizeof(buf), xmlFile /*stdin*/);
 
97
            done = len < sizeof(buf);
 
98
            if (!parser.XML_Parse(buf, len, done)) {
 
99
              fprintf(stderr,
 
100
                      "%s at line %d\n",
 
101
                      XML_ErrorString(parser.XML_GetErrorCode()),
 
102
                      parser.XML_GetCurrentLineNumber());
 
103
              return 1;
 
104
            }
 
105
          } while (!done);
 
106
/*        return 0;  */
 
107
  }
 
108
 
 
109
  puts("\nfinished! - press CR to exit");
 
110
  fgets(filename,80,stdin);
 
111
 
 
112
 }