comparison 3rdparty/tinyxml/tinyxml.h @ 0:a4671277546c tip

created the repository for the thymian project
author ferencd
date Tue, 17 Aug 2021 11:19:54 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:a4671277546c
1 /*
2 www.sourceforge.net/projects/tinyxml
3 Original code by 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( push )
31 #pragma warning( disable : 4530 )
32 #pragma warning( disable : 4786 )
33 #endif
34
35 #define TIXML_USE_STL
36
37
38 #include <ctype.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <assert.h>
43
44 // Help out windows:
45 #if defined( _DEBUG ) && !defined( DEBUG )
46 #define DEBUG
47 #endif
48
49 #ifdef TIXML_USE_STL
50 #include <string>
51 #include <iostream>
52 #include <sstream>
53 #define TIXML_STRING std::string
54 #else
55 #include "tinystr.h"
56 #define TIXML_STRING TiXmlString
57 #endif
58
59 // Deprecated library function hell. Compilers want to use the
60 // new safe versions. This probably doesn't fully address the problem,
61 // but it gets closer. There are too many compilers for me to fully
62 // test. If you get compilation troubles, undefine TIXML_SAFE
63 #define TIXML_SAFE
64
65 #ifdef TIXML_SAFE
66 #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
67 // Microsoft visual studio, version 2005 and higher.
68 #define TIXML_SNPRINTF _snprintf_s
69 #define TIXML_SSCANF sscanf_s
70 #elif defined(_MSC_VER) && (_MSC_VER >= 1200 )
71 // Microsoft visual studio, version 6 and higher.
72 //#pragma message( "Using _sn* functions." )
73 #define TIXML_SNPRINTF _snprintf
74 #define TIXML_SSCANF sscanf
75 #elif defined(__GNUC__) && (__GNUC__ >= 3 )
76 // GCC version 3 and higher.s
77 //#warning( "Using sn* functions." )
78 #define TIXML_SNPRINTF snprintf
79 #define TIXML_SSCANF sscanf
80 #else
81 #define TIXML_SNPRINTF snprintf
82 #define TIXML_SSCANF sscanf
83 #endif
84 #endif
85
86 class TiXmlDocument;
87 class TiXmlElement;
88 class TiXmlComment;
89 class TiXmlUnknown;
90 class TiXmlAttribute;
91 class TiXmlText;
92 class TiXmlDeclaration;
93 class TiXmlParsingData;
94
95 const int TIXML_MAJOR_VERSION = 2;
96 const int TIXML_MINOR_VERSION = 6;
97 const int TIXML_PATCH_VERSION = 2;
98
99 /* Internal structure for tracking location of items
100 in the XML file.
101 */
102 struct TiXmlCursor
103 {
104 TiXmlCursor() { Clear(); }
105 void Clear() { row = col = -1; }
106
107 int row; // 0 based.
108 int col; // 0 based.
109 };
110
111
112 /**
113 Implements the interface to the "Visitor pattern" (see the Accept() method.)
114 If you call the Accept() method, it requires being passed a TiXmlVisitor
115 class to handle callbacks. For nodes that contain other nodes (Document, Element)
116 you will get called with a VisitEnter/VisitExit pair. Nodes that are always leaves
117 are simply called with Visit().
118
119 If you return 'true' from a Visit method, recursive parsing will continue. If you return
120 false, <b>no children of this node or its sibilings</b> will be Visited.
121
122 All flavors of Visit methods have a default implementation that returns 'true' (continue
123 visiting). You need to only override methods that are interesting to you.
124
125 Generally Accept() is called on the TiXmlDocument, although all nodes suppert Visiting.
126
127 You should never change the document from a callback.
128
129 @sa TiXmlNode::Accept()
130 */
131 class TiXmlVisitor
132 {
133 public:
134 virtual ~TiXmlVisitor() {}
135
136 /// Visit a document.
137 virtual bool VisitEnter( const TiXmlDocument& /*doc*/ ) { return true; }
138 /// Visit a document.
139 virtual bool VisitExit( const TiXmlDocument& /*doc*/ ) { return true; }
140
141 /// Visit an element.
142 virtual bool VisitEnter( const TiXmlElement& /*element*/, const TiXmlAttribute* /*firstAttribute*/ ) { return true; }
143 /// Visit an element.
144 virtual bool VisitExit( const TiXmlElement& /*element*/ ) { return true; }
145
146 /// Visit a declaration
147 virtual bool Visit( const TiXmlDeclaration& /*declaration*/ ) { return true; }
148 /// Visit a text node
149 virtual bool Visit( const TiXmlText& /*text*/ ) { return true; }
150 /// Visit a comment node
151 virtual bool Visit( const TiXmlComment& /*comment*/ ) { return true; }
152 /// Visit an unknown node
153 virtual bool Visit( const TiXmlUnknown& /*unknown*/ ) { return true; }
154 };
155
156 // Only used by Attribute::Query functions
157 enum
158 {
159 TIXML_SUCCESS,
160 TIXML_NO_ATTRIBUTE,
161 TIXML_WRONG_TYPE
162 };
163
164
165 // Used by the parsing routines.
166 enum TiXmlEncoding
167 {
168 TIXML_ENCODING_UNKNOWN,
169 TIXML_ENCODING_UTF8,
170 TIXML_ENCODING_LEGACY
171 };
172
173 const TiXmlEncoding TIXML_DEFAULT_ENCODING = TIXML_ENCODING_UNKNOWN;
174
175 /** TiXmlBase is a base class for every class in TinyXml.
176 It does little except to establish that TinyXml classes
177 can be printed and provide some utility functions.
178
179 In XML, the document and elements can contain
180 other elements and other types of nodes.
181
182 @verbatim
183 A Document can contain: Element (container or leaf)
184 Comment (leaf)
185 Unknown (leaf)
186 Declaration( leaf )
187
188 An Element can contain: Element (container or leaf)
189 Text (leaf)
190 Attributes (not on tree)
191 Comment (leaf)
192 Unknown (leaf)
193
194 A Decleration contains: Attributes (not on tree)
195 @endverbatim
196 */
197 class TiXmlBase
198 {
199 friend class TiXmlNode;
200 friend class TiXmlElement;
201 friend class TiXmlDocument;
202
203 public:
204 TiXmlBase() : userData(0) {}
205 virtual ~TiXmlBase() {}
206
207 /** All TinyXml classes can print themselves to a filestream
208 or the string class (TiXmlString in non-STL mode, std::string
209 in STL mode.) Either or both cfile and str can be null.
210
211 This is a formatted print, and will insert
212 tabs and newlines.
213
214 (For an unformatted stream, use the << operator.)
215 */
216 virtual void Print( FILE* cfile, int depth ) const = 0;
217 virtual void Print( std::string& , int depth ) const = 0;
218
219 /** The world does not agree on whether white space should be kept or
220 not. In order to make everyone happy, these global, static functions
221 are provided to set whether or not TinyXml will condense all white space
222 into a single space or not. The default is to condense. Note changing this
223 value is not thread safe.
224 */
225 static void SetCondenseWhiteSpace( bool condense ) { condenseWhiteSpace = condense; }
226
227 /// Return the current white space setting.
228 static bool IsWhiteSpaceCondensed() { return condenseWhiteSpace; }
229
230 /** Return the position, in the original source file, of this node or attribute.
231 The row and column are 1-based. (That is the first row and first column is
232 1,1). If the returns values are 0 or less, then the parser does not have
233 a row and column value.
234
235 Generally, the row and column value will be set when the TiXmlDocument::Load(),
236 TiXmlDocument::LoadFile(), or any TiXmlNode::Parse() is called. It will NOT be set
237 when the DOM was created from operator>>.
238
239 The values reflect the initial load. Once the DOM is modified programmatically
240 (by adding or changing nodes and attributes) the new values will NOT update to
241 reflect changes in the document.
242
243 There is a minor performance cost to computing the row and column. Computation
244 can be disabled if TiXmlDocument::SetTabSize() is called with 0 as the value.
245
246 @sa TiXmlDocument::SetTabSize()
247 */
248 int Row() const { return location.row + 1; }
249 int Column() const { return location.col + 1; } ///< See Row()
250
251 void SetUserData( void* user ) { userData = user; } ///< Set a pointer to arbitrary user data.
252 void* GetUserData() { return userData; } ///< Get a pointer to arbitrary user data.
253 const void* GetUserData() const { return userData; } ///< Get a pointer to arbitrary user data.
254
255 // Table that returs, for a given lead byte, the total number of bytes
256 // in the UTF-8 sequence.
257 static const int utf8ByteTable[256];
258
259 virtual const char* Parse( const char* p,
260 TiXmlParsingData* data,
261 TiXmlEncoding encoding /*= TIXML_ENCODING_UNKNOWN */ ) = 0;
262
263 /** Expands entities in a string. Note this should not contian the tag's '<', '>', etc,
264 or they will be transformed into entities!
265 */
266 static void EncodeString( const TIXML_STRING& str, TIXML_STRING* out );
267
268 enum
269 {
270 TIXML_NO_ERROR = 0,
271 TIXML_ERROR,
272 TIXML_ERROR_OPENING_FILE,
273 TIXML_ERROR_PARSING_ELEMENT,
274 TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME,
275 TIXML_ERROR_READING_ELEMENT_VALUE,
276 TIXML_ERROR_READING_ATTRIBUTES,
277 TIXML_ERROR_PARSING_EMPTY,
278 TIXML_ERROR_READING_END_TAG,
279 TIXML_ERROR_PARSING_UNKNOWN,
280 TIXML_ERROR_PARSING_COMMENT,
281 TIXML_ERROR_PARSING_DECLARATION,
282 TIXML_ERROR_DOCUMENT_EMPTY,
283 TIXML_ERROR_EMBEDDED_NULL,
284 TIXML_ERROR_PARSING_CDATA,
285 TIXML_ERROR_DOCUMENT_TOP_ONLY,
286
287 TIXML_ERROR_STRING_COUNT
288 };
289
290 protected:
291
292 static const char* SkipWhiteSpace( const char*, TiXmlEncoding encoding );
293
294 inline static bool IsWhiteSpace( char c )
295 {
296 return ( isspace( static_cast<unsigned char>(c) ) || c == '\n' || c == '\r' );
297 }
298 inline static bool IsWhiteSpace( int c )
299 {
300 if ( c < 256 )
301 return IsWhiteSpace( static_cast<char> (c) );
302 return false; // Again, only truly correct for English/Latin...but usually works.
303 }
304
305 #ifdef TIXML_USE_STL
306 static bool StreamWhiteSpace( std::istream * in, TIXML_STRING * tag );
307 static bool StreamTo( std::istream * in, int character, TIXML_STRING * tag );
308 #endif
309
310 /* Reads an XML name into the string provided. Returns
311 a pointer just past the last character of the name,
312 or 0 if the function has an error.
313 */
314 static const char* ReadName( const char* p, TIXML_STRING* name, TiXmlEncoding encoding );
315
316 /* Reads text. Returns a pointer past the given end tag.
317 Wickedly complex options, but it keeps the (sensitive) code in one place.
318 */
319 static const char* ReadText( const char* in, // where to start
320 TIXML_STRING* text, // the string read
321 bool ignoreWhiteSpace, // whether to keep the white space
322 const char* endTag, // what ends this text
323 bool ignoreCase, // whether to ignore case in the end tag
324 TiXmlEncoding encoding ); // the current encoding
325
326 // If an entity has been found, transform it into a character.
327 static const char* GetEntity( const char* in, char* value, int* length, TiXmlEncoding encoding );
328
329 // Get a character, while interpreting entities.
330 // The length can be from 0 to 4 bytes.
331 inline static const char* GetChar( const char* p, char* _value, int* length, TiXmlEncoding encoding )
332 {
333 assert( p );
334 if ( encoding == TIXML_ENCODING_UTF8 )
335 {
336 *length = utf8ByteTable[ *( reinterpret_cast<const unsigned char*>(p)) ];
337 assert( *length >= 0 && *length < 5 );
338 }
339 else
340 {
341 *length = 1;
342 }
343
344 if ( *length == 1 )
345 {
346 if ( *p == '&' )
347 return GetEntity( p, _value, length, encoding );
348 *_value = *p;
349 return p+1;
350 }
351 else if ( *length )
352 {
353 //strncpy( _value, p, *length ); // lots of compilers don't like this function (unsafe),
354 // and the null terminator isn't needed
355 for( int i=0; p[i] && i<*length; ++i ) {
356 _value[i] = p[i];
357 }
358 return p + (*length);
359 }
360 else
361 {
362 // Not valid text.
363 return 0;
364 }
365 }
366
367 // Return true if the next characters in the stream are any of the endTag sequences.
368 // Ignore case only works for english, and should only be relied on when comparing
369 // to English words: StringEqual( p, "version", true ) is fine.
370 static bool StringEqual( const char* p,
371 const char* endTag,
372 bool ignoreCase,
373 TiXmlEncoding encoding );
374
375 static const char* errorString[ TIXML_ERROR_STRING_COUNT ];
376
377 TiXmlCursor location;
378
379 /// Field containing a generic user pointer
380 void* userData;
381
382 // None of these methods are reliable for any language except English.
383 // Good for approximation, not great for accuracy.
384 static int IsAlpha( unsigned char anyByte, TiXmlEncoding encoding );
385 static int IsAlphaNum( unsigned char anyByte, TiXmlEncoding encoding );
386 inline static int ToLower( int v, TiXmlEncoding encoding )
387 {
388 if ( encoding == TIXML_ENCODING_UTF8 )
389 {
390 if ( v < 128 ) return tolower( v );
391 return v;
392 }
393 else
394 {
395 return tolower( v );
396 }
397 }
398 static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
399
400 private:
401 TiXmlBase( const TiXmlBase& ); // not implemented.
402 void operator=( const TiXmlBase& base ); // not allowed.
403
404 struct Entity
405 {
406 const char* str;
407 unsigned int strLength;
408 char chr;
409 };
410 enum
411 {
412 NUM_ENTITY = 5,
413 MAX_ENTITY_LENGTH = 6
414
415 };
416 static Entity entity[ NUM_ENTITY ];
417 static bool condenseWhiteSpace;
418 };
419
420
421 /** The parent class for everything in the Document Object Model.
422 (Except for attributes).
423 Nodes have siblings, a parent, and children. A node can be
424 in a document, or stand on its own. The type of a TiXmlNode
425 can be queried, and it can be cast to its more defined type.
426 */
427 class TiXmlNode : public TiXmlBase
428 {
429 friend class TiXmlDocument;
430 friend class TiXmlElement;
431
432 public:
433 #ifdef TIXML_USE_STL
434
435 /** An input stream operator, for every class. Tolerant of newlines and
436 formatting, but doesn't expect them.
437 */
438 friend std::istream& operator >> (std::istream& in, TiXmlNode& base);
439
440 /** An output stream operator, for every class. Note that this outputs
441 without any newlines or formatting, as opposed to Print(), which
442 includes tabs and new lines.
443
444 The operator<< and operator>> are not completely symmetric. Writing
445 a node to a stream is very well defined. You'll get a nice stream
446 of output, without any extra whitespace or newlines.
447
448 But reading is not as well defined. (As it always is.) If you create
449 a TiXmlElement (for example) and read that from an input stream,
450 the text needs to define an element or junk will result. This is
451 true of all input streams, but it's worth keeping in mind.
452
453 A TiXmlDocument will read nodes until it reads a root element, and
454 all the children of that root element.
455 */
456 friend std::ostream& operator<< (std::ostream& out, const TiXmlNode& base);
457
458 /// Appends the XML node or attribute to a std::string.
459 friend std::string& operator<< (std::string& out, const TiXmlNode& base );
460
461 #endif
462
463 /** The types of XML nodes supported by TinyXml. (All the
464 unsupported types are picked up by UNKNOWN.)
465 */
466 enum NodeType
467 {
468 TINYXML_DOCUMENT,
469 TINYXML_ELEMENT,
470 TINYXML_COMMENT,
471 TINYXML_UNKNOWN,
472 TINYXML_TEXT,
473 TINYXML_DECLARATION,
474 TINYXML_TYPECOUNT
475 };
476
477 virtual ~TiXmlNode();
478
479 /** The meaning of 'value' changes for the specific type of
480 TiXmlNode.
481 @verbatim
482 Document: filename of the xml file
483 Element: name of the element
484 Comment: the comment text
485 Unknown: the tag contents
486 Text: the text string
487 @endverbatim
488
489 The subclasses will wrap this function.
490 */
491 const char *Value() const { return value.c_str (); }
492
493 #ifdef TIXML_USE_STL
494 /** Return Value() as a std::string. If you only use STL,
495 this is more efficient than calling Value().
496 Only available in STL mode.
497 */
498 const std::string& ValueStr() const { return value; }
499 #endif
500
501 const TIXML_STRING& ValueTStr() const { return value; }
502
503 /** Changes the value of the node. Defined as:
504 @verbatim
505 Document: filename of the xml file
506 Element: name of the element
507 Comment: the comment text
508 Unknown: the tag contents
509 Text: the text string
510 @endverbatim
511 */
512 void SetValue(const char * _value) { value = _value;}
513
514 #ifdef TIXML_USE_STL
515 /// STL std::string form.
516 void SetValue( const std::string& _value ) { value = _value; }
517 #endif
518
519 /// Delete all the children of this node. Does not affect 'this'.
520 void Clear();
521
522 /// One step up the DOM.
523 TiXmlNode* Parent() { return parent; }
524 const TiXmlNode* Parent() const { return parent; }
525
526 const TiXmlNode* FirstChild() const { return firstChild; } ///< The first child of this node. Will be null if there are no children.
527 TiXmlNode* FirstChild() { return firstChild; }
528 const TiXmlNode* FirstChild( const char * value ) const; ///< The first child of this node with the matching 'value'. Will be null if none found.
529 /// The first child of this node with the matching 'value'. Will be null if none found.
530 TiXmlNode* FirstChild( const char * _value ) {
531 // Call through to the const version - safe since nothing is changed. Exiting syntax: cast this to a const (always safe)
532 // call the method, cast the return back to non-const.
533 return const_cast< TiXmlNode* > ((const_cast< const TiXmlNode* >(this))->FirstChild( _value ));
534 }
535 const TiXmlNode* LastChild() const { return lastChild; } /// The last child of this node. Will be null if there are no children.
536 TiXmlNode* LastChild() { return lastChild; }
537
538 const TiXmlNode* LastChild( const char * value ) const; /// The last child of this node matching 'value'. Will be null if there are no children.
539 TiXmlNode* LastChild( const char * _value ) {
540 return const_cast< TiXmlNode* > ((const_cast< const TiXmlNode* >(this))->LastChild( _value ));
541 }
542
543 #ifdef TIXML_USE_STL
544 const TiXmlNode* FirstChild( const std::string& _value ) const { return FirstChild (_value.c_str ()); } ///< STL std::string form.
545 TiXmlNode* FirstChild( const std::string& _value ) { return FirstChild (_value.c_str ()); } ///< STL std::string form.
546 const TiXmlNode* LastChild( const std::string& _value ) const { return LastChild (_value.c_str ()); } ///< STL std::string form.
547 TiXmlNode* LastChild( const std::string& _value ) { return LastChild (_value.c_str ()); } ///< STL std::string form.
548 #endif
549
550 /** An alternate way to walk the children of a node.
551 One way to iterate over nodes is:
552 @verbatim
553 for( child = parent->FirstChild(); child; child = child->NextSibling() )
554 @endverbatim
555
556 IterateChildren does the same thing with the syntax:
557 @verbatim
558 child = 0;
559 while( child = parent->IterateChildren( child ) )
560 @endverbatim
561
562 IterateChildren takes the previous child as input and finds
563 the next one. If the previous child is null, it returns the
564 first. IterateChildren will return null when done.
565 */
566 const TiXmlNode* IterateChildren( const TiXmlNode* previous ) const;
567 TiXmlNode* IterateChildren( const TiXmlNode* previous ) {
568 return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->IterateChildren( previous ) );
569 }
570
571 /// This flavor of IterateChildren searches for children with a particular 'value'
572 const TiXmlNode* IterateChildren( const char * value, const TiXmlNode* previous ) const;
573 TiXmlNode* IterateChildren( const char * _value, const TiXmlNode* previous ) {
574 return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->IterateChildren( _value, previous ) );
575 }
576
577 #ifdef TIXML_USE_STL
578 const TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) const { return IterateChildren (_value.c_str (), previous); } ///< STL std::string form.
579 TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) { return IterateChildren (_value.c_str (), previous); } ///< STL std::string form.
580 #endif
581
582 /** Add a new node related to this. Adds a child past the LastChild.
583 Returns a pointer to the new object or NULL if an error occured.
584 */
585 TiXmlNode* InsertEndChild( const TiXmlNode& addThis );
586
587
588 /** Add a new node related to this. Adds a child past the LastChild.
589
590 NOTE: the node to be added is passed by pointer, and will be
591 henceforth owned (and deleted) by tinyXml. This method is efficient
592 and avoids an extra copy, but should be used with care as it
593 uses a different memory model than the other insert functions.
594
595 @sa InsertEndChild
596 */
597 TiXmlNode* LinkEndChild( TiXmlNode* addThis );
598
599 /** Add a new node related to this. Adds a child before the specified child.
600 Returns a pointer to the new object or NULL if an error occured.
601 */
602 TiXmlNode* InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis );
603
604 /** Add a new node related to this. Adds a child after the specified child.
605 Returns a pointer to the new object or NULL if an error occured.
606 */
607 TiXmlNode* InsertAfterChild( TiXmlNode* afterThis, const TiXmlNode& addThis );
608
609 /** Replace a child of this node.
610 Returns a pointer to the new object or NULL if an error occured.
611 */
612 TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis );
613
614 /// Delete a child of this node.
615 bool RemoveChild( TiXmlNode* removeThis );
616
617 /// Navigate to a sibling node.
618 const TiXmlNode* PreviousSibling() const { return prev; }
619 TiXmlNode* PreviousSibling() { return prev; }
620
621 /// Navigate to a sibling node.
622 const TiXmlNode* PreviousSibling( const char * ) const;
623 TiXmlNode* PreviousSibling( const char *_prev ) {
624 return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->PreviousSibling( _prev ) );
625 }
626
627 #ifdef TIXML_USE_STL
628 const TiXmlNode* PreviousSibling( const std::string& _value ) const { return PreviousSibling (_value.c_str ()); } ///< STL std::string form.
629 TiXmlNode* PreviousSibling( const std::string& _value ) { return PreviousSibling (_value.c_str ()); } ///< STL std::string form.
630 const TiXmlNode* NextSibling( const std::string& _value) const { return NextSibling (_value.c_str ()); } ///< STL std::string form.
631 TiXmlNode* NextSibling( const std::string& _value) { return NextSibling (_value.c_str ()); } ///< STL std::string form.
632 #endif
633
634 /// Navigate to a sibling node.
635 const TiXmlNode* NextSibling() const { return next; }
636 TiXmlNode* NextSibling() { return next; }
637
638 /// Navigate to a sibling node with the given 'value'.
639 const TiXmlNode* NextSibling( const char * ) const;
640 TiXmlNode* NextSibling( const char* _next ) {
641 return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->NextSibling( _next ) );
642 }
643
644 /** Convenience function to get through elements.
645 Calls NextSibling and ToElement. Will skip all non-Element
646 nodes. Returns 0 if there is not another element.
647 */
648 const TiXmlElement* NextSiblingElement() const;
649 TiXmlElement* NextSiblingElement() {
650 return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->NextSiblingElement() );
651 }
652
653 /** Convenience function to get through elements.
654 Calls NextSibling and ToElement. Will skip all non-Element
655 nodes. Returns 0 if there is not another element.
656 */
657 const TiXmlElement* NextSiblingElement( const char * ) const;
658 TiXmlElement* NextSiblingElement( const char *_next ) {
659 return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->NextSiblingElement( _next ) );
660 }
661
662 #ifdef TIXML_USE_STL
663 const TiXmlElement* NextSiblingElement( const std::string& _value) const { return NextSiblingElement (_value.c_str ()); } ///< STL std::string form.
664 TiXmlElement* NextSiblingElement( const std::string& _value) { return NextSiblingElement (_value.c_str ()); } ///< STL std::string form.
665 #endif
666
667 /// Convenience function to get through elements.
668 const TiXmlElement* FirstChildElement() const;
669 TiXmlElement* FirstChildElement() {
670 return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->FirstChildElement() );
671 }
672
673 /// Convenience function to get through elements.
674 const TiXmlElement* FirstChildElement( const char * _value ) const;
675 TiXmlElement* FirstChildElement( const char * _value ) {
676 return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->FirstChildElement( _value ) );
677 }
678
679 #ifdef TIXML_USE_STL
680 const TiXmlElement* FirstChildElement( const std::string& _value ) const { return FirstChildElement (_value.c_str ()); } ///< STL std::string form.
681 TiXmlElement* FirstChildElement( const std::string& _value ) { return FirstChildElement (_value.c_str ()); } ///< STL std::string form.
682 #endif
683
684 /** Query the type (as an enumerated value, above) of this node.
685 The possible types are: TINYXML_DOCUMENT, TINYXML_ELEMENT, TINYXML_COMMENT,
686 TINYXML_UNKNOWN, TINYXML_TEXT, and TINYXML_DECLARATION.
687 */
688 int Type() const { return type; }
689
690 /** Return a pointer to the Document this node lives in.
691 Returns null if not in a document.
692 */
693 const TiXmlDocument* GetDocument() const;
694 TiXmlDocument* GetDocument() {
695 return const_cast< TiXmlDocument* >( (const_cast< const TiXmlNode* >(this))->GetDocument() );
696 }
697
698 /// Returns true if this node has no children.
699 bool NoChildren() const { return !firstChild; }
700
701 virtual const TiXmlDocument* ToDocument() const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
702 virtual const TiXmlElement* ToElement() const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
703 virtual const TiXmlComment* ToComment() const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
704 virtual const TiXmlUnknown* ToUnknown() const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
705 virtual const TiXmlText* ToText() const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
706 virtual const TiXmlDeclaration* ToDeclaration() const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
707
708 virtual TiXmlDocument* ToDocument() { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
709 virtual TiXmlElement* ToElement() { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
710 virtual TiXmlComment* ToComment() { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
711 virtual TiXmlUnknown* ToUnknown() { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
712 virtual TiXmlText* ToText() { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
713 virtual TiXmlDeclaration* ToDeclaration() { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
714
715 /** Create an exact duplicate of this node and return it. The memory must be deleted
716 by the caller.
717 */
718 virtual TiXmlNode* Clone() const = 0;
719
720 /** Accept a hierchical visit the nodes in the TinyXML DOM. Every node in the
721 XML tree will be conditionally visited and the host will be called back
722 via the TiXmlVisitor interface.
723
724 This is essentially a SAX interface for TinyXML. (Note however it doesn't re-parse
725 the XML for the callbacks, so the performance of TinyXML is unchanged by using this
726 interface versus any other.)
727
728 The interface has been based on ideas from:
729
730 - http://www.saxproject.org/
731 - http://c2.com/cgi/wiki?HierarchicalVisitorPattern
732
733 Which are both good references for "visiting".
734
735 An example of using Accept():
736 @verbatim
737 TiXmlPrinter printer;
738 tinyxmlDoc.Accept( &printer );
739 const char* xmlcstr = printer.CStr();
740 @endverbatim
741 */
742 virtual bool Accept( TiXmlVisitor* visitor ) const = 0;
743
744 protected:
745 TiXmlNode( NodeType _type );
746
747 // Copy to the allocated object. Shared functionality between Clone, Copy constructor,
748 // and the assignment operator.
749 void CopyTo( TiXmlNode* target ) const;
750
751 #ifdef TIXML_USE_STL
752 // The real work of the input operator.
753 virtual void StreamIn( std::istream* in, TIXML_STRING* tag ) = 0;
754 #endif
755
756 // Figure out what is at *p, and parse it. Returns null if it is not an xml node.
757 TiXmlNode* Identify( const char* start, TiXmlEncoding encoding );
758
759 TiXmlNode* parent;
760 NodeType type;
761
762 TiXmlNode* firstChild;
763 TiXmlNode* lastChild;
764
765 TIXML_STRING value;
766
767 TiXmlNode* prev;
768 TiXmlNode* next;
769
770 private:
771 TiXmlNode( const TiXmlNode& ); // not implemented.
772 void operator=( const TiXmlNode& base ); // not allowed.
773 };
774
775
776 /** An attribute is a name-value pair. Elements have an arbitrary
777 number of attributes, each with a unique name.
778
779 @note The attributes are not TiXmlNodes, since they are not
780 part of the tinyXML document object model. There are other
781 suggested ways to look at this problem.
782 */
783 class TiXmlAttribute : public TiXmlBase
784 {
785 friend class TiXmlAttributeSet;
786
787 public:
788 /// Construct an empty attribute.
789 TiXmlAttribute() : TiXmlBase()
790 {
791 document = 0;
792 prev = next = 0;
793 }
794
795 #ifdef TIXML_USE_STL
796 /// std::string constructor.
797 TiXmlAttribute( const std::string& _name, const std::string& _value )
798 {
799 name = _name;
800 value = _value;
801 document = 0;
802 prev = next = 0;
803 }
804 #endif
805
806 /// Construct an attribute with a name and value.
807 TiXmlAttribute( const char * _name, const char * _value )
808 {
809 name = _name;
810 value = _value;
811 document = 0;
812 prev = next = 0;
813 }
814
815 const char* Name() const { return name.c_str(); } ///< Return the name of this attribute.
816 const char* Value() const { return value.c_str(); } ///< Return the value of this attribute.
817 #ifdef TIXML_USE_STL
818 const std::string& ValueStr() const { return value; } ///< Return the value of this attribute.
819 #endif
820 int IntValue() const; ///< Return the value of this attribute, converted to an integer.
821 double DoubleValue() const; ///< Return the value of this attribute, converted to a double.
822
823 // Get the tinyxml string representation
824 const TIXML_STRING& NameTStr() const { return name; }
825
826 /** QueryIntValue examines the value string. It is an alternative to the
827 IntValue() method with richer error checking.
828 If the value is an integer, it is stored in 'value' and
829 the call returns TIXML_SUCCESS. If it is not
830 an integer, it returns TIXML_WRONG_TYPE.
831
832 A specialized but useful call. Note that for success it returns 0,
833 which is the opposite of almost all other TinyXml calls.
834 */
835 int QueryIntValue( int* _value ) const;
836 /// QueryDoubleValue examines the value string. See QueryIntValue().
837 int QueryDoubleValue( double* _value ) const;
838
839 void SetName( const char* _name ) { name = _name; } ///< Set the name of this attribute.
840 void SetValue( const char* _value ) { value = _value; } ///< Set the value.
841
842 void SetIntValue( int _value ); ///< Set the value from an integer.
843 void SetDoubleValue( double _value ); ///< Set the value from a double.
844
845 #ifdef TIXML_USE_STL
846 /// STL std::string form.
847 void SetName( const std::string& _name ) { name = _name; }
848 /// STL std::string form.
849 void SetValue( const std::string& _value ) { value = _value; }
850 #endif
851
852 /// Get the next sibling attribute in the DOM. Returns null at end.
853 const TiXmlAttribute* Next() const;
854 TiXmlAttribute* Next() {
855 return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttribute* >(this))->Next() );
856 }
857
858 /// Get the previous sibling attribute in the DOM. Returns null at beginning.
859 const TiXmlAttribute* Previous() const;
860 TiXmlAttribute* Previous() {
861 return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttribute* >(this))->Previous() );
862 }
863
864 bool operator==( const TiXmlAttribute& rhs ) const { return rhs.name == name; }
865 bool operator<( const TiXmlAttribute& rhs ) const { return name < rhs.name; }
866 bool operator>( const TiXmlAttribute& rhs ) const { return name > rhs.name; }
867
868 /* Attribute parsing starts: first letter of the name
869 returns: the next char after the value end quote
870 */
871 virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
872
873 // Prints this Attribute to a FILE stream.
874 virtual void Print( FILE* cfile, int depth ) const {
875 Print( cfile, depth, 0 );
876 }
877 void Print( FILE* cfile, int depth, TIXML_STRING* str ) const;
878
879 virtual void Print( std::string&, int depth ) const;
880
881 // [internal use]
882 // Set the document pointer so the attribute can report errors.
883 void SetDocument( TiXmlDocument* doc ) { document = doc; }
884
885 private:
886 TiXmlAttribute( const TiXmlAttribute& ); // not implemented.
887 void operator=( const TiXmlAttribute& base ); // not allowed.
888
889 TiXmlDocument* document; // A pointer back to a document, for error reporting.
890 TIXML_STRING name;
891 TIXML_STRING value;
892 TiXmlAttribute* prev;
893 TiXmlAttribute* next;
894 };
895
896
897 /* A class used to manage a group of attributes.
898 It is only used internally, both by the ELEMENT and the DECLARATION.
899
900 The set can be changed transparent to the Element and Declaration
901 classes that use it, but NOT transparent to the Attribute
902 which has to implement a next() and previous() method. Which makes
903 it a bit problematic and prevents the use of STL.
904
905 This version is implemented with circular lists because:
906 - I like circular lists
907 - it demonstrates some independence from the (typical) doubly linked list.
908 */
909 class TiXmlAttributeSet
910 {
911 public:
912 TiXmlAttributeSet();
913 ~TiXmlAttributeSet();
914
915 void Add( TiXmlAttribute* attribute );
916 void Remove( TiXmlAttribute* attribute );
917
918 const TiXmlAttribute* First() const { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
919 TiXmlAttribute* First() { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
920 const TiXmlAttribute* Last() const { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
921 TiXmlAttribute* Last() { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
922
923 TiXmlAttribute* Find( const char* _name ) const;
924 TiXmlAttribute* FindOrCreate( const char* _name );
925
926 # ifdef TIXML_USE_STL
927 TiXmlAttribute* Find( const std::string& _name ) const;
928 TiXmlAttribute* FindOrCreate( const std::string& _name );
929 # endif
930
931
932 private:
933 //*ME: Because of hidden/disabled copy-construktor in TiXmlAttribute (sentinel-element),
934 //*ME: this class must be also use a hidden/disabled copy-constructor !!!
935 TiXmlAttributeSet( const TiXmlAttributeSet& ); // not allowed
936 void operator=( const TiXmlAttributeSet& ); // not allowed (as TiXmlAttribute)
937
938 TiXmlAttribute sentinel;
939 };
940
941
942 /** The element is a container class. It has a value, the element name,
943 and can contain other elements, text, comments, and unknowns.
944 Elements also contain an arbitrary number of attributes.
945 */
946 class TiXmlElement : public TiXmlNode
947 {
948 public:
949 /// Construct an element.
950 TiXmlElement (const char * in_value);
951
952 #ifdef TIXML_USE_STL
953 /// std::string constructor.
954 TiXmlElement( const std::string& _value );
955 #endif
956
957 TiXmlElement( const TiXmlElement& );
958
959 TiXmlElement& operator=( const TiXmlElement& base );
960
961 virtual ~TiXmlElement();
962
963 /** Given an attribute name, Attribute() returns the value
964 for the attribute of that name, or null if none exists.
965 */
966 const char* Attribute( const char* name ) const;
967
968 /** Given an attribute name, Attribute() returns the value
969 for the attribute of that name, or null if none exists.
970 If the attribute exists and can be converted to an integer,
971 the integer value will be put in the return 'i', if 'i'
972 is non-null.
973 */
974 const char* Attribute( const char* name, int* i ) const;
975
976 /** Given an attribute name, Attribute() returns the value
977 for the attribute of that name, or null if none exists.
978 If the attribute exists and can be converted to an double,
979 the double value will be put in the return 'd', if 'd'
980 is non-null.
981 */
982 const char* Attribute( const char* name, double* d ) const;
983
984 /** QueryIntAttribute examines the attribute - it is an alternative to the
985 Attribute() method with richer error checking.
986 If the attribute is an integer, it is stored in 'value' and
987 the call returns TIXML_SUCCESS. If it is not
988 an integer, it returns TIXML_WRONG_TYPE. If the attribute
989 does not exist, then TIXML_NO_ATTRIBUTE is returned.
990 */
991 int QueryIntAttribute( const char* name, int* _value ) const;
992 /// QueryUnsignedAttribute examines the attribute - see QueryIntAttribute().
993 int QueryUnsignedAttribute( const char* name, unsigned* _value ) const;
994 /** QueryBoolAttribute examines the attribute - see QueryIntAttribute().
995 Note that '1', 'true', or 'yes' are considered true, while '0', 'false'
996 and 'no' are considered false.
997 */
998 int QueryBoolAttribute( const char* name, bool* _value ) const;
999 /// QueryDoubleAttribute examines the attribute - see QueryIntAttribute().
1000 int QueryDoubleAttribute( const char* name, double* _value ) const;
1001 /// QueryFloatAttribute examines the attribute - see QueryIntAttribute().
1002 int QueryFloatAttribute( const char* name, float* _value ) const {
1003 double d;
1004 int result = QueryDoubleAttribute( name, &d );
1005 if ( result == TIXML_SUCCESS ) {
1006 *_value = static_cast<float>(d);
1007 }
1008 return result;
1009 }
1010
1011 #ifdef TIXML_USE_STL
1012 /// QueryStringAttribute examines the attribute - see QueryIntAttribute().
1013 int QueryStringAttribute( const char* name, std::string* _value ) const {
1014 const char* cstr = Attribute( name );
1015 if ( cstr ) {
1016 *_value = std::string( cstr );
1017 return TIXML_SUCCESS;
1018 }
1019 return TIXML_NO_ATTRIBUTE;
1020 }
1021
1022 /** Template form of the attribute query which will try to read the
1023 attribute into the specified type. Very easy, very powerful, but
1024 be careful to make sure to call this with the correct type.
1025
1026 NOTE: This method doesn't work correctly for 'string' types that contain spaces.
1027
1028 @return TIXML_SUCCESS, TIXML_WRONG_TYPE, or TIXML_NO_ATTRIBUTE
1029 */
1030 template< typename T > int QueryValueAttribute( const std::string& name, T* outValue ) const
1031 {
1032 const TiXmlAttribute* node = attributeSet.Find( name );
1033 if ( !node )
1034 return TIXML_NO_ATTRIBUTE;
1035
1036 std::stringstream sstream( node->ValueStr() );
1037 sstream >> *outValue;
1038 if ( !sstream.fail() )
1039 return TIXML_SUCCESS;
1040 return TIXML_WRONG_TYPE;
1041 }
1042
1043 int QueryValueAttribute( const std::string& name, std::string* outValue ) const
1044 {
1045 const TiXmlAttribute* node = attributeSet.Find( name );
1046 if ( !node )
1047 return TIXML_NO_ATTRIBUTE;
1048 *outValue = node->ValueStr();
1049 return TIXML_SUCCESS;
1050 }
1051 #endif
1052
1053 /** Sets an attribute of name to a given value. The attribute
1054 will be created if it does not exist, or changed if it does.
1055 */
1056 void SetAttribute( const char* name, const char * _value );
1057
1058 #ifdef TIXML_USE_STL
1059 const std::string* Attribute( const std::string& name ) const;
1060 const std::string* Attribute( const std::string& name, int* i ) const;
1061 const std::string* Attribute( const std::string& name, double* d ) const;
1062 int QueryIntAttribute( const std::string& name, int* _value ) const;
1063 int QueryDoubleAttribute( const std::string& name, double* _value ) const;
1064
1065 /// STL std::string form.
1066 void SetAttribute( const std::string& name, const std::string& _value );
1067 ///< STL std::string form.
1068 void SetAttribute( const std::string& name, int _value );
1069 ///< STL std::string form.
1070 void SetDoubleAttribute( const std::string& name, double value );
1071 #endif
1072
1073 /** Sets an attribute of name to a given value. The attribute
1074 will be created if it does not exist, or changed if it does.
1075 */
1076 void SetAttribute( const char * name, int value );
1077
1078 /** Sets an attribute of name to a given value. The attribute
1079 will be created if it does not exist, or changed if it does.
1080 */
1081 void SetDoubleAttribute( const char * name, double value );
1082
1083 /** Deletes an attribute with the given name.
1084 */
1085 void RemoveAttribute( const char * name );
1086 #ifdef TIXML_USE_STL
1087 void RemoveAttribute( const std::string& name ) { RemoveAttribute (name.c_str ()); } ///< STL std::string form.
1088 #endif
1089
1090 const TiXmlAttribute* FirstAttribute() const { return attributeSet.First(); } ///< Access the first attribute in this element.
1091 TiXmlAttribute* FirstAttribute() { return attributeSet.First(); }
1092 const TiXmlAttribute* LastAttribute() const { return attributeSet.Last(); } ///< Access the last attribute in this element.
1093 TiXmlAttribute* LastAttribute() { return attributeSet.Last(); }
1094
1095 /** Convenience function for easy access to the text inside an element. Although easy
1096 and concise, GetText() is limited compared to getting the TiXmlText child
1097 and accessing it directly.
1098
1099 If the first child of 'this' is a TiXmlText, the GetText()
1100 returns the character string of the Text node, else null is returned.
1101
1102 This is a convenient method for getting the text of simple contained text:
1103 @verbatim
1104 <foo>This is text</foo>
1105 const char* str = fooElement->GetText();
1106 @endverbatim
1107
1108 'str' will be a pointer to "This is text".
1109
1110 Note that this function can be misleading. If the element foo was created from
1111 this XML:
1112 @verbatim
1113 <foo><b>This is text</b></foo>
1114 @endverbatim
1115
1116 then the value of str would be null. The first child node isn't a text node, it is
1117 another element. From this XML:
1118 @verbatim
1119 <foo>This is <b>text</b></foo>
1120 @endverbatim
1121 GetText() will return "This is ".
1122
1123 WARNING: GetText() accesses a child node - don't become confused with the
1124 similarly named TiXmlHandle::Text() and TiXmlNode::ToText() which are
1125 safe type casts on the referenced node.
1126 */
1127 const char* GetText() const;
1128
1129 /// Creates a new Element and returns it - the returned element is a copy.
1130 virtual TiXmlNode* Clone() const;
1131 // Print the Element to a FILE stream.
1132 virtual void Print( FILE* cfile, int depth ) const;
1133 virtual void Print(std::string&target, int depth ) const;
1134
1135 /* Attribtue parsing starts: next char past '<'
1136 returns: next char past '>'
1137 */
1138 virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
1139
1140 virtual const TiXmlElement* ToElement() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
1141 virtual TiXmlElement* ToElement() { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
1142
1143 /** Walk the XML tree visiting this node and all of its children.
1144 */
1145 virtual bool Accept( TiXmlVisitor* visitor ) const;
1146
1147 protected:
1148
1149 void CopyTo( TiXmlElement* target ) const;
1150 void ClearThis(); // like clear, but initializes 'this' object as well
1151
1152 // Used to be public [internal use]
1153 #ifdef TIXML_USE_STL
1154 virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
1155 #endif
1156 /* [internal use]
1157 Reads the "value" of the element -- another element, or text.
1158 This should terminate with the current end tag.
1159 */
1160 const char* ReadValue( const char* in, TiXmlParsingData* prevData, TiXmlEncoding encoding );
1161
1162 private:
1163 TiXmlAttributeSet attributeSet;
1164 };
1165
1166
1167 /** An XML comment.
1168 */
1169 class TiXmlComment : public TiXmlNode
1170 {
1171 public:
1172 /// Constructs an empty comment.
1173 TiXmlComment() : TiXmlNode( TiXmlNode::TINYXML_COMMENT ) {}
1174 /// Construct a comment from text.
1175 TiXmlComment( const char* _value ) : TiXmlNode( TiXmlNode::TINYXML_COMMENT ) {
1176 SetValue( _value );
1177 }
1178 TiXmlComment( const TiXmlComment& );
1179 TiXmlComment& operator=( const TiXmlComment& base );
1180
1181 virtual ~TiXmlComment() {}
1182
1183 /// Returns a copy of this Comment.
1184 virtual TiXmlNode* Clone() const;
1185 // Write this Comment to a FILE stream.
1186 virtual void Print( FILE* cfile, int depth ) const;
1187 virtual void Print( std::string& target, int depth ) const;
1188
1189 /* Attribtue parsing starts: at the ! of the !--
1190 returns: next char past '>'
1191 */
1192 virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
1193
1194 virtual const TiXmlComment* ToComment() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
1195 virtual TiXmlComment* ToComment() { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
1196
1197 /** Walk the XML tree visiting this node and all of its children.
1198 */
1199 virtual bool Accept( TiXmlVisitor* visitor ) const;
1200
1201 protected:
1202 void CopyTo( TiXmlComment* target ) const;
1203
1204 // used to be public
1205 #ifdef TIXML_USE_STL
1206 virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
1207 #endif
1208 // virtual void StreamOut( TIXML_OSTREAM * out ) const;
1209
1210 private:
1211
1212 };
1213
1214
1215 /** XML text. A text node can have 2 ways to output the next. "normal" output
1216 and CDATA. It will default to the mode it was parsed from the XML file and
1217 you generally want to leave it alone, but you can change the output mode with
1218 SetCDATA() and query it with CDATA().
1219 */
1220 class TiXmlText : public TiXmlNode
1221 {
1222 friend class TiXmlElement;
1223 public:
1224 /** Constructor for text element. By default, it is treated as
1225 normal, encoded text. If you want it be output as a CDATA text
1226 element, set the parameter _cdata to 'true'
1227 */
1228 TiXmlText (const char * initValue ) : TiXmlNode (TiXmlNode::TINYXML_TEXT)
1229 {
1230 SetValue( initValue );
1231 cdata = false;
1232 }
1233 virtual ~TiXmlText() {}
1234
1235 #ifdef TIXML_USE_STL
1236 /// Constructor.
1237 TiXmlText( const std::string& initValue ) : TiXmlNode (TiXmlNode::TINYXML_TEXT)
1238 {
1239 SetValue( initValue );
1240 cdata = false;
1241 }
1242 #endif
1243
1244 TiXmlText( const TiXmlText& copy ) : TiXmlNode( TiXmlNode::TINYXML_TEXT ) { copy.CopyTo( this ); }
1245 TiXmlText& operator=( const TiXmlText& base ) { base.CopyTo( this ); return *this; }
1246
1247 // Write this text object to a FILE stream.
1248 virtual void Print( FILE* cfile, int depth ) const;
1249 void Print(std::string& target, int depth ) const;
1250
1251 /// Queries whether this represents text using a CDATA section.
1252 bool CDATA() const { return cdata; }
1253 /// Turns on or off a CDATA representation of text.
1254 void SetCDATA( bool _cdata ) { cdata = _cdata; }
1255
1256 virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
1257
1258 virtual const TiXmlText* ToText() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
1259 virtual TiXmlText* ToText() { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
1260
1261 /** Walk the XML tree visiting this node and all of its children.
1262 */
1263 virtual bool Accept( TiXmlVisitor* content ) const;
1264
1265 protected :
1266 /// [internal use] Creates a new Element and returns it.
1267 virtual TiXmlNode* Clone() const;
1268 void CopyTo( TiXmlText* target ) const;
1269
1270 bool Blank() const; // returns true if all white space and new lines
1271 // [internal use]
1272 #ifdef TIXML_USE_STL
1273 virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
1274 #endif
1275
1276 private:
1277 bool cdata; // true if this should be input and output as a CDATA style text element
1278 };
1279
1280
1281 /** In correct XML the declaration is the first entry in the file.
1282 @verbatim
1283 <?xml version="1.0" standalone="yes"?>
1284 @endverbatim
1285
1286 TinyXml will happily read or write files without a declaration,
1287 however. There are 3 possible attributes to the declaration:
1288 version, encoding, and standalone.
1289
1290 Note: In this version of the code, the attributes are
1291 handled as special cases, not generic attributes, simply
1292 because there can only be at most 3 and they are always the same.
1293 */
1294 class TiXmlDeclaration : public TiXmlNode
1295 {
1296 public:
1297 /// Construct an empty declaration.
1298 TiXmlDeclaration() : TiXmlNode( TiXmlNode::TINYXML_DECLARATION ) {}
1299
1300 #ifdef TIXML_USE_STL
1301 /// Constructor.
1302 TiXmlDeclaration( const std::string& _version,
1303 const std::string& _encoding,
1304 const std::string& _standalone );
1305 #endif
1306
1307 /// Construct.
1308 TiXmlDeclaration( const char* _version,
1309 const char* _encoding,
1310 const char* _standalone );
1311
1312 TiXmlDeclaration( const TiXmlDeclaration& copy );
1313 TiXmlDeclaration& operator=( const TiXmlDeclaration& copy );
1314
1315 virtual ~TiXmlDeclaration() {}
1316
1317 /// Version. Will return an empty string if none was found.
1318 const char *Version() const { return version.c_str (); }
1319 /// Encoding. Will return an empty string if none was found.
1320 const char *Encoding() const { return encoding.c_str (); }
1321 /// Is this a standalone document?
1322 const char *Standalone() const { return standalone.c_str (); }
1323
1324 /// Creates a copy of this Declaration and returns it.
1325 virtual TiXmlNode* Clone() const;
1326 // Print this declaration to a FILE stream.
1327 virtual void Print( FILE* cfile, int depth, TIXML_STRING* str ) const;
1328 virtual void Print( FILE* cfile, int depth ) const {
1329 Print( cfile, depth, 0 );
1330 }
1331 void Print( std::string& target, int /*depth*/) const;
1332
1333 virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
1334
1335 virtual const TiXmlDeclaration* ToDeclaration() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
1336 virtual TiXmlDeclaration* ToDeclaration() { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
1337
1338 /** Walk the XML tree visiting this node and all of its children.
1339 */
1340 virtual bool Accept( TiXmlVisitor* visitor ) const;
1341
1342 protected:
1343 void CopyTo( TiXmlDeclaration* target ) const;
1344 // used to be public
1345 #ifdef TIXML_USE_STL
1346 virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
1347 #endif
1348
1349 private:
1350
1351 TIXML_STRING version;
1352 TIXML_STRING encoding;
1353 TIXML_STRING standalone;
1354 };
1355
1356
1357 /** Any tag that tinyXml doesn't recognize is saved as an
1358 unknown. It is a tag of text, but should not be modified.
1359 It will be written back to the XML, unchanged, when the file
1360 is saved.
1361
1362 DTD tags get thrown into TiXmlUnknowns.
1363 */
1364 class TiXmlUnknown : public TiXmlNode
1365 {
1366 public:
1367 TiXmlUnknown() : TiXmlNode( TiXmlNode::TINYXML_UNKNOWN ) {}
1368 virtual ~TiXmlUnknown() {}
1369
1370 TiXmlUnknown( const TiXmlUnknown& copy ) : TiXmlNode( TiXmlNode::TINYXML_UNKNOWN ) { copy.CopyTo( this ); }
1371 TiXmlUnknown& operator=( const TiXmlUnknown& copy ) { copy.CopyTo( this ); return *this; }
1372
1373 /// Creates a copy of this Unknown and returns it.
1374 virtual TiXmlNode* Clone() const;
1375 // Print this Unknown to a FILE stream.
1376 virtual void Print( FILE* cfile, int depth ) const;
1377 virtual void Print( std::string& target, int depth ) const;
1378
1379
1380 virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
1381
1382 virtual const TiXmlUnknown* ToUnknown() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
1383 virtual TiXmlUnknown* ToUnknown() { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
1384
1385 /** Walk the XML tree visiting this node and all of its children.
1386 */
1387 virtual bool Accept( TiXmlVisitor* content ) const;
1388
1389 protected:
1390 void CopyTo( TiXmlUnknown* target ) const;
1391
1392 #ifdef TIXML_USE_STL
1393 virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
1394 #endif
1395
1396 private:
1397
1398 };
1399
1400
1401 /** Always the top level node. A document binds together all the
1402 XML pieces. It can be saved, loaded, and printed to the screen.
1403 The 'value' of a document node is the xml file name.
1404 */
1405 class TiXmlDocument : public TiXmlNode
1406 {
1407 public:
1408 /// Create an empty document, that has no name.
1409 TiXmlDocument();
1410 /// Create a document with a name. The name of the document is also the filename of the xml.
1411 TiXmlDocument( const char * documentName );
1412
1413 #ifdef TIXML_USE_STL
1414 /// Constructor.
1415 TiXmlDocument( const std::string& documentName );
1416 #endif
1417
1418 TiXmlDocument( const TiXmlDocument& copy );
1419 TiXmlDocument& operator=( const TiXmlDocument& copy );
1420
1421 virtual ~TiXmlDocument() {}
1422
1423 /** Load a file using the current document value.
1424 Returns true if successful. Will delete any existing
1425 document data before loading.
1426 */
1427 bool LoadFile( TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
1428 /// Save a file using the current document value. Returns true if successful.
1429 bool SaveFile() const;
1430 /// Load a file using the given filename. Returns true if successful.
1431 bool LoadFile( const char * filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
1432 /// Save a file using the given filename. Returns true if successful.
1433 bool SaveFile( const char * filename ) const;
1434 /** Load a file using the given FILE*. Returns true if successful. Note that this method
1435 doesn't stream - the entire object pointed at by the FILE*
1436 will be interpreted as an XML file. TinyXML doesn't stream in XML from the current
1437 file location. Streaming may be added in the future.
1438 */
1439 bool LoadFile( FILE*, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
1440 /// Save a file using the given FILE*. Returns true if successful.
1441 bool SaveFile( FILE* ) const;
1442 // loads and parses the buffer from an in-memory buffer
1443 bool FromMemory(const char* source, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
1444
1445 #ifdef TIXML_USE_STL
1446 bool LoadFile( const std::string& filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING ) ///< STL std::string version.
1447 {
1448 return LoadFile( filename.c_str(), encoding );
1449 }
1450 bool SaveFile( const std::string& filename ) const ///< STL std::string version.
1451 {
1452 return SaveFile( filename.c_str() );
1453 }
1454 #endif
1455
1456 /** Parse the given null terminated block of xml data. Passing in an encoding to this
1457 method (either TIXML_ENCODING_LEGACY or TIXML_ENCODING_UTF8 will force TinyXml
1458 to use that encoding, regardless of what TinyXml might otherwise try to detect.
1459 */
1460 virtual const char* Parse( const char* p, TiXmlParsingData* data = 0, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
1461
1462 /** Get the root element -- the only top level element -- of the document.
1463 In well formed XML, there should only be one. TinyXml is tolerant of
1464 multiple elements at the document level.
1465 */
1466 const TiXmlElement* RootElement() const { return FirstChildElement(); }
1467 TiXmlElement* RootElement() { return FirstChildElement(); }
1468
1469 /** If an error occurs, Error will be set to true. Also,
1470 - The ErrorId() will contain the integer identifier of the error (not generally useful)
1471 - The ErrorDesc() method will return the name of the error. (very useful)
1472 - The ErrorRow() and ErrorCol() will return the location of the error (if known)
1473 */
1474 bool Error() const { return error; }
1475
1476 /// Contains a textual (english) description of the error if one occurs.
1477 const char * ErrorDesc() const { return errorDesc.c_str (); }
1478
1479 /** Generally, you probably want the error string ( ErrorDesc() ). But if you
1480 prefer the ErrorId, this function will fetch it.
1481 */
1482 int ErrorId() const { return errorId; }
1483
1484 /** Returns the location (if known) of the error. The first column is column 1,
1485 and the first row is row 1. A value of 0 means the row and column wasn't applicable
1486 (memory errors, for example, have no row/column) or the parser lost the error. (An
1487 error in the error reporting, in that case.)
1488
1489 @sa SetTabSize, Row, Column
1490 */
1491 int ErrorRow() const { return errorLocation.row+1; }
1492 int ErrorCol() const { return errorLocation.col+1; } ///< The column where the error occured. See ErrorRow()
1493
1494 /** SetTabSize() allows the error reporting functions (ErrorRow() and ErrorCol())
1495 to report the correct values for row and column. It does not change the output
1496 or input in any way.
1497
1498 By calling this method, with a tab size
1499 greater than 0, the row and column of each node and attribute is stored
1500 when the file is loaded. Very useful for tracking the DOM back in to
1501 the source file.
1502
1503 The tab size is required for calculating the location of nodes. If not
1504 set, the default of 4 is used. The tabsize is set per document. Setting
1505 the tabsize to 0 disables row/column tracking.
1506
1507 Note that row and column tracking is not supported when using operator>>.
1508
1509 The tab size needs to be enabled before the parse or load. Correct usage:
1510 @verbatim
1511 TiXmlDocument doc;
1512 doc.SetTabSize( 8 );
1513 doc.Load( "myfile.xml" );
1514 @endverbatim
1515
1516 @sa Row, Column
1517 */
1518 void SetTabSize( int _tabsize ) { tabsize = _tabsize; }
1519
1520 int TabSize() const { return tabsize; }
1521
1522 /** If you have handled the error, it can be reset with this call. The error
1523 state is automatically cleared if you Parse a new XML block.
1524 */
1525 void ClearError() { error = false;
1526 errorId = 0;
1527 errorDesc = "";
1528 errorLocation.row = errorLocation.col = 0;
1529 //errorLocation.last = 0;
1530 }
1531
1532 /** Write the document to standard out using formatted printing ("pretty print"). */
1533 void Print() const { Print( stdout, 0 ); }
1534
1535 /* Write the document to a string using formatted printing ("pretty print"). This
1536 will allocate a character array (new char[]) and return it as a pointer. The
1537 calling code pust call delete[] on the return char* to avoid a memory leak.
1538 */
1539 //char* PrintToMemory() const;
1540
1541 /// Print this Document to a FILE stream.
1542 virtual void Print( FILE* cfile, int depth = 0 ) const;
1543 virtual void Print( std::string &, int depth = 0 ) const;
1544 // [internal use]
1545 void SetError( int err, const char* errorLocation, TiXmlParsingData* prevData, TiXmlEncoding encoding );
1546
1547 virtual const TiXmlDocument* ToDocument() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
1548 virtual TiXmlDocument* ToDocument() { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
1549
1550 /** Walk the XML tree visiting this node and all of its children.
1551 */
1552 virtual bool Accept( TiXmlVisitor* content ) const;
1553
1554 protected :
1555 // [internal use]
1556 virtual TiXmlNode* Clone() const;
1557 #ifdef TIXML_USE_STL
1558 virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
1559 #endif
1560
1561 private:
1562 void CopyTo( TiXmlDocument* target ) const;
1563 char *FixLineFeeds(char* buf, size_t length);
1564
1565 bool error;
1566 int errorId;
1567 TIXML_STRING errorDesc;
1568 int tabsize;
1569 TiXmlCursor errorLocation;
1570 bool useMicrosoftBOM; // the UTF-8 BOM were found when read. Note this, and try to write.
1571 };
1572
1573
1574 /**
1575 A TiXmlHandle is a class that wraps a node pointer with null checks; this is
1576 an incredibly useful thing. Note that TiXmlHandle is not part of the TinyXml
1577 DOM structure. It is a separate utility class.
1578
1579 Take an example:
1580 @verbatim
1581 <Document>
1582 <Element attributeA = "valueA">
1583 <Child attributeB = "value1" />
1584 <Child attributeB = "value2" />
1585 </Element>
1586 <Document>
1587 @endverbatim
1588
1589 Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very
1590 easy to write a *lot* of code that looks like:
1591
1592 @verbatim
1593 TiXmlElement* root = document.FirstChildElement( "Document" );
1594 if ( root )
1595 {
1596 TiXmlElement* element = root->FirstChildElement( "Element" );
1597 if ( element )
1598 {
1599 TiXmlElement* child = element->FirstChildElement( "Child" );
1600 if ( child )
1601 {
1602 TiXmlElement* child2 = child->NextSiblingElement( "Child" );
1603 if ( child2 )
1604 {
1605 // Finally do something useful.
1606 @endverbatim
1607
1608 And that doesn't even cover "else" cases. TiXmlHandle addresses the verbosity
1609 of such code. A TiXmlHandle checks for null pointers so it is perfectly safe
1610 and correct to use:
1611
1612 @verbatim
1613 TiXmlHandle docHandle( &document );
1614 TiXmlElement* child2 = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", 1 ).ToElement();
1615 if ( child2 )
1616 {
1617 // do something useful
1618 @endverbatim
1619
1620 Which is MUCH more concise and useful.
1621
1622 It is also safe to copy handles - internally they are nothing more than node pointers.
1623 @verbatim
1624 TiXmlHandle handleCopy = handle;
1625 @endverbatim
1626
1627 What they should not be used for is iteration:
1628
1629 @verbatim
1630 int i=0;
1631 while ( true )
1632 {
1633 TiXmlElement* child = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", i ).ToElement();
1634 if ( !child )
1635 break;
1636 // do something
1637 ++i;
1638 }
1639 @endverbatim
1640
1641 It seems reasonable, but it is in fact two embedded while loops. The Child method is
1642 a linear walk to find the element, so this code would iterate much more than it needs
1643 to. Instead, prefer:
1644
1645 @verbatim
1646 TiXmlElement* child = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).FirstChild( "Child" ).ToElement();
1647
1648 for( child; child; child=child->NextSiblingElement() )
1649 {
1650 // do something
1651 }
1652 @endverbatim
1653 */
1654 class TiXmlHandle
1655 {
1656 public:
1657 /// Create a handle from any node (at any depth of the tree.) This can be a null pointer.
1658 TiXmlHandle( TiXmlNode* _node ) { this->node = _node; }
1659 /// Copy constructor
1660 TiXmlHandle( const TiXmlHandle& ref ) { this->node = ref.node; }
1661 TiXmlHandle operator=( const TiXmlHandle& ref ) { if ( &ref != this ) this->node = ref.node; return *this; }
1662
1663 /// Return a handle to the first child node.
1664 TiXmlHandle FirstChild() const;
1665 /// Return a handle to the first child node with the given name.
1666 TiXmlHandle FirstChild( const char * value ) const;
1667 /// Return a handle to the first child element.
1668 TiXmlHandle FirstChildElement() const;
1669 /// Return a handle to the first child element with the given name.
1670 TiXmlHandle FirstChildElement( const char * value ) const;
1671
1672 /** Return a handle to the "index" child with the given name.
1673 The first child is 0, the second 1, etc.
1674 */
1675 TiXmlHandle Child( const char* value, int index ) const;
1676 /** Return a handle to the "index" child.
1677 The first child is 0, the second 1, etc.
1678 */
1679 TiXmlHandle Child( int index ) const;
1680 /** Return a handle to the "index" child element with the given name.
1681 The first child element is 0, the second 1, etc. Note that only TiXmlElements
1682 are indexed: other types are not counted.
1683 */
1684 TiXmlHandle ChildElement( const char* value, int index ) const;
1685 /** Return a handle to the "index" child element.
1686 The first child element is 0, the second 1, etc. Note that only TiXmlElements
1687 are indexed: other types are not counted.
1688 */
1689 TiXmlHandle ChildElement( int index ) const;
1690
1691 #ifdef TIXML_USE_STL
1692 TiXmlHandle FirstChild( const std::string& _value ) const { return FirstChild( _value.c_str() ); }
1693 TiXmlHandle FirstChildElement( const std::string& _value ) const { return FirstChildElement( _value.c_str() ); }
1694
1695 TiXmlHandle Child( const std::string& _value, int index ) const { return Child( _value.c_str(), index ); }
1696 TiXmlHandle ChildElement( const std::string& _value, int index ) const { return ChildElement( _value.c_str(), index ); }
1697 #endif
1698
1699 /** Return the handle as a TiXmlNode. This may return null.
1700 */
1701 TiXmlNode* ToNode() const { return node; }
1702 /** Return the handle as a TiXmlElement. This may return null.
1703 */
1704 TiXmlElement* ToElement() const { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); }
1705 /** Return the handle as a TiXmlText. This may return null.
1706 */
1707 TiXmlText* ToText() const { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); }
1708 /** Return the handle as a TiXmlUnknown. This may return null.
1709 */
1710 TiXmlUnknown* ToUnknown() const { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); }
1711
1712 /** @deprecated use ToNode.
1713 Return the handle as a TiXmlNode. This may return null.
1714 */
1715 TiXmlNode* Node() const { return ToNode(); }
1716 /** @deprecated use ToElement.
1717 Return the handle as a TiXmlElement. This may return null.
1718 */
1719 TiXmlElement* Element() const { return ToElement(); }
1720 /** @deprecated use ToText()
1721 Return the handle as a TiXmlText. This may return null.
1722 */
1723 TiXmlText* Text() const { return ToText(); }
1724 /** @deprecated use ToUnknown()
1725 Return the handle as a TiXmlUnknown. This may return null.
1726 */
1727 TiXmlUnknown* Unknown() const { return ToUnknown(); }
1728
1729 private:
1730 TiXmlNode* node;
1731 };
1732
1733
1734 /** Print to memory functionality. The TiXmlPrinter is useful when you need to:
1735
1736 -# Print to memory (especially in non-STL mode)
1737 -# Control formatting (line endings, etc.)
1738
1739 When constructed, the TiXmlPrinter is in its default "pretty printing" mode.
1740 Before calling Accept() you can call methods to control the printing
1741 of the XML document. After TiXmlNode::Accept() is called, the printed document can
1742 be accessed via the CStr(), Str(), and Size() methods.
1743
1744 TiXmlPrinter uses the Visitor API.
1745 @verbatim
1746 TiXmlPrinter printer;
1747 printer.SetIndent( "\t" );
1748
1749 doc.Accept( &printer );
1750 fprintf( stdout, "%s", printer.CStr() );
1751 @endverbatim
1752 */
1753 class TiXmlPrinter : public TiXmlVisitor
1754 {
1755 public:
1756 TiXmlPrinter() : depth( 0 ), simpleTextPrint( false ),
1757 buffer(), indent( " " ), lineBreak( "\n" ) {}
1758
1759 virtual bool VisitEnter( const TiXmlDocument& doc );
1760 virtual bool VisitExit( const TiXmlDocument& doc );
1761
1762 virtual bool VisitEnter( const TiXmlElement& element, const TiXmlAttribute* firstAttribute );
1763 virtual bool VisitExit( const TiXmlElement& element );
1764
1765 virtual bool Visit( const TiXmlDeclaration& declaration );
1766 virtual bool Visit( const TiXmlText& text );
1767 virtual bool Visit( const TiXmlComment& comment );
1768 virtual bool Visit( const TiXmlUnknown& unknown );
1769
1770 /** Set the indent characters for printing. By default 4 spaces
1771 but tab (\t) is also useful, or null/empty string for no indentation.
1772 */
1773 void SetIndent( const char* _indent ) { indent = _indent ? _indent : "" ; }
1774 /// Query the indention string.
1775 const char* Indent() { return indent.c_str(); }
1776 /** Set the line breaking string. By default set to newline (\n).
1777 Some operating systems prefer other characters, or can be
1778 set to the null/empty string for no indenation.
1779 */
1780 void SetLineBreak( const char* _lineBreak ) { lineBreak = _lineBreak ? _lineBreak : ""; }
1781 /// Query the current line breaking string.
1782 const char* LineBreak() { return lineBreak.c_str(); }
1783
1784 /** Switch over to "stream printing" which is the most dense formatting without
1785 linebreaks. Common when the XML is needed for network transmission.
1786 */
1787 void SetStreamPrinting() { indent = "";
1788 lineBreak = "";
1789 }
1790 /// Return the result.
1791 const char* CStr() { return buffer.c_str(); }
1792 /// Return the length of the result string.
1793 size_t Size() { return buffer.size(); }
1794
1795 #ifdef TIXML_USE_STL
1796 /// Return the result.
1797 const std::string& Str() { return buffer; }
1798 #endif
1799
1800 private:
1801 void DoIndent() {
1802 for( int i=0; i<depth; ++i )
1803 buffer += indent;
1804 }
1805 void DoLineBreak() {
1806 buffer += lineBreak;
1807 }
1808
1809 int depth;
1810 bool simpleTextPrint;
1811 TIXML_STRING buffer;
1812 TIXML_STRING indent;
1813 TIXML_STRING lineBreak;
1814 };
1815
1816
1817 #ifdef _MSC_VER
1818 #pragma warning( pop )
1819 #endif
1820
1821 #endif
1822