Changeset 738 for code/branches/FICN/src/tinyxml/tinyxml.h
- Timestamp:
- Dec 31, 2007, 12:06:33 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/FICN/src/tinyxml/tinyxml.h
r471 r738 1 1 /* 2 2 www.sourceforge.net/projects/tinyxml 3 Original code (2.0 and earlier )copyright (c) 2000-200 2Lee Thomason (www.grinninglizard.com)3 Original code (2.0 and earlier )copyright (c) 2000-2006 Lee Thomason (www.grinninglizard.com) 4 4 5 5 This software is provided 'as-is', without any express or implied … … 26 26 #ifndef TINYXML_INCLUDED 27 27 #define TINYXML_INCLUDED 28 29 #define TIXML_USE_STL30 28 31 29 #ifdef _MSC_VER … … 46 44 #endif 47 45 48 #if defined( DEBUG ) && defined( _MSC_VER )49 #include <windows.h>50 #define TIXML_LOG OutputDebugString51 #else52 #define TIXML_LOG printf53 #endif54 55 46 #ifdef TIXML_USE_STL 56 47 #include <string> 57 48 #include <iostream> 58 #define TIXML_STRING std::string 59 #define TIXML_ISTREAM std::istream 60 #define TIXML_OSTREAM std::ostream 49 #include <sstream> 50 #define TIXML_STRING std::string 61 51 #else 62 52 #include "tinystr.h" 63 #define TIXML_STRING TiXmlString 64 #define TIXML_OSTREAM TiXmlOutStream 53 #define TIXML_STRING TiXmlString 65 54 #endif 66 55 … … 69 58 // but it gets closer. There are too many compilers for me to fully 70 59 // test. If you get compilation troubles, undefine TIXML_SAFE 71 72 #define TIXML_SAFE // TinyXml isn't fully buffer overrun protected, safe code. This is work in progress. 60 #define TIXML_SAFE 61 73 62 #ifdef TIXML_SAFE 74 #if defined(_MSC_VER) && (_MSC_VER >= 1200 ) 63 #if defined(_MSC_VER) && (_MSC_VER >= 1400 ) 64 // Microsoft visual studio, version 2005 and higher. 65 #define TIXML_SNPRINTF _snprintf_s 66 #define TIXML_SNSCANF _snscanf_s 67 #define TIXML_SSCANF sscanf_s 68 #elif defined(_MSC_VER) && (_MSC_VER >= 1200 ) 75 69 // Microsoft visual studio, version 6 and higher. 76 70 //#pragma message( "Using _sn* functions." ) 77 71 #define TIXML_SNPRINTF _snprintf 78 72 #define TIXML_SNSCANF _snscanf 73 #define TIXML_SSCANF sscanf 79 74 #elif defined(__GNUC__) && (__GNUC__ >= 3 ) 80 75 // GCC version 3 and higher.s … … 82 77 #define TIXML_SNPRINTF snprintf 83 78 #define TIXML_SNSCANF snscanf 84 #endif 85 #endif 79 #define TIXML_SSCANF sscanf 80 #else 81 #define TIXML_SSCANF sscanf 82 #endif 83 #endif 86 84 87 85 class TiXmlDocument; … … 95 93 96 94 const int TIXML_MAJOR_VERSION = 2; 97 const int TIXML_MINOR_VERSION = 4;98 const int TIXML_PATCH_VERSION = 2;99 100 /* Internal structure for tracking location of items 95 const int TIXML_MINOR_VERSION = 5; 96 const int TIXML_PATCH_VERSION = 3; 97 98 /* Internal structure for tracking location of items 101 99 in the XML file. 102 100 */ … … 111 109 112 110 111 /** 112 If you call the Accept() method, it requires being passed a TiXmlVisitor 113 class to handle callbacks. For nodes that contain other nodes (Document, Element) 114 you will get called with a VisitEnter/VisitExit pair. Nodes that are always leaves 115 are simple called with Visit(). 116 117 If you return 'true' from a Visit method, recursive parsing will continue. If you return 118 false, <b>no children of this node or its sibilings</b> will be Visited. 119 120 All flavors of Visit methods have a default implementation that returns 'true' (continue 121 visiting). You need to only override methods that are interesting to you. 122 123 Generally Accept() is called on the TiXmlDocument, although all nodes suppert Visiting. 124 125 You should never change the document from a callback. 126 127 @sa TiXmlNode::Accept() 128 */ 129 class TiXmlVisitor 130 { 131 public: 132 virtual ~TiXmlVisitor() {} 133 134 /// Visit a document. 135 virtual bool VisitEnter( const TiXmlDocument& /*doc*/ ) { return true; } 136 /// Visit a document. 137 virtual bool VisitExit( const TiXmlDocument& /*doc*/ ) { return true; } 138 139 /// Visit an element. 140 virtual bool VisitEnter( const TiXmlElement& /*element*/, const TiXmlAttribute* /*firstAttribute*/ ) { return true; } 141 /// Visit an element. 142 virtual bool VisitExit( const TiXmlElement& /*element*/ ) { return true; } 143 144 /// Visit a declaration 145 virtual bool Visit( const TiXmlDeclaration& /*declaration*/ ) { return true; } 146 /// Visit a text node 147 virtual bool Visit( const TiXmlText& /*text*/ ) { return true; } 148 /// Visit a comment node 149 virtual bool Visit( const TiXmlComment& /*comment*/ ) { return true; } 150 /// Visit an unknow node 151 virtual bool Visit( const TiXmlUnknown& /*unknown*/ ) { return true; } 152 }; 153 113 154 // Only used by Attribute::Query functions 114 enum 115 { 155 enum 156 { 116 157 TIXML_SUCCESS, 117 158 TIXML_NO_ATTRIBUTE, … … 159 200 160 201 public: 161 TiXmlBase() : userData(0) {} 162 virtual ~TiXmlBase() {} 163 164 /** All TinyXml classes can print themselves to a filestream. 165 This is a formatted print, and will insert tabs and newlines. 166 202 TiXmlBase() : userData(0) {} 203 virtual ~TiXmlBase() {} 204 205 /** All TinyXml classes can print themselves to a filestream 206 or the string class (TiXmlString in non-STL mode, std::string 207 in STL mode.) Either or both cfile and str can be null. 208 209 This is a formatted print, and will insert 210 tabs and newlines. 211 167 212 (For an unformatted stream, use the << operator.) 168 213 */ … … 173 218 are provided to set whether or not TinyXml will condense all white space 174 219 into a single space or not. The default is to condense. Note changing this 175 value sis not thread safe.220 value is not thread safe. 176 221 */ 177 222 static void SetCondenseWhiteSpace( bool condense ) { condenseWhiteSpace = condense; } … … 201 246 int Column() const { return location.col + 1; } ///< See Row() 202 247 203 void SetUserData( void* user ) { userData = user; } 204 void* GetUserData() { return userData; } 248 void SetUserData( void* user ) { userData = user; } ///< Set a pointer to arbitrary user data. 249 void* GetUserData() { return userData; } ///< Get a pointer to arbitrary user data. 250 const void* GetUserData() const { return userData; } ///< Get a pointer to arbitrary user data. 205 251 206 252 // Table that returs, for a given lead byte, the total number of bytes … … 208 254 static const int utf8ByteTable[256]; 209 255 210 virtual const char* Parse( const char* p, 211 TiXmlParsingData* data, 256 virtual const char* Parse( const char* p, 257 TiXmlParsingData* data, 212 258 TiXmlEncoding encoding /*= TIXML_ENCODING_UNKNOWN */ ) = 0; 259 260 /** Expands entities in a string. Note this should not contian the tag's '<', '>', etc, 261 or they will be transformed into entities! 262 */ 263 static void EncodeString( const TIXML_STRING& str, TIXML_STRING* out ); 213 264 214 265 enum … … 230 281 TIXML_ERROR_EMBEDDED_NULL, 231 282 TIXML_ERROR_PARSING_CDATA, 283 TIXML_ERROR_DOCUMENT_TOP_ONLY, 232 284 233 285 TIXML_ERROR_STRING_COUNT … … 236 288 protected: 237 289 238 // See STL_STRING_BUG 239 // Utility class to overcome a bug. 240 class StringToBuffer 241 { 242 public: 243 StringToBuffer( const TIXML_STRING& str ); 244 ~StringToBuffer(); 245 char* buffer; 246 }; 247 248 static const char* SkipWhiteSpace( const char*, TiXmlEncoding encoding ); 249 inline static bool IsWhiteSpace( char c ) 250 { 251 return ( isspace( (unsigned char) c ) || c == '\n' || c == '\r' ); 252 } 253 254 virtual void StreamOut (TIXML_OSTREAM *) const = 0; 290 static const char* SkipWhiteSpace( const char*, TiXmlEncoding encoding ); 291 inline static bool IsWhiteSpace( char c ) 292 { 293 return ( isspace( (unsigned char) c ) || c == '\n' || c == '\r' ); 294 } 295 inline static bool IsWhiteSpace( int c ) 296 { 297 if ( c < 256 ) 298 return IsWhiteSpace( (char) c ); 299 return false; // Again, only truly correct for English/Latin...but usually works. 300 } 255 301 256 302 #ifdef TIXML_USE_STL 257 static bool StreamWhiteSpace( TIXML_ISTREAM* in, TIXML_STRING * tag );258 static bool StreamTo( TIXML_ISTREAM* in, int character, TIXML_STRING * tag );303 static bool StreamWhiteSpace( std::istream * in, TIXML_STRING * tag ); 304 static bool StreamTo( std::istream * in, int character, TIXML_STRING * tag ); 259 305 #endif 260 306 … … 285 331 if ( encoding == TIXML_ENCODING_UTF8 ) 286 332 { 287 *length = utf8ByteTable[ *(( unsigned char*)p) ];333 *length = utf8ByteTable[ *((const unsigned char*)p) ]; 288 334 assert( *length >= 0 && *length < 5 ); 289 335 } … … 316 362 } 317 363 318 // Puts a string to a stream, expanding entities as it goes.319 // Note this should not contian the '<', '>', etc, or they will be transformed into entities!320 static void PutString( const TIXML_STRING& str, TIXML_OSTREAM* out );321 322 static void PutString( const TIXML_STRING& str, TIXML_STRING* out );323 324 364 // Return true if the next characters in the stream are any of the endTag sequences. 325 365 // Ignore case only works for english, and should only be relied on when comparing … … 336 376 /// Field containing a generic user pointer 337 377 void* userData; 338 378 339 379 // None of these methods are reliable for any language except English. 340 380 // Good for approximation, not great for accuracy. … … 388 428 389 429 public: 390 #ifdef TIXML_USE_STL 430 #ifdef TIXML_USE_STL 391 431 392 432 /** An input stream operator, for every class. Tolerant of newlines and … … 402 442 a node to a stream is very well defined. You'll get a nice stream 403 443 of output, without any extra whitespace or newlines. 404 444 405 445 But reading is not as well defined. (As it always is.) If you create 406 446 a TiXmlElement (for example) and read that from an input stream, … … 410 450 A TiXmlDocument will read nodes until it reads a root element, and 411 451 all the children of that root element. 412 */ 452 */ 413 453 friend std::ostream& operator<< (std::ostream& out, const TiXmlNode& base); 414 454 … … 416 456 friend std::string& operator<< (std::string& out, const TiXmlNode& base ); 417 457 418 #else419 // Used internally, not part of the public API.420 friend TIXML_OSTREAM& operator<< (TIXML_OSTREAM& out, const TiXmlNode& base);421 458 #endif 422 459 … … 459 496 #endif 460 497 498 const TIXML_STRING& ValueTStr() const { return value; } 499 461 500 /** Changes the value of the node. Defined as: 462 501 @verbatim … … 472 511 #ifdef TIXML_USE_STL 473 512 /// STL std::string form. 474 void SetValue( const std::string& _value ) 475 { 476 StringToBuffer buf( _value ); 477 SetValue( buf.buffer ? buf.buffer : "" ); 478 } 513 void SetValue( const std::string& _value ) { value = _value; } 479 514 #endif 480 515 … … 486 521 const TiXmlNode* Parent() const { return parent; } 487 522 488 const TiXmlNode* FirstChild() const { return firstChild; }///< The first child of this node. Will be null if there are no children.489 TiXmlNode* FirstChild() { return firstChild; }523 const TiXmlNode* FirstChild() const { return firstChild; } ///< The first child of this node. Will be null if there are no children. 524 TiXmlNode* FirstChild() { return firstChild; } 490 525 const TiXmlNode* FirstChild( const char * value ) const; ///< The first child of this node with the matching 'value'. Will be null if none found. 491 TiXmlNode* FirstChild( const char * value ); ///< The first child of this node with the matching 'value'. Will be null if none found. 492 526 /// The first child of this node with the matching 'value'. Will be null if none found. 527 TiXmlNode* FirstChild( const char * _value ) { 528 // Call through to the const version - safe since nothing is changed. Exiting syntax: cast this to a const (always safe) 529 // call the method, cast the return back to non-const. 530 return const_cast< TiXmlNode* > ((const_cast< const TiXmlNode* >(this))->FirstChild( _value )); 531 } 493 532 const TiXmlNode* LastChild() const { return lastChild; } /// The last child of this node. Will be null if there are no children. 494 533 TiXmlNode* LastChild() { return lastChild; } 534 495 535 const TiXmlNode* LastChild( const char * value ) const; /// The last child of this node matching 'value'. Will be null if there are no children. 496 TiXmlNode* LastChild( const char * value ); 536 TiXmlNode* LastChild( const char * _value ) { 537 return const_cast< TiXmlNode* > ((const_cast< const TiXmlNode* >(this))->LastChild( _value )); 538 } 497 539 498 540 #ifdef TIXML_USE_STL … … 520 562 */ 521 563 const TiXmlNode* IterateChildren( const TiXmlNode* previous ) const; 522 TiXmlNode* IterateChildren( TiXmlNode* previous ); 564 TiXmlNode* IterateChildren( const TiXmlNode* previous ) { 565 return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->IterateChildren( previous ) ); 566 } 523 567 524 568 /// This flavor of IterateChildren searches for children with a particular 'value' 525 569 const TiXmlNode* IterateChildren( const char * value, const TiXmlNode* previous ) const; 526 TiXmlNode* IterateChildren( const char * value, TiXmlNode* previous ); 570 TiXmlNode* IterateChildren( const char * _value, const TiXmlNode* previous ) { 571 return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->IterateChildren( _value, previous ) ); 572 } 527 573 528 574 #ifdef TIXML_USE_STL 529 575 const TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) const { return IterateChildren (_value.c_str (), previous); } ///< STL std::string form. 530 TiXmlNode* IterateChildren( const std::string& _value, TiXmlNode* previous ) { return IterateChildren (_value.c_str (), previous); } ///< STL std::string form.576 TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) { return IterateChildren (_value.c_str (), previous); } ///< STL std::string form. 531 577 #endif 532 578 … … 572 618 /// Navigate to a sibling node. 573 619 const TiXmlNode* PreviousSibling( const char * ) const; 574 TiXmlNode* PreviousSibling( const char * ); 620 TiXmlNode* PreviousSibling( const char *_prev ) { 621 return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->PreviousSibling( _prev ) ); 622 } 575 623 576 624 #ifdef TIXML_USE_STL … … 587 635 /// Navigate to a sibling node with the given 'value'. 588 636 const TiXmlNode* NextSibling( const char * ) const; 589 TiXmlNode* NextSibling( const char * ); 637 TiXmlNode* NextSibling( const char* _next ) { 638 return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->NextSibling( _next ) ); 639 } 590 640 591 641 /** Convenience function to get through elements. … … 594 644 */ 595 645 const TiXmlElement* NextSiblingElement() const; 596 TiXmlElement* NextSiblingElement(); 646 TiXmlElement* NextSiblingElement() { 647 return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->NextSiblingElement() ); 648 } 597 649 598 650 /** Convenience function to get through elements. … … 601 653 */ 602 654 const TiXmlElement* NextSiblingElement( const char * ) const; 603 TiXmlElement* NextSiblingElement( const char * ); 655 TiXmlElement* NextSiblingElement( const char *_next ) { 656 return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->NextSiblingElement( _next ) ); 657 } 604 658 605 659 #ifdef TIXML_USE_STL … … 610 664 /// Convenience function to get through elements. 611 665 const TiXmlElement* FirstChildElement() const; 612 TiXmlElement* FirstChildElement(); 666 TiXmlElement* FirstChildElement() { 667 return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->FirstChildElement() ); 668 } 613 669 614 670 /// Convenience function to get through elements. 615 const TiXmlElement* FirstChildElement( const char * value ) const; 616 TiXmlElement* FirstChildElement( const char * value ); 671 const TiXmlElement* FirstChildElement( const char * _value ) const; 672 TiXmlElement* FirstChildElement( const char * _value ) { 673 return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->FirstChildElement( _value ) ); 674 } 617 675 618 676 #ifdef TIXML_USE_STL … … 631 689 */ 632 690 const TiXmlDocument* GetDocument() const; 633 TiXmlDocument* GetDocument(); 691 TiXmlDocument* GetDocument() { 692 return const_cast< TiXmlDocument* >( (const_cast< const TiXmlNode* >(this))->GetDocument() ); 693 } 634 694 635 695 /// Returns true if this node has no children. 636 696 bool NoChildren() const { return !firstChild; } 637 697 638 const TiXmlDocument* ToDocument() const { return ( this && type == DOCUMENT ) ? (const TiXmlDocument*) this : 0; } ///< Cast to a more defined type. Will return nullnot of the requested type.639 const TiXmlElement* ToElement() const { return ( this && type == ELEMENT ) ? (const TiXmlElement*) this : 0; } ///< Cast to a more defined type. Will return nullnot of the requested type.640 const TiXmlComment* ToComment() const { return ( this && type == COMMENT ) ? (const TiXmlComment*) this : 0; } ///< Cast to a more defined type. Will return nullnot of the requested type.641 const TiXmlUnknown* ToUnknown() const { return ( this && type == UNKNOWN ) ? (const TiXmlUnknown*) this : 0; } ///< Cast to a more defined type. Will return nullnot of the requested type.642 const TiXmlText* ToText() const { return ( this && type == TEXT ) ? (const TiXmlText*) this : 0; } ///< Cast to a more defined type. Will return nullnot of the requested type.643 const TiXmlDeclaration* ToDeclaration() const { return ( this && type == DECLARATION ) ? (const TiXmlDeclaration*) this : 0; } ///< Cast to a more defined type. Will return nullnot of the requested type.644 645 TiXmlDocument* ToDocument() { return ( this && type == DOCUMENT ) ? (TiXmlDocument*) this : 0; } ///< Cast to a more defined type. Will return nullnot of the requested type.646 TiXmlElement* ToElement() { return ( this && type == ELEMENT ) ? (TiXmlElement*) this : 0; } ///< Cast to a more defined type. Will return nullnot of the requested type.647 TiXmlComment* ToComment() { return ( this && type == COMMENT ) ? (TiXmlComment*) this : 0; } ///< Cast to a more defined type. Will return nullnot of the requested type.648 TiXmlUnknown* ToUnknown() { return ( this && type == UNKNOWN ) ? (TiXmlUnknown*) this : 0; } ///< Cast to a more defined type. Will return nullnot of the requested type.649 TiXmlText* ToText() { return ( this && type == TEXT ) ? (TiXmlText*) this : 0; } ///< Cast to a more defined type. Will return nullnot of the requested type.650 TiXmlDeclaration* ToDeclaration() { return ( this && type == DECLARATION ) ? (TiXmlDeclaration*) this : 0; } ///< Cast to a more defined type. Will return nullnot of the requested type.698 virtual const TiXmlDocument* ToDocument() const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type. 699 virtual const TiXmlElement* ToElement() const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type. 700 virtual const TiXmlComment* ToComment() const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type. 701 virtual const TiXmlUnknown* ToUnknown() const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type. 702 virtual const TiXmlText* ToText() const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type. 703 virtual const TiXmlDeclaration* ToDeclaration() const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type. 704 705 virtual TiXmlDocument* ToDocument() { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type. 706 virtual TiXmlElement* ToElement() { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type. 707 virtual TiXmlComment* ToComment() { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type. 708 virtual TiXmlUnknown* ToUnknown() { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type. 709 virtual TiXmlText* ToText() { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type. 710 virtual TiXmlDeclaration* ToDeclaration() { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type. 651 711 652 712 /** Create an exact duplicate of this node and return it. The memory must be deleted 653 by the caller. 713 by the caller. 654 714 */ 655 715 virtual TiXmlNode* Clone() const = 0; 716 717 /** Accept a hierchical visit the nodes in the TinyXML DOM. Every node in the 718 XML tree will be conditionally visited and the host will be called back 719 via the TiXmlVisitor interface. 720 721 This is essentially a SAX interface for TinyXML. (Note however it doesn't re-parse 722 the XML for the callbacks, so the performance of TinyXML is unchanged by using this 723 interface versus any other.) 724 725 The interface has been based on ideas from: 726 727 - http://www.saxproject.org/ 728 - http://c2.com/cgi/wiki?HierarchicalVisitorPattern 729 730 Which are both good references for "visiting". 731 732 An example of using Accept(): 733 @verbatim 734 TiXmlPrinter printer; 735 tinyxmlDoc.Accept( &printer ); 736 const char* xmlcstr = printer.CStr(); 737 @endverbatim 738 */ 739 virtual bool Accept( TiXmlVisitor* visitor ) const = 0; 656 740 657 741 protected: … … 664 748 #ifdef TIXML_USE_STL 665 749 // The real work of the input operator. 666 virtual void StreamIn( TIXML_ISTREAM* in, TIXML_STRING* tag ) = 0;750 virtual void StreamIn( std::istream* in, TIXML_STRING* tag ) = 0; 667 751 #endif 668 752 … … 726 810 } 727 811 728 const char* Name() const { return name.c_str (); } ///< Return the name of this attribute. 729 const char* Value() const { return value.c_str (); } ///< Return the value of this attribute. 812 const char* Name() const { return name.c_str(); } ///< Return the name of this attribute. 813 const char* Value() const { return value.c_str(); } ///< Return the value of this attribute. 814 #ifdef TIXML_USE_STL 815 const std::string& ValueStr() const { return value; } ///< Return the value of this attribute. 816 #endif 730 817 int IntValue() const; ///< Return the value of this attribute, converted to an integer. 731 818 double DoubleValue() const; ///< Return the value of this attribute, converted to a double. 732 819 820 // Get the tinyxml string representation 821 const TIXML_STRING& NameTStr() const { return name; } 822 733 823 /** QueryIntValue examines the value string. It is an alternative to the 734 824 IntValue() method with richer error checking. 735 If the value is an integer, it is stored in 'value' and 825 If the value is an integer, it is stored in 'value' and 736 826 the call returns TIXML_SUCCESS. If it is not 737 827 an integer, it returns TIXML_WRONG_TYPE. … … 752 842 #ifdef TIXML_USE_STL 753 843 /// STL std::string form. 754 void SetName( const std::string& _name ) 755 { 756 StringToBuffer buf( _name ); 757 SetName ( buf.buffer ? buf.buffer : "error" ); 758 } 759 /// STL std::string form. 760 void SetValue( const std::string& _value ) 761 { 762 StringToBuffer buf( _value ); 763 SetValue( buf.buffer ? buf.buffer : "error" ); 764 } 844 void SetName( const std::string& _name ) { name = _name; } 845 /// STL std::string form. 846 void SetValue( const std::string& _value ) { value = _value; } 765 847 #endif 766 848 767 849 /// Get the next sibling attribute in the DOM. Returns null at end. 768 850 const TiXmlAttribute* Next() const; 769 TiXmlAttribute* Next(); 851 TiXmlAttribute* Next() { 852 return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttribute* >(this))->Next() ); 853 } 854 770 855 /// Get the previous sibling attribute in the DOM. Returns null at beginning. 771 856 const TiXmlAttribute* Previous() const; 772 TiXmlAttribute* Previous(); 857 TiXmlAttribute* Previous() { 858 return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttribute* >(this))->Previous() ); 859 } 773 860 774 861 bool operator==( const TiXmlAttribute& rhs ) const { return rhs.name == name; } … … 782 869 783 870 // Prints this Attribute to a FILE stream. 784 virtual void Print( FILE* cfile, int depth ) const; 785 786 virtual void StreamOut( TIXML_OSTREAM * out ) const; 871 virtual void Print( FILE* cfile, int depth ) const { 872 Print( cfile, depth, 0 ); 873 } 874 void Print( FILE* cfile, int depth, TIXML_STRING* str ) const; 875 787 876 // [internal use] 788 877 // Set the document pointer so the attribute can report errors. … … 803 892 /* A class used to manage a group of attributes. 804 893 It is only used internally, both by the ELEMENT and the DECLARATION. 805 894 806 895 The set can be changed transparent to the Element and Declaration 807 896 classes that use it, but NOT transparent to the Attribute … … 827 916 TiXmlAttribute* Last() { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; } 828 917 829 const TiXmlAttribute* Find( const char * name ) const; 830 TiXmlAttribute* Find( const char * name ); 918 const TiXmlAttribute* Find( const char* _name ) const; 919 TiXmlAttribute* Find( const char* _name ) { 920 return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttributeSet* >(this))->Find( _name ) ); 921 } 922 #ifdef TIXML_USE_STL 923 const TiXmlAttribute* Find( const std::string& _name ) const; 924 TiXmlAttribute* Find( const std::string& _name ) { 925 return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttributeSet* >(this))->Find( _name ) ); 926 } 927 928 #endif 831 929 832 930 private: … … 884 982 /** QueryIntAttribute examines the attribute - it is an alternative to the 885 983 Attribute() method with richer error checking. 886 If the attribute is an integer, it is stored in 'value' and 984 If the attribute is an integer, it is stored in 'value' and 887 985 the call returns TIXML_SUCCESS. If it is not 888 986 an integer, it returns TIXML_WRONG_TYPE. If the attribute 889 987 does not exist, then TIXML_NO_ATTRIBUTE is returned. 890 */ 988 */ 891 989 int QueryIntAttribute( const char* name, int* _value ) const; 892 990 /// QueryDoubleAttribute examines the attribute - see QueryIntAttribute(). … … 902 1000 } 903 1001 1002 #ifdef TIXML_USE_STL 1003 /** Template form of the attribute query which will try to read the 1004 attribute into the specified type. Very easy, very powerful, but 1005 be careful to make sure to call this with the correct type. 1006 1007 NOTE: This method doesn't work correctly for 'string' types. 1008 1009 @return TIXML_SUCCESS, TIXML_WRONG_TYPE, or TIXML_NO_ATTRIBUTE 1010 */ 1011 template< typename T > int QueryValueAttribute( const std::string& name, T* outValue ) const 1012 { 1013 const TiXmlAttribute* node = attributeSet.Find( name ); 1014 if ( !node ) 1015 return TIXML_NO_ATTRIBUTE; 1016 1017 std::stringstream sstream( node->ValueStr() ); 1018 sstream >> *outValue; 1019 if ( !sstream.fail() ) 1020 return TIXML_SUCCESS; 1021 return TIXML_WRONG_TYPE; 1022 } 1023 /* 1024 This is - in theory - a bug fix for "QueryValueAtribute returns truncated std::string" 1025 but template specialization is hard to get working cross-compiler. Leaving the bug for now. 1026 1027 // The above will fail for std::string because the space character is used as a seperator. 1028 // Specialize for strings. Bug [ 1695429 ] QueryValueAtribute returns truncated std::string 1029 template<> int QueryValueAttribute( const std::string& name, std::string* outValue ) const 1030 { 1031 const TiXmlAttribute* node = attributeSet.Find( name ); 1032 if ( !node ) 1033 return TIXML_NO_ATTRIBUTE; 1034 *outValue = node->ValueStr(); 1035 return TIXML_SUCCESS; 1036 } 1037 */ 1038 #endif 1039 904 1040 /** Sets an attribute of name to a given value. The attribute 905 1041 will be created if it does not exist, or changed if it does. … … 908 1044 909 1045 #ifdef TIXML_USE_STL 910 const char* Attribute( const std::string& name ) const { return Attribute( name.c_str() ); }911 const char* Attribute( const std::string& name, int* i ) const { return Attribute( name.c_str(), i ); }912 const char* Attribute( const std::string& name, double* d ) const { return Attribute( name.c_str(), d ); }913 int QueryIntAttribute( const std::string& name, int* _value ) const { return QueryIntAttribute( name.c_str(), _value ); }914 int QueryDoubleAttribute( const std::string& name, double* _value ) const { return QueryDoubleAttribute( name.c_str(), _value ); }1046 const std::string* Attribute( const std::string& name ) const; 1047 const std::string* Attribute( const std::string& name, int* i ) const; 1048 const std::string* Attribute( const std::string& name, double* d ) const; 1049 int QueryIntAttribute( const std::string& name, int* _value ) const; 1050 int QueryDoubleAttribute( const std::string& name, double* _value ) const; 915 1051 916 1052 /// STL std::string form. 917 void SetAttribute( const std::string& name, const std::string& _value ) 918 { 919 StringToBuffer n( name ); 920 StringToBuffer v( _value ); 921 if ( n.buffer && v.buffer ) 922 SetAttribute (n.buffer, v.buffer ); 923 } 1053 void SetAttribute( const std::string& name, const std::string& _value ); 924 1054 ///< STL std::string form. 925 void SetAttribute( const std::string& name, int _value ) 926 { 927 StringToBuffer n( name ); 928 if ( n.buffer ) 929 SetAttribute (n.buffer, _value); 930 } 1055 void SetAttribute( const std::string& name, int _value ); 931 1056 #endif 932 1057 … … 956 1081 and concise, GetText() is limited compared to getting the TiXmlText child 957 1082 and accessing it directly. 958 1083 959 1084 If the first child of 'this' is a TiXmlText, the GetText() 960 1085 returns the character string of the Text node, else null is returned. … … 966 1091 @endverbatim 967 1092 968 'str' will be a pointer to "This is text". 969 1093 'str' will be a pointer to "This is text". 1094 970 1095 Note that this function can be misleading. If the element foo was created from 971 1096 this XML: 972 1097 @verbatim 973 <foo><b>This is text</b></foo> 1098 <foo><b>This is text</b></foo> 974 1099 @endverbatim 975 1100 … … 977 1102 another element. From this XML: 978 1103 @verbatim 979 <foo>This is <b>text</b></foo> 1104 <foo>This is <b>text</b></foo> 980 1105 @endverbatim 981 1106 GetText() will return "This is ". 982 1107 983 WARNING: GetText() accesses a child node - don't become confused with the 984 similarly named TiXmlHandle::Text() and TiXmlNode::ToText() which are 1108 WARNING: GetText() accesses a child node - don't become confused with the 1109 similarly named TiXmlHandle::Text() and TiXmlNode::ToText() which are 985 1110 safe type casts on the referenced node. 986 1111 */ … … 997 1122 virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding ); 998 1123 1124 virtual const TiXmlElement* ToElement() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type. 1125 virtual TiXmlElement* ToElement() { return this; } ///< Cast to a more defined type. Will return null not of the requested type. 1126 1127 /** Walk the XML tree visiting this node and all of its children. 1128 */ 1129 virtual bool Accept( TiXmlVisitor* visitor ) const; 1130 999 1131 protected: 1000 1132 … … 1004 1136 // Used to be public [internal use] 1005 1137 #ifdef TIXML_USE_STL 1006 virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag ); 1007 #endif 1008 virtual void StreamOut( TIXML_OSTREAM * out ) const; 1009 1138 virtual void StreamIn( std::istream * in, TIXML_STRING * tag ); 1139 #endif 1010 1140 /* [internal use] 1011 1141 Reads the "value" of the element -- another element, or text. … … 1027 1157 /// Constructs an empty comment. 1028 1158 TiXmlComment() : TiXmlNode( TiXmlNode::COMMENT ) {} 1159 /// Construct a comment from text. 1160 TiXmlComment( const char* _value ) : TiXmlNode( TiXmlNode::COMMENT ) { 1161 SetValue( _value ); 1162 } 1029 1163 TiXmlComment( const TiXmlComment& ); 1030 1164 void operator=( const TiXmlComment& base ); … … 1034 1168 /// Returns a copy of this Comment. 1035 1169 virtual TiXmlNode* Clone() const; 1036 // /Write this Comment to a FILE stream.1170 // Write this Comment to a FILE stream. 1037 1171 virtual void Print( FILE* cfile, int depth ) const; 1038 1172 … … 1042 1176 virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding ); 1043 1177 1178 virtual const TiXmlComment* ToComment() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type. 1179 virtual TiXmlComment* ToComment() { return this; } ///< Cast to a more defined type. Will return null not of the requested type. 1180 1181 /** Walk the XML tree visiting this node and all of its children. 1182 */ 1183 virtual bool Accept( TiXmlVisitor* visitor ) const; 1184 1044 1185 protected: 1045 1186 void CopyTo( TiXmlComment* target ) const; … … 1047 1188 // used to be public 1048 1189 #ifdef TIXML_USE_STL 1049 virtual void StreamIn( TIXML_ISTREAM* in, TIXML_STRING * tag );1050 #endif 1051 virtual void StreamOut( TIXML_OSTREAM * out ) const;1190 virtual void StreamIn( std::istream * in, TIXML_STRING * tag ); 1191 #endif 1192 // virtual void StreamOut( TIXML_OSTREAM * out ) const; 1052 1193 1053 1194 private: … … 1056 1197 1057 1198 1058 /** XML text. A text node can have 2 ways to output the next. "normal" output 1199 /** XML text. A text node can have 2 ways to output the next. "normal" output 1059 1200 and CDATA. It will default to the mode it was parsed from the XML file and 1060 you generally want to leave it alone, but you can change the output mode with 1201 you generally want to leave it alone, but you can change the output mode with 1061 1202 SetCDATA() and query it with CDATA(). 1062 1203 */ … … 1065 1206 friend class TiXmlElement; 1066 1207 public: 1067 /** Constructor for text element. By default, it is treated as 1208 /** Constructor for text element. By default, it is treated as 1068 1209 normal, encoded text. If you want it be output as a CDATA text 1069 1210 element, set the parameter _cdata to 'true' … … 1088 1229 void operator=( const TiXmlText& base ) { base.CopyTo( this ); } 1089 1230 1090 // /Write this text object to a FILE stream.1231 // Write this text object to a FILE stream. 1091 1232 virtual void Print( FILE* cfile, int depth ) const; 1092 1233 1093 1234 /// Queries whether this represents text using a CDATA section. 1094 bool CDATA() 1235 bool CDATA() const { return cdata; } 1095 1236 /// Turns on or off a CDATA representation of text. 1096 1237 void SetCDATA( bool _cdata ) { cdata = _cdata; } 1097 1238 1098 1239 virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding ); 1240 1241 virtual const TiXmlText* ToText() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type. 1242 virtual TiXmlText* ToText() { return this; } ///< Cast to a more defined type. Will return null not of the requested type. 1243 1244 /** Walk the XML tree visiting this node and all of its children. 1245 */ 1246 virtual bool Accept( TiXmlVisitor* content ) const; 1099 1247 1100 1248 protected : … … 1103 1251 void CopyTo( TiXmlText* target ) const; 1104 1252 1105 virtual void StreamOut ( TIXML_OSTREAM * out ) const;1106 1253 bool Blank() const; // returns true if all white space and new lines 1107 1254 // [internal use] 1108 1255 #ifdef TIXML_USE_STL 1109 virtual void StreamIn( TIXML_ISTREAM* in, TIXML_STRING * tag );1256 virtual void StreamIn( std::istream * in, TIXML_STRING * tag ); 1110 1257 #endif 1111 1258 … … 1160 1307 /// Creates a copy of this Declaration and returns it. 1161 1308 virtual TiXmlNode* Clone() const; 1162 /// Print this declaration to a FILE stream. 1163 virtual void Print( FILE* cfile, int depth ) const; 1309 // Print this declaration to a FILE stream. 1310 virtual void Print( FILE* cfile, int depth, TIXML_STRING* str ) const; 1311 virtual void Print( FILE* cfile, int depth ) const { 1312 Print( cfile, depth, 0 ); 1313 } 1164 1314 1165 1315 virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding ); 1316 1317 virtual const TiXmlDeclaration* ToDeclaration() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type. 1318 virtual TiXmlDeclaration* ToDeclaration() { return this; } ///< Cast to a more defined type. Will return null not of the requested type. 1319 1320 /** Walk the XML tree visiting this node and all of its children. 1321 */ 1322 virtual bool Accept( TiXmlVisitor* visitor ) const; 1166 1323 1167 1324 protected: … … 1169 1326 // used to be public 1170 1327 #ifdef TIXML_USE_STL 1171 virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag ); 1172 #endif 1173 virtual void StreamOut ( TIXML_OSTREAM * out) const; 1328 virtual void StreamIn( std::istream * in, TIXML_STRING * tag ); 1329 #endif 1174 1330 1175 1331 private: … … 1199 1355 /// Creates a copy of this Unknown and returns it. 1200 1356 virtual TiXmlNode* Clone() const; 1201 // /Print this Unknown to a FILE stream.1357 // Print this Unknown to a FILE stream. 1202 1358 virtual void Print( FILE* cfile, int depth ) const; 1203 1359 1204 1360 virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding ); 1361 1362 virtual const TiXmlUnknown* ToUnknown() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type. 1363 virtual TiXmlUnknown* ToUnknown() { return this; } ///< Cast to a more defined type. Will return null not of the requested type. 1364 1365 /** Walk the XML tree visiting this node and all of its children. 1366 */ 1367 virtual bool Accept( TiXmlVisitor* content ) const; 1205 1368 1206 1369 protected: … … 1208 1371 1209 1372 #ifdef TIXML_USE_STL 1210 virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag ); 1211 #endif 1212 virtual void StreamOut ( TIXML_OSTREAM * out ) const; 1373 virtual void StreamIn( std::istream * in, TIXML_STRING * tag ); 1374 #endif 1213 1375 1214 1376 private: … … 1250 1412 /// Save a file using the given filename. Returns true if successful. 1251 1413 bool SaveFile( const char * filename ) const; 1414 /** Load a file using the given FILE*. Returns true if successful. Note that this method 1415 doesn't stream - the entire object pointed at by the FILE* 1416 will be interpreted as an XML file. TinyXML doesn't stream in XML from the current 1417 file location. Streaming may be added in the future. 1418 */ 1419 bool LoadFile( FILE*, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING ); 1420 /// Save a file using the given FILE*. Returns true if successful. 1421 bool SaveFile( FILE* ) const; 1252 1422 1253 1423 #ifdef TIXML_USE_STL 1254 1424 bool LoadFile( const std::string& filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING ) ///< STL std::string version. 1255 1425 { 1256 StringToBuffer f( filename ); 1257 return ( f.buffer && LoadFile( f.buffer, encoding )); 1426 // StringToBuffer f( filename ); 1427 // return ( f.buffer && LoadFile( f.buffer, encoding )); 1428 return LoadFile( filename.c_str(), encoding ); 1258 1429 } 1259 1430 bool SaveFile( const std::string& filename ) const ///< STL std::string version. 1260 1431 { 1261 StringToBuffer f( filename ); 1262 return ( f.buffer && SaveFile( f.buffer )); 1432 // StringToBuffer f( filename ); 1433 // return ( f.buffer && SaveFile( f.buffer )); 1434 return SaveFile( filename.c_str() ); 1263 1435 } 1264 1436 #endif … … 1281 1453 - The ErrorDesc() method will return the name of the error. (very useful) 1282 1454 - The ErrorRow() and ErrorCol() will return the location of the error (if known) 1283 */ 1455 */ 1284 1456 bool Error() const { return error; } 1285 1457 … … 1292 1464 int ErrorId() const { return errorId; } 1293 1465 1294 /** Returns the location (if known) of the error. The first column is column 1, 1466 /** Returns the location (if known) of the error. The first column is column 1, 1295 1467 and the first row is row 1. A value of 0 means the row and column wasn't applicable 1296 1468 (memory errors, for example, have no row/column) or the parser lost the error. (An … … 1299 1471 @sa SetTabSize, Row, Column 1300 1472 */ 1301 int ErrorRow() { return errorLocation.row+1; }1302 int ErrorCol() { return errorLocation.col+1; } ///< The column where the error occured. See ErrorRow()1473 int ErrorRow() const { return errorLocation.row+1; } 1474 int ErrorCol() const { return errorLocation.col+1; } ///< The column where the error occured. See ErrorRow() 1303 1475 1304 1476 /** SetTabSize() allows the error reporting functions (ErrorRow() and ErrorCol()) 1305 1477 to report the correct values for row and column. It does not change the output 1306 1478 or input in any way. 1307 1479 1308 1480 By calling this method, with a tab size 1309 1481 greater than 0, the row and column of each node and attribute is stored … … 1333 1505 state is automatically cleared if you Parse a new XML block. 1334 1506 */ 1335 void ClearError() { error = false; 1336 errorId = 0; 1337 errorDesc = ""; 1338 errorLocation.row = errorLocation.col = 0; 1339 //errorLocation.last = 0; 1507 void ClearError() { error = false; 1508 errorId = 0; 1509 errorDesc = ""; 1510 errorLocation.row = errorLocation.col = 0; 1511 //errorLocation.last = 0; 1340 1512 } 1341 1513 1342 /** Dump the document to standard out. */1514 /** Write the document to standard out using formatted printing ("pretty print"). */ 1343 1515 void Print() const { Print( stdout, 0 ); } 1516 1517 /* Write the document to a string using formatted printing ("pretty print"). This 1518 will allocate a character array (new char[]) and return it as a pointer. The 1519 calling code pust call delete[] on the return char* to avoid a memory leak. 1520 */ 1521 //char* PrintToMemory() const; 1344 1522 1345 1523 /// Print this Document to a FILE stream. … … 1348 1526 void SetError( int err, const char* errorLocation, TiXmlParsingData* prevData, TiXmlEncoding encoding ); 1349 1527 1528 virtual const TiXmlDocument* ToDocument() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type. 1529 virtual TiXmlDocument* ToDocument() { return this; } ///< Cast to a more defined type. Will return null not of the requested type. 1530 1531 /** Walk the XML tree visiting this node and all of its children. 1532 */ 1533 virtual bool Accept( TiXmlVisitor* content ) const; 1534 1350 1535 protected : 1351 virtual void StreamOut ( TIXML_OSTREAM * out) const;1352 1536 // [internal use] 1353 1537 virtual TiXmlNode* Clone() const; 1354 1538 #ifdef TIXML_USE_STL 1355 virtual void StreamIn( TIXML_ISTREAM* in, TIXML_STRING * tag );1539 virtual void StreamIn( std::istream * in, TIXML_STRING * tag ); 1356 1540 #endif 1357 1541 … … 1383 1567 @endverbatim 1384 1568 1385 Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very 1569 Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very 1386 1570 easy to write a *lot* of code that looks like: 1387 1571 … … 1403 1587 1404 1588 And that doesn't even cover "else" cases. TiXmlHandle addresses the verbosity 1405 of such code. A TiXmlHandle checks for null pointers so it is perfectly safe 1589 of such code. A TiXmlHandle checks for null pointers so it is perfectly safe 1406 1590 and correct to use: 1407 1591 1408 1592 @verbatim 1409 1593 TiXmlHandle docHandle( &document ); 1410 TiXmlElement* child2 = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", 1 ). Element();1594 TiXmlElement* child2 = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", 1 ).ToElement(); 1411 1595 if ( child2 ) 1412 1596 { … … 1424 1608 1425 1609 @verbatim 1426 int i=0; 1610 int i=0; 1427 1611 while ( true ) 1428 1612 { 1429 TiXmlElement* child = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", i ). Element();1613 TiXmlElement* child = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", i ).ToElement(); 1430 1614 if ( !child ) 1431 1615 break; … … 1435 1619 @endverbatim 1436 1620 1437 It seems reasonable, but it is in fact two embedded while loops. The Child method is 1438 a linear walk to find the element, so this code would iterate much more than it needs 1621 It seems reasonable, but it is in fact two embedded while loops. The Child method is 1622 a linear walk to find the element, so this code would iterate much more than it needs 1439 1623 to. Instead, prefer: 1440 1624 1441 1625 @verbatim 1442 TiXmlElement* child = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).FirstChild( "Child" ). Element();1626 TiXmlElement* child = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).FirstChild( "Child" ).ToElement(); 1443 1627 1444 1628 for( child; child; child=child->NextSiblingElement() ) … … 1466 1650 TiXmlHandle FirstChildElement( const char * value ) const; 1467 1651 1468 /** Return a handle to the "index" child with the given name. 1652 /** Return a handle to the "index" child with the given name. 1469 1653 The first child is 0, the second 1, etc. 1470 1654 */ 1471 1655 TiXmlHandle Child( const char* value, int index ) const; 1472 /** Return a handle to the "index" child. 1656 /** Return a handle to the "index" child. 1473 1657 The first child is 0, the second 1, etc. 1474 1658 */ 1475 1659 TiXmlHandle Child( int index ) const; 1476 /** Return a handle to the "index" child element with the given name. 1660 /** Return a handle to the "index" child element with the given name. 1477 1661 The first child element is 0, the second 1, etc. Note that only TiXmlElements 1478 1662 are indexed: other types are not counted. 1479 1663 */ 1480 1664 TiXmlHandle ChildElement( const char* value, int index ) const; 1481 /** Return a handle to the "index" child element. 1665 /** Return a handle to the "index" child element. 1482 1666 The first child element is 0, the second 1, etc. Note that only TiXmlElements 1483 1667 are indexed: other types are not counted. … … 1493 1677 #endif 1494 1678 1495 /// Return the handle as a TiXmlNode. This may return null. 1496 TiXmlNode* Node() const { return node; } 1497 /// Return the handle as a TiXmlElement. This may return null. 1498 TiXmlElement* Element() const { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); } 1499 /// Return the handle as a TiXmlText. This may return null. 1500 TiXmlText* Text() const { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); } 1501 /// Return the handle as a TiXmlUnknown. This may return null; 1502 TiXmlUnknown* Unknown() const { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); } 1679 /** Return the handle as a TiXmlNode. This may return null. 1680 */ 1681 TiXmlNode* ToNode() const { return node; } 1682 /** Return the handle as a TiXmlElement. This may return null. 1683 */ 1684 TiXmlElement* ToElement() const { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); } 1685 /** Return the handle as a TiXmlText. This may return null. 1686 */ 1687 TiXmlText* ToText() const { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); } 1688 /** Return the handle as a TiXmlUnknown. This may return null. 1689 */ 1690 TiXmlUnknown* ToUnknown() const { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); } 1691 1692 /** @deprecated use ToNode. 1693 Return the handle as a TiXmlNode. This may return null. 1694 */ 1695 TiXmlNode* Node() const { return ToNode(); } 1696 /** @deprecated use ToElement. 1697 Return the handle as a TiXmlElement. This may return null. 1698 */ 1699 TiXmlElement* Element() const { return ToElement(); } 1700 /** @deprecated use ToText() 1701 Return the handle as a TiXmlText. This may return null. 1702 */ 1703 TiXmlText* Text() const { return ToText(); } 1704 /** @deprecated use ToUnknown() 1705 Return the handle as a TiXmlUnknown. This may return null. 1706 */ 1707 TiXmlUnknown* Unknown() const { return ToUnknown(); } 1503 1708 1504 1709 private: … … 1506 1711 }; 1507 1712 1713 1714 /** Print to memory functionality. The TiXmlPrinter is useful when you need to: 1715 1716 -# Print to memory (especially in non-STL mode) 1717 -# Control formatting (line endings, etc.) 1718 1719 When constructed, the TiXmlPrinter is in its default "pretty printing" mode. 1720 Before calling Accept() you can call methods to control the printing 1721 of the XML document. After TiXmlNode::Accept() is called, the printed document can 1722 be accessed via the CStr(), Str(), and Size() methods. 1723 1724 TiXmlPrinter uses the Visitor API. 1725 @verbatim 1726 TiXmlPrinter printer; 1727 printer.SetIndent( "\t" ); 1728 1729 doc.Accept( &printer ); 1730 fprintf( stdout, "%s", printer.CStr() ); 1731 @endverbatim 1732 */ 1733 class TiXmlPrinter : public TiXmlVisitor 1734 { 1735 public: 1736 TiXmlPrinter() : depth( 0 ), simpleTextPrint( false ), 1737 buffer(), indent( " " ), lineBreak( "\n" ) {} 1738 1739 virtual bool VisitEnter( const TiXmlDocument& doc ); 1740 virtual bool VisitExit( const TiXmlDocument& doc ); 1741 1742 virtual bool VisitEnter( const TiXmlElement& element, const TiXmlAttribute* firstAttribute ); 1743 virtual bool VisitExit( const TiXmlElement& element ); 1744 1745 virtual bool Visit( const TiXmlDeclaration& declaration ); 1746 virtual bool Visit( const TiXmlText& text ); 1747 virtual bool Visit( const TiXmlComment& comment ); 1748 virtual bool Visit( const TiXmlUnknown& unknown ); 1749 1750 /** Set the indent characters for printing. By default 4 spaces 1751 but tab (\t) is also useful, or null/empty string for no indentation. 1752 */ 1753 void SetIndent( const char* _indent ) { indent = _indent ? _indent : "" ; } 1754 /// Query the indention string. 1755 const char* Indent() { return indent.c_str(); } 1756 /** Set the line breaking string. By default set to newline (\n). 1757 Some operating systems prefer other characters, or can be 1758 set to the null/empty string for no indenation. 1759 */ 1760 void SetLineBreak( const char* _lineBreak ) { lineBreak = _lineBreak ? _lineBreak : ""; } 1761 /// Query the current line breaking string. 1762 const char* LineBreak() { return lineBreak.c_str(); } 1763 1764 /** Switch over to "stream printing" which is the most dense formatting without 1765 linebreaks. Common when the XML is needed for network transmission. 1766 */ 1767 void SetStreamPrinting() { indent = ""; 1768 lineBreak = ""; 1769 } 1770 /// Return the result. 1771 const char* CStr() { return buffer.c_str(); } 1772 /// Return the length of the result string. 1773 size_t Size() { return buffer.size(); } 1774 1775 #ifdef TIXML_USE_STL 1776 /// Return the result. 1777 const std::string& Str() { return buffer; } 1778 #endif 1779 1780 private: 1781 void DoIndent() { 1782 for( int i=0; i<depth; ++i ) 1783 buffer += indent; 1784 } 1785 void DoLineBreak() { 1786 buffer += lineBreak; 1787 } 1788 1789 int depth; 1790 bool simpleTextPrint; 1791 TIXML_STRING buffer; 1792 TIXML_STRING indent; 1793 TIXML_STRING lineBreak; 1794 }; 1795 1796 1508 1797 #ifdef _MSC_VER 1509 1798 #pragma warning( pop )
Note: See TracChangeset
for help on using the changeset viewer.