1 | /*
|
---|
2 | www.sourceforge.net/projects/tinyxml
|
---|
3 | Original code (2.0 and earlier )copyright (c) 2000-2002 Lee Thomason (www.grinninglizard.com)
|
---|
4 |
|
---|
5 | This software is provided 'as-is', without any express or implied
|
---|
6 | warranty. In no event will the authors be held liable for any
|
---|
7 | damages arising from the use of this software.
|
---|
8 |
|
---|
9 | Permission is granted to anyone to use this software for any
|
---|
10 | purpose, including commercial applications, and to alter it and
|
---|
11 | redistribute it freely, subject to the following restrictions:
|
---|
12 |
|
---|
13 | 1. The origin of this software must not be misrepresented; you must
|
---|
14 | not claim that you wrote the original software. If you use this
|
---|
15 | software in a product, an acknowledgment in the product documentation
|
---|
16 | would be appreciated but is not required.
|
---|
17 |
|
---|
18 | 2. Altered source versions must be plainly marked as such, and
|
---|
19 | must not be misrepresented as being the original software.
|
---|
20 |
|
---|
21 | 3. This notice may not be removed or altered from any source
|
---|
22 | distribution.
|
---|
23 | */
|
---|
24 |
|
---|
25 |
|
---|
26 | #ifndef TINYXML_INCLUDED
|
---|
27 | #define TINYXML_INCLUDED
|
---|
28 |
|
---|
29 | #ifdef _MSC_VER
|
---|
30 | #pragma warning( disable : 4530 )
|
---|
31 | #pragma warning( disable : 4786 )
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #include <ctype.h>
|
---|
35 | #include <stdio.h>
|
---|
36 | #include <stdlib.h>
|
---|
37 | #include <string.h>
|
---|
38 | #include <assert.h>
|
---|
39 |
|
---|
40 | // Help out windows:
|
---|
41 | #if defined( _DEBUG ) && !defined( DEBUG )
|
---|
42 | #define DEBUG
|
---|
43 | #endif
|
---|
44 |
|
---|
45 | #if defined( DEBUG ) && defined( _MSC_VER )
|
---|
46 | #define NOMINMAX
|
---|
47 | #include <windows.h>
|
---|
48 | #define TIXML_LOG OutputDebugString
|
---|
49 | #else
|
---|
50 | #define TIXML_LOG printf
|
---|
51 | #endif
|
---|
52 |
|
---|
53 |
|
---|
54 | #ifdef TIXML_USE_STL
|
---|
55 | #include <string>
|
---|
56 | #include <iostream>
|
---|
57 | #define TIXML_STRING std::string
|
---|
58 | #define TIXML_ISTREAM std::istream
|
---|
59 | #define TIXML_OSTREAM std::ostream
|
---|
60 | #else
|
---|
61 | #include "tinystr.h"
|
---|
62 | #define TIXML_STRING TiXmlString
|
---|
63 | #define TIXML_OSTREAM TiXmlOutStream
|
---|
64 | #endif
|
---|
65 |
|
---|
66 | class TiXmlDocument;
|
---|
67 | class TiXmlElement;
|
---|
68 | class TiXmlComment;
|
---|
69 | class TiXmlUnknown;
|
---|
70 | class TiXmlAttribute;
|
---|
71 | class TiXmlText;
|
---|
72 | class TiXmlDeclaration;
|
---|
73 |
|
---|
74 |
|
---|
75 | /** TiXmlBase is a base class for every class in TinyXml.
|
---|
76 | It does little except to establish that TinyXml classes
|
---|
77 | can be printed and provide some utility functions.
|
---|
78 |
|
---|
79 | In XML, the document and elements can contain
|
---|
80 | other elements and other types of nodes.
|
---|
81 |
|
---|
82 | @verbatim
|
---|
83 | A Document can contain: Element (container or leaf)
|
---|
84 | Comment (leaf)
|
---|
85 | Unknown (leaf)
|
---|
86 | Declaration( leaf )
|
---|
87 |
|
---|
88 | An Element can contain: Element (container or leaf)
|
---|
89 | Text (leaf)
|
---|
90 | Attributes (not on tree)
|
---|
91 | Comment (leaf)
|
---|
92 | Unknown (leaf)
|
---|
93 |
|
---|
94 | A Decleration contains: Attributes (not on tree)
|
---|
95 | @endverbatim
|
---|
96 | */
|
---|
97 | class TiXmlBase
|
---|
98 | {
|
---|
99 | friend class TiXmlNode;
|
---|
100 | friend class TiXmlElement;
|
---|
101 | friend class TiXmlDocument;
|
---|
102 |
|
---|
103 | public:
|
---|
104 | TiXmlBase() {}
|
---|
105 | virtual ~TiXmlBase() {}
|
---|
106 |
|
---|
107 | /** All TinyXml classes can print themselves to a filestream.
|
---|
108 | This is a formatted print, and will insert tabs and newlines.
|
---|
109 |
|
---|
110 | (For an unformatted stream, use the << operator.)
|
---|
111 | */
|
---|
112 | virtual void Print( FILE* cfile, int depth ) const = 0;
|
---|
113 |
|
---|
114 | /** The world does not agree on whether white space should be kept or
|
---|
115 | not. In order to make everyone happy, these global, static functions
|
---|
116 | are provided to set whether or not TinyXml will condense all white space
|
---|
117 | into a single space or not. The default is to condense. Note changing these
|
---|
118 | values is not thread safe.
|
---|
119 | */
|
---|
120 | static void SetCondenseWhiteSpace( bool condense ) { condenseWhiteSpace = condense; }
|
---|
121 |
|
---|
122 | /// Return the current white space setting.
|
---|
123 | static bool IsWhiteSpaceCondensed() { return condenseWhiteSpace; }
|
---|
124 |
|
---|
125 | protected:
|
---|
126 |
|
---|
127 | static const char* SkipWhiteSpace( const char* );
|
---|
128 | inline static bool IsWhiteSpace( int c ) { return ( isspace( c ) || c == '\n' || c == '\r' ); }
|
---|
129 |
|
---|
130 | virtual void StreamOut (TIXML_OSTREAM *) const = 0;
|
---|
131 |
|
---|
132 | #ifdef TIXML_USE_STL
|
---|
133 | static bool StreamWhiteSpace( TIXML_ISTREAM * in, TIXML_STRING * tag );
|
---|
134 | static bool StreamTo( TIXML_ISTREAM * in, int character, TIXML_STRING * tag );
|
---|
135 | #endif
|
---|
136 |
|
---|
137 | /* Reads an XML name into the string provided. Returns
|
---|
138 | a pointer just past the last character of the name,
|
---|
139 | or 0 if the function has an error.
|
---|
140 | */
|
---|
141 | static const char* ReadName( const char* p, TIXML_STRING* name );
|
---|
142 |
|
---|
143 | /* Reads text. Returns a pointer past the given end tag.
|
---|
144 | Wickedly complex options, but it keeps the (sensitive) code in one place.
|
---|
145 | */
|
---|
146 | static const char* ReadText( const char* in, // where to start
|
---|
147 | TIXML_STRING* text, // the string read
|
---|
148 | bool ignoreWhiteSpace, // whether to keep the white space
|
---|
149 | const char* endTag, // what ends this text
|
---|
150 | bool ignoreCase ); // whether to ignore case in the end tag
|
---|
151 | virtual const char* Parse( const char* p ) = 0;
|
---|
152 |
|
---|
153 | // If an entity has been found, transform it into a character.
|
---|
154 | static const char* GetEntity( const char* in, char* value );
|
---|
155 |
|
---|
156 | // Get a character, while interpreting entities.
|
---|
157 | inline static const char* GetChar( const char* p, char* value )
|
---|
158 | {
|
---|
159 | assert( p );
|
---|
160 | if ( *p == '&' )
|
---|
161 | {
|
---|
162 | return GetEntity( p, value );
|
---|
163 | }
|
---|
164 | else
|
---|
165 | {
|
---|
166 | *value = *p;
|
---|
167 | return p+1;
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | // Puts a string to a stream, expanding entities as it goes.
|
---|
172 | // Note this should not contian the '<', '>', etc, or they will be transformed into entities!
|
---|
173 | static void PutString( const TIXML_STRING& str, TIXML_OSTREAM* out );
|
---|
174 |
|
---|
175 | static void PutString( const TIXML_STRING& str, TIXML_STRING* out );
|
---|
176 |
|
---|
177 | // Return true if the next characters in the stream are any of the endTag sequences.
|
---|
178 | bool static StringEqual( const char* p,
|
---|
179 | const char* endTag,
|
---|
180 | bool ignoreCase );
|
---|
181 |
|
---|
182 |
|
---|
183 | enum
|
---|
184 | {
|
---|
185 | TIXML_NO_ERROR = 0,
|
---|
186 | TIXML_ERROR,
|
---|
187 | TIXML_ERROR_OPENING_FILE,
|
---|
188 | TIXML_ERROR_OUT_OF_MEMORY,
|
---|
189 | TIXML_ERROR_PARSING_ELEMENT,
|
---|
190 | TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME,
|
---|
191 | TIXML_ERROR_READING_ELEMENT_VALUE,
|
---|
192 | TIXML_ERROR_READING_ATTRIBUTES,
|
---|
193 | TIXML_ERROR_PARSING_EMPTY,
|
---|
194 | TIXML_ERROR_READING_END_TAG,
|
---|
195 | TIXML_ERROR_PARSING_UNKNOWN,
|
---|
196 | TIXML_ERROR_PARSING_COMMENT,
|
---|
197 | TIXML_ERROR_PARSING_DECLARATION,
|
---|
198 | TIXML_ERROR_DOCUMENT_EMPTY,
|
---|
199 |
|
---|
200 | TIXML_ERROR_STRING_COUNT
|
---|
201 | };
|
---|
202 | static const char* errorString[ TIXML_ERROR_STRING_COUNT ];
|
---|
203 |
|
---|
204 | private:
|
---|
205 | struct Entity
|
---|
206 | {
|
---|
207 | const char* str;
|
---|
208 | unsigned int strLength;
|
---|
209 | char chr;
|
---|
210 | };
|
---|
211 | enum
|
---|
212 | {
|
---|
213 | NUM_ENTITY = 5,
|
---|
214 | MAX_ENTITY_LENGTH = 6
|
---|
215 |
|
---|
216 | };
|
---|
217 | static Entity entity[ NUM_ENTITY ];
|
---|
218 | static bool condenseWhiteSpace;
|
---|
219 | };
|
---|
220 |
|
---|
221 |
|
---|
222 | /** The parent class for everything in the Document Object Model.
|
---|
223 | (Except for attributes, which are contained in elements.)
|
---|
224 | Nodes have siblings, a parent, and children. A node can be
|
---|
225 | in a document, or stand on its own. The type of a TiXmlNode
|
---|
226 | can be queried, and it can be cast to its more defined type.
|
---|
227 | */
|
---|
228 | class TiXmlNode : public TiXmlBase
|
---|
229 | {
|
---|
230 | friend class TiXmlDocument;
|
---|
231 | friend class TiXmlElement;
|
---|
232 |
|
---|
233 | public:
|
---|
234 | #ifdef TIXML_USE_STL
|
---|
235 |
|
---|
236 | /** An input stream operator, for every class. Tolerant of newlines and
|
---|
237 | formatting, but doesn't expect them.
|
---|
238 | */
|
---|
239 | friend std::istream& operator >> (std::istream& in, TiXmlNode& base);
|
---|
240 |
|
---|
241 | /** An output stream operator, for every class. Note that this outputs
|
---|
242 | without any newlines or formatting, as opposed to Print(), which
|
---|
243 | includes tabs and new lines.
|
---|
244 |
|
---|
245 | The operator<< and operator>> are not completely symmetric. Writing
|
---|
246 | a node to a stream is very well defined. You'll get a nice stream
|
---|
247 | of output, without any extra whitespace or newlines.
|
---|
248 |
|
---|
249 | But reading is not as well defined. (As it always is.) If you create
|
---|
250 | a TiXmlElement (for example) and read that from an input stream,
|
---|
251 | the text needs to define an element or junk will result. This is
|
---|
252 | true of all input streams, but it's worth keeping in mind.
|
---|
253 |
|
---|
254 | A TiXmlDocument will read nodes until it reads a root element.
|
---|
255 | */
|
---|
256 | friend std::ostream & operator<< (std::ostream& out, const TiXmlNode& base);
|
---|
257 |
|
---|
258 | #else
|
---|
259 | // Used internally, not part of the public API.
|
---|
260 | friend TIXML_OSTREAM& operator<< (TIXML_OSTREAM& out, const TiXmlNode& base);
|
---|
261 | #endif
|
---|
262 |
|
---|
263 | /** The types of XML nodes supported by TinyXml. (All the
|
---|
264 | unsupported types are picked up by UNKNOWN.)
|
---|
265 | */
|
---|
266 | enum NodeType
|
---|
267 | {
|
---|
268 | DOCUMENT,
|
---|
269 | ELEMENT,
|
---|
270 | COMMENT,
|
---|
271 | UNKNOWN,
|
---|
272 | TEXT,
|
---|
273 | DECLARATION,
|
---|
274 | TYPECOUNT
|
---|
275 | };
|
---|
276 |
|
---|
277 | virtual ~TiXmlNode();
|
---|
278 |
|
---|
279 | /** The meaning of 'value' changes for the specific type of
|
---|
280 | TiXmlNode.
|
---|
281 | @verbatim
|
---|
282 | Document: filename of the xml file
|
---|
283 | Element: name of the element
|
---|
284 | Comment: the comment text
|
---|
285 | Unknown: the tag contents
|
---|
286 | Text: the text string
|
---|
287 | @endverbatim
|
---|
288 |
|
---|
289 | The subclasses will wrap this function.
|
---|
290 | */
|
---|
291 | const char * Value () const { return value.c_str (); }
|
---|
292 |
|
---|
293 | /** Changes the value of the node. Defined as:
|
---|
294 | @verbatim
|
---|
295 | Document: filename of the xml file
|
---|
296 | Element: name of the element
|
---|
297 | Comment: the comment text
|
---|
298 | Unknown: the tag contents
|
---|
299 | Text: the text string
|
---|
300 | @endverbatim
|
---|
301 | */
|
---|
302 | void SetValue (const char * _value) { value = _value;}
|
---|
303 |
|
---|
304 | #ifdef TIXML_USE_STL
|
---|
305 | /// STL std::string form.
|
---|
306 | void SetValue( const std::string& value )
|
---|
307 | {
|
---|
308 | SetValue( value.c_str() );
|
---|
309 | }
|
---|
310 | #endif
|
---|
311 |
|
---|
312 | /// Delete all the children of this node. Does not affect 'this'.
|
---|
313 | void Clear();
|
---|
314 |
|
---|
315 | /// One step up the DOM.
|
---|
316 | TiXmlNode* Parent() const { return parent; }
|
---|
317 |
|
---|
318 | TiXmlNode* FirstChild() const { return firstChild; } ///< The first child of this node. Will be null if there are no children.
|
---|
319 | TiXmlNode* FirstChild( const char * value ) const; ///< The first child of this node with the matching 'value'. Will be null if none found.
|
---|
320 |
|
---|
321 | TiXmlNode* LastChild() const { return lastChild; } /// The last child of this node. Will be null if there are no children.
|
---|
322 | TiXmlNode* LastChild( const char * value ) const; /// The last child of this node matching 'value'. Will be null if there are no children.
|
---|
323 |
|
---|
324 | #ifdef TIXML_USE_STL
|
---|
325 | TiXmlNode* FirstChild( const std::string& value ) const { return FirstChild (value.c_str ()); } ///< STL std::string form.
|
---|
326 | TiXmlNode* LastChild( const std::string& value ) const { return LastChild (value.c_str ()); } ///< STL std::string form.
|
---|
327 | #endif
|
---|
328 |
|
---|
329 | /** An alternate way to walk the children of a node.
|
---|
330 | One way to iterate over nodes is:
|
---|
331 | @verbatim
|
---|
332 | for( child = parent->FirstChild(); child; child = child->NextSibling() )
|
---|
333 | @endverbatim
|
---|
334 |
|
---|
335 | IterateChildren does the same thing with the syntax:
|
---|
336 | @verbatim
|
---|
337 | child = 0;
|
---|
338 | while( child = parent->IterateChildren( child ) )
|
---|
339 | @endverbatim
|
---|
340 |
|
---|
341 | IterateChildren takes the previous child as input and finds
|
---|
342 | the next one. If the previous child is null, it returns the
|
---|
343 | first. IterateChildren will return null when done.
|
---|
344 | */
|
---|
345 | TiXmlNode* IterateChildren( TiXmlNode* previous ) const;
|
---|
346 |
|
---|
347 | /// This flavor of IterateChildren searches for children with a particular 'value'
|
---|
348 | TiXmlNode* IterateChildren( const char * value, TiXmlNode* previous ) const;
|
---|
349 |
|
---|
350 | #ifdef TIXML_USE_STL
|
---|
351 | TiXmlNode* IterateChildren( const std::string& value, TiXmlNode* previous ) const { return IterateChildren (value.c_str (), previous); } ///< STL std::string form.
|
---|
352 | #endif
|
---|
353 |
|
---|
354 | /** Add a new node related to this. Adds a child past the LastChild.
|
---|
355 | Returns a pointer to the new object or NULL if an error occured.
|
---|
356 | */
|
---|
357 | TiXmlNode* InsertEndChild( const TiXmlNode& addThis );
|
---|
358 |
|
---|
359 | /** Add a new node related to this. Adds a child before the specified child.
|
---|
360 | Returns a pointer to the new object or NULL if an error occured.
|
---|
361 | */
|
---|
362 | TiXmlNode* InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis );
|
---|
363 |
|
---|
364 | /** Add a new node related to this. Adds a child after the specified child.
|
---|
365 | Returns a pointer to the new object or NULL if an error occured.
|
---|
366 | */
|
---|
367 | TiXmlNode* InsertAfterChild( TiXmlNode* afterThis, const TiXmlNode& addThis );
|
---|
368 |
|
---|
369 | /** Replace a child of this node.
|
---|
370 | Returns a pointer to the new object or NULL if an error occured.
|
---|
371 | */
|
---|
372 | TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis );
|
---|
373 |
|
---|
374 | /// Delete a child of this node.
|
---|
375 | bool RemoveChild( TiXmlNode* removeThis );
|
---|
376 |
|
---|
377 | /// Navigate to a sibling node.
|
---|
378 | TiXmlNode* PreviousSibling() const { return prev; }
|
---|
379 |
|
---|
380 | /// Navigate to a sibling node.
|
---|
381 | TiXmlNode* PreviousSibling( const char * ) const;
|
---|
382 |
|
---|
383 | #ifdef TIXML_USE_STL
|
---|
384 | TiXmlNode* PreviousSibling( const std::string& value ) const { return PreviousSibling (value.c_str ()); } ///< STL std::string form.
|
---|
385 | TiXmlNode* NextSibling( const std::string& value) const { return NextSibling (value.c_str ()); } ///< STL std::string form.
|
---|
386 | #endif
|
---|
387 |
|
---|
388 | /// Navigate to a sibling node.
|
---|
389 | TiXmlNode* NextSibling() const { return next; }
|
---|
390 |
|
---|
391 | /// Navigate to a sibling node with the given 'value'.
|
---|
392 | TiXmlNode* NextSibling( const char * ) const;
|
---|
393 |
|
---|
394 | /** Convenience function to get through elements.
|
---|
395 | Calls NextSibling and ToElement. Will skip all non-Element
|
---|
396 | nodes. Returns 0 if there is not another element.
|
---|
397 | */
|
---|
398 | TiXmlElement* NextSiblingElement() const;
|
---|
399 |
|
---|
400 | /** Convenience function to get through elements.
|
---|
401 | Calls NextSibling and ToElement. Will skip all non-Element
|
---|
402 | nodes. Returns 0 if there is not another element.
|
---|
403 | */
|
---|
404 | TiXmlElement* NextSiblingElement( const char * ) const;
|
---|
405 |
|
---|
406 | #ifdef TIXML_USE_STL
|
---|
407 | TiXmlElement* NextSiblingElement( const std::string& value) const { return NextSiblingElement (value.c_str ()); } ///< STL std::string form.
|
---|
408 | #endif
|
---|
409 |
|
---|
410 | /// Convenience function to get through elements.
|
---|
411 | TiXmlElement* FirstChildElement() const;
|
---|
412 |
|
---|
413 | /// Convenience function to get through elements.
|
---|
414 | TiXmlElement* FirstChildElement( const char * value ) const;
|
---|
415 |
|
---|
416 | #ifdef TIXML_USE_STL
|
---|
417 | TiXmlElement* FirstChildElement( const std::string& value ) const { return FirstChildElement (value.c_str ()); } ///< STL std::string form.
|
---|
418 | #endif
|
---|
419 |
|
---|
420 | /// Query the type (as an enumerated value, above) of this node.
|
---|
421 | virtual int Type() const { return type; }
|
---|
422 |
|
---|
423 | /** Return a pointer to the Document this node lives in.
|
---|
424 | Returns null if not in a document.
|
---|
425 | */
|
---|
426 | TiXmlDocument* GetDocument() const;
|
---|
427 |
|
---|
428 | /// Returns true if this node has no children.
|
---|
429 | bool NoChildren() const { return !firstChild; }
|
---|
430 |
|
---|
431 | TiXmlDocument* ToDocument() const { return ( this && type == DOCUMENT ) ? (TiXmlDocument*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
|
---|
432 | TiXmlElement* ToElement() const { return ( this && type == ELEMENT ) ? (TiXmlElement*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
|
---|
433 | TiXmlComment* ToComment() const { return ( this && type == COMMENT ) ? (TiXmlComment*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
|
---|
434 | TiXmlUnknown* ToUnknown() const { return ( this && type == UNKNOWN ) ? (TiXmlUnknown*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
|
---|
435 | TiXmlText* ToText() const { return ( this && type == TEXT ) ? (TiXmlText*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
|
---|
436 | TiXmlDeclaration* ToDeclaration() const { return ( this && type == DECLARATION ) ? (TiXmlDeclaration*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
|
---|
437 |
|
---|
438 | virtual TiXmlNode* Clone() const = 0;
|
---|
439 |
|
---|
440 | void SetUserData( void* user ) { userData = user; }
|
---|
441 | void* GetUserData() { return userData; }
|
---|
442 |
|
---|
443 | protected:
|
---|
444 | TiXmlNode( NodeType type );
|
---|
445 |
|
---|
446 | #ifdef TIXML_USE_STL
|
---|
447 | // The real work of the input operator.
|
---|
448 | virtual void StreamIn( TIXML_ISTREAM* in, TIXML_STRING* tag ) = 0;
|
---|
449 | #endif
|
---|
450 |
|
---|
451 | // The node is passed in by ownership. This object will delete it.
|
---|
452 | TiXmlNode* LinkEndChild( TiXmlNode* addThis );
|
---|
453 |
|
---|
454 | // Figure out what is at *p, and parse it. Returns null if it is not an xml node.
|
---|
455 | TiXmlNode* Identify( const char* start );
|
---|
456 | void CopyToClone( TiXmlNode* target ) const { target->SetValue (value.c_str() );
|
---|
457 | target->userData = userData; }
|
---|
458 |
|
---|
459 | // Internal Value function returning a TIXML_STRING
|
---|
460 | TIXML_STRING SValue() const { return value ; }
|
---|
461 |
|
---|
462 | TiXmlNode* parent;
|
---|
463 | NodeType type;
|
---|
464 |
|
---|
465 | TiXmlNode* firstChild;
|
---|
466 | TiXmlNode* lastChild;
|
---|
467 |
|
---|
468 | TIXML_STRING value;
|
---|
469 |
|
---|
470 | TiXmlNode* prev;
|
---|
471 | TiXmlNode* next;
|
---|
472 | void* userData;
|
---|
473 | };
|
---|
474 |
|
---|
475 |
|
---|
476 | /** An attribute is a name-value pair. Elements have an arbitrary
|
---|
477 | number of attributes, each with a unique name.
|
---|
478 |
|
---|
479 | @note The attributes are not TiXmlNodes, since they are not
|
---|
480 | part of the tinyXML document object model. There are other
|
---|
481 | suggested ways to look at this problem.
|
---|
482 |
|
---|
483 | @note Attributes have a parent
|
---|
484 | */
|
---|
485 | class TiXmlAttribute : public TiXmlBase
|
---|
486 | {
|
---|
487 | friend class TiXmlAttributeSet;
|
---|
488 |
|
---|
489 | public:
|
---|
490 | /// Construct an empty attribute.
|
---|
491 | TiXmlAttribute() : prev( 0 ), next( 0 ) {}
|
---|
492 |
|
---|
493 | #ifdef TIXML_USE_STL
|
---|
494 | /// std::string constructor.
|
---|
495 | TiXmlAttribute( const std::string& _name, const std::string& _value )
|
---|
496 | {
|
---|
497 | name = _name;
|
---|
498 | value = _value;
|
---|
499 | }
|
---|
500 | #endif
|
---|
501 |
|
---|
502 | /// Construct an attribute with a name and value.
|
---|
503 | TiXmlAttribute( const char * _name, const char * _value ): name( _name ), value( _value ), prev( 0 ), next( 0 ) {}
|
---|
504 | const char* Name() const { return name.c_str (); } ///< Return the name of this attribute.
|
---|
505 | const char* Value() const { return value.c_str (); } ///< Return the value of this attribute.
|
---|
506 | const int IntValue() const; ///< Return the value of this attribute, converted to an integer.
|
---|
507 | const double DoubleValue() const; ///< Return the value of this attribute, converted to a double.
|
---|
508 |
|
---|
509 | void SetName( const char* _name ) { name = _name; } ///< Set the name of this attribute.
|
---|
510 | void SetValue( const char* _value ) { value = _value; } ///< Set the value.
|
---|
511 |
|
---|
512 | void SetIntValue( int value ); ///< Set the value from an integer.
|
---|
513 | void SetDoubleValue( double value ); ///< Set the value from a double.
|
---|
514 |
|
---|
515 | #ifdef TIXML_USE_STL
|
---|
516 | /// STL std::string form.
|
---|
517 | void SetName( const std::string& _name )
|
---|
518 | {
|
---|
519 | SetName(_name.c_str() );
|
---|
520 | }
|
---|
521 | /// STL std::string form.
|
---|
522 | void SetValue( const std::string& _value )
|
---|
523 | {
|
---|
524 | SetValue( value.c_str() );
|
---|
525 | }
|
---|
526 | #endif
|
---|
527 |
|
---|
528 | /// Get the next sibling attribute in the DOM. Returns null at end.
|
---|
529 | TiXmlAttribute* Next() const;
|
---|
530 | /// Get the previous sibling attribute in the DOM. Returns null at beginning.
|
---|
531 | TiXmlAttribute* Previous() const;
|
---|
532 |
|
---|
533 | bool operator==( const TiXmlAttribute& rhs ) const { return rhs.name == name; }
|
---|
534 | bool operator<( const TiXmlAttribute& rhs ) const { return name < rhs.name; }
|
---|
535 | bool operator>( const TiXmlAttribute& rhs ) const { return name > rhs.name; }
|
---|
536 |
|
---|
537 | /* [internal use]
|
---|
538 | Attribtue parsing starts: first letter of the name
|
---|
539 | returns: the next char after the value end quote
|
---|
540 | */
|
---|
541 | virtual const char* Parse( const char* p );
|
---|
542 |
|
---|
543 | // [internal use]
|
---|
544 | virtual void Print( FILE* cfile, int depth ) const;
|
---|
545 |
|
---|
546 | virtual void StreamOut( TIXML_OSTREAM * out ) const;
|
---|
547 | // [internal use]
|
---|
548 | // Set the document pointer so the attribute can report errors.
|
---|
549 | void SetDocument( TiXmlDocument* doc ) { document = doc; }
|
---|
550 |
|
---|
551 | private:
|
---|
552 | TiXmlDocument* document; // A pointer back to a document, for error reporting.
|
---|
553 | TIXML_STRING name;
|
---|
554 | TIXML_STRING value;
|
---|
555 | TiXmlAttribute* prev;
|
---|
556 | TiXmlAttribute* next;
|
---|
557 | };
|
---|
558 |
|
---|
559 |
|
---|
560 | /* A class used to manage a group of attributes.
|
---|
561 | It is only used internally, both by the ELEMENT and the DECLARATION.
|
---|
562 |
|
---|
563 | The set can be changed transparent to the Element and Declaration
|
---|
564 | classes that use it, but NOT transparent to the Attribute
|
---|
565 | which has to implement a next() and previous() method. Which makes
|
---|
566 | it a bit problematic and prevents the use of STL.
|
---|
567 |
|
---|
568 | This version is implemented with circular lists because:
|
---|
569 | - I like circular lists
|
---|
570 | - it demonstrates some independence from the (typical) doubly linked list.
|
---|
571 | */
|
---|
572 | class TiXmlAttributeSet
|
---|
573 | {
|
---|
574 | public:
|
---|
575 | TiXmlAttributeSet();
|
---|
576 | ~TiXmlAttributeSet();
|
---|
577 |
|
---|
578 | void Add( TiXmlAttribute* attribute );
|
---|
579 | void Remove( TiXmlAttribute* attribute );
|
---|
580 |
|
---|
581 | TiXmlAttribute* First() const { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
|
---|
582 | TiXmlAttribute* Last() const { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
|
---|
583 | TiXmlAttribute* Find( const char * name ) const;
|
---|
584 |
|
---|
585 | private:
|
---|
586 | TiXmlAttribute sentinel;
|
---|
587 | };
|
---|
588 |
|
---|
589 |
|
---|
590 | /** The element is a container class. It has a value, the element name,
|
---|
591 | and can contain other elements, text, comments, and unknowns.
|
---|
592 | Elements also contain an arbitrary number of attributes.
|
---|
593 | */
|
---|
594 | class TiXmlElement : public TiXmlNode
|
---|
595 | {
|
---|
596 | public:
|
---|
597 | /// Construct an element.
|
---|
598 | TiXmlElement (const char * in_value);
|
---|
599 |
|
---|
600 | #ifdef TIXML_USE_STL
|
---|
601 | /// std::string constructor.
|
---|
602 | TiXmlElement( const std::string& _value ) : TiXmlNode( TiXmlNode::ELEMENT )
|
---|
603 | {
|
---|
604 | firstChild = lastChild = 0;
|
---|
605 | value = _value;
|
---|
606 | }
|
---|
607 | #endif
|
---|
608 |
|
---|
609 | virtual ~TiXmlElement();
|
---|
610 |
|
---|
611 | // OGRE CHANGE
|
---|
612 | // Resolve ambiguity of std::string / const char* conversion of Ogre::String
|
---|
613 | #ifndef TIXML_USE_STL
|
---|
614 | // OGRE CHANGE
|
---|
615 | /** Given an attribute name, attribute returns the value
|
---|
616 | for the attribute of that name, or null if none exists.
|
---|
617 | */
|
---|
618 | const char* Attribute( const char* name ) const;
|
---|
619 |
|
---|
620 | /** Given an attribute name, attribute returns the value
|
---|
621 | for the attribute of that name, or null if none exists.
|
---|
622 | If the attribute exists and can be converted to an integer,
|
---|
623 | the integer value will be put in the return 'i', if 'i'
|
---|
624 | is non-null.
|
---|
625 | */
|
---|
626 | const char* Attribute( const char* name, int* i ) const;
|
---|
627 |
|
---|
628 | /** Sets an attribute of name to a given value. The attribute
|
---|
629 | will be created if it does not exist, or changed if it does.
|
---|
630 | */
|
---|
631 | void SetAttribute( const char* name, const char * value );
|
---|
632 | // OGRE CHANGE
|
---|
633 | #endif
|
---|
634 | // OGRE CHANGE
|
---|
635 | #ifdef TIXML_USE_STL
|
---|
636 | const char* Attribute( const std::string& name ) const;
|
---|
637 | const char* Attribute( const std::string& name, int* i ) const;
|
---|
638 |
|
---|
639 | /// STL std::string form.
|
---|
640 | void SetAttribute( const std::string& name, const std::string& value );
|
---|
641 | ///< STL std::string form.
|
---|
642 | void SetAttribute( const std::string& name, int value );
|
---|
643 | #endif
|
---|
644 |
|
---|
645 | /** Sets an attribute of name to a given value. The attribute
|
---|
646 | will be created if it does not exist, or changed if it does.
|
---|
647 | */
|
---|
648 | void SetAttribute( const char * name, int value );
|
---|
649 |
|
---|
650 | /** Deletes an attribute with the given name.
|
---|
651 | */
|
---|
652 | void RemoveAttribute( const char * name );
|
---|
653 | #ifdef TIXML_USE_STL
|
---|
654 | void RemoveAttribute( const std::string& name ) { RemoveAttribute (name.c_str ()); } ///< STL std::string form.
|
---|
655 | #endif
|
---|
656 |
|
---|
657 | TiXmlAttribute* FirstAttribute() const { return attributeSet.First(); } ///< Access the first attribute in this element.
|
---|
658 | TiXmlAttribute* LastAttribute() const { return attributeSet.Last(); } ///< Access the last attribute in this element.
|
---|
659 |
|
---|
660 | // [internal use] Creates a new Element and returs it.
|
---|
661 | virtual TiXmlNode* Clone() const;
|
---|
662 | // [internal use]
|
---|
663 |
|
---|
664 | virtual void Print( FILE* cfile, int depth ) const;
|
---|
665 |
|
---|
666 | protected:
|
---|
667 |
|
---|
668 | // Used to be public [internal use]
|
---|
669 | #ifdef TIXML_USE_STL
|
---|
670 | virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
|
---|
671 | #endif
|
---|
672 | virtual void StreamOut( TIXML_OSTREAM * out ) const;
|
---|
673 |
|
---|
674 | /* [internal use]
|
---|
675 | Attribtue parsing starts: next char past '<'
|
---|
676 | returns: next char past '>'
|
---|
677 | */
|
---|
678 | virtual const char* Parse( const char* p );
|
---|
679 |
|
---|
680 | /* [internal use]
|
---|
681 | Reads the "value" of the element -- another element, or text.
|
---|
682 | This should terminate with the current end tag.
|
---|
683 | */
|
---|
684 | const char* ReadValue( const char* in );
|
---|
685 |
|
---|
686 | private:
|
---|
687 | TiXmlAttributeSet attributeSet;
|
---|
688 | };
|
---|
689 |
|
---|
690 |
|
---|
691 | /** An XML comment.
|
---|
692 | */
|
---|
693 | class TiXmlComment : public TiXmlNode
|
---|
694 | {
|
---|
695 | public:
|
---|
696 | /// Constructs an empty comment.
|
---|
697 | TiXmlComment() : TiXmlNode( TiXmlNode::COMMENT ) {}
|
---|
698 | virtual ~TiXmlComment() {}
|
---|
699 |
|
---|
700 | // [internal use] Creates a new Element and returs it.
|
---|
701 | virtual TiXmlNode* Clone() const;
|
---|
702 | // [internal use]
|
---|
703 | virtual void Print( FILE* cfile, int depth ) const;
|
---|
704 | protected:
|
---|
705 | // used to be public
|
---|
706 | #ifdef TIXML_USE_STL
|
---|
707 | virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
|
---|
708 | #endif
|
---|
709 | virtual void StreamOut( TIXML_OSTREAM * out ) const;
|
---|
710 | /* [internal use]
|
---|
711 | Attribtue parsing starts: at the ! of the !--
|
---|
712 | returns: next char past '>'
|
---|
713 | */
|
---|
714 | virtual const char* Parse( const char* p );
|
---|
715 | };
|
---|
716 |
|
---|
717 |
|
---|
718 | /** XML text. Contained in an element.
|
---|
719 | */
|
---|
720 | class TiXmlText : public TiXmlNode
|
---|
721 | {
|
---|
722 | friend class TiXmlElement;
|
---|
723 | public:
|
---|
724 | /// Constructor.
|
---|
725 | TiXmlText (const char * initValue) : TiXmlNode (TiXmlNode::TEXT)
|
---|
726 | {
|
---|
727 | SetValue( initValue );
|
---|
728 | }
|
---|
729 | virtual ~TiXmlText() {}
|
---|
730 |
|
---|
731 | #ifdef TIXML_USE_STL
|
---|
732 | /// Constructor.
|
---|
733 | TiXmlText( const std::string& initValue ) : TiXmlNode (TiXmlNode::TEXT)
|
---|
734 | {
|
---|
735 | SetValue( initValue );
|
---|
736 | }
|
---|
737 | #endif
|
---|
738 |
|
---|
739 | protected :
|
---|
740 | // [internal use] Creates a new Element and returns it.
|
---|
741 | virtual TiXmlNode* Clone() const;
|
---|
742 | // [internal use]
|
---|
743 | virtual void Print( FILE* cfile, int depth ) const;
|
---|
744 | virtual void StreamOut ( TIXML_OSTREAM * out ) const;
|
---|
745 | // [internal use]
|
---|
746 | bool Blank() const; // returns true if all white space and new lines
|
---|
747 | /* [internal use]
|
---|
748 | Attribtue parsing starts: First char of the text
|
---|
749 | returns: next char past '>'
|
---|
750 | */
|
---|
751 | virtual const char* Parse( const char* p );
|
---|
752 | // [internal use]
|
---|
753 | #ifdef TIXML_USE_STL
|
---|
754 | virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
|
---|
755 | #endif
|
---|
756 | };
|
---|
757 |
|
---|
758 |
|
---|
759 | /** In correct XML the declaration is the first entry in the file.
|
---|
760 | @verbatim
|
---|
761 | <?xml version="1.0" standalone="yes"?>
|
---|
762 | @endverbatim
|
---|
763 |
|
---|
764 | TinyXml will happily read or write files without a declaration,
|
---|
765 | however. There are 3 possible attributes to the declaration:
|
---|
766 | version, encoding, and standalone.
|
---|
767 |
|
---|
768 | Note: In this version of the code, the attributes are
|
---|
769 | handled as special cases, not generic attributes, simply
|
---|
770 | because there can only be at most 3 and they are always the same.
|
---|
771 | */
|
---|
772 | class TiXmlDeclaration : public TiXmlNode
|
---|
773 | {
|
---|
774 | public:
|
---|
775 | /// Construct an empty declaration.
|
---|
776 | TiXmlDeclaration() : TiXmlNode( TiXmlNode::DECLARATION ) {}
|
---|
777 |
|
---|
778 | #ifdef TIXML_USE_STL
|
---|
779 | /// Constructor.
|
---|
780 | TiXmlDeclaration(
|
---|
781 | const std::string& _version,
|
---|
782 | const std::string& _encoding,
|
---|
783 | const std::string& _standalone )
|
---|
784 | : TiXmlNode( TiXmlNode::DECLARATION )
|
---|
785 | {
|
---|
786 | version = _version;
|
---|
787 | encoding = _encoding;
|
---|
788 | standalone = _standalone;
|
---|
789 | }
|
---|
790 | #endif
|
---|
791 |
|
---|
792 | /// Construct.
|
---|
793 | TiXmlDeclaration::TiXmlDeclaration( const char * _version,
|
---|
794 | const char * _encoding,
|
---|
795 | const char * _standalone );
|
---|
796 |
|
---|
797 | virtual ~TiXmlDeclaration() {}
|
---|
798 |
|
---|
799 | /// Version. Will return empty if none was found.
|
---|
800 | const char * Version() const { return version.c_str (); }
|
---|
801 | /// Encoding. Will return empty if none was found.
|
---|
802 | const char * Encoding() const { return encoding.c_str (); }
|
---|
803 | /// Is this a standalone document?
|
---|
804 | const char * Standalone() const { return standalone.c_str (); }
|
---|
805 |
|
---|
806 | // [internal use] Creates a new Element and returs it.
|
---|
807 | virtual TiXmlNode* Clone() const;
|
---|
808 | // [internal use]
|
---|
809 | virtual void Print( FILE* cfile, int depth ) const;
|
---|
810 |
|
---|
811 | protected:
|
---|
812 | // used to be public
|
---|
813 | #ifdef TIXML_USE_STL
|
---|
814 | virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
|
---|
815 | #endif
|
---|
816 | virtual void StreamOut ( TIXML_OSTREAM * out) const;
|
---|
817 | // [internal use]
|
---|
818 | // Attribtue parsing starts: next char past '<'
|
---|
819 | // returns: next char past '>'
|
---|
820 |
|
---|
821 | virtual const char* Parse( const char* p );
|
---|
822 |
|
---|
823 | private:
|
---|
824 | TIXML_STRING version;
|
---|
825 | TIXML_STRING encoding;
|
---|
826 | TIXML_STRING standalone;
|
---|
827 | };
|
---|
828 |
|
---|
829 |
|
---|
830 | /** Any tag that tinyXml doesn't recognize is save as an
|
---|
831 | unknown. It is a tag of text, but should not be modified.
|
---|
832 | It will be written back to the XML, unchanged, when the file
|
---|
833 | is saved.
|
---|
834 | */
|
---|
835 | class TiXmlUnknown : public TiXmlNode
|
---|
836 | {
|
---|
837 | public:
|
---|
838 | TiXmlUnknown() : TiXmlNode( TiXmlNode::UNKNOWN ) {}
|
---|
839 | virtual ~TiXmlUnknown() {}
|
---|
840 |
|
---|
841 | // [internal use]
|
---|
842 | virtual TiXmlNode* Clone() const;
|
---|
843 | // [internal use]
|
---|
844 | virtual void Print( FILE* cfile, int depth ) const;
|
---|
845 | protected:
|
---|
846 | // used to be public
|
---|
847 | #ifdef TIXML_USE_STL
|
---|
848 | virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
|
---|
849 | #endif
|
---|
850 | virtual void StreamOut ( TIXML_OSTREAM * out ) const;
|
---|
851 | /* [internal use]
|
---|
852 | Attribute parsing starts: First char of the text
|
---|
853 | returns: next char past '>'
|
---|
854 | */
|
---|
855 | virtual const char* Parse( const char* p );
|
---|
856 | };
|
---|
857 |
|
---|
858 |
|
---|
859 | /** Always the top level node. A document binds together all the
|
---|
860 | XML pieces. It can be saved, loaded, and printed to the screen.
|
---|
861 | The 'value' of a document node is the xml file name.
|
---|
862 | */
|
---|
863 | class TiXmlDocument : public TiXmlNode
|
---|
864 | {
|
---|
865 | public:
|
---|
866 | /// Create an empty document, that has no name.
|
---|
867 | TiXmlDocument();
|
---|
868 | /// Create a document with a name. The name of the document is also the filename of the xml.
|
---|
869 | TiXmlDocument( const char * documentName );
|
---|
870 |
|
---|
871 | #ifdef TIXML_USE_STL
|
---|
872 | /// Constructor.
|
---|
873 | TiXmlDocument( const std::string& documentName ) :
|
---|
874 | TiXmlNode( TiXmlNode::DOCUMENT )
|
---|
875 | {
|
---|
876 | value = documentName;
|
---|
877 | error = false;
|
---|
878 | }
|
---|
879 | #endif
|
---|
880 |
|
---|
881 | virtual ~TiXmlDocument() {}
|
---|
882 |
|
---|
883 | /** Load a file using the current document value.
|
---|
884 | Returns true if successful. Will delete any existing
|
---|
885 | document data before loading.
|
---|
886 | */
|
---|
887 | bool LoadFile();
|
---|
888 | /// Save a file using the current document value. Returns true if successful.
|
---|
889 | bool SaveFile() const;
|
---|
890 | /// Load a file using the given filename. Returns true if successful.
|
---|
891 | bool LoadFile( const char * filename );
|
---|
892 | /// Save a file using the given filename. Returns true if successful.
|
---|
893 | bool SaveFile( const char * filename ) const;
|
---|
894 |
|
---|
895 | #ifdef TIXML_USE_STL
|
---|
896 | bool LoadFile( const std::string& filename ) ///< STL std::string version.
|
---|
897 | {
|
---|
898 | return ( LoadFile( filename.c_str() ));
|
---|
899 | }
|
---|
900 | bool SaveFile( const std::string& filename ) const ///< STL std::string version.
|
---|
901 | {
|
---|
902 | //StringToBuffer f( filename );
|
---|
903 | return ( SaveFile( filename.c_str() ));
|
---|
904 | }
|
---|
905 | #endif
|
---|
906 |
|
---|
907 | /// Parse the given null terminated block of xml data.
|
---|
908 | virtual const char* Parse( const char* p );
|
---|
909 |
|
---|
910 | /** Get the root element -- the only top level element -- of the document.
|
---|
911 | In well formed XML, there should only be one. TinyXml is tolerant of
|
---|
912 | multiple elements at the document level.
|
---|
913 | */
|
---|
914 | TiXmlElement* RootElement() const { return FirstChildElement(); }
|
---|
915 |
|
---|
916 | /// If, during parsing, a error occurs, Error will be set to true.
|
---|
917 | bool Error() const { return error; }
|
---|
918 |
|
---|
919 | /// Contains a textual (english) description of the error if one occurs.
|
---|
920 | const char * ErrorDesc() const { return errorDesc.c_str (); }
|
---|
921 |
|
---|
922 | /** Generally, you probably want the error string ( ErrorDesc() ). But if you
|
---|
923 | prefer the ErrorId, this function will fetch it.
|
---|
924 | */
|
---|
925 | const int ErrorId() const { return errorId; }
|
---|
926 |
|
---|
927 | /// If you have handled the error, it can be reset with this call.
|
---|
928 | void ClearError() { error = false; errorId = 0; errorDesc = ""; }
|
---|
929 |
|
---|
930 | /** Dump the document to standard out. */
|
---|
931 | void Print() const { Print( stdout, 0 ); }
|
---|
932 |
|
---|
933 | // [internal use]
|
---|
934 | virtual void Print( FILE* cfile, int depth = 0 ) const;
|
---|
935 | // [internal use]
|
---|
936 | void SetError( int err ) { assert( err > 0 && err < TIXML_ERROR_STRING_COUNT );
|
---|
937 | error = true;
|
---|
938 | errorId = err;
|
---|
939 | errorDesc = errorString[ errorId ]; }
|
---|
940 |
|
---|
941 | protected :
|
---|
942 | virtual void StreamOut ( TIXML_OSTREAM * out) const;
|
---|
943 | // [internal use]
|
---|
944 | virtual TiXmlNode* Clone() const;
|
---|
945 | #ifdef TIXML_USE_STL
|
---|
946 | virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
|
---|
947 | #endif
|
---|
948 |
|
---|
949 | private:
|
---|
950 | bool error;
|
---|
951 | int errorId;
|
---|
952 | TIXML_STRING errorDesc;
|
---|
953 | };
|
---|
954 |
|
---|
955 | #endif
|
---|
956 |
|
---|