nuclear@0: /* nuclear@0: Original code by Lee Thomason (www.grinninglizard.com) nuclear@0: nuclear@0: This software is provided 'as-is', without any express or implied nuclear@0: warranty. In no event will the authors be held liable for any nuclear@0: damages arising from the use of this software. nuclear@0: nuclear@0: Permission is granted to anyone to use this software for any nuclear@0: purpose, including commercial applications, and to alter it and nuclear@0: redistribute it freely, subject to the following restrictions: nuclear@0: nuclear@0: 1. The origin of this software must not be misrepresented; you must nuclear@0: not claim that you wrote the original software. If you use this nuclear@0: software in a product, an acknowledgment in the product documentation nuclear@0: would be appreciated but is not required. nuclear@0: nuclear@0: 2. Altered source versions must be plainly marked as such, and nuclear@0: must not be misrepresented as being the original software. nuclear@0: nuclear@0: 3. This notice may not be removed or altered from any source nuclear@0: distribution. nuclear@0: */ nuclear@0: nuclear@0: #ifndef TINYXML2_INCLUDED nuclear@0: #define TINYXML2_INCLUDED nuclear@0: nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: nuclear@0: /* nuclear@0: TODO: intern strings instead of allocation. nuclear@0: */ nuclear@0: /* nuclear@0: gcc: g++ -Wall tinyxml2.cpp xmltest.cpp -o gccxmltest.exe nuclear@0: */ nuclear@0: nuclear@0: #if defined( _DEBUG ) || defined( DEBUG ) || defined (__DEBUG__) nuclear@0: #ifndef DEBUG nuclear@0: #define DEBUG nuclear@0: #endif nuclear@0: #endif nuclear@0: nuclear@0: nuclear@0: #if defined(DEBUG) nuclear@0: #if defined(_MSC_VER) nuclear@0: #define TIXMLASSERT( x ) if ( !(x)) { __debugbreak(); } //if ( !(x)) WinDebugBreak() nuclear@0: #elif defined (ANDROID_NDK) nuclear@0: #include nuclear@0: #define TIXMLASSERT( x ) if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); } nuclear@0: #else nuclear@0: #include nuclear@0: #define TIXMLASSERT assert nuclear@0: #endif nuclear@0: #else nuclear@0: #define TIXMLASSERT( x ) {} nuclear@0: #endif nuclear@0: nuclear@0: nuclear@0: #if defined(_MSC_VER) && (_MSC_VER >= 1400 ) nuclear@0: // Microsoft visual studio, version 2005 and higher. nuclear@0: /*int _snprintf_s( nuclear@0: char *buffer, nuclear@0: size_t sizeOfBuffer, nuclear@0: size_t count, nuclear@0: const char *format [, nuclear@0: argument] ... nuclear@0: );*/ nuclear@0: inline int TIXML_SNPRINTF( char* buffer, size_t size, const char* format, ... ) { nuclear@0: va_list va; nuclear@0: va_start( va, format ); nuclear@0: int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va ); nuclear@0: va_end( va ); nuclear@0: return result; nuclear@0: } nuclear@0: #define TIXML_SSCANF sscanf_s nuclear@0: #else nuclear@0: // GCC version 3 and higher nuclear@0: //#warning( "Using sn* functions." ) nuclear@0: #define TIXML_SNPRINTF snprintf nuclear@0: #define TIXML_SSCANF sscanf nuclear@0: #endif nuclear@0: nuclear@0: static const int TIXML2_MAJOR_VERSION = 1; nuclear@0: static const int TIXML2_MINOR_VERSION = 0; nuclear@0: static const int TIXML2_PATCH_VERSION = 1; nuclear@0: nuclear@0: namespace tinyxml2 nuclear@0: { nuclear@0: class XMLDocument; nuclear@0: class XMLElement; nuclear@0: class XMLAttribute; nuclear@0: class XMLComment; nuclear@0: class XMLNode; nuclear@0: class XMLText; nuclear@0: class XMLDeclaration; nuclear@0: class XMLUnknown; nuclear@0: nuclear@0: class XMLPrinter; nuclear@0: nuclear@0: /* nuclear@0: A class that wraps strings. Normally stores the start and end nuclear@0: pointers into the XML file itself, and will apply normalization nuclear@0: and entity translation if actually read. Can also store (and memory nuclear@0: manage) a traditional char[] nuclear@0: */ nuclear@0: class StrPair nuclear@0: { nuclear@0: public: nuclear@0: enum { nuclear@0: NEEDS_ENTITY_PROCESSING = 0x01, nuclear@0: NEEDS_NEWLINE_NORMALIZATION = 0x02, nuclear@0: nuclear@0: TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION, nuclear@0: TEXT_ELEMENT_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION, nuclear@0: ATTRIBUTE_NAME = 0, nuclear@0: ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION, nuclear@0: ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION, nuclear@0: COMMENT = NEEDS_NEWLINE_NORMALIZATION nuclear@0: }; nuclear@0: nuclear@0: StrPair() : flags( 0 ), start( 0 ), end( 0 ) {} nuclear@0: ~StrPair(); nuclear@0: nuclear@0: void Set( char* _start, char* _end, int _flags ) { nuclear@0: Reset(); nuclear@0: this->start = _start; this->end = _end; this->flags = _flags | NEEDS_FLUSH; nuclear@0: } nuclear@0: const char* GetStr(); nuclear@0: bool Empty() const { return start == end; } nuclear@0: nuclear@0: void SetInternedStr( const char* str ) { Reset(); this->start = const_cast(str); } nuclear@0: void SetStr( const char* str, int flags=0 ); nuclear@0: nuclear@0: char* ParseText( char* in, const char* endTag, int strFlags ); nuclear@0: char* ParseName( char* in ); nuclear@0: nuclear@0: nuclear@0: private: nuclear@0: void Reset(); nuclear@0: nuclear@0: enum { nuclear@0: NEEDS_FLUSH = 0x100, nuclear@0: NEEDS_DELETE = 0x200 nuclear@0: }; nuclear@0: nuclear@0: // After parsing, if *end != 0, it can be set to zero. nuclear@0: int flags; nuclear@0: char* start; nuclear@0: char* end; nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: /* nuclear@0: A dynamic array of Plain Old Data. Doesn't support constructors, etc. nuclear@0: Has a small initial memory pool, so that low or no usage will not nuclear@0: cause a call to new/delete nuclear@0: */ nuclear@0: template nuclear@0: class DynArray nuclear@0: { nuclear@0: public: nuclear@0: DynArray< T, INIT >() nuclear@0: { nuclear@0: mem = pool; nuclear@0: allocated = INIT; nuclear@0: size = 0; nuclear@0: } nuclear@0: ~DynArray() nuclear@0: { nuclear@0: if ( mem != pool ) { nuclear@0: delete [] mem; nuclear@0: } nuclear@0: } nuclear@0: void Push( T t ) nuclear@0: { nuclear@0: EnsureCapacity( size+1 ); nuclear@0: mem[size++] = t; nuclear@0: } nuclear@0: nuclear@0: T* PushArr( int count ) nuclear@0: { nuclear@0: EnsureCapacity( size+count ); nuclear@0: T* ret = &mem[size]; nuclear@0: size += count; nuclear@0: return ret; nuclear@0: } nuclear@0: T Pop() { nuclear@0: return mem[--size]; nuclear@0: } nuclear@0: void PopArr( int count ) nuclear@0: { nuclear@0: TIXMLASSERT( size >= count ); nuclear@0: size -= count; nuclear@0: } nuclear@0: nuclear@0: bool Empty() const { return size == 0; } nuclear@0: T& operator[](int i) { TIXMLASSERT( i>= 0 && i < size ); return mem[i]; } nuclear@0: const T& operator[](int i) const { TIXMLASSERT( i>= 0 && i < size ); return mem[i]; } nuclear@0: int Size() const { return size; } nuclear@0: int Capacity() const { return allocated; } nuclear@0: const T* Mem() const { return mem; } nuclear@0: T* Mem() { return mem; } nuclear@0: nuclear@0: nuclear@0: private: nuclear@0: void EnsureCapacity( int cap ) { nuclear@0: if ( cap > allocated ) { nuclear@0: int newAllocated = cap * 2; nuclear@0: T* newMem = new T[newAllocated]; nuclear@0: memcpy( newMem, mem, sizeof(T)*size ); // warning: not using constructors, only works for PODs nuclear@0: if ( mem != pool ) delete [] mem; nuclear@0: mem = newMem; nuclear@0: allocated = newAllocated; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: T* mem; nuclear@0: T pool[INIT]; nuclear@0: int allocated; // objects allocated nuclear@0: int size; // number objects in use nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: /* nuclear@0: Parent virtual class of a pool for fast allocation nuclear@0: and deallocation of objects. nuclear@0: */ nuclear@0: class MemPool nuclear@0: { nuclear@0: public: nuclear@0: MemPool() {} nuclear@0: virtual ~MemPool() {} nuclear@0: nuclear@0: virtual int ItemSize() const = 0; nuclear@0: virtual void* Alloc() = 0; nuclear@0: virtual void Free( void* ) = 0; nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: /* nuclear@0: Template child class to create pools of the correct type. nuclear@0: */ nuclear@0: template< int SIZE > nuclear@0: class MemPoolT : public MemPool nuclear@0: { nuclear@0: public: nuclear@0: MemPoolT() : root(0), currentAllocs(0), nAllocs(0), maxAllocs(0) {} nuclear@0: ~MemPoolT() { nuclear@0: // Delete the blocks. nuclear@0: for( int i=0; ichunk[i].next = &block->chunk[i+1]; nuclear@0: } nuclear@0: block->chunk[COUNT-1].next = 0; nuclear@0: root = block->chunk; nuclear@0: } nuclear@0: void* result = root; nuclear@0: root = root->next; nuclear@0: nuclear@0: ++currentAllocs; nuclear@0: if ( currentAllocs > maxAllocs ) maxAllocs = currentAllocs; nuclear@0: nAllocs++; nuclear@0: return result; nuclear@0: } nuclear@0: virtual void Free( void* mem ) { nuclear@0: if ( !mem ) return; nuclear@0: --currentAllocs; nuclear@0: Chunk* chunk = (Chunk*)mem; nuclear@0: memset( chunk, 0xfe, sizeof(Chunk) ); nuclear@0: chunk->next = root; nuclear@0: root = chunk; nuclear@0: } nuclear@0: void Trace( const char* name ) { nuclear@0: printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n", nuclear@0: name, maxAllocs, maxAllocs*SIZE/1024, currentAllocs, SIZE, nAllocs, blockPtrs.Size() ); nuclear@0: } nuclear@0: nuclear@0: private: nuclear@0: enum { COUNT = 1024/SIZE }; nuclear@0: union Chunk { nuclear@0: Chunk* next; nuclear@0: char mem[SIZE]; nuclear@0: }; nuclear@0: struct Block { nuclear@0: Chunk chunk[COUNT]; nuclear@0: }; nuclear@0: DynArray< Block*, 10 > blockPtrs; nuclear@0: Chunk* root; nuclear@0: nuclear@0: int currentAllocs; nuclear@0: int nAllocs; nuclear@0: int maxAllocs; nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: nuclear@0: /** nuclear@0: Implements the interface to the "Visitor pattern" (see the Accept() method.) nuclear@0: If you call the Accept() method, it requires being passed a XMLVisitor nuclear@0: class to handle callbacks. For nodes that contain other nodes (Document, Element) nuclear@0: you will get called with a VisitEnter/VisitExit pair. Nodes that are always leafs nuclear@0: are simply called with Visit(). nuclear@0: nuclear@0: If you return 'true' from a Visit method, recursive parsing will continue. If you return nuclear@0: false, no children of this node or its sibilings will be visited. nuclear@0: nuclear@0: All flavors of Visit methods have a default implementation that returns 'true' (continue nuclear@0: visiting). You need to only override methods that are interesting to you. nuclear@0: nuclear@0: Generally Accept() is called on the TiXmlDocument, although all nodes support visiting. nuclear@0: nuclear@0: You should never change the document from a callback. nuclear@0: nuclear@0: @sa XMLNode::Accept() nuclear@0: */ nuclear@0: class XMLVisitor nuclear@0: { nuclear@0: public: nuclear@0: virtual ~XMLVisitor() {} nuclear@0: nuclear@0: /// Visit a document. nuclear@0: virtual bool VisitEnter( const XMLDocument& /*doc*/ ) { return true; } nuclear@0: /// Visit a document. nuclear@0: virtual bool VisitExit( const XMLDocument& /*doc*/ ) { return true; } nuclear@0: nuclear@0: /// Visit an element. nuclear@0: virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) { return true; } nuclear@0: /// Visit an element. nuclear@0: virtual bool VisitExit( const XMLElement& /*element*/ ) { return true; } nuclear@0: nuclear@0: /// Visit a declaration. nuclear@0: virtual bool Visit( const XMLDeclaration& /*declaration*/ ) { return true; } nuclear@0: /// Visit a text node. nuclear@0: virtual bool Visit( const XMLText& /*text*/ ) { return true; } nuclear@0: /// Visit a comment node. nuclear@0: virtual bool Visit( const XMLComment& /*comment*/ ) { return true; } nuclear@0: /// Visit an unknown node. nuclear@0: virtual bool Visit( const XMLUnknown& /*unknown*/ ) { return true; } nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: /* nuclear@0: Utility functionality. nuclear@0: */ nuclear@0: class XMLUtil nuclear@0: { nuclear@0: public: nuclear@0: // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't nuclear@0: // correct, but simple, and usually works. nuclear@0: static const char* SkipWhiteSpace( const char* p ) { while( !IsUTF8Continuation(*p) && isspace( *p ) ) { ++p; } return p; } nuclear@0: static char* SkipWhiteSpace( char* p ) { while( !IsUTF8Continuation(*p) && isspace( *p ) ) { ++p; } return p; } nuclear@0: nuclear@0: inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) { nuclear@0: int n = 0; nuclear@0: if ( p == q ) { nuclear@0: return true; nuclear@0: } nuclear@0: while( *p && *q && *p == *q && n(const_cast(this)->FirstChildElement( _value )); } nuclear@0: nuclear@0: /// Get the last child node, or null if none exists. nuclear@0: const XMLNode* LastChild() const { return lastChild; } nuclear@0: XMLNode* LastChild() { return const_cast(const_cast(this)->LastChild() ); } nuclear@0: nuclear@0: /** Get the last child element or optionally the last child nuclear@0: element with the specified name. nuclear@0: */ nuclear@0: const XMLElement* LastChildElement( const char* value=0 ) const; nuclear@0: XMLElement* LastChildElement( const char* _value=0 ) { return const_cast(const_cast(this)->LastChildElement(_value) ); } nuclear@0: nuclear@0: /// Get the previous (left) sibling node of this node. nuclear@0: const XMLNode* PreviousSibling() const { return prev; } nuclear@0: XMLNode* PreviousSibling() { return prev; } nuclear@0: nuclear@0: /// Get the previous (left) sibling element of this node, with an opitionally supplied name. nuclear@0: const XMLElement* PreviousSiblingElement( const char* value=0 ) const ; nuclear@0: XMLElement* PreviousSiblingElement( const char* _value=0 ) { return const_cast(const_cast(this)->PreviousSiblingElement( _value ) ); } nuclear@0: nuclear@0: /// Get the next (right) sibling node of this node. nuclear@0: const XMLNode* NextSibling() const { return next; } nuclear@0: XMLNode* NextSibling() { return next; } nuclear@0: nuclear@0: /// Get the next (right) sibling element of this node, with an opitionally supplied name. nuclear@0: const XMLElement* NextSiblingElement( const char* value=0 ) const; nuclear@0: XMLElement* NextSiblingElement( const char* _value=0 ) { return const_cast(const_cast(this)->NextSiblingElement( _value ) ); } nuclear@0: nuclear@0: /** nuclear@0: Add a child node as the last (right) child. nuclear@0: */ nuclear@0: XMLNode* InsertEndChild( XMLNode* addThis ); nuclear@0: nuclear@0: XMLNode* LinkEndChild( XMLNode* addThis ) { return InsertEndChild( addThis ); } nuclear@0: /** nuclear@0: Add a child node as the first (left) child. nuclear@0: */ nuclear@0: XMLNode* InsertFirstChild( XMLNode* addThis ); nuclear@0: /** nuclear@0: Add a node after the specified child node. nuclear@0: */ nuclear@0: XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis ); nuclear@0: nuclear@0: /** nuclear@0: Delete all the children of this node. nuclear@0: */ nuclear@0: void DeleteChildren(); nuclear@0: nuclear@0: /** nuclear@0: Delete a child of this node. nuclear@0: */ nuclear@0: void DeleteChild( XMLNode* node ); nuclear@0: nuclear@0: /** nuclear@0: Make a copy of this node, but not its children. nuclear@0: You may pass in a Document pointer that will be nuclear@0: the owner of the new Node. If the 'document' is nuclear@0: null, then the node returned will be allocated nuclear@0: from the current Document. (this->GetDocument()) nuclear@0: nuclear@0: Note: if called on a XMLDocument, this will return null. nuclear@0: */ nuclear@0: virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0; nuclear@0: nuclear@0: /** nuclear@0: Test if 2 nodes are the same, but don't test children. nuclear@0: The 2 nodes do not need to be in the same Document. nuclear@0: nuclear@0: Note: if called on a XMLDocument, this will return false. nuclear@0: */ nuclear@0: virtual bool ShallowEqual( const XMLNode* compare ) const = 0; nuclear@0: nuclear@0: /** Accept a hierarchical visit of the nodes in the TinyXML DOM. Every node in the nuclear@0: XML tree will be conditionally visited and the host will be called back nuclear@0: via the TiXmlVisitor interface. nuclear@0: nuclear@0: This is essentially a SAX interface for TinyXML. (Note however it doesn't re-parse nuclear@0: the XML for the callbacks, so the performance of TinyXML is unchanged by using this nuclear@0: interface versus any other.) nuclear@0: nuclear@0: The interface has been based on ideas from: nuclear@0: nuclear@0: - http://www.saxproject.org/ nuclear@0: - http://c2.com/cgi/wiki?HierarchicalVisitorPattern nuclear@0: nuclear@0: Which are both good references for "visiting". nuclear@0: nuclear@0: An example of using Accept(): nuclear@0: @verbatim nuclear@0: TiXmlPrinter printer; nuclear@0: tinyxmlDoc.Accept( &printer ); nuclear@0: const char* xmlcstr = printer.CStr(); nuclear@0: @endverbatim nuclear@0: */ nuclear@0: virtual bool Accept( XMLVisitor* visitor ) const = 0; nuclear@0: nuclear@0: // internal nuclear@0: virtual char* ParseDeep( char*, StrPair* ); nuclear@0: nuclear@0: protected: nuclear@0: XMLNode( XMLDocument* ); nuclear@0: virtual ~XMLNode(); nuclear@0: XMLNode( const XMLNode& ); // not supported nuclear@0: void operator=( const XMLNode& ); // not supported nuclear@0: nuclear@0: XMLDocument* document; nuclear@0: XMLNode* parent; nuclear@0: mutable StrPair value; nuclear@0: nuclear@0: XMLNode* firstChild; nuclear@0: XMLNode* lastChild; nuclear@0: nuclear@0: XMLNode* prev; nuclear@0: XMLNode* next; nuclear@0: nuclear@0: private: nuclear@0: MemPool* memPool; nuclear@0: void Unlink( XMLNode* child ); nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: /** XML text. nuclear@0: nuclear@0: Note that a text node can have child element nodes, for example: nuclear@0: @verbatim nuclear@0: This is bold nuclear@0: @endverbatim nuclear@0: nuclear@0: A text node can have 2 ways to output the next. "normal" output nuclear@0: and CDATA. It will default to the mode it was parsed from the XML file and nuclear@0: you generally want to leave it alone, but you can change the output mode with nuclear@0: SetCDATA() and query it with CDATA(). nuclear@0: */ nuclear@0: class XMLText : public XMLNode nuclear@0: { nuclear@0: friend class XMLBase; nuclear@0: friend class XMLDocument; nuclear@0: public: nuclear@0: virtual bool Accept( XMLVisitor* visitor ) const; nuclear@0: nuclear@0: virtual XMLText* ToText() { return this; } nuclear@0: virtual const XMLText* ToText() const { return this; } nuclear@0: nuclear@0: /// Declare whether this should be CDATA or standard text. nuclear@0: void SetCData( bool _isCData ) { this->isCData = _isCData; } nuclear@0: /// Returns true if this is a CDATA text element. nuclear@0: bool CData() const { return isCData; } nuclear@0: nuclear@0: char* ParseDeep( char*, StrPair* endTag ); nuclear@0: virtual XMLNode* ShallowClone( XMLDocument* document ) const; nuclear@0: virtual bool ShallowEqual( const XMLNode* compare ) const; nuclear@0: nuclear@0: nuclear@0: protected: nuclear@0: XMLText( XMLDocument* doc ) : XMLNode( doc ), isCData( false ) {} nuclear@0: virtual ~XMLText() {} nuclear@0: XMLText( const XMLText& ); // not supported nuclear@0: void operator=( const XMLText& ); // not supported nuclear@0: nuclear@0: private: nuclear@0: bool isCData; nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: /** An XML Comment. */ nuclear@0: class XMLComment : public XMLNode nuclear@0: { nuclear@0: friend class XMLDocument; nuclear@0: public: nuclear@0: virtual XMLComment* ToComment() { return this; } nuclear@0: virtual const XMLComment* ToComment() const { return this; } nuclear@0: nuclear@0: virtual bool Accept( XMLVisitor* visitor ) const; nuclear@0: nuclear@0: char* ParseDeep( char*, StrPair* endTag ); nuclear@0: virtual XMLNode* ShallowClone( XMLDocument* document ) const; nuclear@0: virtual bool ShallowEqual( const XMLNode* compare ) const; nuclear@0: nuclear@0: protected: nuclear@0: XMLComment( XMLDocument* doc ); nuclear@0: virtual ~XMLComment(); nuclear@0: XMLComment( const XMLComment& ); // not supported nuclear@0: void operator=( const XMLComment& ); // not supported nuclear@0: nuclear@0: private: nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: /** In correct XML the declaration is the first entry in the file. nuclear@0: @verbatim nuclear@0: nuclear@0: @endverbatim nuclear@0: nuclear@0: TinyXML2 will happily read or write files without a declaration, nuclear@0: however. nuclear@0: nuclear@0: The text of the declaration isn't interpreted. It is parsed nuclear@0: and written as a string. nuclear@0: */ nuclear@0: class XMLDeclaration : public XMLNode nuclear@0: { nuclear@0: friend class XMLDocument; nuclear@0: public: nuclear@0: virtual XMLDeclaration* ToDeclaration() { return this; } nuclear@0: virtual const XMLDeclaration* ToDeclaration() const { return this; } nuclear@0: nuclear@0: virtual bool Accept( XMLVisitor* visitor ) const; nuclear@0: nuclear@0: char* ParseDeep( char*, StrPair* endTag ); nuclear@0: virtual XMLNode* ShallowClone( XMLDocument* document ) const; nuclear@0: virtual bool ShallowEqual( const XMLNode* compare ) const; nuclear@0: nuclear@0: protected: nuclear@0: XMLDeclaration( XMLDocument* doc ); nuclear@0: virtual ~XMLDeclaration(); nuclear@0: XMLDeclaration( const XMLDeclaration& ); // not supported nuclear@0: void operator=( const XMLDeclaration& ); // not supported nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: /** Any tag that tinyXml doesn't recognize is saved as an nuclear@0: unknown. It is a tag of text, but should not be modified. nuclear@0: It will be written back to the XML, unchanged, when the file nuclear@0: is saved. nuclear@0: nuclear@0: DTD tags get thrown into TiXmlUnknowns. nuclear@0: */ nuclear@0: class XMLUnknown : public XMLNode nuclear@0: { nuclear@0: friend class XMLDocument; nuclear@0: public: nuclear@0: virtual XMLUnknown* ToUnknown() { return this; } nuclear@0: virtual const XMLUnknown* ToUnknown() const { return this; } nuclear@0: nuclear@0: virtual bool Accept( XMLVisitor* visitor ) const; nuclear@0: nuclear@0: char* ParseDeep( char*, StrPair* endTag ); nuclear@0: virtual XMLNode* ShallowClone( XMLDocument* document ) const; nuclear@0: virtual bool ShallowEqual( const XMLNode* compare ) const; nuclear@0: nuclear@0: protected: nuclear@0: XMLUnknown( XMLDocument* doc ); nuclear@0: virtual ~XMLUnknown(); nuclear@0: XMLUnknown( const XMLUnknown& ); // not supported nuclear@0: void operator=( const XMLUnknown& ); // not supported nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: enum { nuclear@0: XML_NO_ERROR = 0, nuclear@0: XML_SUCCESS = 0, nuclear@0: nuclear@0: XML_NO_ATTRIBUTE, nuclear@0: XML_WRONG_ATTRIBUTE_TYPE, nuclear@0: nuclear@0: XML_ERROR_FILE_NOT_FOUND, nuclear@0: XML_ERROR_FILE_COULD_NOT_BE_OPENED, nuclear@0: XML_ERROR_ELEMENT_MISMATCH, nuclear@0: XML_ERROR_PARSING_ELEMENT, nuclear@0: XML_ERROR_PARSING_ATTRIBUTE, nuclear@0: XML_ERROR_IDENTIFYING_TAG, nuclear@0: XML_ERROR_PARSING_TEXT, nuclear@0: XML_ERROR_PARSING_CDATA, nuclear@0: XML_ERROR_PARSING_COMMENT, nuclear@0: XML_ERROR_PARSING_DECLARATION, nuclear@0: XML_ERROR_PARSING_UNKNOWN, nuclear@0: XML_ERROR_EMPTY_DOCUMENT, nuclear@0: XML_ERROR_MISMATCHED_ELEMENT, nuclear@0: XML_ERROR_PARSING nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: /** An attribute is a name-value pair. Elements have an arbitrary nuclear@0: number of attributes, each with a unique name. nuclear@0: nuclear@0: @note The attributes are not XMLNodes. You may only query the nuclear@0: Next() attribute in a list. nuclear@0: */ nuclear@0: class XMLAttribute nuclear@0: { nuclear@0: friend class XMLElement; nuclear@0: public: nuclear@0: const char* Name() const { return name.GetStr(); } ///< The name of the attribute. nuclear@0: const char* Value() const { return value.GetStr(); } ///< The value of the attribute. nuclear@0: const XMLAttribute* Next() const { return next; } ///< The next attribute in the list. nuclear@0: nuclear@0: /** IntAttribute interprets the attribute as an integer, and returns the value. nuclear@0: If the value isn't an integer, 0 will be returned. There is no error checking; nuclear@0: use QueryIntAttribute() if you need error checking. nuclear@0: */ nuclear@0: int IntValue() const { int i=0; QueryIntValue( &i ); return i; } nuclear@0: /// Query as an unsigned integer. See IntAttribute() nuclear@0: unsigned UnsignedValue() const { unsigned i=0; QueryUnsignedValue( &i ); return i; } nuclear@0: /// Query as a boolean. See IntAttribute() nuclear@0: bool BoolValue() const { bool b=false; QueryBoolValue( &b ); return b; } nuclear@0: /// Query as a double. See IntAttribute() nuclear@0: double DoubleValue() const { double d=0; QueryDoubleValue( &d ); return d; } nuclear@0: /// Query as a float. See IntAttribute() nuclear@0: float FloatValue() const { float f=0; QueryFloatValue( &f ); return f; } nuclear@0: nuclear@0: /** QueryIntAttribute interprets the attribute as an integer, and returns the value nuclear@0: in the provided paremeter. The function will return XML_NO_ERROR on success, nuclear@0: and XML_WRONG_ATTRIBUTE_TYPE if the conversion is not successful. nuclear@0: */ nuclear@0: int QueryIntValue( int* value ) const; nuclear@0: /// See QueryIntAttribute nuclear@0: int QueryUnsignedValue( unsigned int* value ) const; nuclear@0: /// See QueryIntAttribute nuclear@0: int QueryBoolValue( bool* value ) const; nuclear@0: /// See QueryIntAttribute nuclear@0: int QueryDoubleValue( double* value ) const; nuclear@0: /// See QueryIntAttribute nuclear@0: int QueryFloatValue( float* value ) const; nuclear@0: nuclear@0: /// Set the attribute to a string value. nuclear@0: void SetAttribute( const char* value ); nuclear@0: /// Set the attribute to value. nuclear@0: void SetAttribute( int value ); nuclear@0: /// Set the attribute to value. nuclear@0: void SetAttribute( unsigned value ); nuclear@0: /// Set the attribute to value. nuclear@0: void SetAttribute( bool value ); nuclear@0: /// Set the attribute to value. nuclear@0: void SetAttribute( double value ); nuclear@0: /// Set the attribute to value. nuclear@0: void SetAttribute( float value ); nuclear@0: nuclear@0: private: nuclear@0: enum { BUF_SIZE = 200 }; nuclear@0: nuclear@0: XMLAttribute() : next( 0 ) {} nuclear@0: virtual ~XMLAttribute() {} nuclear@0: XMLAttribute( const XMLAttribute& ); // not supported nuclear@0: void operator=( const XMLAttribute& ); // not supported nuclear@0: void SetName( const char* name ); nuclear@0: nuclear@0: char* ParseDeep( char* p, bool processEntities ); nuclear@0: nuclear@0: mutable StrPair name; nuclear@0: mutable StrPair value; nuclear@0: XMLAttribute* next; nuclear@0: MemPool* memPool; nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: /** The element is a container class. It has a value, the element name, nuclear@0: and can contain other elements, text, comments, and unknowns. nuclear@0: Elements also contain an arbitrary number of attributes. nuclear@0: */ nuclear@0: class XMLElement : public XMLNode nuclear@0: { nuclear@0: friend class XMLBase; nuclear@0: friend class XMLDocument; nuclear@0: public: nuclear@0: /// Get the name of an element (which is the Value() of the node.) nuclear@0: const char* Name() const { return Value(); } nuclear@0: /// Set the name of the element. nuclear@0: void SetName( const char* str, bool staticMem=false ) { SetValue( str, staticMem ); } nuclear@0: nuclear@0: virtual XMLElement* ToElement() { return this; } nuclear@0: virtual const XMLElement* ToElement() const { return this; } nuclear@0: virtual bool Accept( XMLVisitor* visitor ) const; nuclear@0: nuclear@0: /** Given an attribute name, Attribute() returns the value nuclear@0: for the attribute of that name, or null if none nuclear@0: exists. For example: nuclear@0: nuclear@0: @verbatim nuclear@0: const char* value = ele->Attribute( "foo" ); nuclear@0: @endverbatim nuclear@0: nuclear@0: The 'value' parameter is normally null. However, if specified, nuclear@0: the attribute will only be returned if the 'name' and 'value' nuclear@0: match. This allow you to write code: nuclear@0: nuclear@0: @verbatim nuclear@0: if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar(); nuclear@0: @endverbatim nuclear@0: nuclear@0: rather than: nuclear@0: @verbatim nuclear@0: if ( ele->Attribute( "foo" ) ) { nuclear@0: if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar(); nuclear@0: } nuclear@0: @endverbatim nuclear@0: */ nuclear@0: const char* Attribute( const char* name, const char* value=0 ) const; nuclear@0: nuclear@0: /** Given an attribute name, IntAttribute() returns the value nuclear@0: of the attribute interpreted as an integer. 0 will be nuclear@0: returned if there is an error. For a method with error nuclear@0: checking, see QueryIntAttribute() nuclear@0: */ nuclear@0: int IntAttribute( const char* name ) const { int i=0; QueryIntAttribute( name, &i ); return i; } nuclear@0: /// See IntAttribute() nuclear@0: unsigned UnsignedAttribute( const char* name ) const{ unsigned i=0; QueryUnsignedAttribute( name, &i ); return i; } nuclear@0: /// See IntAttribute() nuclear@0: bool BoolAttribute( const char* name ) const { bool b=false; QueryBoolAttribute( name, &b ); return b; } nuclear@0: /// See IntAttribute() nuclear@0: double DoubleAttribute( const char* name ) const { double d=0; QueryDoubleAttribute( name, &d ); return d; } nuclear@0: /// See IntAttribute() nuclear@0: float FloatAttribute( const char* name ) const { float f=0; QueryFloatAttribute( name, &f ); return f; } nuclear@0: nuclear@0: /** Given an attribute name, QueryIntAttribute() returns nuclear@0: XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion nuclear@0: can't be performed, or XML_NO_ATTRIBUTE if the attribute nuclear@0: doesn't exist. If successful, the result of the conversion nuclear@0: will be written to 'value'. If not successful, nothing will nuclear@0: be written to 'value'. This allows you to provide default nuclear@0: value: nuclear@0: nuclear@0: @verbatim nuclear@0: int value = 10; nuclear@0: QueryIntAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10 nuclear@0: @endverbatim nuclear@0: */ nuclear@0: int QueryIntAttribute( const char* name, int* _value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return XML_NO_ATTRIBUTE; return a->QueryIntValue( _value ); } nuclear@0: /// See QueryIntAttribute() nuclear@0: int QueryUnsignedAttribute( const char* name, unsigned int* _value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return XML_NO_ATTRIBUTE; return a->QueryUnsignedValue( _value ); } nuclear@0: /// See QueryIntAttribute() nuclear@0: int QueryBoolAttribute( const char* name, bool* _value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return XML_NO_ATTRIBUTE; return a->QueryBoolValue( _value ); } nuclear@0: /// See QueryIntAttribute() nuclear@0: int QueryDoubleAttribute( const char* name, double* _value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return XML_NO_ATTRIBUTE; return a->QueryDoubleValue( _value ); } nuclear@0: /// See QueryIntAttribute() nuclear@0: int QueryFloatAttribute( const char* name, float* _value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return XML_NO_ATTRIBUTE; return a->QueryFloatValue( _value ); } nuclear@0: nuclear@0: /// Sets the named attribute to value. nuclear@0: void SetAttribute( const char* name, const char* _value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( _value ); } nuclear@0: /// Sets the named attribute to value. nuclear@0: void SetAttribute( const char* name, int _value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( _value ); } nuclear@0: /// Sets the named attribute to value. nuclear@0: void SetAttribute( const char* name, unsigned _value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( _value ); } nuclear@0: /// Sets the named attribute to value. nuclear@0: void SetAttribute( const char* name, bool _value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( _value ); } nuclear@0: /// Sets the named attribute to value. nuclear@0: void SetAttribute( const char* name, double _value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( _value ); } nuclear@0: nuclear@0: /** nuclear@0: Delete an attribute. nuclear@0: */ nuclear@0: void DeleteAttribute( const char* name ); nuclear@0: nuclear@0: /// Return the first attribute in the list. nuclear@0: const XMLAttribute* FirstAttribute() const { return rootAttribute; } nuclear@0: /// Query a specific attribute in the list. nuclear@0: const XMLAttribute* FindAttribute( const char* name ) const; nuclear@0: nuclear@0: /** Convenience function for easy access to the text inside an element. Although easy nuclear@0: and concise, GetText() is limited compared to getting the TiXmlText child nuclear@0: and accessing it directly. nuclear@0: nuclear@0: If the first child of 'this' is a TiXmlText, the GetText() nuclear@0: returns the character string of the Text node, else null is returned. nuclear@0: nuclear@0: This is a convenient method for getting the text of simple contained text: nuclear@0: @verbatim nuclear@0: This is text nuclear@0: const char* str = fooElement->GetText(); nuclear@0: @endverbatim nuclear@0: nuclear@0: 'str' will be a pointer to "This is text". nuclear@0: nuclear@0: Note that this function can be misleading. If the element foo was created from nuclear@0: this XML: nuclear@0: @verbatim nuclear@0: This is text nuclear@0: @endverbatim nuclear@0: nuclear@0: then the value of str would be null. The first child node isn't a text node, it is nuclear@0: another element. From this XML: nuclear@0: @verbatim nuclear@0: This is text nuclear@0: @endverbatim nuclear@0: GetText() will return "This is ". nuclear@0: */ nuclear@0: const char* GetText() const; nuclear@0: nuclear@0: // internal: nuclear@0: enum { nuclear@0: OPEN, // nuclear@0: CLOSED, // nuclear@0: CLOSING // nuclear@0: }; nuclear@0: int ClosingType() const { return closingType; } nuclear@0: char* ParseDeep( char* p, StrPair* endTag ); nuclear@0: virtual XMLNode* ShallowClone( XMLDocument* document ) const; nuclear@0: virtual bool ShallowEqual( const XMLNode* compare ) const; nuclear@0: nuclear@0: private: nuclear@0: XMLElement( XMLDocument* doc ); nuclear@0: virtual ~XMLElement(); nuclear@0: XMLElement( const XMLElement& ); // not supported nuclear@0: void operator=( const XMLElement& ); // not supported nuclear@0: nuclear@0: XMLAttribute* FindAttribute( const char* name ); nuclear@0: XMLAttribute* FindOrCreateAttribute( const char* name ); nuclear@0: //void LinkAttribute( XMLAttribute* attrib ); nuclear@0: char* ParseAttributes( char* p ); nuclear@0: nuclear@0: int closingType; nuclear@0: // The attribute list is ordered; there is no 'lastAttribute' nuclear@0: // because the list needs to be scanned for dupes before adding nuclear@0: // a new attribute. nuclear@0: XMLAttribute* rootAttribute; nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: /** A Document binds together all the functionality. nuclear@0: It can be saved, loaded, and printed to the screen. nuclear@0: All Nodes are connected and allocated to a Document. nuclear@0: If the Document is deleted, all its Nodes are also deleted. nuclear@0: */ nuclear@0: class XMLDocument : public XMLNode nuclear@0: { nuclear@0: friend class XMLElement; nuclear@0: public: nuclear@0: /// constructor nuclear@0: XMLDocument( bool processEntities = true ); nuclear@0: ~XMLDocument(); nuclear@0: nuclear@0: virtual XMLDocument* ToDocument() { return this; } nuclear@0: virtual const XMLDocument* ToDocument() const { return this; } nuclear@0: nuclear@0: /** nuclear@0: Parse an XML file from a character string. nuclear@0: Returns XML_NO_ERROR (0) on success, or nuclear@0: an errorID. nuclear@0: */ nuclear@0: int Parse( const char* xml ); nuclear@0: nuclear@0: /** nuclear@0: Load an XML file from disk. nuclear@0: Returns XML_NO_ERROR (0) on success, or nuclear@0: an errorID. nuclear@0: */ nuclear@0: int LoadFile( const char* filename ); nuclear@0: nuclear@0: /** nuclear@0: Load an XML file from disk. You are responsible nuclear@0: for providing and closing the FILE*. nuclear@0: nuclear@0: Returns XML_NO_ERROR (0) on success, or nuclear@0: an errorID. nuclear@0: */ nuclear@0: int LoadFile( FILE* ); nuclear@0: nuclear@0: /** nuclear@0: Save the XML file to disk. nuclear@0: Returns XML_NO_ERROR (0) on success, or nuclear@0: an errorID. nuclear@0: */ nuclear@0: int SaveFile( const char* filename ); nuclear@0: nuclear@0: /** nuclear@0: Save the XML file to disk. You are responsible nuclear@0: for providing and closing the FILE*. nuclear@0: nuclear@0: Returns XML_NO_ERROR (0) on success, or nuclear@0: an errorID. nuclear@0: */ nuclear@0: int SaveFile( FILE* ); nuclear@0: nuclear@0: bool ProcessEntities() const { return processEntities; } nuclear@0: nuclear@0: /** nuclear@0: Returns true if this document has a leading Byte Order Mark of UTF8. nuclear@0: */ nuclear@0: bool HasBOM() const { return writeBOM; } nuclear@0: /** Sets whether to write the BOM when writing the file. nuclear@0: */ nuclear@0: void SetBOM( bool useBOM ) { writeBOM = useBOM; } nuclear@0: nuclear@0: /** Return the root element of DOM. Equivalent to FirstChildElement(). nuclear@0: To get the first node, use FirstChild(). nuclear@0: */ nuclear@0: XMLElement* RootElement() { return FirstChildElement(); } nuclear@0: const XMLElement* RootElement() const { return FirstChildElement(); } nuclear@0: nuclear@0: /** Print the Document. If the Printer is not provided, it will nuclear@0: print to stdout. If you provide Printer, this can print to a file: nuclear@0: @verbatim nuclear@0: XMLPrinter printer( fp ); nuclear@0: doc.Print( &printer ); nuclear@0: @endverbatim nuclear@0: nuclear@0: Or you can use a printer to print to memory: nuclear@0: @verbatim nuclear@0: XMLPrinter printer; nuclear@0: doc->Print( &printer ); nuclear@0: // printer.CStr() has a const char* to the XML nuclear@0: @endverbatim nuclear@0: */ nuclear@0: void Print( XMLPrinter* streamer=0 ); nuclear@0: virtual bool Accept( XMLVisitor* visitor ) const; nuclear@0: nuclear@0: /** nuclear@0: Create a new Element associated with nuclear@0: this Document. The memory for the Element nuclear@0: is managed by the Document. nuclear@0: */ nuclear@0: XMLElement* NewElement( const char* name ); nuclear@0: /** nuclear@0: Create a new Comment associated with nuclear@0: this Document. The memory for the Comment nuclear@0: is managed by the Document. nuclear@0: */ nuclear@0: XMLComment* NewComment( const char* comment ); nuclear@0: /** nuclear@0: Create a new Text associated with nuclear@0: this Document. The memory for the Text nuclear@0: is managed by the Document. nuclear@0: */ nuclear@0: XMLText* NewText( const char* text ); nuclear@0: /** nuclear@0: Create a new Declaration associated with nuclear@0: this Document. The memory for the object nuclear@0: is managed by the Document. nuclear@0: nuclear@0: If the 'text' param is null, the standard nuclear@0: declaration is used.: nuclear@0: @verbatim nuclear@0: nuclear@0: @endverbatim nuclear@0: */ nuclear@0: XMLDeclaration* NewDeclaration( const char* text=0 ); nuclear@0: /** nuclear@0: Create a new Unknown associated with nuclear@0: this Document. The memory for the object nuclear@0: is managed by the Document. nuclear@0: */ nuclear@0: XMLUnknown* NewUnknown( const char* text ); nuclear@0: nuclear@0: /** nuclear@0: Delete a node associated with this document. nuclear@0: It will be unlinked from the DOM. nuclear@0: */ nuclear@0: void DeleteNode( XMLNode* node ) { node->parent->DeleteChild( node ); } nuclear@0: nuclear@0: void SetError( int error, const char* str1, const char* str2 ); nuclear@0: nuclear@0: /// Return true if there was an error parsing the document. nuclear@0: bool Error() const { return errorID != XML_NO_ERROR; } nuclear@0: /// Return the errorID. nuclear@0: int ErrorID() const { return errorID; } nuclear@0: /// Return a possibly helpful diagnostic location or string. nuclear@0: const char* GetErrorStr1() const { return errorStr1; } nuclear@0: /// Return a possibly helpful secondary diagnostic location or string. nuclear@0: const char* GetErrorStr2() const { return errorStr2; } nuclear@0: /// If there is an error, print it to stdout. nuclear@0: void PrintError() const; nuclear@0: nuclear@0: // internal nuclear@0: char* Identify( char* p, XMLNode** node ); nuclear@0: nuclear@0: virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const { return 0; } nuclear@0: virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const { return false; } nuclear@0: nuclear@0: private: nuclear@0: XMLDocument( const XMLDocument& ); // not supported nuclear@0: void operator=( const XMLDocument& ); // not supported nuclear@0: void InitDocument(); nuclear@0: nuclear@0: bool writeBOM; nuclear@0: bool processEntities; nuclear@0: int errorID; nuclear@0: const char* errorStr1; nuclear@0: const char* errorStr2; nuclear@0: char* charBuffer; nuclear@0: nuclear@0: MemPoolT< sizeof(XMLElement) > elementPool; nuclear@0: MemPoolT< sizeof(XMLAttribute) > attributePool; nuclear@0: MemPoolT< sizeof(XMLText) > textPool; nuclear@0: MemPoolT< sizeof(XMLComment) > commentPool; nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: /** nuclear@0: A XMLHandle is a class that wraps a node pointer with null checks; this is nuclear@0: an incredibly useful thing. Note that XMLHandle is not part of the TinyXML nuclear@0: DOM structure. It is a separate utility class. nuclear@0: nuclear@0: Take an example: nuclear@0: @verbatim nuclear@0: nuclear@0: nuclear@0: nuclear@0: nuclear@0: nuclear@0: nuclear@0: @endverbatim nuclear@0: nuclear@0: Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very nuclear@0: easy to write a *lot* of code that looks like: nuclear@0: nuclear@0: @verbatim nuclear@0: XMLElement* root = document.FirstChildElement( "Document" ); nuclear@0: if ( root ) nuclear@0: { nuclear@0: XMLElement* element = root->FirstChildElement( "Element" ); nuclear@0: if ( element ) nuclear@0: { nuclear@0: XMLElement* child = element->FirstChildElement( "Child" ); nuclear@0: if ( child ) nuclear@0: { nuclear@0: XMLElement* child2 = child->NextSiblingElement( "Child" ); nuclear@0: if ( child2 ) nuclear@0: { nuclear@0: // Finally do something useful. nuclear@0: @endverbatim nuclear@0: nuclear@0: And that doesn't even cover "else" cases. XMLHandle addresses the verbosity nuclear@0: of such code. A XMLHandle checks for null pointers so it is perfectly safe nuclear@0: and correct to use: nuclear@0: nuclear@0: @verbatim nuclear@0: XMLHandle docHandle( &document ); nuclear@0: XMLElement* child2 = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).FirstChild().NextSibling().ToElement(); nuclear@0: if ( child2 ) nuclear@0: { nuclear@0: // do something useful nuclear@0: @endverbatim nuclear@0: nuclear@0: Which is MUCH more concise and useful. nuclear@0: nuclear@0: It is also safe to copy handles - internally they are nothing more than node pointers. nuclear@0: @verbatim nuclear@0: XMLHandle handleCopy = handle; nuclear@0: @endverbatim nuclear@0: nuclear@0: See also XMLConstHandle, which is the same as XMLHandle, but operates on const objects. nuclear@0: */ nuclear@0: class XMLHandle nuclear@0: { nuclear@0: public: nuclear@0: /// Create a handle from any node (at any depth of the tree.) This can be a null pointer. nuclear@0: XMLHandle( XMLNode* _node ) { node = _node; } nuclear@0: /// Create a handle from a node. nuclear@0: XMLHandle( XMLNode& _node ) { node = &_node; } nuclear@0: /// Copy constructor nuclear@0: XMLHandle( const XMLHandle& ref ) { node = ref.node; } nuclear@0: /// Assignment nuclear@0: XMLHandle operator=( const XMLHandle& ref ) { node = ref.node; return *this; } nuclear@0: nuclear@0: /// Get the first child of this handle. nuclear@0: XMLHandle FirstChild() { return XMLHandle( node ? node->FirstChild() : 0 ); } nuclear@0: /// Get the first child element of this handle. nuclear@0: XMLHandle FirstChildElement( const char* value=0 ) { return XMLHandle( node ? node->FirstChildElement( value ) : 0 ); } nuclear@0: /// Get the last child of this handle. nuclear@0: XMLHandle LastChild() { return XMLHandle( node ? node->LastChild() : 0 ); } nuclear@0: /// Get the last child element of this handle. nuclear@0: XMLHandle LastChildElement( const char* _value=0 ) { return XMLHandle( node ? node->LastChildElement( _value ) : 0 ); } nuclear@0: /// Get the previous sibling of this handle. nuclear@0: XMLHandle PreviousSibling() { return XMLHandle( node ? node->PreviousSibling() : 0 ); } nuclear@0: /// Get the previous sibling element of this handle. nuclear@0: XMLHandle PreviousSiblingElement( const char* _value=0 ) { return XMLHandle( node ? node->PreviousSiblingElement( _value ) : 0 ); } nuclear@0: /// Get the next sibling of this handle. nuclear@0: XMLHandle NextSibling() { return XMLHandle( node ? node->NextSibling() : 0 ); } nuclear@0: /// Get the next sibling element of this handle. nuclear@0: XMLHandle NextSiblingElement( const char* _value=0 ) { return XMLHandle( node ? node->NextSiblingElement( _value ) : 0 ); } nuclear@0: nuclear@0: /// Safe cast to XMLNode. This can return null. nuclear@0: XMLNode* ToNode() { return node; } nuclear@0: /// Safe cast to XMLElement. This can return null. nuclear@0: XMLElement* ToElement() { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); } nuclear@0: /// Safe cast to XMLText. This can return null. nuclear@0: XMLText* ToText() { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); } nuclear@0: /// Safe cast to XMLUnknown. This can return null. nuclear@0: XMLUnknown* ToUnknown() { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); } nuclear@0: /// Safe cast to XMLDeclaration. This can return null. nuclear@0: XMLDeclaration* ToDeclaration() { return ( ( node && node->ToDeclaration() ) ? node->ToDeclaration() : 0 ); } nuclear@0: nuclear@0: private: nuclear@0: XMLNode* node; nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: /** nuclear@0: A variant of the XMLHandle class for working with const XMLNodes and Documents. It is the nuclear@0: same in all regards, except for the 'const' qualifiers. See XMLHandle for API. nuclear@0: */ nuclear@0: class XMLConstHandle nuclear@0: { nuclear@0: public: nuclear@0: XMLConstHandle( const XMLNode* _node ) { node = _node; } nuclear@0: XMLConstHandle( const XMLNode& _node ) { node = &_node; } nuclear@0: XMLConstHandle( const XMLConstHandle& ref ) { node = ref.node; } nuclear@0: nuclear@0: XMLConstHandle operator=( const XMLConstHandle& ref ) { node = ref.node; return *this; } nuclear@0: nuclear@0: const XMLConstHandle FirstChild() const { return XMLConstHandle( node ? node->FirstChild() : 0 ); } nuclear@0: const XMLConstHandle FirstChildElement( const char* value=0 ) const { return XMLConstHandle( node ? node->FirstChildElement( value ) : 0 ); } nuclear@0: const XMLConstHandle LastChild() const { return XMLConstHandle( node ? node->LastChild() : 0 ); } nuclear@0: const XMLConstHandle LastChildElement( const char* _value=0 ) const { return XMLConstHandle( node ? node->LastChildElement( _value ) : 0 ); } nuclear@0: const XMLConstHandle PreviousSibling() const { return XMLConstHandle( node ? node->PreviousSibling() : 0 ); } nuclear@0: const XMLConstHandle PreviousSiblingElement( const char* _value=0 ) const { return XMLConstHandle( node ? node->PreviousSiblingElement( _value ) : 0 ); } nuclear@0: const XMLConstHandle NextSibling() const { return XMLConstHandle( node ? node->NextSibling() : 0 ); } nuclear@0: const XMLConstHandle NextSiblingElement( const char* _value=0 ) const { return XMLConstHandle( node ? node->NextSiblingElement( _value ) : 0 ); } nuclear@0: nuclear@0: nuclear@0: const XMLNode* ToNode() const { return node; } nuclear@0: const XMLElement* ToElement() const { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); } nuclear@0: const XMLText* ToText() const { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); } nuclear@0: const XMLUnknown* ToUnknown() const { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); } nuclear@0: const XMLDeclaration* ToDeclaration() const { return ( ( node && node->ToDeclaration() ) ? node->ToDeclaration() : 0 ); } nuclear@0: nuclear@0: private: nuclear@0: const XMLNode* node; nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: /** nuclear@0: Printing functionality. The XMLPrinter gives you more nuclear@0: options than the XMLDocument::Print() method. nuclear@0: nuclear@0: It can: nuclear@0: -# Print to memory. nuclear@0: -# Print to a file you provide. nuclear@0: -# Print XML without a XMLDocument. nuclear@0: nuclear@0: Print to Memory nuclear@0: nuclear@0: @verbatim nuclear@0: XMLPrinter printer; nuclear@0: doc->Print( &printer ); nuclear@0: SomeFunction( printer.CStr() ); nuclear@0: @endverbatim nuclear@0: nuclear@0: Print to a File nuclear@0: nuclear@0: You provide the file pointer. nuclear@0: @verbatim nuclear@0: XMLPrinter printer( fp ); nuclear@0: doc.Print( &printer ); nuclear@0: @endverbatim nuclear@0: nuclear@0: Print without a XMLDocument nuclear@0: nuclear@0: When loading, an XML parser is very useful. However, sometimes nuclear@0: when saving, it just gets in the way. The code is often set up nuclear@0: for streaming, and constructing the DOM is just overhead. nuclear@0: nuclear@0: The Printer supports the streaming case. The following code nuclear@0: prints out a trivially simple XML file without ever creating nuclear@0: an XML document. nuclear@0: nuclear@0: @verbatim nuclear@0: XMLPrinter printer( fp ); nuclear@0: printer.OpenElement( "foo" ); nuclear@0: printer.PushAttribute( "foo", "bar" ); nuclear@0: printer.CloseElement(); nuclear@0: @endverbatim nuclear@0: */ nuclear@0: class XMLPrinter : public XMLVisitor nuclear@0: { nuclear@0: public: nuclear@0: /** Construct the printer. If the FILE* is specified, nuclear@0: this will print to the FILE. Else it will print nuclear@0: to memory, and the result is available in CStr() nuclear@0: */ nuclear@0: XMLPrinter( FILE* file=0 ); nuclear@0: ~XMLPrinter() {} nuclear@0: nuclear@0: /** If streaming, write the BOM and declaration. */ nuclear@0: void PushHeader( bool writeBOM, bool writeDeclaration ); nuclear@0: /** If streaming, start writing an element. nuclear@0: The element must be closed with CloseElement() nuclear@0: */ nuclear@0: void OpenElement( const char* name ); nuclear@0: /// If streaming, add an attribute to an open element. nuclear@0: void PushAttribute( const char* name, const char* value ); nuclear@0: void PushAttribute( const char* name, int value ); nuclear@0: void PushAttribute( const char* name, unsigned value ); nuclear@0: void PushAttribute( const char* name, bool value ); nuclear@0: void PushAttribute( const char* name, double value ); nuclear@0: /// If streaming, close the Element. nuclear@0: void CloseElement(); nuclear@0: nuclear@0: /// Add a text node. nuclear@0: void PushText( const char* text, bool cdata=false ); nuclear@0: /// Add a comment. nuclear@0: void PushComment( const char* comment ); nuclear@0: nuclear@0: void PushDeclaration( const char* value ); nuclear@0: void PushUnknown( const char* value ); nuclear@0: nuclear@0: virtual bool VisitEnter( const XMLDocument& /*doc*/ ); nuclear@0: virtual bool VisitExit( const XMLDocument& /*doc*/ ) { return true; } nuclear@0: nuclear@0: virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute ); nuclear@0: virtual bool VisitExit( const XMLElement& element ); nuclear@0: nuclear@0: virtual bool Visit( const XMLText& text ); nuclear@0: virtual bool Visit( const XMLComment& comment ); nuclear@0: virtual bool Visit( const XMLDeclaration& declaration ); nuclear@0: virtual bool Visit( const XMLUnknown& unknown ); nuclear@0: nuclear@0: /** nuclear@0: If in print to memory mode, return a pointer to nuclear@0: the XML file in memory. nuclear@0: */ nuclear@0: const char* CStr() const { return buffer.Mem(); } nuclear@0: nuclear@0: private: nuclear@0: void SealElement(); nuclear@0: void PrintSpace( int depth ); nuclear@0: void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities. nuclear@0: void Print( const char* format, ... ); nuclear@0: nuclear@0: bool elementJustOpened; nuclear@0: bool firstElement; nuclear@0: FILE* fp; nuclear@0: int depth; nuclear@0: int textDepth; nuclear@0: bool processEntities; nuclear@0: nuclear@0: enum { nuclear@0: ENTITY_RANGE = 64, nuclear@0: BUF_SIZE = 200 nuclear@0: }; nuclear@0: bool entityFlag[ENTITY_RANGE]; nuclear@0: bool restrictedEntityFlag[ENTITY_RANGE]; nuclear@0: nuclear@0: DynArray< const char*, 10 > stack; nuclear@0: DynArray< char, 20 > buffer, accumulator; nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: } // tinyxml2 nuclear@0: nuclear@0: nuclear@0: #endif // TINYXML2_INCLUDED