]> git.mxchange.org Git - simgear.git/blob - simgear/xml/easyxml.cxx
Oops, fixed ... :-)
[simgear.git] / simgear / xml / easyxml.cxx
1 // easyxml.cxx - implementation of EasyXML interfaces.
2
3 #ifdef HAVE_CONFIG_H
4 #  include <config.h>
5 #endif
6
7 #include <simgear/compiler.h>
8
9 #include "easyxml.hxx"
10 #include "xmlparse.h"
11
12 \f
13 ////////////////////////////////////////////////////////////////////////
14 // Implementation of XMLAttributes.
15 ////////////////////////////////////////////////////////////////////////
16
17 XMLAttributes::XMLAttributes ()
18 {
19 }
20
21 XMLAttributes::~XMLAttributes ()
22 {
23 }
24
25 int
26 XMLAttributes::findAttribute (const char * name) const
27 {
28   int s = size();
29   for (int i = 0; i < s; i++) {
30     if (strcmp(name, getName(i)) == 0)
31       return i;
32   }
33   return -1;
34 }
35
36 bool
37 XMLAttributes::hasAttribute (const char * name) const
38 {
39   return (findAttribute(name) != -1);
40 }
41
42 const char *
43 XMLAttributes::getValue (const char * name) const
44 {
45   int pos = findAttribute(name);
46   if (pos >= 0)
47     return getValue(pos);
48   else
49     return 0;
50 }
51
52 \f
53 ////////////////////////////////////////////////////////////////////////
54 // Implementation of XMLAttributesDefault.
55 ////////////////////////////////////////////////////////////////////////
56
57 XMLAttributesDefault::XMLAttributesDefault ()
58 {
59 }
60
61 XMLAttributesDefault::XMLAttributesDefault (const XMLAttributes &atts)
62 {
63   int s = atts.size();
64   for (int i = 0; i < s; i++)
65     addAttribute(atts.getName(i), atts.getValue(i));
66 }
67
68 XMLAttributesDefault::~XMLAttributesDefault ()
69 {
70 }
71
72 int
73 XMLAttributesDefault::size () const
74 {
75   return _atts.size() / 2;
76 }
77
78 const char *
79 XMLAttributesDefault::getName (int i) const
80 {
81   return _atts[i*2].c_str();
82 }
83
84 const char *
85 XMLAttributesDefault::getValue (int i) const
86 {
87   return _atts[i*2+1].c_str();
88 }
89
90 void
91 XMLAttributesDefault::addAttribute (const char * name, const char * value)
92 {
93   _atts.push_back(name);
94   _atts.push_back(value);
95 }
96
97 void
98 XMLAttributesDefault::setName (int i, const char * name)
99 {
100   _atts[i*2] = name;
101 }
102
103 void
104 XMLAttributesDefault::setValue (int i, const char * name)
105 {
106   _atts[i*2+1] = name;
107 }
108
109 void
110 XMLAttributesDefault::setValue (const char * name, const char * value)
111 {
112   int pos = findAttribute(name);
113   if (pos >= 0) {
114     setName(pos, name);
115     setValue(pos, value);
116   } else {
117     addAttribute(name, value);
118   }
119 }
120
121
122 \f
123 ////////////////////////////////////////////////////////////////////////
124 // Attribute list wrapper for Expat.
125 ////////////////////////////////////////////////////////////////////////
126
127 class ExpatAtts : public XMLAttributes
128 {
129 public:
130   ExpatAtts (const char ** atts) : _atts(atts) {}
131   
132   virtual int size () const;
133   virtual const char * getName (int i) const;
134   virtual const char * getValue (int i) const;
135   
136 private:
137   const char ** _atts;
138 };
139
140 int
141 ExpatAtts::size () const
142 {
143   int s = 0;
144   for (int i = 0; _atts[i] != 0; i += 2)
145     s++;
146   return s;
147 }
148
149 const char *
150 ExpatAtts::getName (int i) const
151 {
152   return _atts[i*2];
153 }
154
155 const char *
156 ExpatAtts::getValue (int i) const
157 {
158   return _atts[i*2+1];
159 }
160
161
162 \f
163 ////////////////////////////////////////////////////////////////////////
164 // Static callback functions for Expat.
165 ////////////////////////////////////////////////////////////////////////
166
167 #define VISITOR (*((XMLVisitor*)userData))
168
169 static void
170 start_element (void * userData, const char * name, const char ** atts)
171 {
172   VISITOR.startElement(name, ExpatAtts(atts));
173 }
174
175 static void
176 end_element (void * userData, const char * name)
177 {
178   VISITOR.endElement(name);
179 }
180
181 static void
182 character_data (void * userData, const char * s, int len)
183 {
184   VISITOR.data(s, len);
185 }
186
187 static void
188 processing_instruction (void * userData,
189                         const char * target,
190                         const char * data)
191 {
192   VISITOR.pi(target, data);
193 }
194
195 #undef VISITOR
196
197
198 \f
199 ////////////////////////////////////////////////////////////////////////
200 // Implementation of XMLReader.
201 ////////////////////////////////////////////////////////////////////////
202
203 bool
204 readXML (istream &input, XMLVisitor &visitor)
205 {
206   bool retval = true;
207
208   XML_Parser parser = XML_ParserCreate(0);
209   XML_SetUserData(parser, &visitor);
210   XML_SetElementHandler(parser, start_element, end_element);
211   XML_SetCharacterDataHandler(parser, character_data);
212   XML_SetProcessingInstructionHandler(parser, processing_instruction);
213
214   visitor.startXML();
215
216   char buf[16384];
217   while (!input.eof()) {
218
219     input.read(buf,16384);
220                                 // TODO: deal with bad stream.
221     if (!XML_Parse(parser, buf, input.gcount(), false)) {
222       visitor.error(XML_ErrorString(XML_GetErrorCode(parser)),
223                     XML_GetCurrentColumnNumber(parser),
224                     XML_GetCurrentLineNumber(parser));
225       retval = false;
226     }
227   }
228
229   if (retval)
230     visitor.endXML();
231
232   XML_ParserFree(parser);
233
234   return retval;
235 }
236
237 // end of easyxml.cxx