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