- can save NPCs and Structures
- more shadering
+
+1/6/2015:
+=========
+
+ - flashlight!
+ - XML-scripted worlds!!!
+ - !!!!!!!!!!!!!!!!!!!!!!!!
#include <iostream>
#include <cstdlib>
+#include <string>
#include <vector>
#include <math.h>
#include <string>
unsigned int millis(void);
#endif // __WIN32__
+int getdir(const char *dir, std::vector<std::string> &files);
+
#endif // COMMON_H
--- /dev/null
+/*\r
+Original code by Lee Thomason (www.grinninglizard.com)\r
+\r
+This software is provided 'as-is', without any express or implied\r
+warranty. In no event will the authors be held liable for any\r
+damages arising from the use of this software.\r
+\r
+Permission is granted to anyone to use this software for any\r
+purpose, including commercial applications, and to alter it and\r
+redistribute it freely, subject to the following restrictions:\r
+\r
+1. The origin of this software must not be misrepresented; you must\r
+not claim that you wrote the original software. If you use this\r
+software in a product, an acknowledgment in the product documentation\r
+would be appreciated but is not required.\r
+\r
+2. Altered source versions must be plainly marked as such, and\r
+must not be misrepresented as being the original software.\r
+\r
+3. This notice may not be removed or altered from any source\r
+distribution.\r
+*/\r
+\r
+#ifndef TINYXML2_INCLUDED\r
+#define TINYXML2_INCLUDED\r
+\r
+#if defined(ANDROID_NDK) || defined(__BORLANDC__) || defined(__QNXNTO__)\r
+# include <ctype.h>\r
+# include <limits.h>\r
+# include <stdio.h>\r
+# include <stdlib.h>\r
+# include <string.h>\r
+#else\r
+# include <cctype>\r
+# include <climits>\r
+# include <cstdio>\r
+# include <cstdlib>\r
+# include <cstring>\r
+#endif\r
+\r
+/*\r
+ TODO: intern strings instead of allocation.\r
+*/\r
+/*\r
+ gcc:\r
+ g++ -Wall -DDEBUG tinyxml2.cpp xmltest.cpp -o gccxmltest.exe\r
+\r
+ Formatting, Artistic Style:\r
+ AStyle.exe --style=1tbs --indent-switches --break-closing-brackets --indent-preprocessor tinyxml2.cpp tinyxml2.h\r
+*/\r
+\r
+#if defined( _DEBUG ) || defined( DEBUG ) || defined (__DEBUG__)\r
+# ifndef DEBUG\r
+# define DEBUG\r
+# endif\r
+#endif\r
+\r
+#ifdef _MSC_VER\r
+# pragma warning(push)\r
+# pragma warning(disable: 4251)\r
+#endif\r
+\r
+#ifdef _WIN32\r
+# ifdef TINYXML2_EXPORT\r
+# define TINYXML2_LIB __declspec(dllexport)\r
+# elif defined(TINYXML2_IMPORT)\r
+# define TINYXML2_LIB __declspec(dllimport)\r
+# else\r
+# define TINYXML2_LIB\r
+# endif\r
+#else\r
+# define TINYXML2_LIB\r
+#endif\r
+\r
+\r
+#if defined(DEBUG)\r
+# if defined(_MSC_VER)\r
+# // "(void)0," is for suppressing C4127 warning in "assert(false)", "assert(true)" and the like\r
+# define TIXMLASSERT( x ) if ( !((void)0,(x))) { __debugbreak(); }\r
+# elif defined (ANDROID_NDK)\r
+# include <android/log.h>\r
+# define TIXMLASSERT( x ) if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); }\r
+# else\r
+# include <assert.h>\r
+# define TIXMLASSERT assert\r
+# endif\r
+#else\r
+# define TIXMLASSERT( x ) {}\r
+#endif\r
+\r
+\r
+/* Versioning, past 1.0.14:\r
+ http://semver.org/\r
+*/\r
+static const int TIXML2_MAJOR_VERSION = 3;\r
+static const int TIXML2_MINOR_VERSION = 0;\r
+static const int TIXML2_PATCH_VERSION = 0;\r
+\r
+namespace tinyxml2\r
+{\r
+class XMLDocument;\r
+class XMLElement;\r
+class XMLAttribute;\r
+class XMLComment;\r
+class XMLText;\r
+class XMLDeclaration;\r
+class XMLUnknown;\r
+class XMLPrinter;\r
+\r
+/*\r
+ A class that wraps strings. Normally stores the start and end\r
+ pointers into the XML file itself, and will apply normalization\r
+ and entity translation if actually read. Can also store (and memory\r
+ manage) a traditional char[]\r
+*/\r
+class StrPair\r
+{\r
+public:\r
+ enum {\r
+ NEEDS_ENTITY_PROCESSING = 0x01,\r
+ NEEDS_NEWLINE_NORMALIZATION = 0x02,\r
+ NEEDS_WHITESPACE_COLLAPSING = 0x04,\r
+\r
+ TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,\r
+ TEXT_ELEMENT_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,\r
+ ATTRIBUTE_NAME = 0,\r
+ ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,\r
+ ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,\r
+ COMMENT = NEEDS_NEWLINE_NORMALIZATION\r
+ };\r
+\r
+ StrPair() : _flags( 0 ), _start( 0 ), _end( 0 ) {}\r
+ ~StrPair();\r
+\r
+ void Set( char* start, char* end, int flags ) {\r
+ Reset();\r
+ _start = start;\r
+ _end = end;\r
+ _flags = flags | NEEDS_FLUSH;\r
+ }\r
+\r
+ const char* GetStr();\r
+\r
+ bool Empty() const {\r
+ return _start == _end;\r
+ }\r
+\r
+ void SetInternedStr( const char* str ) {\r
+ Reset();\r
+ _start = const_cast<char*>(str);\r
+ }\r
+\r
+ void SetStr( const char* str, int flags=0 );\r
+\r
+ char* ParseText( char* in, const char* endTag, int strFlags );\r
+ char* ParseName( char* in );\r
+\r
+ void TransferTo( StrPair* other );\r
+\r
+private:\r
+ void Reset();\r
+ void CollapseWhitespace();\r
+\r
+ enum {\r
+ NEEDS_FLUSH = 0x100,\r
+ NEEDS_DELETE = 0x200\r
+ };\r
+\r
+ int _flags;\r
+ char* _start;\r
+ char* _end;\r
+\r
+ StrPair( const StrPair& other ); // not supported\r
+ void operator=( StrPair& other ); // not supported, use TransferTo()\r
+};\r
+\r
+\r
+/*\r
+ A dynamic array of Plain Old Data. Doesn't support constructors, etc.\r
+ Has a small initial memory pool, so that low or no usage will not\r
+ cause a call to new/delete\r
+*/\r
+template <class T, int INITIAL_SIZE>\r
+class DynArray\r
+{\r
+public:\r
+ DynArray() {\r
+ _mem = _pool;\r
+ _allocated = INITIAL_SIZE;\r
+ _size = 0;\r
+ }\r
+\r
+ ~DynArray() {\r
+ if ( _mem != _pool ) {\r
+ delete [] _mem;\r
+ }\r
+ }\r
+\r
+ void Clear() {\r
+ _size = 0;\r
+ }\r
+\r
+ void Push( T t ) {\r
+ TIXMLASSERT( _size < INT_MAX );\r
+ EnsureCapacity( _size+1 );\r
+ _mem[_size++] = t;\r
+ }\r
+\r
+ T* PushArr( int count ) {\r
+ TIXMLASSERT( count >= 0 );\r
+ TIXMLASSERT( _size <= INT_MAX - count );\r
+ EnsureCapacity( _size+count );\r
+ T* ret = &_mem[_size];\r
+ _size += count;\r
+ return ret;\r
+ }\r
+\r
+ T Pop() {\r
+ TIXMLASSERT( _size > 0 );\r
+ return _mem[--_size];\r
+ }\r
+\r
+ void PopArr( int count ) {\r
+ TIXMLASSERT( _size >= count );\r
+ _size -= count;\r
+ }\r
+\r
+ bool Empty() const {\r
+ return _size == 0;\r
+ }\r
+\r
+ T& operator[](int i) {\r
+ TIXMLASSERT( i>= 0 && i < _size );\r
+ return _mem[i];\r
+ }\r
+\r
+ const T& operator[](int i) const {\r
+ TIXMLASSERT( i>= 0 && i < _size );\r
+ return _mem[i];\r
+ }\r
+\r
+ const T& PeekTop() const {\r
+ TIXMLASSERT( _size > 0 );\r
+ return _mem[ _size - 1];\r
+ }\r
+\r
+ int Size() const {\r
+ TIXMLASSERT( _size >= 0 );\r
+ return _size;\r
+ }\r
+\r
+ int Capacity() const {\r
+ TIXMLASSERT( _allocated >= INITIAL_SIZE );\r
+ return _allocated;\r
+ }\r
+\r
+ const T* Mem() const {\r
+ TIXMLASSERT( _mem );\r
+ return _mem;\r
+ }\r
+\r
+ T* Mem() {\r
+ TIXMLASSERT( _mem );\r
+ return _mem;\r
+ }\r
+\r
+private:\r
+ DynArray( const DynArray& ); // not supported\r
+ void operator=( const DynArray& ); // not supported\r
+\r
+ void EnsureCapacity( int cap ) {\r
+ TIXMLASSERT( cap > 0 );\r
+ if ( cap > _allocated ) {\r
+ TIXMLASSERT( cap <= INT_MAX / 2 );\r
+ int newAllocated = cap * 2;\r
+ T* newMem = new T[newAllocated];\r
+ memcpy( newMem, _mem, sizeof(T)*_size ); // warning: not using constructors, only works for PODs\r
+ if ( _mem != _pool ) {\r
+ delete [] _mem;\r
+ }\r
+ _mem = newMem;\r
+ _allocated = newAllocated;\r
+ }\r
+ }\r
+\r
+ T* _mem;\r
+ T _pool[INITIAL_SIZE];\r
+ int _allocated; // objects allocated\r
+ int _size; // number objects in use\r
+};\r
+\r
+\r
+/*\r
+ Parent virtual class of a pool for fast allocation\r
+ and deallocation of objects.\r
+*/\r
+class MemPool\r
+{\r
+public:\r
+ MemPool() {}\r
+ virtual ~MemPool() {}\r
+\r
+ virtual int ItemSize() const = 0;\r
+ virtual void* Alloc() = 0;\r
+ virtual void Free( void* ) = 0;\r
+ virtual void SetTracked() = 0;\r
+ virtual void Clear() = 0;\r
+};\r
+\r
+\r
+/*\r
+ Template child class to create pools of the correct type.\r
+*/\r
+template< int SIZE >\r
+class MemPoolT : public MemPool\r
+{\r
+public:\r
+ MemPoolT() : _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {}\r
+ ~MemPoolT() {\r
+ Clear();\r
+ }\r
+ \r
+ void Clear() {\r
+ // Delete the blocks.\r
+ while( !_blockPtrs.Empty()) {\r
+ Block* b = _blockPtrs.Pop();\r
+ delete b;\r
+ }\r
+ _root = 0;\r
+ _currentAllocs = 0;\r
+ _nAllocs = 0;\r
+ _maxAllocs = 0;\r
+ _nUntracked = 0;\r
+ }\r
+\r
+ virtual int ItemSize() const {\r
+ return SIZE;\r
+ }\r
+ int CurrentAllocs() const {\r
+ return _currentAllocs;\r
+ }\r
+\r
+ virtual void* Alloc() {\r
+ if ( !_root ) {\r
+ // Need a new block.\r
+ Block* block = new Block();\r
+ _blockPtrs.Push( block );\r
+\r
+ for( int i=0; i<COUNT-1; ++i ) {\r
+ block->chunk[i].next = &block->chunk[i+1];\r
+ }\r
+ block->chunk[COUNT-1].next = 0;\r
+ _root = block->chunk;\r
+ }\r
+ void* result = _root;\r
+ _root = _root->next;\r
+\r
+ ++_currentAllocs;\r
+ if ( _currentAllocs > _maxAllocs ) {\r
+ _maxAllocs = _currentAllocs;\r
+ }\r
+ _nAllocs++;\r
+ _nUntracked++;\r
+ return result;\r
+ }\r
+ \r
+ virtual void Free( void* mem ) {\r
+ if ( !mem ) {\r
+ return;\r
+ }\r
+ --_currentAllocs;\r
+ Chunk* chunk = static_cast<Chunk*>( mem );\r
+#ifdef DEBUG\r
+ memset( chunk, 0xfe, sizeof(Chunk) );\r
+#endif\r
+ chunk->next = _root;\r
+ _root = chunk;\r
+ }\r
+ void Trace( const char* name ) {\r
+ printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",\r
+ name, _maxAllocs, _maxAllocs*SIZE/1024, _currentAllocs, SIZE, _nAllocs, _blockPtrs.Size() );\r
+ }\r
+\r
+ void SetTracked() {\r
+ _nUntracked--;\r
+ }\r
+\r
+ int Untracked() const {\r
+ return _nUntracked;\r
+ }\r
+\r
+ // This number is perf sensitive. 4k seems like a good tradeoff on my machine.\r
+ // The test file is large, 170k.\r
+ // Release: VS2010 gcc(no opt)\r
+ // 1k: 4000\r
+ // 2k: 4000\r
+ // 4k: 3900 21000\r
+ // 16k: 5200\r
+ // 32k: 4300\r
+ // 64k: 4000 21000\r
+ enum { COUNT = (4*1024)/SIZE }; // Some compilers do not accept to use COUNT in private part if COUNT is private\r
+\r
+private:\r
+ MemPoolT( const MemPoolT& ); // not supported\r
+ void operator=( const MemPoolT& ); // not supported\r
+\r
+ union Chunk {\r
+ Chunk* next;\r
+ char mem[SIZE];\r
+ };\r
+ struct Block {\r
+ Chunk chunk[COUNT];\r
+ };\r
+ DynArray< Block*, 10 > _blockPtrs;\r
+ Chunk* _root;\r
+\r
+ int _currentAllocs;\r
+ int _nAllocs;\r
+ int _maxAllocs;\r
+ int _nUntracked;\r
+};\r
+\r
+\r
+\r
+/**\r
+ Implements the interface to the "Visitor pattern" (see the Accept() method.)\r
+ If you call the Accept() method, it requires being passed a XMLVisitor\r
+ class to handle callbacks. For nodes that contain other nodes (Document, Element)\r
+ you will get called with a VisitEnter/VisitExit pair. Nodes that are always leafs\r
+ are simply called with Visit().\r
+\r
+ If you return 'true' from a Visit method, recursive parsing will continue. If you return\r
+ false, <b>no children of this node or its siblings</b> will be visited.\r
+\r
+ All flavors of Visit methods have a default implementation that returns 'true' (continue\r
+ visiting). You need to only override methods that are interesting to you.\r
+\r
+ Generally Accept() is called on the XMLDocument, although all nodes support visiting.\r
+\r
+ You should never change the document from a callback.\r
+\r
+ @sa XMLNode::Accept()\r
+*/\r
+class TINYXML2_LIB XMLVisitor\r
+{\r
+public:\r
+ virtual ~XMLVisitor() {}\r
+\r
+ /// Visit a document.\r
+ virtual bool VisitEnter( const XMLDocument& /*doc*/ ) {\r
+ return true;\r
+ }\r
+ /// Visit a document.\r
+ virtual bool VisitExit( const XMLDocument& /*doc*/ ) {\r
+ return true;\r
+ }\r
+\r
+ /// Visit an element.\r
+ virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) {\r
+ return true;\r
+ }\r
+ /// Visit an element.\r
+ virtual bool VisitExit( const XMLElement& /*element*/ ) {\r
+ return true;\r
+ }\r
+\r
+ /// Visit a declaration.\r
+ virtual bool Visit( const XMLDeclaration& /*declaration*/ ) {\r
+ return true;\r
+ }\r
+ /// Visit a text node.\r
+ virtual bool Visit( const XMLText& /*text*/ ) {\r
+ return true;\r
+ }\r
+ /// Visit a comment node.\r
+ virtual bool Visit( const XMLComment& /*comment*/ ) {\r
+ return true;\r
+ }\r
+ /// Visit an unknown node.\r
+ virtual bool Visit( const XMLUnknown& /*unknown*/ ) {\r
+ return true;\r
+ }\r
+};\r
+\r
+// WARNING: must match XMLDocument::_errorNames[]\r
+enum XMLError {\r
+ XML_SUCCESS = 0,\r
+ XML_NO_ERROR = 0,\r
+ XML_NO_ATTRIBUTE,\r
+ XML_WRONG_ATTRIBUTE_TYPE,\r
+ XML_ERROR_FILE_NOT_FOUND,\r
+ XML_ERROR_FILE_COULD_NOT_BE_OPENED,\r
+ XML_ERROR_FILE_READ_ERROR,\r
+ XML_ERROR_ELEMENT_MISMATCH,\r
+ XML_ERROR_PARSING_ELEMENT,\r
+ XML_ERROR_PARSING_ATTRIBUTE,\r
+ XML_ERROR_IDENTIFYING_TAG,\r
+ XML_ERROR_PARSING_TEXT,\r
+ XML_ERROR_PARSING_CDATA,\r
+ XML_ERROR_PARSING_COMMENT,\r
+ XML_ERROR_PARSING_DECLARATION,\r
+ XML_ERROR_PARSING_UNKNOWN,\r
+ XML_ERROR_EMPTY_DOCUMENT,\r
+ XML_ERROR_MISMATCHED_ELEMENT,\r
+ XML_ERROR_PARSING,\r
+ XML_CAN_NOT_CONVERT_TEXT,\r
+ XML_NO_TEXT_NODE,\r
+\r
+ XML_ERROR_COUNT\r
+};\r
+\r
+\r
+/*\r
+ Utility functionality.\r
+*/\r
+class XMLUtil\r
+{\r
+public:\r
+ static const char* SkipWhiteSpace( const char* p ) {\r
+ TIXMLASSERT( p );\r
+ while( IsWhiteSpace(*p) ) {\r
+ ++p;\r
+ }\r
+ TIXMLASSERT( p );\r
+ return p;\r
+ }\r
+ static char* SkipWhiteSpace( char* p ) {\r
+ return const_cast<char*>( SkipWhiteSpace( const_cast<const char*>(p) ) );\r
+ }\r
+\r
+ // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't\r
+ // correct, but simple, and usually works.\r
+ static bool IsWhiteSpace( char p ) {\r
+ return !IsUTF8Continuation(p) && isspace( static_cast<unsigned char>(p) );\r
+ }\r
+ \r
+ inline static bool IsNameStartChar( unsigned char ch ) {\r
+ if ( ch >= 128 ) {\r
+ // This is a heuristic guess in attempt to not implement Unicode-aware isalpha()\r
+ return true;\r
+ }\r
+ if ( isalpha( ch ) ) {\r
+ return true;\r
+ }\r
+ return ch == ':' || ch == '_';\r
+ }\r
+ \r
+ inline static bool IsNameChar( unsigned char ch ) {\r
+ return IsNameStartChar( ch )\r
+ || isdigit( ch )\r
+ || ch == '.'\r
+ || ch == '-';\r
+ }\r
+\r
+ inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {\r
+ if ( p == q ) {\r
+ return true;\r
+ }\r
+ return strncmp( p, q, nChar ) == 0;\r
+ }\r
+ \r
+ inline static bool IsUTF8Continuation( char p ) {\r
+ return ( p & 0x80 ) != 0;\r
+ }\r
+\r
+ static const char* ReadBOM( const char* p, bool* hasBOM );\r
+ // p is the starting location,\r
+ // the UTF-8 value of the entity will be placed in value, and length filled in.\r
+ static const char* GetCharacterRef( const char* p, char* value, int* length );\r
+ static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );\r
+\r
+ // converts primitive types to strings\r
+ static void ToStr( int v, char* buffer, int bufferSize );\r
+ static void ToStr( unsigned v, char* buffer, int bufferSize );\r
+ static void ToStr( bool v, char* buffer, int bufferSize );\r
+ static void ToStr( float v, char* buffer, int bufferSize );\r
+ static void ToStr( double v, char* buffer, int bufferSize );\r
+\r
+ // converts strings to primitive types\r
+ static bool ToInt( const char* str, int* value );\r
+ static bool ToUnsigned( const char* str, unsigned* value );\r
+ static bool ToBool( const char* str, bool* value );\r
+ static bool ToFloat( const char* str, float* value );\r
+ static bool ToDouble( const char* str, double* value );\r
+};\r
+\r
+\r
+/** XMLNode is a base class for every object that is in the\r
+ XML Document Object Model (DOM), except XMLAttributes.\r
+ Nodes have siblings, a parent, and children which can\r
+ be navigated. A node is always in a XMLDocument.\r
+ The type of a XMLNode can be queried, and it can\r
+ be cast to its more defined type.\r
+\r
+ A XMLDocument allocates memory for all its Nodes.\r
+ When the XMLDocument gets deleted, all its Nodes\r
+ will also be deleted.\r
+\r
+ @verbatim\r
+ A Document can contain: Element (container or leaf)\r
+ Comment (leaf)\r
+ Unknown (leaf)\r
+ Declaration( leaf )\r
+\r
+ An Element can contain: Element (container or leaf)\r
+ Text (leaf)\r
+ Attributes (not on tree)\r
+ Comment (leaf)\r
+ Unknown (leaf)\r
+\r
+ @endverbatim\r
+*/\r
+class TINYXML2_LIB XMLNode\r
+{\r
+ friend class XMLDocument;\r
+ friend class XMLElement;\r
+public:\r
+\r
+ /// Get the XMLDocument that owns this XMLNode.\r
+ const XMLDocument* GetDocument() const {\r
+ TIXMLASSERT( _document );\r
+ return _document;\r
+ }\r
+ /// Get the XMLDocument that owns this XMLNode.\r
+ XMLDocument* GetDocument() {\r
+ TIXMLASSERT( _document );\r
+ return _document;\r
+ }\r
+\r
+ /// Safely cast to an Element, or null.\r
+ virtual XMLElement* ToElement() {\r
+ return 0;\r
+ }\r
+ /// Safely cast to Text, or null.\r
+ virtual XMLText* ToText() {\r
+ return 0;\r
+ }\r
+ /// Safely cast to a Comment, or null.\r
+ virtual XMLComment* ToComment() {\r
+ return 0;\r
+ }\r
+ /// Safely cast to a Document, or null.\r
+ virtual XMLDocument* ToDocument() {\r
+ return 0;\r
+ }\r
+ /// Safely cast to a Declaration, or null.\r
+ virtual XMLDeclaration* ToDeclaration() {\r
+ return 0;\r
+ }\r
+ /// Safely cast to an Unknown, or null.\r
+ virtual XMLUnknown* ToUnknown() {\r
+ return 0;\r
+ }\r
+\r
+ virtual const XMLElement* ToElement() const {\r
+ return 0;\r
+ }\r
+ virtual const XMLText* ToText() const {\r
+ return 0;\r
+ }\r
+ virtual const XMLComment* ToComment() const {\r
+ return 0;\r
+ }\r
+ virtual const XMLDocument* ToDocument() const {\r
+ return 0;\r
+ }\r
+ virtual const XMLDeclaration* ToDeclaration() const {\r
+ return 0;\r
+ }\r
+ virtual const XMLUnknown* ToUnknown() const {\r
+ return 0;\r
+ }\r
+\r
+ /** The meaning of 'value' changes for the specific type.\r
+ @verbatim\r
+ Document: empty (NULL is returned, not an empty string)\r
+ Element: name of the element\r
+ Comment: the comment text\r
+ Unknown: the tag contents\r
+ Text: the text string\r
+ @endverbatim\r
+ */\r
+ const char* Value() const;\r
+\r
+ /** Set the Value of an XML node.\r
+ @sa Value()\r
+ */\r
+ void SetValue( const char* val, bool staticMem=false );\r
+\r
+ /// Get the parent of this node on the DOM.\r
+ const XMLNode* Parent() const {\r
+ return _parent;\r
+ }\r
+\r
+ XMLNode* Parent() {\r
+ return _parent;\r
+ }\r
+\r
+ /// Returns true if this node has no children.\r
+ bool NoChildren() const {\r
+ return !_firstChild;\r
+ }\r
+\r
+ /// Get the first child node, or null if none exists.\r
+ const XMLNode* FirstChild() const {\r
+ return _firstChild;\r
+ }\r
+\r
+ XMLNode* FirstChild() {\r
+ return _firstChild;\r
+ }\r
+\r
+ /** Get the first child element, or optionally the first child\r
+ element with the specified name.\r
+ */\r
+ const XMLElement* FirstChildElement( const char* name = 0 ) const;\r
+\r
+ XMLElement* FirstChildElement( const char* name = 0 ) {\r
+ return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->FirstChildElement( name ));\r
+ }\r
+\r
+ /// Get the last child node, or null if none exists.\r
+ const XMLNode* LastChild() const {\r
+ return _lastChild;\r
+ }\r
+\r
+ XMLNode* LastChild() {\r
+ return _lastChild;\r
+ }\r
+\r
+ /** Get the last child element or optionally the last child\r
+ element with the specified name.\r
+ */\r
+ const XMLElement* LastChildElement( const char* name = 0 ) const;\r
+\r
+ XMLElement* LastChildElement( const char* name = 0 ) {\r
+ return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->LastChildElement(name) );\r
+ }\r
+\r
+ /// Get the previous (left) sibling node of this node.\r
+ const XMLNode* PreviousSibling() const {\r
+ return _prev;\r
+ }\r
+\r
+ XMLNode* PreviousSibling() {\r
+ return _prev;\r
+ }\r
+\r
+ /// Get the previous (left) sibling element of this node, with an optionally supplied name.\r
+ const XMLElement* PreviousSiblingElement( const char* name = 0 ) const ;\r
+\r
+ XMLElement* PreviousSiblingElement( const char* name = 0 ) {\r
+ return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->PreviousSiblingElement( name ) );\r
+ }\r
+\r
+ /// Get the next (right) sibling node of this node.\r
+ const XMLNode* NextSibling() const {\r
+ return _next;\r
+ }\r
+\r
+ XMLNode* NextSibling() {\r
+ return _next;\r
+ }\r
+\r
+ /// Get the next (right) sibling element of this node, with an optionally supplied name.\r
+ const XMLElement* NextSiblingElement( const char* name = 0 ) const;\r
+\r
+ XMLElement* NextSiblingElement( const char* name = 0 ) {\r
+ return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->NextSiblingElement( name ) );\r
+ }\r
+\r
+ /**\r
+ Add a child node as the last (right) child.\r
+ If the child node is already part of the document,\r
+ it is moved from its old location to the new location.\r
+ Returns the addThis argument or 0 if the node does not\r
+ belong to the same document.\r
+ */\r
+ XMLNode* InsertEndChild( XMLNode* addThis );\r
+\r
+ XMLNode* LinkEndChild( XMLNode* addThis ) {\r
+ return InsertEndChild( addThis );\r
+ }\r
+ /**\r
+ Add a child node as the first (left) child.\r
+ If the child node is already part of the document,\r
+ it is moved from its old location to the new location.\r
+ Returns the addThis argument or 0 if the node does not\r
+ belong to the same document.\r
+ */\r
+ XMLNode* InsertFirstChild( XMLNode* addThis );\r
+ /**\r
+ Add a node after the specified child node.\r
+ If the child node is already part of the document,\r
+ it is moved from its old location to the new location.\r
+ Returns the addThis argument or 0 if the afterThis node\r
+ is not a child of this node, or if the node does not\r
+ belong to the same document.\r
+ */\r
+ XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis );\r
+\r
+ /**\r
+ Delete all the children of this node.\r
+ */\r
+ void DeleteChildren();\r
+\r
+ /**\r
+ Delete a child of this node.\r
+ */\r
+ void DeleteChild( XMLNode* node );\r
+\r
+ /**\r
+ Make a copy of this node, but not its children.\r
+ You may pass in a Document pointer that will be\r
+ the owner of the new Node. If the 'document' is\r
+ null, then the node returned will be allocated\r
+ from the current Document. (this->GetDocument())\r
+\r
+ Note: if called on a XMLDocument, this will return null.\r
+ */\r
+ virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0;\r
+\r
+ /**\r
+ Test if 2 nodes are the same, but don't test children.\r
+ The 2 nodes do not need to be in the same Document.\r
+\r
+ Note: if called on a XMLDocument, this will return false.\r
+ */\r
+ virtual bool ShallowEqual( const XMLNode* compare ) const = 0;\r
+\r
+ /** Accept a hierarchical visit of the nodes in the TinyXML-2 DOM. Every node in the\r
+ XML tree will be conditionally visited and the host will be called back\r
+ via the XMLVisitor interface.\r
+\r
+ This is essentially a SAX interface for TinyXML-2. (Note however it doesn't re-parse\r
+ the XML for the callbacks, so the performance of TinyXML-2 is unchanged by using this\r
+ interface versus any other.)\r
+\r
+ The interface has been based on ideas from:\r
+\r
+ - http://www.saxproject.org/\r
+ - http://c2.com/cgi/wiki?HierarchicalVisitorPattern\r
+\r
+ Which are both good references for "visiting".\r
+\r
+ An example of using Accept():\r
+ @verbatim\r
+ XMLPrinter printer;\r
+ tinyxmlDoc.Accept( &printer );\r
+ const char* xmlcstr = printer.CStr();\r
+ @endverbatim\r
+ */\r
+ virtual bool Accept( XMLVisitor* visitor ) const = 0;\r
+\r
+protected:\r
+ XMLNode( XMLDocument* );\r
+ virtual ~XMLNode();\r
+\r
+ virtual char* ParseDeep( char*, StrPair* );\r
+\r
+ XMLDocument* _document;\r
+ XMLNode* _parent;\r
+ mutable StrPair _value;\r
+\r
+ XMLNode* _firstChild;\r
+ XMLNode* _lastChild;\r
+\r
+ XMLNode* _prev;\r
+ XMLNode* _next;\r
+\r
+private:\r
+ MemPool* _memPool;\r
+ void Unlink( XMLNode* child );\r
+ static void DeleteNode( XMLNode* node );\r
+ void InsertChildPreamble( XMLNode* insertThis ) const;\r
+\r
+ XMLNode( const XMLNode& ); // not supported\r
+ XMLNode& operator=( const XMLNode& ); // not supported\r
+};\r
+\r
+\r
+/** XML text.\r
+\r
+ Note that a text node can have child element nodes, for example:\r
+ @verbatim\r
+ <root>This is <b>bold</b></root>\r
+ @endverbatim\r
+\r
+ A text node can have 2 ways to output the next. "normal" output\r
+ and CDATA. It will default to the mode it was parsed from the XML file and\r
+ you generally want to leave it alone, but you can change the output mode with\r
+ SetCData() and query it with CData().\r
+*/\r
+class TINYXML2_LIB XMLText : public XMLNode\r
+{\r
+ friend class XMLBase;\r
+ friend class XMLDocument;\r
+public:\r
+ virtual bool Accept( XMLVisitor* visitor ) const;\r
+\r
+ virtual XMLText* ToText() {\r
+ return this;\r
+ }\r
+ virtual const XMLText* ToText() const {\r
+ return this;\r
+ }\r
+\r
+ /// Declare whether this should be CDATA or standard text.\r
+ void SetCData( bool isCData ) {\r
+ _isCData = isCData;\r
+ }\r
+ /// Returns true if this is a CDATA text element.\r
+ bool CData() const {\r
+ return _isCData;\r
+ }\r
+\r
+ virtual XMLNode* ShallowClone( XMLDocument* document ) const;\r
+ virtual bool ShallowEqual( const XMLNode* compare ) const;\r
+\r
+protected:\r
+ XMLText( XMLDocument* doc ) : XMLNode( doc ), _isCData( false ) {}\r
+ virtual ~XMLText() {}\r
+\r
+ char* ParseDeep( char*, StrPair* endTag );\r
+\r
+private:\r
+ bool _isCData;\r
+\r
+ XMLText( const XMLText& ); // not supported\r
+ XMLText& operator=( const XMLText& ); // not supported\r
+};\r
+\r
+\r
+/** An XML Comment. */\r
+class TINYXML2_LIB XMLComment : public XMLNode\r
+{\r
+ friend class XMLDocument;\r
+public:\r
+ virtual XMLComment* ToComment() {\r
+ return this;\r
+ }\r
+ virtual const XMLComment* ToComment() const {\r
+ return this;\r
+ }\r
+\r
+ virtual bool Accept( XMLVisitor* visitor ) const;\r
+\r
+ virtual XMLNode* ShallowClone( XMLDocument* document ) const;\r
+ virtual bool ShallowEqual( const XMLNode* compare ) const;\r
+\r
+protected:\r
+ XMLComment( XMLDocument* doc );\r
+ virtual ~XMLComment();\r
+\r
+ char* ParseDeep( char*, StrPair* endTag );\r
+\r
+private:\r
+ XMLComment( const XMLComment& ); // not supported\r
+ XMLComment& operator=( const XMLComment& ); // not supported\r
+};\r
+\r
+\r
+/** In correct XML the declaration is the first entry in the file.\r
+ @verbatim\r
+ <?xml version="1.0" standalone="yes"?>\r
+ @endverbatim\r
+\r
+ TinyXML-2 will happily read or write files without a declaration,\r
+ however.\r
+\r
+ The text of the declaration isn't interpreted. It is parsed\r
+ and written as a string.\r
+*/\r
+class TINYXML2_LIB XMLDeclaration : public XMLNode\r
+{\r
+ friend class XMLDocument;\r
+public:\r
+ virtual XMLDeclaration* ToDeclaration() {\r
+ return this;\r
+ }\r
+ virtual const XMLDeclaration* ToDeclaration() const {\r
+ return this;\r
+ }\r
+\r
+ virtual bool Accept( XMLVisitor* visitor ) const;\r
+\r
+ virtual XMLNode* ShallowClone( XMLDocument* document ) const;\r
+ virtual bool ShallowEqual( const XMLNode* compare ) const;\r
+\r
+protected:\r
+ XMLDeclaration( XMLDocument* doc );\r
+ virtual ~XMLDeclaration();\r
+\r
+ char* ParseDeep( char*, StrPair* endTag );\r
+\r
+private:\r
+ XMLDeclaration( const XMLDeclaration& ); // not supported\r
+ XMLDeclaration& operator=( const XMLDeclaration& ); // not supported\r
+};\r
+\r
+\r
+/** Any tag that TinyXML-2 doesn't recognize is saved as an\r
+ unknown. It is a tag of text, but should not be modified.\r
+ It will be written back to the XML, unchanged, when the file\r
+ is saved.\r
+\r
+ DTD tags get thrown into XMLUnknowns.\r
+*/\r
+class TINYXML2_LIB XMLUnknown : public XMLNode\r
+{\r
+ friend class XMLDocument;\r
+public:\r
+ virtual XMLUnknown* ToUnknown() {\r
+ return this;\r
+ }\r
+ virtual const XMLUnknown* ToUnknown() const {\r
+ return this;\r
+ }\r
+\r
+ virtual bool Accept( XMLVisitor* visitor ) const;\r
+\r
+ virtual XMLNode* ShallowClone( XMLDocument* document ) const;\r
+ virtual bool ShallowEqual( const XMLNode* compare ) const;\r
+\r
+protected:\r
+ XMLUnknown( XMLDocument* doc );\r
+ virtual ~XMLUnknown();\r
+\r
+ char* ParseDeep( char*, StrPair* endTag );\r
+\r
+private:\r
+ XMLUnknown( const XMLUnknown& ); // not supported\r
+ XMLUnknown& operator=( const XMLUnknown& ); // not supported\r
+};\r
+\r
+\r
+\r
+/** An attribute is a name-value pair. Elements have an arbitrary\r
+ number of attributes, each with a unique name.\r
+\r
+ @note The attributes are not XMLNodes. You may only query the\r
+ Next() attribute in a list.\r
+*/\r
+class TINYXML2_LIB XMLAttribute\r
+{\r
+ friend class XMLElement;\r
+public:\r
+ /// The name of the attribute.\r
+ const char* Name() const;\r
+\r
+ /// The value of the attribute.\r
+ const char* Value() const;\r
+\r
+ /// The next attribute in the list.\r
+ const XMLAttribute* Next() const {\r
+ return _next;\r
+ }\r
+\r
+ /** IntValue interprets the attribute as an integer, and returns the value.\r
+ If the value isn't an integer, 0 will be returned. There is no error checking;\r
+ use QueryIntValue() if you need error checking.\r
+ */\r
+ int IntValue() const {\r
+ int i=0;\r
+ QueryIntValue( &i );\r
+ return i;\r
+ }\r
+ /// Query as an unsigned integer. See IntValue()\r
+ unsigned UnsignedValue() const {\r
+ unsigned i=0;\r
+ QueryUnsignedValue( &i );\r
+ return i;\r
+ }\r
+ /// Query as a boolean. See IntValue()\r
+ bool BoolValue() const {\r
+ bool b=false;\r
+ QueryBoolValue( &b );\r
+ return b;\r
+ }\r
+ /// Query as a double. See IntValue()\r
+ double DoubleValue() const {\r
+ double d=0;\r
+ QueryDoubleValue( &d );\r
+ return d;\r
+ }\r
+ /// Query as a float. See IntValue()\r
+ float FloatValue() const {\r
+ float f=0;\r
+ QueryFloatValue( &f );\r
+ return f;\r
+ }\r
+\r
+ /** QueryIntValue interprets the attribute as an integer, and returns the value\r
+ in the provided parameter. The function will return XML_NO_ERROR on success,\r
+ and XML_WRONG_ATTRIBUTE_TYPE if the conversion is not successful.\r
+ */\r
+ XMLError QueryIntValue( int* value ) const;\r
+ /// See QueryIntValue\r
+ XMLError QueryUnsignedValue( unsigned int* value ) const;\r
+ /// See QueryIntValue\r
+ XMLError QueryBoolValue( bool* value ) const;\r
+ /// See QueryIntValue\r
+ XMLError QueryDoubleValue( double* value ) const;\r
+ /// See QueryIntValue\r
+ XMLError QueryFloatValue( float* value ) const;\r
+\r
+ /// Set the attribute to a string value.\r
+ void SetAttribute( const char* value );\r
+ /// Set the attribute to value.\r
+ void SetAttribute( int value );\r
+ /// Set the attribute to value.\r
+ void SetAttribute( unsigned value );\r
+ /// Set the attribute to value.\r
+ void SetAttribute( bool value );\r
+ /// Set the attribute to value.\r
+ void SetAttribute( double value );\r
+ /// Set the attribute to value.\r
+ void SetAttribute( float value );\r
+\r
+private:\r
+ enum { BUF_SIZE = 200 };\r
+\r
+ XMLAttribute() : _next( 0 ), _memPool( 0 ) {}\r
+ virtual ~XMLAttribute() {}\r
+\r
+ XMLAttribute( const XMLAttribute& ); // not supported\r
+ void operator=( const XMLAttribute& ); // not supported\r
+ void SetName( const char* name );\r
+\r
+ char* ParseDeep( char* p, bool processEntities );\r
+\r
+ mutable StrPair _name;\r
+ mutable StrPair _value;\r
+ XMLAttribute* _next;\r
+ MemPool* _memPool;\r
+};\r
+\r
+\r
+/** The element is a container class. It has a value, the element name,\r
+ and can contain other elements, text, comments, and unknowns.\r
+ Elements also contain an arbitrary number of attributes.\r
+*/\r
+class TINYXML2_LIB XMLElement : public XMLNode\r
+{\r
+ friend class XMLBase;\r
+ friend class XMLDocument;\r
+public:\r
+ /// Get the name of an element (which is the Value() of the node.)\r
+ const char* Name() const {\r
+ return Value();\r
+ }\r
+ /// Set the name of the element.\r
+ void SetName( const char* str, bool staticMem=false ) {\r
+ SetValue( str, staticMem );\r
+ }\r
+\r
+ virtual XMLElement* ToElement() {\r
+ return this;\r
+ }\r
+ virtual const XMLElement* ToElement() const {\r
+ return this;\r
+ }\r
+ virtual bool Accept( XMLVisitor* visitor ) const;\r
+\r
+ /** Given an attribute name, Attribute() returns the value\r
+ for the attribute of that name, or null if none\r
+ exists. For example:\r
+\r
+ @verbatim\r
+ const char* value = ele->Attribute( "foo" );\r
+ @endverbatim\r
+\r
+ The 'value' parameter is normally null. However, if specified,\r
+ the attribute will only be returned if the 'name' and 'value'\r
+ match. This allow you to write code:\r
+\r
+ @verbatim\r
+ if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar();\r
+ @endverbatim\r
+\r
+ rather than:\r
+ @verbatim\r
+ if ( ele->Attribute( "foo" ) ) {\r
+ if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();\r
+ }\r
+ @endverbatim\r
+ */\r
+ const char* Attribute( const char* name, const char* value=0 ) const;\r
+\r
+ /** Given an attribute name, IntAttribute() returns the value\r
+ of the attribute interpreted as an integer. 0 will be\r
+ returned if there is an error. For a method with error\r
+ checking, see QueryIntAttribute()\r
+ */\r
+ int IntAttribute( const char* name ) const {\r
+ int i=0;\r
+ QueryIntAttribute( name, &i );\r
+ return i;\r
+ }\r
+ /// See IntAttribute()\r
+ unsigned UnsignedAttribute( const char* name ) const {\r
+ unsigned i=0;\r
+ QueryUnsignedAttribute( name, &i );\r
+ return i;\r
+ }\r
+ /// See IntAttribute()\r
+ bool BoolAttribute( const char* name ) const {\r
+ bool b=false;\r
+ QueryBoolAttribute( name, &b );\r
+ return b;\r
+ }\r
+ /// See IntAttribute()\r
+ double DoubleAttribute( const char* name ) const {\r
+ double d=0;\r
+ QueryDoubleAttribute( name, &d );\r
+ return d;\r
+ }\r
+ /// See IntAttribute()\r
+ float FloatAttribute( const char* name ) const {\r
+ float f=0;\r
+ QueryFloatAttribute( name, &f );\r
+ return f;\r
+ }\r
+\r
+ /** Given an attribute name, QueryIntAttribute() returns\r
+ XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion\r
+ can't be performed, or XML_NO_ATTRIBUTE if the attribute\r
+ doesn't exist. If successful, the result of the conversion\r
+ will be written to 'value'. If not successful, nothing will\r
+ be written to 'value'. This allows you to provide default\r
+ value:\r
+\r
+ @verbatim\r
+ int value = 10;\r
+ QueryIntAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10\r
+ @endverbatim\r
+ */\r
+ XMLError QueryIntAttribute( const char* name, int* value ) const {\r
+ const XMLAttribute* a = FindAttribute( name );\r
+ if ( !a ) {\r
+ return XML_NO_ATTRIBUTE;\r
+ }\r
+ return a->QueryIntValue( value );\r
+ }\r
+ /// See QueryIntAttribute()\r
+ XMLError QueryUnsignedAttribute( const char* name, unsigned int* value ) const {\r
+ const XMLAttribute* a = FindAttribute( name );\r
+ if ( !a ) {\r
+ return XML_NO_ATTRIBUTE;\r
+ }\r
+ return a->QueryUnsignedValue( value );\r
+ }\r
+ /// See QueryIntAttribute()\r
+ XMLError QueryBoolAttribute( const char* name, bool* value ) const {\r
+ const XMLAttribute* a = FindAttribute( name );\r
+ if ( !a ) {\r
+ return XML_NO_ATTRIBUTE;\r
+ }\r
+ return a->QueryBoolValue( value );\r
+ }\r
+ /// See QueryIntAttribute()\r
+ XMLError QueryDoubleAttribute( const char* name, double* value ) const {\r
+ const XMLAttribute* a = FindAttribute( name );\r
+ if ( !a ) {\r
+ return XML_NO_ATTRIBUTE;\r
+ }\r
+ return a->QueryDoubleValue( value );\r
+ }\r
+ /// See QueryIntAttribute()\r
+ XMLError QueryFloatAttribute( const char* name, float* value ) const {\r
+ const XMLAttribute* a = FindAttribute( name );\r
+ if ( !a ) {\r
+ return XML_NO_ATTRIBUTE;\r
+ }\r
+ return a->QueryFloatValue( value );\r
+ }\r
+\r
+ \r
+ /** Given an attribute name, QueryAttribute() returns\r
+ XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion\r
+ can't be performed, or XML_NO_ATTRIBUTE if the attribute\r
+ doesn't exist. It is overloaded for the primitive types,\r
+ and is a generally more convenient replacement of\r
+ QueryIntAttribute() and related functions.\r
+ \r
+ If successful, the result of the conversion\r
+ will be written to 'value'. If not successful, nothing will\r
+ be written to 'value'. This allows you to provide default\r
+ value:\r
+\r
+ @verbatim\r
+ int value = 10;\r
+ QueryAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10\r
+ @endverbatim\r
+ */\r
+ int QueryAttribute( const char* name, int* value ) const {\r
+ return QueryIntAttribute( name, value );\r
+ }\r
+\r
+ int QueryAttribute( const char* name, unsigned int* value ) const {\r
+ return QueryUnsignedAttribute( name, value );\r
+ }\r
+\r
+ int QueryAttribute( const char* name, bool* value ) const {\r
+ return QueryBoolAttribute( name, value );\r
+ }\r
+\r
+ int QueryAttribute( const char* name, double* value ) const {\r
+ return QueryDoubleAttribute( name, value );\r
+ }\r
+\r
+ int QueryAttribute( const char* name, float* value ) const {\r
+ return QueryFloatAttribute( name, value );\r
+ }\r
+\r
+ /// Sets the named attribute to value.\r
+ void SetAttribute( const char* name, const char* value ) {\r
+ XMLAttribute* a = FindOrCreateAttribute( name );\r
+ a->SetAttribute( value );\r
+ }\r
+ /// Sets the named attribute to value.\r
+ void SetAttribute( const char* name, int value ) {\r
+ XMLAttribute* a = FindOrCreateAttribute( name );\r
+ a->SetAttribute( value );\r
+ }\r
+ /// Sets the named attribute to value.\r
+ void SetAttribute( const char* name, unsigned value ) {\r
+ XMLAttribute* a = FindOrCreateAttribute( name );\r
+ a->SetAttribute( value );\r
+ }\r
+ /// Sets the named attribute to value.\r
+ void SetAttribute( const char* name, bool value ) {\r
+ XMLAttribute* a = FindOrCreateAttribute( name );\r
+ a->SetAttribute( value );\r
+ }\r
+ /// Sets the named attribute to value.\r
+ void SetAttribute( const char* name, double value ) {\r
+ XMLAttribute* a = FindOrCreateAttribute( name );\r
+ a->SetAttribute( value );\r
+ }\r
+ /// Sets the named attribute to value.\r
+ void SetAttribute( const char* name, float value ) {\r
+ XMLAttribute* a = FindOrCreateAttribute( name );\r
+ a->SetAttribute( value );\r
+ }\r
+\r
+ /**\r
+ Delete an attribute.\r
+ */\r
+ void DeleteAttribute( const char* name );\r
+\r
+ /// Return the first attribute in the list.\r
+ const XMLAttribute* FirstAttribute() const {\r
+ return _rootAttribute;\r
+ }\r
+ /// Query a specific attribute in the list.\r
+ const XMLAttribute* FindAttribute( const char* name ) const;\r
+\r
+ /** Convenience function for easy access to the text inside an element. Although easy\r
+ and concise, GetText() is limited compared to getting the XMLText child\r
+ and accessing it directly.\r
+\r
+ If the first child of 'this' is a XMLText, the GetText()\r
+ returns the character string of the Text node, else null is returned.\r
+\r
+ This is a convenient method for getting the text of simple contained text:\r
+ @verbatim\r
+ <foo>This is text</foo>\r
+ const char* str = fooElement->GetText();\r
+ @endverbatim\r
+\r
+ 'str' will be a pointer to "This is text".\r
+\r
+ Note that this function can be misleading. If the element foo was created from\r
+ this XML:\r
+ @verbatim\r
+ <foo><b>This is text</b></foo>\r
+ @endverbatim\r
+\r
+ then the value of str would be null. The first child node isn't a text node, it is\r
+ another element. From this XML:\r
+ @verbatim\r
+ <foo>This is <b>text</b></foo>\r
+ @endverbatim\r
+ GetText() will return "This is ".\r
+ */\r
+ const char* GetText() const;\r
+\r
+ /** Convenience function for easy access to the text inside an element. Although easy\r
+ and concise, SetText() is limited compared to creating an XMLText child\r
+ and mutating it directly.\r
+\r
+ If the first child of 'this' is a XMLText, SetText() sets its value to\r
+ the given string, otherwise it will create a first child that is an XMLText.\r
+\r
+ This is a convenient method for setting the text of simple contained text:\r
+ @verbatim\r
+ <foo>This is text</foo>\r
+ fooElement->SetText( "Hullaballoo!" );\r
+ <foo>Hullaballoo!</foo>\r
+ @endverbatim\r
+\r
+ Note that this function can be misleading. If the element foo was created from\r
+ this XML:\r
+ @verbatim\r
+ <foo><b>This is text</b></foo>\r
+ @endverbatim\r
+\r
+ then it will not change "This is text", but rather prefix it with a text element:\r
+ @verbatim\r
+ <foo>Hullaballoo!<b>This is text</b></foo>\r
+ @endverbatim\r
+ \r
+ For this XML:\r
+ @verbatim\r
+ <foo />\r
+ @endverbatim\r
+ SetText() will generate\r
+ @verbatim\r
+ <foo>Hullaballoo!</foo>\r
+ @endverbatim\r
+ */\r
+ void SetText( const char* inText );\r
+ /// Convenience method for setting text inside an element. See SetText() for important limitations.\r
+ void SetText( int value );\r
+ /// Convenience method for setting text inside an element. See SetText() for important limitations.\r
+ void SetText( unsigned value ); \r
+ /// Convenience method for setting text inside an element. See SetText() for important limitations.\r
+ void SetText( bool value ); \r
+ /// Convenience method for setting text inside an element. See SetText() for important limitations.\r
+ void SetText( double value ); \r
+ /// Convenience method for setting text inside an element. See SetText() for important limitations.\r
+ void SetText( float value ); \r
+\r
+ /**\r
+ Convenience method to query the value of a child text node. This is probably best\r
+ shown by example. Given you have a document is this form:\r
+ @verbatim\r
+ <point>\r
+ <x>1</x>\r
+ <y>1.4</y>\r
+ </point>\r
+ @endverbatim\r
+\r
+ The QueryIntText() and similar functions provide a safe and easier way to get to the\r
+ "value" of x and y.\r
+\r
+ @verbatim\r
+ int x = 0;\r
+ float y = 0; // types of x and y are contrived for example\r
+ const XMLElement* xElement = pointElement->FirstChildElement( "x" );\r
+ const XMLElement* yElement = pointElement->FirstChildElement( "y" );\r
+ xElement->QueryIntText( &x );\r
+ yElement->QueryFloatText( &y );\r
+ @endverbatim\r
+\r
+ @returns XML_SUCCESS (0) on success, XML_CAN_NOT_CONVERT_TEXT if the text cannot be converted\r
+ to the requested type, and XML_NO_TEXT_NODE if there is no child text to query.\r
+\r
+ */\r
+ XMLError QueryIntText( int* ival ) const;\r
+ /// See QueryIntText()\r
+ XMLError QueryUnsignedText( unsigned* uval ) const;\r
+ /// See QueryIntText()\r
+ XMLError QueryBoolText( bool* bval ) const;\r
+ /// See QueryIntText()\r
+ XMLError QueryDoubleText( double* dval ) const;\r
+ /// See QueryIntText()\r
+ XMLError QueryFloatText( float* fval ) const;\r
+\r
+ // internal:\r
+ enum {\r
+ OPEN, // <foo>\r
+ CLOSED, // <foo/>\r
+ CLOSING // </foo>\r
+ };\r
+ int ClosingType() const {\r
+ return _closingType;\r
+ }\r
+ virtual XMLNode* ShallowClone( XMLDocument* document ) const;\r
+ virtual bool ShallowEqual( const XMLNode* compare ) const;\r
+\r
+protected:\r
+ char* ParseDeep( char* p, StrPair* endTag );\r
+\r
+private:\r
+ XMLElement( XMLDocument* doc );\r
+ virtual ~XMLElement();\r
+ XMLElement( const XMLElement& ); // not supported\r
+ void operator=( const XMLElement& ); // not supported\r
+\r
+ XMLAttribute* FindAttribute( const char* name ) {\r
+ return const_cast<XMLAttribute*>(const_cast<const XMLElement*>(this)->FindAttribute( name ));\r
+ }\r
+ XMLAttribute* FindOrCreateAttribute( const char* name );\r
+ //void LinkAttribute( XMLAttribute* attrib );\r
+ char* ParseAttributes( char* p );\r
+ static void DeleteAttribute( XMLAttribute* attribute );\r
+\r
+ enum { BUF_SIZE = 200 };\r
+ int _closingType;\r
+ // The attribute list is ordered; there is no 'lastAttribute'\r
+ // because the list needs to be scanned for dupes before adding\r
+ // a new attribute.\r
+ XMLAttribute* _rootAttribute;\r
+};\r
+\r
+\r
+enum Whitespace {\r
+ PRESERVE_WHITESPACE,\r
+ COLLAPSE_WHITESPACE\r
+};\r
+\r
+\r
+/** A Document binds together all the functionality.\r
+ It can be saved, loaded, and printed to the screen.\r
+ All Nodes are connected and allocated to a Document.\r
+ If the Document is deleted, all its Nodes are also deleted.\r
+*/\r
+class TINYXML2_LIB XMLDocument : public XMLNode\r
+{\r
+ friend class XMLElement;\r
+public:\r
+ /// constructor\r
+ XMLDocument( bool processEntities = true, Whitespace = PRESERVE_WHITESPACE );\r
+ ~XMLDocument();\r
+\r
+ virtual XMLDocument* ToDocument() {\r
+ TIXMLASSERT( this == _document );\r
+ return this;\r
+ }\r
+ virtual const XMLDocument* ToDocument() const {\r
+ TIXMLASSERT( this == _document );\r
+ return this;\r
+ }\r
+\r
+ /**\r
+ Parse an XML file from a character string.\r
+ Returns XML_NO_ERROR (0) on success, or\r
+ an errorID.\r
+\r
+ You may optionally pass in the 'nBytes', which is\r
+ the number of bytes which will be parsed. If not\r
+ specified, TinyXML-2 will assume 'xml' points to a\r
+ null terminated string.\r
+ */\r
+ XMLError Parse( const char* xml, size_t nBytes=(size_t)(-1) );\r
+\r
+ /**\r
+ Load an XML file from disk.\r
+ Returns XML_NO_ERROR (0) on success, or\r
+ an errorID.\r
+ */\r
+ XMLError LoadFile( const char* filename );\r
+\r
+ /**\r
+ Load an XML file from disk. You are responsible\r
+ for providing and closing the FILE*. \r
+ \r
+ NOTE: The file should be opened as binary ("rb")\r
+ not text in order for TinyXML-2 to correctly\r
+ do newline normalization.\r
+\r
+ Returns XML_NO_ERROR (0) on success, or\r
+ an errorID.\r
+ */\r
+ XMLError LoadFile( FILE* );\r
+\r
+ /**\r
+ Save the XML file to disk.\r
+ Returns XML_NO_ERROR (0) on success, or\r
+ an errorID.\r
+ */\r
+ XMLError SaveFile( const char* filename, bool compact = false );\r
+\r
+ /**\r
+ Save the XML file to disk. You are responsible\r
+ for providing and closing the FILE*.\r
+\r
+ Returns XML_NO_ERROR (0) on success, or\r
+ an errorID.\r
+ */\r
+ XMLError SaveFile( FILE* fp, bool compact = false );\r
+\r
+ bool ProcessEntities() const {\r
+ return _processEntities;\r
+ }\r
+ Whitespace WhitespaceMode() const {\r
+ return _whitespace;\r
+ }\r
+\r
+ /**\r
+ Returns true if this document has a leading Byte Order Mark of UTF8.\r
+ */\r
+ bool HasBOM() const {\r
+ return _writeBOM;\r
+ }\r
+ /** Sets whether to write the BOM when writing the file.\r
+ */\r
+ void SetBOM( bool useBOM ) {\r
+ _writeBOM = useBOM;\r
+ }\r
+\r
+ /** Return the root element of DOM. Equivalent to FirstChildElement().\r
+ To get the first node, use FirstChild().\r
+ */\r
+ XMLElement* RootElement() {\r
+ return FirstChildElement();\r
+ }\r
+ const XMLElement* RootElement() const {\r
+ return FirstChildElement();\r
+ }\r
+\r
+ /** Print the Document. If the Printer is not provided, it will\r
+ print to stdout. If you provide Printer, this can print to a file:\r
+ @verbatim\r
+ XMLPrinter printer( fp );\r
+ doc.Print( &printer );\r
+ @endverbatim\r
+\r
+ Or you can use a printer to print to memory:\r
+ @verbatim\r
+ XMLPrinter printer;\r
+ doc.Print( &printer );\r
+ // printer.CStr() has a const char* to the XML\r
+ @endverbatim\r
+ */\r
+ void Print( XMLPrinter* streamer=0 ) const;\r
+ virtual bool Accept( XMLVisitor* visitor ) const;\r
+\r
+ /**\r
+ Create a new Element associated with\r
+ this Document. The memory for the Element\r
+ is managed by the Document.\r
+ */\r
+ XMLElement* NewElement( const char* name );\r
+ /**\r
+ Create a new Comment associated with\r
+ this Document. The memory for the Comment\r
+ is managed by the Document.\r
+ */\r
+ XMLComment* NewComment( const char* comment );\r
+ /**\r
+ Create a new Text associated with\r
+ this Document. The memory for the Text\r
+ is managed by the Document.\r
+ */\r
+ XMLText* NewText( const char* text );\r
+ /**\r
+ Create a new Declaration associated with\r
+ this Document. The memory for the object\r
+ is managed by the Document.\r
+\r
+ If the 'text' param is null, the standard\r
+ declaration is used.:\r
+ @verbatim\r
+ <?xml version="1.0" encoding="UTF-8"?>\r
+ @endverbatim\r
+ */\r
+ XMLDeclaration* NewDeclaration( const char* text=0 );\r
+ /**\r
+ Create a new Unknown associated with\r
+ this Document. The memory for the object\r
+ is managed by the Document.\r
+ */\r
+ XMLUnknown* NewUnknown( const char* text );\r
+\r
+ /**\r
+ Delete a node associated with this document.\r
+ It will be unlinked from the DOM.\r
+ */\r
+ void DeleteNode( XMLNode* node );\r
+\r
+ void SetError( XMLError error, const char* str1, const char* str2 );\r
+\r
+ /// Return true if there was an error parsing the document.\r
+ bool Error() const {\r
+ return _errorID != XML_NO_ERROR;\r
+ }\r
+ /// Return the errorID.\r
+ XMLError ErrorID() const {\r
+ return _errorID;\r
+ }\r
+ const char* ErrorName() const;\r
+\r
+ /// Return a possibly helpful diagnostic location or string.\r
+ const char* GetErrorStr1() const {\r
+ return _errorStr1;\r
+ }\r
+ /// Return a possibly helpful secondary diagnostic location or string.\r
+ const char* GetErrorStr2() const {\r
+ return _errorStr2;\r
+ }\r
+ /// If there is an error, print it to stdout.\r
+ void PrintError() const;\r
+ \r
+ /// Clear the document, resetting it to the initial state.\r
+ void Clear();\r
+\r
+ // internal\r
+ char* Identify( char* p, XMLNode** node );\r
+\r
+ virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const {\r
+ return 0;\r
+ }\r
+ virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const {\r
+ return false;\r
+ }\r
+\r
+private:\r
+ XMLDocument( const XMLDocument& ); // not supported\r
+ void operator=( const XMLDocument& ); // not supported\r
+\r
+ bool _writeBOM;\r
+ bool _processEntities;\r
+ XMLError _errorID;\r
+ Whitespace _whitespace;\r
+ const char* _errorStr1;\r
+ const char* _errorStr2;\r
+ char* _charBuffer;\r
+\r
+ MemPoolT< sizeof(XMLElement) > _elementPool;\r
+ MemPoolT< sizeof(XMLAttribute) > _attributePool;\r
+ MemPoolT< sizeof(XMLText) > _textPool;\r
+ MemPoolT< sizeof(XMLComment) > _commentPool;\r
+\r
+ static const char* _errorNames[XML_ERROR_COUNT];\r
+\r
+ void Parse();\r
+};\r
+\r
+\r
+/**\r
+ A XMLHandle is a class that wraps a node pointer with null checks; this is\r
+ an incredibly useful thing. Note that XMLHandle is not part of the TinyXML-2\r
+ DOM structure. It is a separate utility class.\r
+\r
+ Take an example:\r
+ @verbatim\r
+ <Document>\r
+ <Element attributeA = "valueA">\r
+ <Child attributeB = "value1" />\r
+ <Child attributeB = "value2" />\r
+ </Element>\r
+ </Document>\r
+ @endverbatim\r
+\r
+ Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very\r
+ easy to write a *lot* of code that looks like:\r
+\r
+ @verbatim\r
+ XMLElement* root = document.FirstChildElement( "Document" );\r
+ if ( root )\r
+ {\r
+ XMLElement* element = root->FirstChildElement( "Element" );\r
+ if ( element )\r
+ {\r
+ XMLElement* child = element->FirstChildElement( "Child" );\r
+ if ( child )\r
+ {\r
+ XMLElement* child2 = child->NextSiblingElement( "Child" );\r
+ if ( child2 )\r
+ {\r
+ // Finally do something useful.\r
+ @endverbatim\r
+\r
+ And that doesn't even cover "else" cases. XMLHandle addresses the verbosity\r
+ of such code. A XMLHandle checks for null pointers so it is perfectly safe\r
+ and correct to use:\r
+\r
+ @verbatim\r
+ XMLHandle docHandle( &document );\r
+ XMLElement* child2 = docHandle.FirstChildElement( "Document" ).FirstChildElement( "Element" ).FirstChildElement().NextSiblingElement();\r
+ if ( child2 )\r
+ {\r
+ // do something useful\r
+ @endverbatim\r
+\r
+ Which is MUCH more concise and useful.\r
+\r
+ It is also safe to copy handles - internally they are nothing more than node pointers.\r
+ @verbatim\r
+ XMLHandle handleCopy = handle;\r
+ @endverbatim\r
+\r
+ See also XMLConstHandle, which is the same as XMLHandle, but operates on const objects.\r
+*/\r
+class TINYXML2_LIB XMLHandle\r
+{\r
+public:\r
+ /// Create a handle from any node (at any depth of the tree.) This can be a null pointer.\r
+ XMLHandle( XMLNode* node ) {\r
+ _node = node;\r
+ }\r
+ /// Create a handle from a node.\r
+ XMLHandle( XMLNode& node ) {\r
+ _node = &node;\r
+ }\r
+ /// Copy constructor\r
+ XMLHandle( const XMLHandle& ref ) {\r
+ _node = ref._node;\r
+ }\r
+ /// Assignment\r
+ XMLHandle& operator=( const XMLHandle& ref ) {\r
+ _node = ref._node;\r
+ return *this;\r
+ }\r
+\r
+ /// Get the first child of this handle.\r
+ XMLHandle FirstChild() {\r
+ return XMLHandle( _node ? _node->FirstChild() : 0 );\r
+ }\r
+ /// Get the first child element of this handle.\r
+ XMLHandle FirstChildElement( const char* name = 0 ) {\r
+ return XMLHandle( _node ? _node->FirstChildElement( name ) : 0 );\r
+ }\r
+ /// Get the last child of this handle.\r
+ XMLHandle LastChild() {\r
+ return XMLHandle( _node ? _node->LastChild() : 0 );\r
+ }\r
+ /// Get the last child element of this handle.\r
+ XMLHandle LastChildElement( const char* name = 0 ) {\r
+ return XMLHandle( _node ? _node->LastChildElement( name ) : 0 );\r
+ }\r
+ /// Get the previous sibling of this handle.\r
+ XMLHandle PreviousSibling() {\r
+ return XMLHandle( _node ? _node->PreviousSibling() : 0 );\r
+ }\r
+ /// Get the previous sibling element of this handle.\r
+ XMLHandle PreviousSiblingElement( const char* name = 0 ) {\r
+ return XMLHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );\r
+ }\r
+ /// Get the next sibling of this handle.\r
+ XMLHandle NextSibling() {\r
+ return XMLHandle( _node ? _node->NextSibling() : 0 );\r
+ }\r
+ /// Get the next sibling element of this handle.\r
+ XMLHandle NextSiblingElement( const char* name = 0 ) {\r
+ return XMLHandle( _node ? _node->NextSiblingElement( name ) : 0 );\r
+ }\r
+\r
+ /// Safe cast to XMLNode. This can return null.\r
+ XMLNode* ToNode() {\r
+ return _node;\r
+ }\r
+ /// Safe cast to XMLElement. This can return null.\r
+ XMLElement* ToElement() {\r
+ return ( ( _node == 0 ) ? 0 : _node->ToElement() );\r
+ }\r
+ /// Safe cast to XMLText. This can return null.\r
+ XMLText* ToText() {\r
+ return ( ( _node == 0 ) ? 0 : _node->ToText() );\r
+ }\r
+ /// Safe cast to XMLUnknown. This can return null.\r
+ XMLUnknown* ToUnknown() {\r
+ return ( ( _node == 0 ) ? 0 : _node->ToUnknown() );\r
+ }\r
+ /// Safe cast to XMLDeclaration. This can return null.\r
+ XMLDeclaration* ToDeclaration() {\r
+ return ( ( _node == 0 ) ? 0 : _node->ToDeclaration() );\r
+ }\r
+\r
+private:\r
+ XMLNode* _node;\r
+};\r
+\r
+\r
+/**\r
+ A variant of the XMLHandle class for working with const XMLNodes and Documents. It is the\r
+ same in all regards, except for the 'const' qualifiers. See XMLHandle for API.\r
+*/\r
+class TINYXML2_LIB XMLConstHandle\r
+{\r
+public:\r
+ XMLConstHandle( const XMLNode* node ) {\r
+ _node = node;\r
+ }\r
+ XMLConstHandle( const XMLNode& node ) {\r
+ _node = &node;\r
+ }\r
+ XMLConstHandle( const XMLConstHandle& ref ) {\r
+ _node = ref._node;\r
+ }\r
+\r
+ XMLConstHandle& operator=( const XMLConstHandle& ref ) {\r
+ _node = ref._node;\r
+ return *this;\r
+ }\r
+\r
+ const XMLConstHandle FirstChild() const {\r
+ return XMLConstHandle( _node ? _node->FirstChild() : 0 );\r
+ }\r
+ const XMLConstHandle FirstChildElement( const char* name = 0 ) const {\r
+ return XMLConstHandle( _node ? _node->FirstChildElement( name ) : 0 );\r
+ }\r
+ const XMLConstHandle LastChild() const {\r
+ return XMLConstHandle( _node ? _node->LastChild() : 0 );\r
+ }\r
+ const XMLConstHandle LastChildElement( const char* name = 0 ) const {\r
+ return XMLConstHandle( _node ? _node->LastChildElement( name ) : 0 );\r
+ }\r
+ const XMLConstHandle PreviousSibling() const {\r
+ return XMLConstHandle( _node ? _node->PreviousSibling() : 0 );\r
+ }\r
+ const XMLConstHandle PreviousSiblingElement( const char* name = 0 ) const {\r
+ return XMLConstHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );\r
+ }\r
+ const XMLConstHandle NextSibling() const {\r
+ return XMLConstHandle( _node ? _node->NextSibling() : 0 );\r
+ }\r
+ const XMLConstHandle NextSiblingElement( const char* name = 0 ) const {\r
+ return XMLConstHandle( _node ? _node->NextSiblingElement( name ) : 0 );\r
+ }\r
+\r
+\r
+ const XMLNode* ToNode() const {\r
+ return _node;\r
+ }\r
+ const XMLElement* ToElement() const {\r
+ return ( ( _node == 0 ) ? 0 : _node->ToElement() );\r
+ }\r
+ const XMLText* ToText() const {\r
+ return ( ( _node == 0 ) ? 0 : _node->ToText() );\r
+ }\r
+ const XMLUnknown* ToUnknown() const {\r
+ return ( ( _node == 0 ) ? 0 : _node->ToUnknown() );\r
+ }\r
+ const XMLDeclaration* ToDeclaration() const {\r
+ return ( ( _node == 0 ) ? 0 : _node->ToDeclaration() );\r
+ }\r
+\r
+private:\r
+ const XMLNode* _node;\r
+};\r
+\r
+\r
+/**\r
+ Printing functionality. The XMLPrinter gives you more\r
+ options than the XMLDocument::Print() method.\r
+\r
+ It can:\r
+ -# Print to memory.\r
+ -# Print to a file you provide.\r
+ -# Print XML without a XMLDocument.\r
+\r
+ Print to Memory\r
+\r
+ @verbatim\r
+ XMLPrinter printer;\r
+ doc.Print( &printer );\r
+ SomeFunction( printer.CStr() );\r
+ @endverbatim\r
+\r
+ Print to a File\r
+\r
+ You provide the file pointer.\r
+ @verbatim\r
+ XMLPrinter printer( fp );\r
+ doc.Print( &printer );\r
+ @endverbatim\r
+\r
+ Print without a XMLDocument\r
+\r
+ When loading, an XML parser is very useful. However, sometimes\r
+ when saving, it just gets in the way. The code is often set up\r
+ for streaming, and constructing the DOM is just overhead.\r
+\r
+ The Printer supports the streaming case. The following code\r
+ prints out a trivially simple XML file without ever creating\r
+ an XML document.\r
+\r
+ @verbatim\r
+ XMLPrinter printer( fp );\r
+ printer.OpenElement( "foo" );\r
+ printer.PushAttribute( "foo", "bar" );\r
+ printer.CloseElement();\r
+ @endverbatim\r
+*/\r
+class TINYXML2_LIB XMLPrinter : public XMLVisitor\r
+{\r
+public:\r
+ /** Construct the printer. If the FILE* is specified,\r
+ this will print to the FILE. Else it will print\r
+ to memory, and the result is available in CStr().\r
+ If 'compact' is set to true, then output is created\r
+ with only required whitespace and newlines.\r
+ */\r
+ XMLPrinter( FILE* file=0, bool compact = false, int depth = 0 );\r
+ virtual ~XMLPrinter() {}\r
+\r
+ /** If streaming, write the BOM and declaration. */\r
+ void PushHeader( bool writeBOM, bool writeDeclaration );\r
+ /** If streaming, start writing an element.\r
+ The element must be closed with CloseElement()\r
+ */\r
+ void OpenElement( const char* name, bool compactMode=false );\r
+ /// If streaming, add an attribute to an open element.\r
+ void PushAttribute( const char* name, const char* value );\r
+ void PushAttribute( const char* name, int value );\r
+ void PushAttribute( const char* name, unsigned value );\r
+ void PushAttribute( const char* name, bool value );\r
+ void PushAttribute( const char* name, double value );\r
+ /// If streaming, close the Element.\r
+ virtual void CloseElement( bool compactMode=false );\r
+\r
+ /// Add a text node.\r
+ void PushText( const char* text, bool cdata=false );\r
+ /// Add a text node from an integer.\r
+ void PushText( int value );\r
+ /// Add a text node from an unsigned.\r
+ void PushText( unsigned value );\r
+ /// Add a text node from a bool.\r
+ void PushText( bool value );\r
+ /// Add a text node from a float.\r
+ void PushText( float value );\r
+ /// Add a text node from a double.\r
+ void PushText( double value );\r
+\r
+ /// Add a comment\r
+ void PushComment( const char* comment );\r
+\r
+ void PushDeclaration( const char* value );\r
+ void PushUnknown( const char* value );\r
+\r
+ virtual bool VisitEnter( const XMLDocument& /*doc*/ );\r
+ virtual bool VisitExit( const XMLDocument& /*doc*/ ) {\r
+ return true;\r
+ }\r
+\r
+ virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );\r
+ virtual bool VisitExit( const XMLElement& element );\r
+\r
+ virtual bool Visit( const XMLText& text );\r
+ virtual bool Visit( const XMLComment& comment );\r
+ virtual bool Visit( const XMLDeclaration& declaration );\r
+ virtual bool Visit( const XMLUnknown& unknown );\r
+\r
+ /**\r
+ If in print to memory mode, return a pointer to\r
+ the XML file in memory.\r
+ */\r
+ const char* CStr() const {\r
+ return _buffer.Mem();\r
+ }\r
+ /**\r
+ If in print to memory mode, return the size\r
+ of the XML file in memory. (Note the size returned\r
+ includes the terminating null.)\r
+ */\r
+ int CStrSize() const {\r
+ return _buffer.Size();\r
+ }\r
+ /**\r
+ If in print to memory mode, reset the buffer to the\r
+ beginning.\r
+ */\r
+ void ClearBuffer() {\r
+ _buffer.Clear();\r
+ _buffer.Push(0);\r
+ }\r
+\r
+protected:\r
+ virtual bool CompactMode( const XMLElement& ) { return _compactMode; }\r
+\r
+ /** Prints out the space before an element. You may override to change\r
+ the space and tabs used. A PrintSpace() override should call Print().\r
+ */\r
+ virtual void PrintSpace( int depth );\r
+ void Print( const char* format, ... );\r
+\r
+ void SealElementIfJustOpened();\r
+ bool _elementJustOpened;\r
+ DynArray< const char*, 10 > _stack;\r
+\r
+private:\r
+ void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities.\r
+\r
+ bool _firstElement;\r
+ FILE* _fp;\r
+ int _depth;\r
+ int _textDepth;\r
+ bool _processEntities;\r
+ bool _compactMode;\r
+\r
+ enum {\r
+ ENTITY_RANGE = 64,\r
+ BUF_SIZE = 200\r
+ };\r
+ bool _entityFlag[ENTITY_RANGE];\r
+ bool _restrictedEntityFlag[ENTITY_RANGE];\r
+\r
+ DynArray< char, 20 > _buffer;\r
+};\r
+\r
+\r
+} // tinyxml2\r
+\r
+#if defined(_MSC_VER)\r
+# pragma warning(pop)\r
+#endif\r
+\r
+#endif // TINYXML2_INCLUDED\r
fadeIntensity = 250;
initEverything();
+ if(!currentWorld){
+ std::cout<<"asscock"<<std::endl;
+ system("systemctl poweroff");
+ abort();
+ }
+
/*
* Load sprites used in the inventory menu. See src/inventory.cpp
*/
#include <cstdio>
#include <chrono>
+#ifndef __WIN32__
+#include <sys/types.h>
+#include <dirent.h>
+#include <errno.h>
+#include <vector>
+#endif // __WIN32__
+
#ifndef __WIN32__
unsigned int millis(void){
if(a<0)a=0;
glColor4ub(r,g,b,a);
}
+
+int getdir(const char *dir, std::vector<std::string> &files){
+ DIR *dp;
+ struct dirent *dirp;
+ if(!(dp = opendir(dir))){
+ std::cout <<"Error ("<<errno<<") opening "<<dir<<std::endl;
+ return errno;
+ }
+ while((dirp = readdir(dp)))
+ files.push_back(std::string(dirp->d_name));
+ closedir(dp);
+ return 0;
+}
#include <world.h>
#include <ui.h>
-extern World *currentWorld;
-extern Player *player;
-
-/*
- * int (npc*)
- *
- * dialog
- * wait...
- *
- * switch optchosen
- *
- * qh.assign
- * addAIFunc?
- *
- * return 1 = repeat
- */
-
-void story(Mob *callee){
- player->vel.x = 0;
- Mix_FadeOutMusic(0);
- ui::importantText("It was a dark and stormy night...");
- ui::waitForDialog();
- callee->alive = false;
-}
-
-/*
- * Gens
- */
-
-float gen_worldSpawnHill1(float x){
- return (float)(pow(2,(-x+200)/5) + GEN_MIN);
-}
-
-float gen_worldSpawnHill3(float x){
- float tmp = 60*atan(-(x/30-20))+GEN_MIN*2;
- return tmp>GEN_MIN?tmp:GEN_MIN;
-}
-
-/*
- * Thing-thangs
- */
-
-void worldSpawnHill1_hillBlock(Mob *callee){
- player->vel.x = 0;
- player->loc.x = callee->loc.x + callee->width;
- ui::dialogBox(player->name,NULL,false,"This hill seems to steep to climb up...");
- callee->alive = true;
-}
-
-static Arena *a;
-void worldSpawnHill2_infoSprint(Mob *callee){
-
- ui::dialogBox(player->name,":Sure:Nah",false,"This page would like to take you somewhere.");
- ui::waitForDialog();
- switch(ui::dialogOptChosen){
- case 1:
- ui::dialogBox(player->name,NULL,true,"Cool.");
- callee->alive = false;
- a = new Arena(currentWorld,player);
- a->setBackground(BG_FOREST);
- a->setBGM("assets/music/embark.wav");
- ui::toggleWhiteFast();
- ui::waitForCover();
- currentWorld = a;
- ui::toggleWhiteFast();
- break;
- case 2:
- default:
- ui::dialogBox(player->name,NULL,false,"Okay then.");
- break;
- }
-
- //ui::dialogBox("B-) ",NULL,true,"Press \'Shift\' to run!");
-}
+#include <tinyxml2.h>
-int worldSpawnHill2_Quest2(NPC *callee){
- ui::dialogBox(callee->name,NULL,false,"Yo.");
- ui::waitForDialog();
- return 0;
-}
+using namespace tinyxml2;
-int worldSpawnHill2_Quest1(NPC *callee){
- ui::dialogBox(callee->name,":Cool.",false,"Did you know that I\'m the coolest NPC in the world?");
- ui::waitForDialog();
- if(ui::dialogOptChosen == 1){
- ui::dialogBox(callee->name,NULL,false,"Yeah, it is.");
- currentWorld->getAvailableNPC()->addAIFunc(worldSpawnHill2_Quest2,true);
- ui::waitForDialog();
- return 0;
- }
- return 1;
-}
+extern World *currentWorld;
+extern Player *player;
/*
* new world
* World definitions
*/
-static World *worldSpawnHill1;
-static World *worldSpawnHill2;
-static World *worldSpawnHill3;
-
-static IndoorWorld *worldSpawnHill2_Building1;
-
-static World *worldFirstVillage;
/*
* initEverything() start
*/
+std::vector<World *> earth;
+
void destroyEverything(void);
void initEverything(void){
- //static std::ifstream i ("world.dat",std::ifstream::in | std::ifstream::binary);
-
- worldSpawnHill1 = new World();
- worldSpawnHill1->setBackground(BG_FOREST);
- /*if(!i.fail()){
- worldSpawnHill1->load(&i);
- i.close();
- }else{*/
- worldSpawnHill1->generateFunc(400,gen_worldSpawnHill1);
- worldSpawnHill1->setBGM("assets/music/embark.wav");
- //}
- worldSpawnHill1->addMob(MS_TRIGGER,0,0,worldSpawnHill1_hillBlock);
- worldSpawnHill1->addNPC(300,100);
-
- worldSpawnHill2 = new World();
- worldSpawnHill2->setBackground(BG_FOREST);
- worldSpawnHill2->setBGM("assets/music/ozone.wav");
- worldSpawnHill2->generate(700);
- worldSpawnHill2->addMob(MS_PAGE,-400,0,worldSpawnHill2_infoSprint);
-
- worldSpawnHill3 = new World();
- worldSpawnHill3->generateFunc(1000,gen_worldSpawnHill3);
- worldSpawnHill3->setBackground(BG_FOREST);
- worldSpawnHill3->setBGM("assets/music/ozone.wav");
-
- worldFirstVillage = new World();
- worldFirstVillage->setBackground(BG_FOREST);
- worldFirstVillage->setBGM("assets/music/embark.wav");
- worldFirstVillage->generate(1000);
-
- worldSpawnHill1->toRight = worldSpawnHill2;
- worldSpawnHill2->toLeft = worldSpawnHill1;
- worldSpawnHill2->toRight = worldSpawnHill3;
- worldSpawnHill3->toLeft = worldSpawnHill2;
- worldSpawnHill3->toRight = worldFirstVillage;
- worldFirstVillage->toLeft = worldSpawnHill3;
-
- /*
- * Spawn some entities.
- */
-
- //playerSpawnHill->addMob(MS_TRIGGER,player->loc.x,0,story);
-
- //playerSpawnHill->addStructure(STRUCTURET,FOUNTAIN,(rand()%120*HLINE)+100*HLINE,100,test,iw);
- //playerSpawnHill->addStructure(STRUCTURET,HOUSE2,(rand()%120*HLINE)+300*HLINE,100,test,iw);
-
- //playerSpawnHill->addVillage(5,1,4,STRUCTURET,rand()%500+120,(float)200,playerSpawnHill,iw);
- //playerSpawnHill->addMob(MS_TRIGGER,-1300,0,CUTSCENEEE);*/
-
-
- worldSpawnHill2_Building1 = new IndoorWorld();
- worldSpawnHill2_Building1->generate(300);
- worldSpawnHill2_Building1->setBackground(BG_WOODHOUSE);
- worldSpawnHill2_Building1->setBGM("assets/music/theme_jazz.wav");
-
- worldSpawnHill2->addStructure(STRUCTURET,HOUSE,(rand()%120*HLINE),100,worldSpawnHill2_Building1);
- worldSpawnHill2->addLight({300,100},{1.0f,1.0f,1.0f});
- worldSpawnHill2->getAvailableNPC()->addAIFunc(worldSpawnHill2_Quest1,false);
-
- worldFirstVillage->addVillage(5,0,0,STRUCTURET,worldSpawnHill2_Building1);
+ const char *gentype;
+ std::vector<std::string> xmlFiles;
+ char *file;
+ XMLDocument xml;
+ if(getdir("./xml/",xmlFiles)){
+ std::cout<<"Error reading XML files!!!1"<<std::endl;
+ abort();
+ }
+ for(auto x : xmlFiles){
+ if(strncmp(x.c_str(),".",1) && strncmp(x.c_str(),"..",2)){
+ file = new char[4 + x.size()];
+ strncpy(file,"xml/",4);
+ strcpy(file+4,x.c_str());
+ xml.LoadFile(file);
+ delete[] file;
+
+ earth.push_back(new World());
+ earth.back()->setBackground((WORLD_BG_TYPE)atoi(xml.FirstChildElement("World")->FirstChildElement("Background")->GetText()));
+ earth.back()->setBGM(xml.FirstChildElement("World")->FirstChildElement("BGM")->GetText());
+ gentype = xml.FirstChildElement("World")->FirstChildElement("GenerationType")->GetText();
+ if(!strcmp(gentype,"Random")){
+ std::cout<<"rand\n";
+ earth.back()->generate(atoi(xml.FirstChildElement("World")->FirstChildElement("GenerationWidth")->GetText()));
+ }else{
+ abort();
+ }
+ }
+ }
- //worldSpawnHill2->addStructure(STRUCTURET,HOUSE,(rand()%120*HLINE),100,worldSpawnHill1,worldSpawnHill2);
player = new Player();
player->spawn(200,100);
- currentWorld = worldSpawnHill1;
+ currentWorld = earth.front();
currentWorld->bgmPlay(NULL);
atexit(destroyEverything);
}
extern std::vector<NPC *> AIpreaddr;
void destroyEverything(void){
- /*static std::ofstream o;
- o.open("world.dat",std::ifstream::binary);
- worldSpawnHill2->save(&o);
- o.close();*/
while(!AIpreload.empty())
AIpreload.pop_back();
--- /dev/null
+/*\r
+Original code by Lee Thomason (www.grinninglizard.com)\r
+\r
+This software is provided 'as-is', without any express or implied\r
+warranty. In no event will the authors be held liable for any\r
+damages arising from the use of this software.\r
+\r
+Permission is granted to anyone to use this software for any\r
+purpose, including commercial applications, and to alter it and\r
+redistribute it freely, subject to the following restrictions:\r
+\r
+1. The origin of this software must not be misrepresented; you must\r
+not claim that you wrote the original software. If you use this\r
+software in a product, an acknowledgment in the product documentation\r
+would be appreciated but is not required.\r
+\r
+2. Altered source versions must be plainly marked as such, and\r
+must not be misrepresented as being the original software.\r
+\r
+3. This notice may not be removed or altered from any source\r
+distribution.\r
+*/\r
+\r
+#include "tinyxml2.h"\r
+\r
+#include <new> // yes, this one new style header, is in the Android SDK.\r
+#if defined(ANDROID_NDK) || defined(__QNXNTO__)\r
+# include <stddef.h>\r
+# include <stdarg.h>\r
+#else\r
+# include <cstddef>\r
+# include <cstdarg>\r
+#endif\r
+\r
+#if defined(_MSC_VER) && (_MSC_VER >= 1400 ) && (!defined WINCE)\r
+ // Microsoft Visual Studio, version 2005 and higher. Not WinCE.\r
+ /*int _snprintf_s(\r
+ char *buffer,\r
+ size_t sizeOfBuffer,\r
+ size_t count,\r
+ const char *format [,\r
+ argument] ...\r
+ );*/\r
+ static inline int TIXML_SNPRINTF( char* buffer, size_t size, const char* format, ... )\r
+ {\r
+ va_list va;\r
+ va_start( va, format );\r
+ int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va );\r
+ va_end( va );\r
+ return result;\r
+ }\r
+\r
+ static inline int TIXML_VSNPRINTF( char* buffer, size_t size, const char* format, va_list va )\r
+ {\r
+ int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va );\r
+ return result;\r
+ }\r
+\r
+ #define TIXML_VSCPRINTF _vscprintf\r
+ #define TIXML_SSCANF sscanf_s\r
+#elif defined _MSC_VER\r
+ // Microsoft Visual Studio 2003 and earlier or WinCE\r
+ #define TIXML_SNPRINTF _snprintf\r
+ #define TIXML_VSNPRINTF _vsnprintf\r
+ #define TIXML_SSCANF sscanf\r
+ #if (_MSC_VER < 1400 ) && (!defined WINCE)\r
+ // Microsoft Visual Studio 2003 and not WinCE.\r
+ #define TIXML_VSCPRINTF _vscprintf // VS2003's C runtime has this, but VC6 C runtime or WinCE SDK doesn't have.\r
+ #else\r
+ // Microsoft Visual Studio 2003 and earlier or WinCE.\r
+ static inline int TIXML_VSCPRINTF( const char* format, va_list va )\r
+ {\r
+ int len = 512;\r
+ for (;;) {\r
+ len = len*2;\r
+ char* str = new char[len]();\r
+ const int required = _vsnprintf(str, len, format, va);\r
+ delete[] str;\r
+ if ( required != -1 ) {\r
+ TIXMLASSERT( required >= 0 );\r
+ len = required;\r
+ break;\r
+ }\r
+ }\r
+ TIXMLASSERT( len >= 0 );\r
+ return len;\r
+ }\r
+ #endif\r
+#else\r
+ // GCC version 3 and higher\r
+ //#warning( "Using sn* functions." )\r
+ #define TIXML_SNPRINTF snprintf\r
+ #define TIXML_VSNPRINTF vsnprintf\r
+ static inline int TIXML_VSCPRINTF( const char* format, va_list va )\r
+ {\r
+ int len = vsnprintf( 0, 0, format, va );\r
+ TIXMLASSERT( len >= 0 );\r
+ return len;\r
+ }\r
+ #define TIXML_SSCANF sscanf\r
+#endif\r
+\r
+\r
+static const char LINE_FEED = (char)0x0a; // all line endings are normalized to LF\r
+static const char LF = LINE_FEED;\r
+static const char CARRIAGE_RETURN = (char)0x0d; // CR gets filtered out\r
+static const char CR = CARRIAGE_RETURN;\r
+static const char SINGLE_QUOTE = '\'';\r
+static const char DOUBLE_QUOTE = '\"';\r
+\r
+// Bunch of unicode info at:\r
+// http://www.unicode.org/faq/utf_bom.html\r
+// ef bb bf (Microsoft "lead bytes") - designates UTF-8\r
+\r
+static const unsigned char TIXML_UTF_LEAD_0 = 0xefU;\r
+static const unsigned char TIXML_UTF_LEAD_1 = 0xbbU;\r
+static const unsigned char TIXML_UTF_LEAD_2 = 0xbfU;\r
+\r
+namespace tinyxml2\r
+{\r
+\r
+struct Entity {\r
+ const char* pattern;\r
+ int length;\r
+ char value;\r
+};\r
+\r
+static const int NUM_ENTITIES = 5;\r
+static const Entity entities[NUM_ENTITIES] = {\r
+ { "quot", 4, DOUBLE_QUOTE },\r
+ { "amp", 3, '&' },\r
+ { "apos", 4, SINGLE_QUOTE },\r
+ { "lt", 2, '<' },\r
+ { "gt", 2, '>' }\r
+};\r
+\r
+\r
+StrPair::~StrPair()\r
+{\r
+ Reset();\r
+}\r
+\r
+\r
+void StrPair::TransferTo( StrPair* other )\r
+{\r
+ if ( this == other ) {\r
+ return;\r
+ }\r
+ // This in effect implements the assignment operator by "moving"\r
+ // ownership (as in auto_ptr).\r
+\r
+ TIXMLASSERT( other->_flags == 0 );\r
+ TIXMLASSERT( other->_start == 0 );\r
+ TIXMLASSERT( other->_end == 0 );\r
+\r
+ other->Reset();\r
+\r
+ other->_flags = _flags;\r
+ other->_start = _start;\r
+ other->_end = _end;\r
+\r
+ _flags = 0;\r
+ _start = 0;\r
+ _end = 0;\r
+}\r
+\r
+void StrPair::Reset()\r
+{\r
+ if ( _flags & NEEDS_DELETE ) {\r
+ delete [] _start;\r
+ }\r
+ _flags = 0;\r
+ _start = 0;\r
+ _end = 0;\r
+}\r
+\r
+\r
+void StrPair::SetStr( const char* str, int flags )\r
+{\r
+ TIXMLASSERT( str );\r
+ Reset();\r
+ size_t len = strlen( str );\r
+ TIXMLASSERT( _start == 0 );\r
+ _start = new char[ len+1 ];\r
+ memcpy( _start, str, len+1 );\r
+ _end = _start + len;\r
+ _flags = flags | NEEDS_DELETE;\r
+}\r
+\r
+\r
+char* StrPair::ParseText( char* p, const char* endTag, int strFlags )\r
+{\r
+ TIXMLASSERT( endTag && *endTag );\r
+\r
+ char* start = p;\r
+ char endChar = *endTag;\r
+ size_t length = strlen( endTag );\r
+\r
+ // Inner loop of text parsing.\r
+ while ( *p ) {\r
+ if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {\r
+ Set( start, p, strFlags );\r
+ return p + length;\r
+ }\r
+ ++p;\r
+ }\r
+ return 0;\r
+}\r
+\r
+\r
+char* StrPair::ParseName( char* p )\r
+{\r
+ if ( !p || !(*p) ) {\r
+ return 0;\r
+ }\r
+ if ( !XMLUtil::IsNameStartChar( *p ) ) {\r
+ return 0;\r
+ }\r
+\r
+ char* const start = p;\r
+ ++p;\r
+ while ( *p && XMLUtil::IsNameChar( *p ) ) {\r
+ ++p;\r
+ }\r
+\r
+ Set( start, p, 0 );\r
+ return p;\r
+}\r
+\r
+\r
+void StrPair::CollapseWhitespace()\r
+{\r
+ // Adjusting _start would cause undefined behavior on delete[]\r
+ TIXMLASSERT( ( _flags & NEEDS_DELETE ) == 0 );\r
+ // Trim leading space.\r
+ _start = XMLUtil::SkipWhiteSpace( _start );\r
+\r
+ if ( *_start ) {\r
+ char* p = _start; // the read pointer\r
+ char* q = _start; // the write pointer\r
+\r
+ while( *p ) {\r
+ if ( XMLUtil::IsWhiteSpace( *p )) {\r
+ p = XMLUtil::SkipWhiteSpace( p );\r
+ if ( *p == 0 ) {\r
+ break; // don't write to q; this trims the trailing space.\r
+ }\r
+ *q = ' ';\r
+ ++q;\r
+ }\r
+ *q = *p;\r
+ ++q;\r
+ ++p;\r
+ }\r
+ *q = 0;\r
+ }\r
+}\r
+\r
+\r
+const char* StrPair::GetStr()\r
+{\r
+ TIXMLASSERT( _start );\r
+ TIXMLASSERT( _end );\r
+ if ( _flags & NEEDS_FLUSH ) {\r
+ *_end = 0;\r
+ _flags ^= NEEDS_FLUSH;\r
+\r
+ if ( _flags ) {\r
+ char* p = _start; // the read pointer\r
+ char* q = _start; // the write pointer\r
+\r
+ while( p < _end ) {\r
+ if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == CR ) {\r
+ // CR-LF pair becomes LF\r
+ // CR alone becomes LF\r
+ // LF-CR becomes LF\r
+ if ( *(p+1) == LF ) {\r
+ p += 2;\r
+ }\r
+ else {\r
+ ++p;\r
+ }\r
+ *q++ = LF;\r
+ }\r
+ else if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == LF ) {\r
+ if ( *(p+1) == CR ) {\r
+ p += 2;\r
+ }\r
+ else {\r
+ ++p;\r
+ }\r
+ *q++ = LF;\r
+ }\r
+ else if ( (_flags & NEEDS_ENTITY_PROCESSING) && *p == '&' ) {\r
+ // Entities handled by tinyXML2:\r
+ // - special entities in the entity table [in/out]\r
+ // - numeric character reference [in]\r
+ // 中 or 中\r
+\r
+ if ( *(p+1) == '#' ) {\r
+ const int buflen = 10;\r
+ char buf[buflen] = { 0 };\r
+ int len = 0;\r
+ char* adjusted = const_cast<char*>( XMLUtil::GetCharacterRef( p, buf, &len ) );\r
+ if ( adjusted == 0 ) {\r
+ *q = *p;\r
+ ++p;\r
+ ++q;\r
+ }\r
+ else {\r
+ TIXMLASSERT( 0 <= len && len <= buflen );\r
+ TIXMLASSERT( q + len <= adjusted );\r
+ p = adjusted;\r
+ memcpy( q, buf, len );\r
+ q += len;\r
+ }\r
+ }\r
+ else {\r
+ bool entityFound = false;\r
+ for( int i = 0; i < NUM_ENTITIES; ++i ) {\r
+ const Entity& entity = entities[i];\r
+ if ( strncmp( p + 1, entity.pattern, entity.length ) == 0\r
+ && *( p + entity.length + 1 ) == ';' ) {\r
+ // Found an entity - convert.\r
+ *q = entity.value;\r
+ ++q;\r
+ p += entity.length + 2;\r
+ entityFound = true;\r
+ break;\r
+ }\r
+ }\r
+ if ( !entityFound ) {\r
+ // fixme: treat as error?\r
+ ++p;\r
+ ++q;\r
+ }\r
+ }\r
+ }\r
+ else {\r
+ *q = *p;\r
+ ++p;\r
+ ++q;\r
+ }\r
+ }\r
+ *q = 0;\r
+ }\r
+ // The loop below has plenty going on, and this\r
+ // is a less useful mode. Break it out.\r
+ if ( _flags & NEEDS_WHITESPACE_COLLAPSING ) {\r
+ CollapseWhitespace();\r
+ }\r
+ _flags = (_flags & NEEDS_DELETE);\r
+ }\r
+ TIXMLASSERT( _start );\r
+ return _start;\r
+}\r
+\r
+\r
+\r
+\r
+// --------- XMLUtil ----------- //\r
+\r
+const char* XMLUtil::ReadBOM( const char* p, bool* bom )\r
+{\r
+ TIXMLASSERT( p );\r
+ TIXMLASSERT( bom );\r
+ *bom = false;\r
+ const unsigned char* pu = reinterpret_cast<const unsigned char*>(p);\r
+ // Check for BOM:\r
+ if ( *(pu+0) == TIXML_UTF_LEAD_0\r
+ && *(pu+1) == TIXML_UTF_LEAD_1\r
+ && *(pu+2) == TIXML_UTF_LEAD_2 ) {\r
+ *bom = true;\r
+ p += 3;\r
+ }\r
+ TIXMLASSERT( p );\r
+ return p;\r
+}\r
+\r
+\r
+void XMLUtil::ConvertUTF32ToUTF8( unsigned long input, char* output, int* length )\r
+{\r
+ const unsigned long BYTE_MASK = 0xBF;\r
+ const unsigned long BYTE_MARK = 0x80;\r
+ const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };\r
+\r
+ if (input < 0x80) {\r
+ *length = 1;\r
+ }\r
+ else if ( input < 0x800 ) {\r
+ *length = 2;\r
+ }\r
+ else if ( input < 0x10000 ) {\r
+ *length = 3;\r
+ }\r
+ else if ( input < 0x200000 ) {\r
+ *length = 4;\r
+ }\r
+ else {\r
+ *length = 0; // This code won't convert this correctly anyway.\r
+ return;\r
+ }\r
+\r
+ output += *length;\r
+\r
+ // Scary scary fall throughs.\r
+ switch (*length) {\r
+ case 4:\r
+ --output;\r
+ *output = (char)((input | BYTE_MARK) & BYTE_MASK);\r
+ input >>= 6;\r
+ case 3:\r
+ --output;\r
+ *output = (char)((input | BYTE_MARK) & BYTE_MASK);\r
+ input >>= 6;\r
+ case 2:\r
+ --output;\r
+ *output = (char)((input | BYTE_MARK) & BYTE_MASK);\r
+ input >>= 6;\r
+ case 1:\r
+ --output;\r
+ *output = (char)(input | FIRST_BYTE_MARK[*length]);\r
+ break;\r
+ default:\r
+ TIXMLASSERT( false );\r
+ }\r
+}\r
+\r
+\r
+const char* XMLUtil::GetCharacterRef( const char* p, char* value, int* length )\r
+{\r
+ // Presume an entity, and pull it out.\r
+ *length = 0;\r
+\r
+ if ( *(p+1) == '#' && *(p+2) ) {\r
+ unsigned long ucs = 0;\r
+ TIXMLASSERT( sizeof( ucs ) >= 4 );\r
+ ptrdiff_t delta = 0;\r
+ unsigned mult = 1;\r
+ static const char SEMICOLON = ';';\r
+\r
+ if ( *(p+2) == 'x' ) {\r
+ // Hexadecimal.\r
+ const char* q = p+3;\r
+ if ( !(*q) ) {\r
+ return 0;\r
+ }\r
+\r
+ q = strchr( q, SEMICOLON );\r
+\r
+ if ( !q ) {\r
+ return 0;\r
+ }\r
+ TIXMLASSERT( *q == SEMICOLON );\r
+\r
+ delta = q-p;\r
+ --q;\r
+\r
+ while ( *q != 'x' ) {\r
+ unsigned int digit = 0;\r
+\r
+ if ( *q >= '0' && *q <= '9' ) {\r
+ digit = *q - '0';\r
+ }\r
+ else if ( *q >= 'a' && *q <= 'f' ) {\r
+ digit = *q - 'a' + 10;\r
+ }\r
+ else if ( *q >= 'A' && *q <= 'F' ) {\r
+ digit = *q - 'A' + 10;\r
+ }\r
+ else {\r
+ return 0;\r
+ }\r
+ TIXMLASSERT( digit >= 0 && digit < 16);\r
+ TIXMLASSERT( digit == 0 || mult <= UINT_MAX / digit );\r
+ const unsigned int digitScaled = mult * digit;\r
+ TIXMLASSERT( ucs <= ULONG_MAX - digitScaled );\r
+ ucs += digitScaled;\r
+ TIXMLASSERT( mult <= UINT_MAX / 16 );\r
+ mult *= 16;\r
+ --q;\r
+ }\r
+ }\r
+ else {\r
+ // Decimal.\r
+ const char* q = p+2;\r
+ if ( !(*q) ) {\r
+ return 0;\r
+ }\r
+\r
+ q = strchr( q, SEMICOLON );\r
+\r
+ if ( !q ) {\r
+ return 0;\r
+ }\r
+ TIXMLASSERT( *q == SEMICOLON );\r
+\r
+ delta = q-p;\r
+ --q;\r
+\r
+ while ( *q != '#' ) {\r
+ if ( *q >= '0' && *q <= '9' ) {\r
+ const unsigned int digit = *q - '0';\r
+ TIXMLASSERT( digit >= 0 && digit < 10);\r
+ TIXMLASSERT( digit == 0 || mult <= UINT_MAX / digit );\r
+ const unsigned int digitScaled = mult * digit;\r
+ TIXMLASSERT( ucs <= ULONG_MAX - digitScaled );\r
+ ucs += digitScaled;\r
+ }\r
+ else {\r
+ return 0;\r
+ }\r
+ TIXMLASSERT( mult <= UINT_MAX / 10 );\r
+ mult *= 10;\r
+ --q;\r
+ }\r
+ }\r
+ // convert the UCS to UTF-8\r
+ ConvertUTF32ToUTF8( ucs, value, length );\r
+ return p + delta + 1;\r
+ }\r
+ return p+1;\r
+}\r
+\r
+\r
+void XMLUtil::ToStr( int v, char* buffer, int bufferSize )\r
+{\r
+ TIXML_SNPRINTF( buffer, bufferSize, "%d", v );\r
+}\r
+\r
+\r
+void XMLUtil::ToStr( unsigned v, char* buffer, int bufferSize )\r
+{\r
+ TIXML_SNPRINTF( buffer, bufferSize, "%u", v );\r
+}\r
+\r
+\r
+void XMLUtil::ToStr( bool v, char* buffer, int bufferSize )\r
+{\r
+ TIXML_SNPRINTF( buffer, bufferSize, "%d", v ? 1 : 0 );\r
+}\r
+\r
+/*\r
+ ToStr() of a number is a very tricky topic.\r
+ https://github.com/leethomason/tinyxml2/issues/106\r
+*/\r
+void XMLUtil::ToStr( float v, char* buffer, int bufferSize )\r
+{\r
+ TIXML_SNPRINTF( buffer, bufferSize, "%.8g", v );\r
+}\r
+\r
+\r
+void XMLUtil::ToStr( double v, char* buffer, int bufferSize )\r
+{\r
+ TIXML_SNPRINTF( buffer, bufferSize, "%.17g", v );\r
+}\r
+\r
+\r
+bool XMLUtil::ToInt( const char* str, int* value )\r
+{\r
+ if ( TIXML_SSCANF( str, "%d", value ) == 1 ) {\r
+ return true;\r
+ }\r
+ return false;\r
+}\r
+\r
+bool XMLUtil::ToUnsigned( const char* str, unsigned *value )\r
+{\r
+ if ( TIXML_SSCANF( str, "%u", value ) == 1 ) {\r
+ return true;\r
+ }\r
+ return false;\r
+}\r
+\r
+bool XMLUtil::ToBool( const char* str, bool* value )\r
+{\r
+ int ival = 0;\r
+ if ( ToInt( str, &ival )) {\r
+ *value = (ival==0) ? false : true;\r
+ return true;\r
+ }\r
+ if ( StringEqual( str, "true" ) ) {\r
+ *value = true;\r
+ return true;\r
+ }\r
+ else if ( StringEqual( str, "false" ) ) {\r
+ *value = false;\r
+ return true;\r
+ }\r
+ return false;\r
+}\r
+\r
+\r
+bool XMLUtil::ToFloat( const char* str, float* value )\r
+{\r
+ if ( TIXML_SSCANF( str, "%f", value ) == 1 ) {\r
+ return true;\r
+ }\r
+ return false;\r
+}\r
+\r
+bool XMLUtil::ToDouble( const char* str, double* value )\r
+{\r
+ if ( TIXML_SSCANF( str, "%lf", value ) == 1 ) {\r
+ return true;\r
+ }\r
+ return false;\r
+}\r
+\r
+\r
+char* XMLDocument::Identify( char* p, XMLNode** node )\r
+{\r
+ TIXMLASSERT( node );\r
+ TIXMLASSERT( p );\r
+ char* const start = p;\r
+ p = XMLUtil::SkipWhiteSpace( p );\r
+ if( !*p ) {\r
+ *node = 0;\r
+ TIXMLASSERT( p );\r
+ return p;\r
+ }\r
+\r
+ // These strings define the matching patterns:\r
+ static const char* xmlHeader = { "<?" };\r
+ static const char* commentHeader = { "<!--" };\r
+ static const char* cdataHeader = { "<![CDATA[" };\r
+ static const char* dtdHeader = { "<!" };\r
+ static const char* elementHeader = { "<" }; // and a header for everything else; check last.\r
+\r
+ static const int xmlHeaderLen = 2;\r
+ static const int commentHeaderLen = 4;\r
+ static const int cdataHeaderLen = 9;\r
+ static const int dtdHeaderLen = 2;\r
+ static const int elementHeaderLen = 1;\r
+\r
+ TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLUnknown ) ); // use same memory pool\r
+ TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLDeclaration ) ); // use same memory pool\r
+ XMLNode* returnNode = 0;\r
+ if ( XMLUtil::StringEqual( p, xmlHeader, xmlHeaderLen ) ) {\r
+ TIXMLASSERT( sizeof( XMLDeclaration ) == _commentPool.ItemSize() );\r
+ returnNode = new (_commentPool.Alloc()) XMLDeclaration( this );\r
+ returnNode->_memPool = &_commentPool;\r
+ p += xmlHeaderLen;\r
+ }\r
+ else if ( XMLUtil::StringEqual( p, commentHeader, commentHeaderLen ) ) {\r
+ TIXMLASSERT( sizeof( XMLComment ) == _commentPool.ItemSize() );\r
+ returnNode = new (_commentPool.Alloc()) XMLComment( this );\r
+ returnNode->_memPool = &_commentPool;\r
+ p += commentHeaderLen;\r
+ }\r
+ else if ( XMLUtil::StringEqual( p, cdataHeader, cdataHeaderLen ) ) {\r
+ TIXMLASSERT( sizeof( XMLText ) == _textPool.ItemSize() );\r
+ XMLText* text = new (_textPool.Alloc()) XMLText( this );\r
+ returnNode = text;\r
+ returnNode->_memPool = &_textPool;\r
+ p += cdataHeaderLen;\r
+ text->SetCData( true );\r
+ }\r
+ else if ( XMLUtil::StringEqual( p, dtdHeader, dtdHeaderLen ) ) {\r
+ TIXMLASSERT( sizeof( XMLUnknown ) == _commentPool.ItemSize() );\r
+ returnNode = new (_commentPool.Alloc()) XMLUnknown( this );\r
+ returnNode->_memPool = &_commentPool;\r
+ p += dtdHeaderLen;\r
+ }\r
+ else if ( XMLUtil::StringEqual( p, elementHeader, elementHeaderLen ) ) {\r
+ TIXMLASSERT( sizeof( XMLElement ) == _elementPool.ItemSize() );\r
+ returnNode = new (_elementPool.Alloc()) XMLElement( this );\r
+ returnNode->_memPool = &_elementPool;\r
+ p += elementHeaderLen;\r
+ }\r
+ else {\r
+ TIXMLASSERT( sizeof( XMLText ) == _textPool.ItemSize() );\r
+ returnNode = new (_textPool.Alloc()) XMLText( this );\r
+ returnNode->_memPool = &_textPool;\r
+ p = start; // Back it up, all the text counts.\r
+ }\r
+\r
+ TIXMLASSERT( returnNode );\r
+ TIXMLASSERT( p );\r
+ *node = returnNode;\r
+ return p;\r
+}\r
+\r
+\r
+bool XMLDocument::Accept( XMLVisitor* visitor ) const\r
+{\r
+ TIXMLASSERT( visitor );\r
+ if ( visitor->VisitEnter( *this ) ) {\r
+ for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) {\r
+ if ( !node->Accept( visitor ) ) {\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ return visitor->VisitExit( *this );\r
+}\r
+\r
+\r
+// --------- XMLNode ----------- //\r
+\r
+XMLNode::XMLNode( XMLDocument* doc ) :\r
+ _document( doc ),\r
+ _parent( 0 ),\r
+ _firstChild( 0 ), _lastChild( 0 ),\r
+ _prev( 0 ), _next( 0 ),\r
+ _memPool( 0 )\r
+{\r
+}\r
+\r
+\r
+XMLNode::~XMLNode()\r
+{\r
+ DeleteChildren();\r
+ if ( _parent ) {\r
+ _parent->Unlink( this );\r
+ }\r
+}\r
+\r
+const char* XMLNode::Value() const \r
+{\r
+ // Catch an edge case: XMLDocuments don't have a a Value. Carefully return nullptr.\r
+ if ( this->ToDocument() )\r
+ return 0;\r
+ return _value.GetStr();\r
+}\r
+\r
+void XMLNode::SetValue( const char* str, bool staticMem )\r
+{\r
+ if ( staticMem ) {\r
+ _value.SetInternedStr( str );\r
+ }\r
+ else {\r
+ _value.SetStr( str );\r
+ }\r
+}\r
+\r
+\r
+void XMLNode::DeleteChildren()\r
+{\r
+ while( _firstChild ) {\r
+ TIXMLASSERT( _lastChild );\r
+ TIXMLASSERT( _firstChild->_document == _document );\r
+ XMLNode* node = _firstChild;\r
+ Unlink( node );\r
+\r
+ DeleteNode( node );\r
+ }\r
+ _firstChild = _lastChild = 0;\r
+}\r
+\r
+\r
+void XMLNode::Unlink( XMLNode* child )\r
+{\r
+ TIXMLASSERT( child );\r
+ TIXMLASSERT( child->_document == _document );\r
+ TIXMLASSERT( child->_parent == this );\r
+ if ( child == _firstChild ) {\r
+ _firstChild = _firstChild->_next;\r
+ }\r
+ if ( child == _lastChild ) {\r
+ _lastChild = _lastChild->_prev;\r
+ }\r
+\r
+ if ( child->_prev ) {\r
+ child->_prev->_next = child->_next;\r
+ }\r
+ if ( child->_next ) {\r
+ child->_next->_prev = child->_prev;\r
+ }\r
+ child->_parent = 0;\r
+}\r
+\r
+\r
+void XMLNode::DeleteChild( XMLNode* node )\r
+{\r
+ TIXMLASSERT( node );\r
+ TIXMLASSERT( node->_document == _document );\r
+ TIXMLASSERT( node->_parent == this );\r
+ Unlink( node );\r
+ DeleteNode( node );\r
+}\r
+\r
+\r
+XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )\r
+{\r
+ TIXMLASSERT( addThis );\r
+ if ( addThis->_document != _document ) {\r
+ TIXMLASSERT( false );\r
+ return 0;\r
+ }\r
+ InsertChildPreamble( addThis );\r
+\r
+ if ( _lastChild ) {\r
+ TIXMLASSERT( _firstChild );\r
+ TIXMLASSERT( _lastChild->_next == 0 );\r
+ _lastChild->_next = addThis;\r
+ addThis->_prev = _lastChild;\r
+ _lastChild = addThis;\r
+\r
+ addThis->_next = 0;\r
+ }\r
+ else {\r
+ TIXMLASSERT( _firstChild == 0 );\r
+ _firstChild = _lastChild = addThis;\r
+\r
+ addThis->_prev = 0;\r
+ addThis->_next = 0;\r
+ }\r
+ addThis->_parent = this;\r
+ return addThis;\r
+}\r
+\r
+\r
+XMLNode* XMLNode::InsertFirstChild( XMLNode* addThis )\r
+{\r
+ TIXMLASSERT( addThis );\r
+ if ( addThis->_document != _document ) {\r
+ TIXMLASSERT( false );\r
+ return 0;\r
+ }\r
+ InsertChildPreamble( addThis );\r
+\r
+ if ( _firstChild ) {\r
+ TIXMLASSERT( _lastChild );\r
+ TIXMLASSERT( _firstChild->_prev == 0 );\r
+\r
+ _firstChild->_prev = addThis;\r
+ addThis->_next = _firstChild;\r
+ _firstChild = addThis;\r
+\r
+ addThis->_prev = 0;\r
+ }\r
+ else {\r
+ TIXMLASSERT( _lastChild == 0 );\r
+ _firstChild = _lastChild = addThis;\r
+\r
+ addThis->_prev = 0;\r
+ addThis->_next = 0;\r
+ }\r
+ addThis->_parent = this;\r
+ return addThis;\r
+}\r
+\r
+\r
+XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis )\r
+{\r
+ TIXMLASSERT( addThis );\r
+ if ( addThis->_document != _document ) {\r
+ TIXMLASSERT( false );\r
+ return 0;\r
+ }\r
+\r
+ TIXMLASSERT( afterThis );\r
+\r
+ if ( afterThis->_parent != this ) {\r
+ TIXMLASSERT( false );\r
+ return 0;\r
+ }\r
+\r
+ if ( afterThis->_next == 0 ) {\r
+ // The last node or the only node.\r
+ return InsertEndChild( addThis );\r
+ }\r
+ InsertChildPreamble( addThis );\r
+ addThis->_prev = afterThis;\r
+ addThis->_next = afterThis->_next;\r
+ afterThis->_next->_prev = addThis;\r
+ afterThis->_next = addThis;\r
+ addThis->_parent = this;\r
+ return addThis;\r
+}\r
+\r
+\r
+\r
+\r
+const XMLElement* XMLNode::FirstChildElement( const char* name ) const\r
+{\r
+ for( const XMLNode* node = _firstChild; node; node = node->_next ) {\r
+ const XMLElement* element = node->ToElement();\r
+ if ( element ) {\r
+ if ( !name || XMLUtil::StringEqual( element->Name(), name ) ) {\r
+ return element;\r
+ }\r
+ }\r
+ }\r
+ return 0;\r
+}\r
+\r
+\r
+const XMLElement* XMLNode::LastChildElement( const char* name ) const\r
+{\r
+ for( const XMLNode* node = _lastChild; node; node = node->_prev ) {\r
+ const XMLElement* element = node->ToElement();\r
+ if ( element ) {\r
+ if ( !name || XMLUtil::StringEqual( element->Name(), name ) ) {\r
+ return element;\r
+ }\r
+ }\r
+ }\r
+ return 0;\r
+}\r
+\r
+\r
+const XMLElement* XMLNode::NextSiblingElement( const char* name ) const\r
+{\r
+ for( const XMLNode* node = _next; node; node = node->_next ) {\r
+ const XMLElement* element = node->ToElement();\r
+ if ( element\r
+ && (!name || XMLUtil::StringEqual( name, element->Name() ))) {\r
+ return element;\r
+ }\r
+ }\r
+ return 0;\r
+}\r
+\r
+\r
+const XMLElement* XMLNode::PreviousSiblingElement( const char* name ) const\r
+{\r
+ for( const XMLNode* node = _prev; node; node = node->_prev ) {\r
+ const XMLElement* element = node->ToElement();\r
+ if ( element\r
+ && (!name || XMLUtil::StringEqual( name, element->Name() ))) {\r
+ return element;\r
+ }\r
+ }\r
+ return 0;\r
+}\r
+\r
+\r
+char* XMLNode::ParseDeep( char* p, StrPair* parentEnd )\r
+{\r
+ // This is a recursive method, but thinking about it "at the current level"\r
+ // it is a pretty simple flat list:\r
+ // <foo/>\r
+ // <!-- comment -->\r
+ //\r
+ // With a special case:\r
+ // <foo>\r
+ // </foo>\r
+ // <!-- comment -->\r
+ //\r
+ // Where the closing element (/foo) *must* be the next thing after the opening\r
+ // element, and the names must match. BUT the tricky bit is that the closing\r
+ // element will be read by the child.\r
+ //\r
+ // 'endTag' is the end tag for this node, it is returned by a call to a child.\r
+ // 'parentEnd' is the end tag for the parent, which is filled in and returned.\r
+\r
+ while( p && *p ) {\r
+ XMLNode* node = 0;\r
+\r
+ p = _document->Identify( p, &node );\r
+ if ( node == 0 ) {\r
+ break;\r
+ }\r
+\r
+ StrPair endTag;\r
+ p = node->ParseDeep( p, &endTag );\r
+ if ( !p ) {\r
+ DeleteNode( node );\r
+ if ( !_document->Error() ) {\r
+ _document->SetError( XML_ERROR_PARSING, 0, 0 );\r
+ }\r
+ break;\r
+ }\r
+\r
+ XMLDeclaration* decl = node->ToDeclaration();\r
+ if ( decl ) {\r
+ // A declaration can only be the first child of a document.\r
+ // Set error, if document already has children.\r
+ if ( !_document->NoChildren() ) {\r
+ _document->SetError( XML_ERROR_PARSING_DECLARATION, decl->Value(), 0);\r
+ DeleteNode( decl );\r
+ break;\r
+ }\r
+ }\r
+\r
+ XMLElement* ele = node->ToElement();\r
+ if ( ele ) {\r
+ // We read the end tag. Return it to the parent.\r
+ if ( ele->ClosingType() == XMLElement::CLOSING ) {\r
+ if ( parentEnd ) {\r
+ ele->_value.TransferTo( parentEnd );\r
+ }\r
+ node->_memPool->SetTracked(); // created and then immediately deleted.\r
+ DeleteNode( node );\r
+ return p;\r
+ }\r
+\r
+ // Handle an end tag returned to this level.\r
+ // And handle a bunch of annoying errors.\r
+ bool mismatch = false;\r
+ if ( endTag.Empty() ) {\r
+ if ( ele->ClosingType() == XMLElement::OPEN ) {\r
+ mismatch = true;\r
+ }\r
+ }\r
+ else {\r
+ if ( ele->ClosingType() != XMLElement::OPEN ) {\r
+ mismatch = true;\r
+ }\r
+ else if ( !XMLUtil::StringEqual( endTag.GetStr(), ele->Name() ) ) {\r
+ mismatch = true;\r
+ }\r
+ }\r
+ if ( mismatch ) {\r
+ _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, ele->Name(), 0 );\r
+ DeleteNode( node );\r
+ break;\r
+ }\r
+ }\r
+ InsertEndChild( node );\r
+ }\r
+ return 0;\r
+}\r
+\r
+void XMLNode::DeleteNode( XMLNode* node )\r
+{\r
+ if ( node == 0 ) {\r
+ return;\r
+ }\r
+ MemPool* pool = node->_memPool;\r
+ node->~XMLNode();\r
+ pool->Free( node );\r
+}\r
+\r
+void XMLNode::InsertChildPreamble( XMLNode* insertThis ) const\r
+{\r
+ TIXMLASSERT( insertThis );\r
+ TIXMLASSERT( insertThis->_document == _document );\r
+\r
+ if ( insertThis->_parent )\r
+ insertThis->_parent->Unlink( insertThis );\r
+ else\r
+ insertThis->_memPool->SetTracked();\r
+}\r
+\r
+// --------- XMLText ---------- //\r
+char* XMLText::ParseDeep( char* p, StrPair* )\r
+{\r
+ const char* start = p;\r
+ if ( this->CData() ) {\r
+ p = _value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION );\r
+ if ( !p ) {\r
+ _document->SetError( XML_ERROR_PARSING_CDATA, start, 0 );\r
+ }\r
+ return p;\r
+ }\r
+ else {\r
+ int flags = _document->ProcessEntities() ? StrPair::TEXT_ELEMENT : StrPair::TEXT_ELEMENT_LEAVE_ENTITIES;\r
+ if ( _document->WhitespaceMode() == COLLAPSE_WHITESPACE ) {\r
+ flags |= StrPair::NEEDS_WHITESPACE_COLLAPSING;\r
+ }\r
+\r
+ p = _value.ParseText( p, "<", flags );\r
+ if ( p && *p ) {\r
+ return p-1;\r
+ }\r
+ if ( !p ) {\r
+ _document->SetError( XML_ERROR_PARSING_TEXT, start, 0 );\r
+ }\r
+ }\r
+ return 0;\r
+}\r
+\r
+\r
+XMLNode* XMLText::ShallowClone( XMLDocument* doc ) const\r
+{\r
+ if ( !doc ) {\r
+ doc = _document;\r
+ }\r
+ XMLText* text = doc->NewText( Value() ); // fixme: this will always allocate memory. Intern?\r
+ text->SetCData( this->CData() );\r
+ return text;\r
+}\r
+\r
+\r
+bool XMLText::ShallowEqual( const XMLNode* compare ) const\r
+{\r
+ const XMLText* text = compare->ToText();\r
+ return ( text && XMLUtil::StringEqual( text->Value(), Value() ) );\r
+}\r
+\r
+\r
+bool XMLText::Accept( XMLVisitor* visitor ) const\r
+{\r
+ TIXMLASSERT( visitor );\r
+ return visitor->Visit( *this );\r
+}\r
+\r
+\r
+// --------- XMLComment ---------- //\r
+\r
+XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )\r
+{\r
+}\r
+\r
+\r
+XMLComment::~XMLComment()\r
+{\r
+}\r
+\r
+\r
+char* XMLComment::ParseDeep( char* p, StrPair* )\r
+{\r
+ // Comment parses as text.\r
+ const char* start = p;\r
+ p = _value.ParseText( p, "-->", StrPair::COMMENT );\r
+ if ( p == 0 ) {\r
+ _document->SetError( XML_ERROR_PARSING_COMMENT, start, 0 );\r
+ }\r
+ return p;\r
+}\r
+\r
+\r
+XMLNode* XMLComment::ShallowClone( XMLDocument* doc ) const\r
+{\r
+ if ( !doc ) {\r
+ doc = _document;\r
+ }\r
+ XMLComment* comment = doc->NewComment( Value() ); // fixme: this will always allocate memory. Intern?\r
+ return comment;\r
+}\r
+\r
+\r
+bool XMLComment::ShallowEqual( const XMLNode* compare ) const\r
+{\r
+ TIXMLASSERT( compare );\r
+ const XMLComment* comment = compare->ToComment();\r
+ return ( comment && XMLUtil::StringEqual( comment->Value(), Value() ));\r
+}\r
+\r
+\r
+bool XMLComment::Accept( XMLVisitor* visitor ) const\r
+{\r
+ TIXMLASSERT( visitor );\r
+ return visitor->Visit( *this );\r
+}\r
+\r
+\r
+// --------- XMLDeclaration ---------- //\r
+\r
+XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc )\r
+{\r
+}\r
+\r
+\r
+XMLDeclaration::~XMLDeclaration()\r
+{\r
+ //printf( "~XMLDeclaration\n" );\r
+}\r
+\r
+\r
+char* XMLDeclaration::ParseDeep( char* p, StrPair* )\r
+{\r
+ // Declaration parses as text.\r
+ const char* start = p;\r
+ p = _value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION );\r
+ if ( p == 0 ) {\r
+ _document->SetError( XML_ERROR_PARSING_DECLARATION, start, 0 );\r
+ }\r
+ return p;\r
+}\r
+\r
+\r
+XMLNode* XMLDeclaration::ShallowClone( XMLDocument* doc ) const\r
+{\r
+ if ( !doc ) {\r
+ doc = _document;\r
+ }\r
+ XMLDeclaration* dec = doc->NewDeclaration( Value() ); // fixme: this will always allocate memory. Intern?\r
+ return dec;\r
+}\r
+\r
+\r
+bool XMLDeclaration::ShallowEqual( const XMLNode* compare ) const\r
+{\r
+ TIXMLASSERT( compare );\r
+ const XMLDeclaration* declaration = compare->ToDeclaration();\r
+ return ( declaration && XMLUtil::StringEqual( declaration->Value(), Value() ));\r
+}\r
+\r
+\r
+\r
+bool XMLDeclaration::Accept( XMLVisitor* visitor ) const\r
+{\r
+ TIXMLASSERT( visitor );\r
+ return visitor->Visit( *this );\r
+}\r
+\r
+// --------- XMLUnknown ---------- //\r
+\r
+XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc )\r
+{\r
+}\r
+\r
+\r
+XMLUnknown::~XMLUnknown()\r
+{\r
+}\r
+\r
+\r
+char* XMLUnknown::ParseDeep( char* p, StrPair* )\r
+{\r
+ // Unknown parses as text.\r
+ const char* start = p;\r
+\r
+ p = _value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION );\r
+ if ( !p ) {\r
+ _document->SetError( XML_ERROR_PARSING_UNKNOWN, start, 0 );\r
+ }\r
+ return p;\r
+}\r
+\r
+\r
+XMLNode* XMLUnknown::ShallowClone( XMLDocument* doc ) const\r
+{\r
+ if ( !doc ) {\r
+ doc = _document;\r
+ }\r
+ XMLUnknown* text = doc->NewUnknown( Value() ); // fixme: this will always allocate memory. Intern?\r
+ return text;\r
+}\r
+\r
+\r
+bool XMLUnknown::ShallowEqual( const XMLNode* compare ) const\r
+{\r
+ TIXMLASSERT( compare );\r
+ const XMLUnknown* unknown = compare->ToUnknown();\r
+ return ( unknown && XMLUtil::StringEqual( unknown->Value(), Value() ));\r
+}\r
+\r
+\r
+bool XMLUnknown::Accept( XMLVisitor* visitor ) const\r
+{\r
+ TIXMLASSERT( visitor );\r
+ return visitor->Visit( *this );\r
+}\r
+\r
+// --------- XMLAttribute ---------- //\r
+\r
+const char* XMLAttribute::Name() const \r
+{\r
+ return _name.GetStr();\r
+}\r
+\r
+const char* XMLAttribute::Value() const \r
+{\r
+ return _value.GetStr();\r
+}\r
+\r
+char* XMLAttribute::ParseDeep( char* p, bool processEntities )\r
+{\r
+ // Parse using the name rules: bug fix, was using ParseText before\r
+ p = _name.ParseName( p );\r
+ if ( !p || !*p ) {\r
+ return 0;\r
+ }\r
+\r
+ // Skip white space before =\r
+ p = XMLUtil::SkipWhiteSpace( p );\r
+ if ( *p != '=' ) {\r
+ return 0;\r
+ }\r
+\r
+ ++p; // move up to opening quote\r
+ p = XMLUtil::SkipWhiteSpace( p );\r
+ if ( *p != '\"' && *p != '\'' ) {\r
+ return 0;\r
+ }\r
+\r
+ char endTag[2] = { *p, 0 };\r
+ ++p; // move past opening quote\r
+\r
+ p = _value.ParseText( p, endTag, processEntities ? StrPair::ATTRIBUTE_VALUE : StrPair::ATTRIBUTE_VALUE_LEAVE_ENTITIES );\r
+ return p;\r
+}\r
+\r
+\r
+void XMLAttribute::SetName( const char* n )\r
+{\r
+ _name.SetStr( n );\r
+}\r
+\r
+\r
+XMLError XMLAttribute::QueryIntValue( int* value ) const\r
+{\r
+ if ( XMLUtil::ToInt( Value(), value )) {\r
+ return XML_NO_ERROR;\r
+ }\r
+ return XML_WRONG_ATTRIBUTE_TYPE;\r
+}\r
+\r
+\r
+XMLError XMLAttribute::QueryUnsignedValue( unsigned int* value ) const\r
+{\r
+ if ( XMLUtil::ToUnsigned( Value(), value )) {\r
+ return XML_NO_ERROR;\r
+ }\r
+ return XML_WRONG_ATTRIBUTE_TYPE;\r
+}\r
+\r
+\r
+XMLError XMLAttribute::QueryBoolValue( bool* value ) const\r
+{\r
+ if ( XMLUtil::ToBool( Value(), value )) {\r
+ return XML_NO_ERROR;\r
+ }\r
+ return XML_WRONG_ATTRIBUTE_TYPE;\r
+}\r
+\r
+\r
+XMLError XMLAttribute::QueryFloatValue( float* value ) const\r
+{\r
+ if ( XMLUtil::ToFloat( Value(), value )) {\r
+ return XML_NO_ERROR;\r
+ }\r
+ return XML_WRONG_ATTRIBUTE_TYPE;\r
+}\r
+\r
+\r
+XMLError XMLAttribute::QueryDoubleValue( double* value ) const\r
+{\r
+ if ( XMLUtil::ToDouble( Value(), value )) {\r
+ return XML_NO_ERROR;\r
+ }\r
+ return XML_WRONG_ATTRIBUTE_TYPE;\r
+}\r
+\r
+\r
+void XMLAttribute::SetAttribute( const char* v )\r
+{\r
+ _value.SetStr( v );\r
+}\r
+\r
+\r
+void XMLAttribute::SetAttribute( int v )\r
+{\r
+ char buf[BUF_SIZE];\r
+ XMLUtil::ToStr( v, buf, BUF_SIZE );\r
+ _value.SetStr( buf );\r
+}\r
+\r
+\r
+void XMLAttribute::SetAttribute( unsigned v )\r
+{\r
+ char buf[BUF_SIZE];\r
+ XMLUtil::ToStr( v, buf, BUF_SIZE );\r
+ _value.SetStr( buf );\r
+}\r
+\r
+\r
+void XMLAttribute::SetAttribute( bool v )\r
+{\r
+ char buf[BUF_SIZE];\r
+ XMLUtil::ToStr( v, buf, BUF_SIZE );\r
+ _value.SetStr( buf );\r
+}\r
+\r
+void XMLAttribute::SetAttribute( double v )\r
+{\r
+ char buf[BUF_SIZE];\r
+ XMLUtil::ToStr( v, buf, BUF_SIZE );\r
+ _value.SetStr( buf );\r
+}\r
+\r
+void XMLAttribute::SetAttribute( float v )\r
+{\r
+ char buf[BUF_SIZE];\r
+ XMLUtil::ToStr( v, buf, BUF_SIZE );\r
+ _value.SetStr( buf );\r
+}\r
+\r
+\r
+// --------- XMLElement ---------- //\r
+XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),\r
+ _closingType( 0 ),\r
+ _rootAttribute( 0 )\r
+{\r
+}\r
+\r
+\r
+XMLElement::~XMLElement()\r
+{\r
+ while( _rootAttribute ) {\r
+ XMLAttribute* next = _rootAttribute->_next;\r
+ DeleteAttribute( _rootAttribute );\r
+ _rootAttribute = next;\r
+ }\r
+}\r
+\r
+\r
+const XMLAttribute* XMLElement::FindAttribute( const char* name ) const\r
+{\r
+ for( XMLAttribute* a = _rootAttribute; a; a = a->_next ) {\r
+ if ( XMLUtil::StringEqual( a->Name(), name ) ) {\r
+ return a;\r
+ }\r
+ }\r
+ return 0;\r
+}\r
+\r
+\r
+const char* XMLElement::Attribute( const char* name, const char* value ) const\r
+{\r
+ const XMLAttribute* a = FindAttribute( name );\r
+ if ( !a ) {\r
+ return 0;\r
+ }\r
+ if ( !value || XMLUtil::StringEqual( a->Value(), value )) {\r
+ return a->Value();\r
+ }\r
+ return 0;\r
+}\r
+\r
+\r
+const char* XMLElement::GetText() const\r
+{\r
+ if ( FirstChild() && FirstChild()->ToText() ) {\r
+ return FirstChild()->Value();\r
+ }\r
+ return 0;\r
+}\r
+\r
+\r
+void XMLElement::SetText( const char* inText )\r
+{\r
+ if ( FirstChild() && FirstChild()->ToText() )\r
+ FirstChild()->SetValue( inText );\r
+ else {\r
+ XMLText* theText = GetDocument()->NewText( inText );\r
+ InsertFirstChild( theText );\r
+ }\r
+}\r
+\r
+\r
+void XMLElement::SetText( int v ) \r
+{\r
+ char buf[BUF_SIZE];\r
+ XMLUtil::ToStr( v, buf, BUF_SIZE );\r
+ SetText( buf );\r
+}\r
+\r
+\r
+void XMLElement::SetText( unsigned v ) \r
+{\r
+ char buf[BUF_SIZE];\r
+ XMLUtil::ToStr( v, buf, BUF_SIZE );\r
+ SetText( buf );\r
+}\r
+\r
+\r
+void XMLElement::SetText( bool v ) \r
+{\r
+ char buf[BUF_SIZE];\r
+ XMLUtil::ToStr( v, buf, BUF_SIZE );\r
+ SetText( buf );\r
+}\r
+\r
+\r
+void XMLElement::SetText( float v ) \r
+{\r
+ char buf[BUF_SIZE];\r
+ XMLUtil::ToStr( v, buf, BUF_SIZE );\r
+ SetText( buf );\r
+}\r
+\r
+\r
+void XMLElement::SetText( double v ) \r
+{\r
+ char buf[BUF_SIZE];\r
+ XMLUtil::ToStr( v, buf, BUF_SIZE );\r
+ SetText( buf );\r
+}\r
+\r
+\r
+XMLError XMLElement::QueryIntText( int* ival ) const\r
+{\r
+ if ( FirstChild() && FirstChild()->ToText() ) {\r
+ const char* t = FirstChild()->Value();\r
+ if ( XMLUtil::ToInt( t, ival ) ) {\r
+ return XML_SUCCESS;\r
+ }\r
+ return XML_CAN_NOT_CONVERT_TEXT;\r
+ }\r
+ return XML_NO_TEXT_NODE;\r
+}\r
+\r
+\r
+XMLError XMLElement::QueryUnsignedText( unsigned* uval ) const\r
+{\r
+ if ( FirstChild() && FirstChild()->ToText() ) {\r
+ const char* t = FirstChild()->Value();\r
+ if ( XMLUtil::ToUnsigned( t, uval ) ) {\r
+ return XML_SUCCESS;\r
+ }\r
+ return XML_CAN_NOT_CONVERT_TEXT;\r
+ }\r
+ return XML_NO_TEXT_NODE;\r
+}\r
+\r
+\r
+XMLError XMLElement::QueryBoolText( bool* bval ) const\r
+{\r
+ if ( FirstChild() && FirstChild()->ToText() ) {\r
+ const char* t = FirstChild()->Value();\r
+ if ( XMLUtil::ToBool( t, bval ) ) {\r
+ return XML_SUCCESS;\r
+ }\r
+ return XML_CAN_NOT_CONVERT_TEXT;\r
+ }\r
+ return XML_NO_TEXT_NODE;\r
+}\r
+\r
+\r
+XMLError XMLElement::QueryDoubleText( double* dval ) const\r
+{\r
+ if ( FirstChild() && FirstChild()->ToText() ) {\r
+ const char* t = FirstChild()->Value();\r
+ if ( XMLUtil::ToDouble( t, dval ) ) {\r
+ return XML_SUCCESS;\r
+ }\r
+ return XML_CAN_NOT_CONVERT_TEXT;\r
+ }\r
+ return XML_NO_TEXT_NODE;\r
+}\r
+\r
+\r
+XMLError XMLElement::QueryFloatText( float* fval ) const\r
+{\r
+ if ( FirstChild() && FirstChild()->ToText() ) {\r
+ const char* t = FirstChild()->Value();\r
+ if ( XMLUtil::ToFloat( t, fval ) ) {\r
+ return XML_SUCCESS;\r
+ }\r
+ return XML_CAN_NOT_CONVERT_TEXT;\r
+ }\r
+ return XML_NO_TEXT_NODE;\r
+}\r
+\r
+\r
+\r
+XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name )\r
+{\r
+ XMLAttribute* last = 0;\r
+ XMLAttribute* attrib = 0;\r
+ for( attrib = _rootAttribute;\r
+ attrib;\r
+ last = attrib, attrib = attrib->_next ) {\r
+ if ( XMLUtil::StringEqual( attrib->Name(), name ) ) {\r
+ break;\r
+ }\r
+ }\r
+ if ( !attrib ) {\r
+ TIXMLASSERT( sizeof( XMLAttribute ) == _document->_attributePool.ItemSize() );\r
+ attrib = new (_document->_attributePool.Alloc() ) XMLAttribute();\r
+ attrib->_memPool = &_document->_attributePool;\r
+ if ( last ) {\r
+ last->_next = attrib;\r
+ }\r
+ else {\r
+ _rootAttribute = attrib;\r
+ }\r
+ attrib->SetName( name );\r
+ attrib->_memPool->SetTracked(); // always created and linked.\r
+ }\r
+ return attrib;\r
+}\r
+\r
+\r
+void XMLElement::DeleteAttribute( const char* name )\r
+{\r
+ XMLAttribute* prev = 0;\r
+ for( XMLAttribute* a=_rootAttribute; a; a=a->_next ) {\r
+ if ( XMLUtil::StringEqual( name, a->Name() ) ) {\r
+ if ( prev ) {\r
+ prev->_next = a->_next;\r
+ }\r
+ else {\r
+ _rootAttribute = a->_next;\r
+ }\r
+ DeleteAttribute( a );\r
+ break;\r
+ }\r
+ prev = a;\r
+ }\r
+}\r
+\r
+\r
+char* XMLElement::ParseAttributes( char* p )\r
+{\r
+ const char* start = p;\r
+ XMLAttribute* prevAttribute = 0;\r
+\r
+ // Read the attributes.\r
+ while( p ) {\r
+ p = XMLUtil::SkipWhiteSpace( p );\r
+ if ( !(*p) ) {\r
+ _document->SetError( XML_ERROR_PARSING_ELEMENT, start, Name() );\r
+ return 0;\r
+ }\r
+\r
+ // attribute.\r
+ if (XMLUtil::IsNameStartChar( *p ) ) {\r
+ TIXMLASSERT( sizeof( XMLAttribute ) == _document->_attributePool.ItemSize() );\r
+ XMLAttribute* attrib = new (_document->_attributePool.Alloc() ) XMLAttribute();\r
+ attrib->_memPool = &_document->_attributePool;\r
+ attrib->_memPool->SetTracked();\r
+\r
+ p = attrib->ParseDeep( p, _document->ProcessEntities() );\r
+ if ( !p || Attribute( attrib->Name() ) ) {\r
+ DeleteAttribute( attrib );\r
+ _document->SetError( XML_ERROR_PARSING_ATTRIBUTE, start, p );\r
+ return 0;\r
+ }\r
+ // There is a minor bug here: if the attribute in the source xml\r
+ // document is duplicated, it will not be detected and the\r
+ // attribute will be doubly added. However, tracking the 'prevAttribute'\r
+ // avoids re-scanning the attribute list. Preferring performance for\r
+ // now, may reconsider in the future.\r
+ if ( prevAttribute ) {\r
+ prevAttribute->_next = attrib;\r
+ }\r
+ else {\r
+ _rootAttribute = attrib;\r
+ }\r
+ prevAttribute = attrib;\r
+ }\r
+ // end of the tag\r
+ else if ( *p == '>' ) {\r
+ ++p;\r
+ break;\r
+ }\r
+ // end of the tag\r
+ else if ( *p == '/' && *(p+1) == '>' ) {\r
+ _closingType = CLOSED;\r
+ return p+2; // done; sealed element.\r
+ }\r
+ else {\r
+ _document->SetError( XML_ERROR_PARSING_ELEMENT, start, p );\r
+ return 0;\r
+ }\r
+ }\r
+ return p;\r
+}\r
+\r
+void XMLElement::DeleteAttribute( XMLAttribute* attribute )\r
+{\r
+ if ( attribute == 0 ) {\r
+ return;\r
+ }\r
+ MemPool* pool = attribute->_memPool;\r
+ attribute->~XMLAttribute();\r
+ pool->Free( attribute );\r
+}\r
+\r
+//\r
+// <ele></ele>\r
+// <ele>foo<b>bar</b></ele>\r
+//\r
+char* XMLElement::ParseDeep( char* p, StrPair* strPair )\r
+{\r
+ // Read the element name.\r
+ p = XMLUtil::SkipWhiteSpace( p );\r
+\r
+ // The closing element is the </element> form. It is\r
+ // parsed just like a regular element then deleted from\r
+ // the DOM.\r
+ if ( *p == '/' ) {\r
+ _closingType = CLOSING;\r
+ ++p;\r
+ }\r
+\r
+ p = _value.ParseName( p );\r
+ if ( _value.Empty() ) {\r
+ return 0;\r
+ }\r
+\r
+ p = ParseAttributes( p );\r
+ if ( !p || !*p || _closingType ) {\r
+ return p;\r
+ }\r
+\r
+ p = XMLNode::ParseDeep( p, strPair );\r
+ return p;\r
+}\r
+\r
+\r
+\r
+XMLNode* XMLElement::ShallowClone( XMLDocument* doc ) const\r
+{\r
+ if ( !doc ) {\r
+ doc = _document;\r
+ }\r
+ XMLElement* element = doc->NewElement( Value() ); // fixme: this will always allocate memory. Intern?\r
+ for( const XMLAttribute* a=FirstAttribute(); a; a=a->Next() ) {\r
+ element->SetAttribute( a->Name(), a->Value() ); // fixme: this will always allocate memory. Intern?\r
+ }\r
+ return element;\r
+}\r
+\r
+\r
+bool XMLElement::ShallowEqual( const XMLNode* compare ) const\r
+{\r
+ TIXMLASSERT( compare );\r
+ const XMLElement* other = compare->ToElement();\r
+ if ( other && XMLUtil::StringEqual( other->Name(), Name() )) {\r
+\r
+ const XMLAttribute* a=FirstAttribute();\r
+ const XMLAttribute* b=other->FirstAttribute();\r
+\r
+ while ( a && b ) {\r
+ if ( !XMLUtil::StringEqual( a->Value(), b->Value() ) ) {\r
+ return false;\r
+ }\r
+ a = a->Next();\r
+ b = b->Next();\r
+ }\r
+ if ( a || b ) {\r
+ // different count\r
+ return false;\r
+ }\r
+ return true;\r
+ }\r
+ return false;\r
+}\r
+\r
+\r
+bool XMLElement::Accept( XMLVisitor* visitor ) const\r
+{\r
+ TIXMLASSERT( visitor );\r
+ if ( visitor->VisitEnter( *this, _rootAttribute ) ) {\r
+ for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) {\r
+ if ( !node->Accept( visitor ) ) {\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ return visitor->VisitExit( *this );\r
+}\r
+\r
+\r
+// --------- XMLDocument ----------- //\r
+\r
+// Warning: List must match 'enum XMLError'\r
+const char* XMLDocument::_errorNames[XML_ERROR_COUNT] = {\r
+ "XML_SUCCESS",\r
+ "XML_NO_ATTRIBUTE",\r
+ "XML_WRONG_ATTRIBUTE_TYPE",\r
+ "XML_ERROR_FILE_NOT_FOUND",\r
+ "XML_ERROR_FILE_COULD_NOT_BE_OPENED",\r
+ "XML_ERROR_FILE_READ_ERROR",\r
+ "XML_ERROR_ELEMENT_MISMATCH",\r
+ "XML_ERROR_PARSING_ELEMENT",\r
+ "XML_ERROR_PARSING_ATTRIBUTE",\r
+ "XML_ERROR_IDENTIFYING_TAG",\r
+ "XML_ERROR_PARSING_TEXT",\r
+ "XML_ERROR_PARSING_CDATA",\r
+ "XML_ERROR_PARSING_COMMENT",\r
+ "XML_ERROR_PARSING_DECLARATION",\r
+ "XML_ERROR_PARSING_UNKNOWN",\r
+ "XML_ERROR_EMPTY_DOCUMENT",\r
+ "XML_ERROR_MISMATCHED_ELEMENT",\r
+ "XML_ERROR_PARSING",\r
+ "XML_CAN_NOT_CONVERT_TEXT",\r
+ "XML_NO_TEXT_NODE"\r
+};\r
+\r
+\r
+XMLDocument::XMLDocument( bool processEntities, Whitespace whitespace ) :\r
+ XMLNode( 0 ),\r
+ _writeBOM( false ),\r
+ _processEntities( processEntities ),\r
+ _errorID( XML_NO_ERROR ),\r
+ _whitespace( whitespace ),\r
+ _errorStr1( 0 ),\r
+ _errorStr2( 0 ),\r
+ _charBuffer( 0 )\r
+{\r
+ // avoid VC++ C4355 warning about 'this' in initializer list (C4355 is off by default in VS2012+)\r
+ _document = this;\r
+}\r
+\r
+\r
+XMLDocument::~XMLDocument()\r
+{\r
+ Clear();\r
+}\r
+\r
+\r
+void XMLDocument::Clear()\r
+{\r
+ DeleteChildren();\r
+\r
+#ifdef DEBUG\r
+ const bool hadError = Error();\r
+#endif\r
+ _errorID = XML_NO_ERROR;\r
+ _errorStr1 = 0;\r
+ _errorStr2 = 0;\r
+\r
+ delete [] _charBuffer;\r
+ _charBuffer = 0;\r
+\r
+#if 0\r
+ _textPool.Trace( "text" );\r
+ _elementPool.Trace( "element" );\r
+ _commentPool.Trace( "comment" );\r
+ _attributePool.Trace( "attribute" );\r
+#endif\r
+ \r
+#ifdef DEBUG\r
+ if ( !hadError ) {\r
+ TIXMLASSERT( _elementPool.CurrentAllocs() == _elementPool.Untracked() );\r
+ TIXMLASSERT( _attributePool.CurrentAllocs() == _attributePool.Untracked() );\r
+ TIXMLASSERT( _textPool.CurrentAllocs() == _textPool.Untracked() );\r
+ TIXMLASSERT( _commentPool.CurrentAllocs() == _commentPool.Untracked() );\r
+ }\r
+#endif\r
+}\r
+\r
+\r
+XMLElement* XMLDocument::NewElement( const char* name )\r
+{\r
+ TIXMLASSERT( sizeof( XMLElement ) == _elementPool.ItemSize() );\r
+ XMLElement* ele = new (_elementPool.Alloc()) XMLElement( this );\r
+ ele->_memPool = &_elementPool;\r
+ ele->SetName( name );\r
+ return ele;\r
+}\r
+\r
+\r
+XMLComment* XMLDocument::NewComment( const char* str )\r
+{\r
+ TIXMLASSERT( sizeof( XMLComment ) == _commentPool.ItemSize() );\r
+ XMLComment* comment = new (_commentPool.Alloc()) XMLComment( this );\r
+ comment->_memPool = &_commentPool;\r
+ comment->SetValue( str );\r
+ return comment;\r
+}\r
+\r
+\r
+XMLText* XMLDocument::NewText( const char* str )\r
+{\r
+ TIXMLASSERT( sizeof( XMLText ) == _textPool.ItemSize() );\r
+ XMLText* text = new (_textPool.Alloc()) XMLText( this );\r
+ text->_memPool = &_textPool;\r
+ text->SetValue( str );\r
+ return text;\r
+}\r
+\r
+\r
+XMLDeclaration* XMLDocument::NewDeclaration( const char* str )\r
+{\r
+ TIXMLASSERT( sizeof( XMLDeclaration ) == _commentPool.ItemSize() );\r
+ XMLDeclaration* dec = new (_commentPool.Alloc()) XMLDeclaration( this );\r
+ dec->_memPool = &_commentPool;\r
+ dec->SetValue( str ? str : "xml version=\"1.0\" encoding=\"UTF-8\"" );\r
+ return dec;\r
+}\r
+\r
+\r
+XMLUnknown* XMLDocument::NewUnknown( const char* str )\r
+{\r
+ TIXMLASSERT( sizeof( XMLUnknown ) == _commentPool.ItemSize() );\r
+ XMLUnknown* unk = new (_commentPool.Alloc()) XMLUnknown( this );\r
+ unk->_memPool = &_commentPool;\r
+ unk->SetValue( str );\r
+ return unk;\r
+}\r
+\r
+static FILE* callfopen( const char* filepath, const char* mode )\r
+{\r
+ TIXMLASSERT( filepath );\r
+ TIXMLASSERT( mode );\r
+#if defined(_MSC_VER) && (_MSC_VER >= 1400 ) && (!defined WINCE)\r
+ FILE* fp = 0;\r
+ errno_t err = fopen_s( &fp, filepath, mode );\r
+ if ( err ) {\r
+ return 0;\r
+ }\r
+#else\r
+ FILE* fp = fopen( filepath, mode );\r
+#endif\r
+ return fp;\r
+}\r
+ \r
+void XMLDocument::DeleteNode( XMLNode* node ) {\r
+ TIXMLASSERT( node );\r
+ TIXMLASSERT(node->_document == this );\r
+ if (node->_parent) {\r
+ node->_parent->DeleteChild( node );\r
+ }\r
+ else {\r
+ // Isn't in the tree.\r
+ // Use the parent delete.\r
+ // Also, we need to mark it tracked: we 'know'\r
+ // it was never used.\r
+ node->_memPool->SetTracked();\r
+ // Call the static XMLNode version:\r
+ XMLNode::DeleteNode(node);\r
+ }\r
+}\r
+\r
+\r
+XMLError XMLDocument::LoadFile( const char* filename )\r
+{\r
+ Clear();\r
+ FILE* fp = callfopen( filename, "rb" );\r
+ if ( !fp ) {\r
+ SetError( XML_ERROR_FILE_NOT_FOUND, filename, 0 );\r
+ return _errorID;\r
+ }\r
+ LoadFile( fp );\r
+ fclose( fp );\r
+ return _errorID;\r
+}\r
+\r
+// This is likely overengineered template art to have a check that unsigned long value incremented\r
+// by one still fits into size_t. If size_t type is larger than unsigned long type\r
+// (x86_64-w64-mingw32 target) then the check is redundant and gcc and clang emit\r
+// -Wtype-limits warning. This piece makes the compiler select code with a check when a check\r
+// is useful and code with no check when a check is redundant depending on how size_t and unsigned long\r
+// types sizes relate to each other.\r
+template\r
+<bool = (sizeof(unsigned long) >= sizeof(size_t))>\r
+struct LongFitsIntoSizeTMinusOne {\r
+ static bool Fits( unsigned long value )\r
+ {\r
+ return value < (size_t)-1;\r
+ }\r
+};\r
+\r
+template <>\r
+bool LongFitsIntoSizeTMinusOne<false>::Fits( unsigned long /*value*/ )\r
+{\r
+ return true;\r
+}\r
+\r
+XMLError XMLDocument::LoadFile( FILE* fp )\r
+{\r
+ Clear();\r
+\r
+ fseek( fp, 0, SEEK_SET );\r
+ if ( fgetc( fp ) == EOF && ferror( fp ) != 0 ) {\r
+ SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );\r
+ return _errorID;\r
+ }\r
+\r
+ fseek( fp, 0, SEEK_END );\r
+ const long filelength = ftell( fp );\r
+ fseek( fp, 0, SEEK_SET );\r
+ if ( filelength == -1L ) {\r
+ SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );\r
+ return _errorID;\r
+ }\r
+\r
+ if ( !LongFitsIntoSizeTMinusOne<>::Fits( filelength ) ) {\r
+ // Cannot handle files which won't fit in buffer together with null terminator\r
+ SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );\r
+ return _errorID;\r
+ }\r
+\r
+ if ( filelength == 0 ) {\r
+ SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );\r
+ return _errorID;\r
+ }\r
+\r
+ const size_t size = filelength;\r
+ TIXMLASSERT( _charBuffer == 0 );\r
+ _charBuffer = new char[size+1];\r
+ size_t read = fread( _charBuffer, 1, size, fp );\r
+ if ( read != size ) {\r
+ SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );\r
+ return _errorID;\r
+ }\r
+\r
+ _charBuffer[size] = 0;\r
+\r
+ Parse();\r
+ return _errorID;\r
+}\r
+\r
+\r
+XMLError XMLDocument::SaveFile( const char* filename, bool compact )\r
+{\r
+ FILE* fp = callfopen( filename, "w" );\r
+ if ( !fp ) {\r
+ SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, filename, 0 );\r
+ return _errorID;\r
+ }\r
+ SaveFile(fp, compact);\r
+ fclose( fp );\r
+ return _errorID;\r
+}\r
+\r
+\r
+XMLError XMLDocument::SaveFile( FILE* fp, bool compact )\r
+{\r
+ // Clear any error from the last save, otherwise it will get reported\r
+ // for *this* call.\r
+ SetError( XML_NO_ERROR, 0, 0 );\r
+ XMLPrinter stream( fp, compact );\r
+ Print( &stream );\r
+ return _errorID;\r
+}\r
+\r
+\r
+XMLError XMLDocument::Parse( const char* p, size_t len )\r
+{\r
+ Clear();\r
+\r
+ if ( len == 0 || !p || !*p ) {\r
+ SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );\r
+ return _errorID;\r
+ }\r
+ if ( len == (size_t)(-1) ) {\r
+ len = strlen( p );\r
+ }\r
+ TIXMLASSERT( _charBuffer == 0 );\r
+ _charBuffer = new char[ len+1 ];\r
+ memcpy( _charBuffer, p, len );\r
+ _charBuffer[len] = 0;\r
+\r
+ Parse();\r
+ if ( Error() ) {\r
+ // clean up now essentially dangling memory.\r
+ // and the parse fail can put objects in the\r
+ // pools that are dead and inaccessible.\r
+ DeleteChildren();\r
+ _elementPool.Clear();\r
+ _attributePool.Clear();\r
+ _textPool.Clear();\r
+ _commentPool.Clear();\r
+ }\r
+ return _errorID;\r
+}\r
+\r
+\r
+void XMLDocument::Print( XMLPrinter* streamer ) const\r
+{\r
+ if ( streamer ) {\r
+ Accept( streamer );\r
+ }\r
+ else {\r
+ XMLPrinter stdoutStreamer( stdout );\r
+ Accept( &stdoutStreamer );\r
+ }\r
+}\r
+\r
+\r
+void XMLDocument::SetError( XMLError error, const char* str1, const char* str2 )\r
+{\r
+ TIXMLASSERT( error >= 0 && error < XML_ERROR_COUNT );\r
+ _errorID = error;\r
+ _errorStr1 = str1;\r
+ _errorStr2 = str2;\r
+}\r
+\r
+const char* XMLDocument::ErrorName() const\r
+{\r
+ TIXMLASSERT( _errorID >= 0 && _errorID < XML_ERROR_COUNT );\r
+ const char* errorName = _errorNames[_errorID];\r
+ TIXMLASSERT( errorName && errorName[0] );\r
+ return errorName;\r
+}\r
+\r
+void XMLDocument::PrintError() const\r
+{\r
+ if ( Error() ) {\r
+ static const int LEN = 20;\r
+ char buf1[LEN] = { 0 };\r
+ char buf2[LEN] = { 0 };\r
+\r
+ if ( _errorStr1 ) {\r
+ TIXML_SNPRINTF( buf1, LEN, "%s", _errorStr1 );\r
+ }\r
+ if ( _errorStr2 ) {\r
+ TIXML_SNPRINTF( buf2, LEN, "%s", _errorStr2 );\r
+ }\r
+\r
+ // Should check INT_MIN <= _errorID && _errorId <= INT_MAX, but that\r
+ // causes a clang "always true" -Wtautological-constant-out-of-range-compare warning\r
+ TIXMLASSERT( 0 <= _errorID && XML_ERROR_COUNT - 1 <= INT_MAX );\r
+ printf( "XMLDocument error id=%d '%s' str1=%s str2=%s\n",\r
+ static_cast<int>( _errorID ), ErrorName(), buf1, buf2 );\r
+ }\r
+}\r
+\r
+void XMLDocument::Parse()\r
+{\r
+ TIXMLASSERT( NoChildren() ); // Clear() must have been called previously\r
+ TIXMLASSERT( _charBuffer );\r
+ char* p = _charBuffer;\r
+ p = XMLUtil::SkipWhiteSpace( p );\r
+ p = const_cast<char*>( XMLUtil::ReadBOM( p, &_writeBOM ) );\r
+ if ( !*p ) {\r
+ SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );\r
+ return;\r
+ }\r
+ ParseDeep(p, 0 );\r
+}\r
+\r
+XMLPrinter::XMLPrinter( FILE* file, bool compact, int depth ) :\r
+ _elementJustOpened( false ),\r
+ _firstElement( true ),\r
+ _fp( file ),\r
+ _depth( depth ),\r
+ _textDepth( -1 ),\r
+ _processEntities( true ),\r
+ _compactMode( compact )\r
+{\r
+ for( int i=0; i<ENTITY_RANGE; ++i ) {\r
+ _entityFlag[i] = false;\r
+ _restrictedEntityFlag[i] = false;\r
+ }\r
+ for( int i=0; i<NUM_ENTITIES; ++i ) {\r
+ const char entityValue = entities[i].value;\r
+ TIXMLASSERT( 0 <= entityValue && entityValue < ENTITY_RANGE );\r
+ _entityFlag[ (unsigned char)entityValue ] = true;\r
+ }\r
+ _restrictedEntityFlag[(unsigned char)'&'] = true;\r
+ _restrictedEntityFlag[(unsigned char)'<'] = true;\r
+ _restrictedEntityFlag[(unsigned char)'>'] = true; // not required, but consistency is nice\r
+ _buffer.Push( 0 );\r
+}\r
+\r
+\r
+void XMLPrinter::Print( const char* format, ... )\r
+{\r
+ va_list va;\r
+ va_start( va, format );\r
+\r
+ if ( _fp ) {\r
+ vfprintf( _fp, format, va );\r
+ }\r
+ else {\r
+ const int len = TIXML_VSCPRINTF( format, va );\r
+ // Close out and re-start the va-args\r
+ va_end( va );\r
+ TIXMLASSERT( len >= 0 );\r
+ va_start( va, format );\r
+ TIXMLASSERT( _buffer.Size() > 0 && _buffer[_buffer.Size() - 1] == 0 );\r
+ char* p = _buffer.PushArr( len ) - 1; // back up over the null terminator.\r
+ TIXML_VSNPRINTF( p, len+1, format, va );\r
+ }\r
+ va_end( va );\r
+}\r
+\r
+\r
+void XMLPrinter::PrintSpace( int depth )\r
+{\r
+ for( int i=0; i<depth; ++i ) {\r
+ Print( " " );\r
+ }\r
+}\r
+\r
+\r
+void XMLPrinter::PrintString( const char* p, bool restricted )\r
+{\r
+ // Look for runs of bytes between entities to print.\r
+ const char* q = p;\r
+\r
+ if ( _processEntities ) {\r
+ const bool* flag = restricted ? _restrictedEntityFlag : _entityFlag;\r
+ while ( *q ) {\r
+ TIXMLASSERT( p <= q );\r
+ // Remember, char is sometimes signed. (How many times has that bitten me?)\r
+ if ( *q > 0 && *q < ENTITY_RANGE ) {\r
+ // Check for entities. If one is found, flush\r
+ // the stream up until the entity, write the\r
+ // entity, and keep looking.\r
+ if ( flag[(unsigned char)(*q)] ) {\r
+ while ( p < q ) {\r
+ const size_t delta = q - p;\r
+ // %.*s accepts type int as "precision"\r
+ const int toPrint = ( INT_MAX < delta ) ? INT_MAX : (int)delta;\r
+ Print( "%.*s", toPrint, p );\r
+ p += toPrint;\r
+ }\r
+ bool entityPatternPrinted = false;\r
+ for( int i=0; i<NUM_ENTITIES; ++i ) {\r
+ if ( entities[i].value == *q ) {\r
+ Print( "&%s;", entities[i].pattern );\r
+ entityPatternPrinted = true;\r
+ break;\r
+ }\r
+ }\r
+ if ( !entityPatternPrinted ) {\r
+ // TIXMLASSERT( entityPatternPrinted ) causes gcc -Wunused-but-set-variable in release\r
+ TIXMLASSERT( false );\r
+ }\r
+ ++p;\r
+ }\r
+ }\r
+ ++q;\r
+ TIXMLASSERT( p <= q );\r
+ }\r
+ }\r
+ // Flush the remaining string. This will be the entire\r
+ // string if an entity wasn't found.\r
+ TIXMLASSERT( p <= q );\r
+ if ( !_processEntities || ( p < q ) ) {\r
+ Print( "%s", p );\r
+ }\r
+}\r
+\r
+\r
+void XMLPrinter::PushHeader( bool writeBOM, bool writeDec )\r
+{\r
+ if ( writeBOM ) {\r
+ static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 };\r
+ Print( "%s", bom );\r
+ }\r
+ if ( writeDec ) {\r
+ PushDeclaration( "xml version=\"1.0\"" );\r
+ }\r
+}\r
+\r
+\r
+void XMLPrinter::OpenElement( const char* name, bool compactMode )\r
+{\r
+ SealElementIfJustOpened();\r
+ _stack.Push( name );\r
+\r
+ if ( _textDepth < 0 && !_firstElement && !compactMode ) {\r
+ Print( "\n" );\r
+ }\r
+ if ( !compactMode ) {\r
+ PrintSpace( _depth );\r
+ }\r
+\r
+ Print( "<%s", name );\r
+ _elementJustOpened = true;\r
+ _firstElement = false;\r
+ ++_depth;\r
+}\r
+\r
+\r
+void XMLPrinter::PushAttribute( const char* name, const char* value )\r
+{\r
+ TIXMLASSERT( _elementJustOpened );\r
+ Print( " %s=\"", name );\r
+ PrintString( value, false );\r
+ Print( "\"" );\r
+}\r
+\r
+\r
+void XMLPrinter::PushAttribute( const char* name, int v )\r
+{\r
+ char buf[BUF_SIZE];\r
+ XMLUtil::ToStr( v, buf, BUF_SIZE );\r
+ PushAttribute( name, buf );\r
+}\r
+\r
+\r
+void XMLPrinter::PushAttribute( const char* name, unsigned v )\r
+{\r
+ char buf[BUF_SIZE];\r
+ XMLUtil::ToStr( v, buf, BUF_SIZE );\r
+ PushAttribute( name, buf );\r
+}\r
+\r
+\r
+void XMLPrinter::PushAttribute( const char* name, bool v )\r
+{\r
+ char buf[BUF_SIZE];\r
+ XMLUtil::ToStr( v, buf, BUF_SIZE );\r
+ PushAttribute( name, buf );\r
+}\r
+\r
+\r
+void XMLPrinter::PushAttribute( const char* name, double v )\r
+{\r
+ char buf[BUF_SIZE];\r
+ XMLUtil::ToStr( v, buf, BUF_SIZE );\r
+ PushAttribute( name, buf );\r
+}\r
+\r
+\r
+void XMLPrinter::CloseElement( bool compactMode )\r
+{\r
+ --_depth;\r
+ const char* name = _stack.Pop();\r
+\r
+ if ( _elementJustOpened ) {\r
+ Print( "/>" );\r
+ }\r
+ else {\r
+ if ( _textDepth < 0 && !compactMode) {\r
+ Print( "\n" );\r
+ PrintSpace( _depth );\r
+ }\r
+ Print( "</%s>", name );\r
+ }\r
+\r
+ if ( _textDepth == _depth ) {\r
+ _textDepth = -1;\r
+ }\r
+ if ( _depth == 0 && !compactMode) {\r
+ Print( "\n" );\r
+ }\r
+ _elementJustOpened = false;\r
+}\r
+\r
+\r
+void XMLPrinter::SealElementIfJustOpened()\r
+{\r
+ if ( !_elementJustOpened ) {\r
+ return;\r
+ }\r
+ _elementJustOpened = false;\r
+ Print( ">" );\r
+}\r
+\r
+\r
+void XMLPrinter::PushText( const char* text, bool cdata )\r
+{\r
+ _textDepth = _depth-1;\r
+\r
+ SealElementIfJustOpened();\r
+ if ( cdata ) {\r
+ Print( "<![CDATA[%s]]>", text );\r
+ }\r
+ else {\r
+ PrintString( text, true );\r
+ }\r
+}\r
+\r
+void XMLPrinter::PushText( int value )\r
+{\r
+ char buf[BUF_SIZE];\r
+ XMLUtil::ToStr( value, buf, BUF_SIZE );\r
+ PushText( buf, false );\r
+}\r
+\r
+\r
+void XMLPrinter::PushText( unsigned value )\r
+{\r
+ char buf[BUF_SIZE];\r
+ XMLUtil::ToStr( value, buf, BUF_SIZE );\r
+ PushText( buf, false );\r
+}\r
+\r
+\r
+void XMLPrinter::PushText( bool value )\r
+{\r
+ char buf[BUF_SIZE];\r
+ XMLUtil::ToStr( value, buf, BUF_SIZE );\r
+ PushText( buf, false );\r
+}\r
+\r
+\r
+void XMLPrinter::PushText( float value )\r
+{\r
+ char buf[BUF_SIZE];\r
+ XMLUtil::ToStr( value, buf, BUF_SIZE );\r
+ PushText( buf, false );\r
+}\r
+\r
+\r
+void XMLPrinter::PushText( double value )\r
+{\r
+ char buf[BUF_SIZE];\r
+ XMLUtil::ToStr( value, buf, BUF_SIZE );\r
+ PushText( buf, false );\r
+}\r
+\r
+\r
+void XMLPrinter::PushComment( const char* comment )\r
+{\r
+ SealElementIfJustOpened();\r
+ if ( _textDepth < 0 && !_firstElement && !_compactMode) {\r
+ Print( "\n" );\r
+ PrintSpace( _depth );\r
+ }\r
+ _firstElement = false;\r
+ Print( "<!--%s-->", comment );\r
+}\r
+\r
+\r
+void XMLPrinter::PushDeclaration( const char* value )\r
+{\r
+ SealElementIfJustOpened();\r
+ if ( _textDepth < 0 && !_firstElement && !_compactMode) {\r
+ Print( "\n" );\r
+ PrintSpace( _depth );\r
+ }\r
+ _firstElement = false;\r
+ Print( "<?%s?>", value );\r
+}\r
+\r
+\r
+void XMLPrinter::PushUnknown( const char* value )\r
+{\r
+ SealElementIfJustOpened();\r
+ if ( _textDepth < 0 && !_firstElement && !_compactMode) {\r
+ Print( "\n" );\r
+ PrintSpace( _depth );\r
+ }\r
+ _firstElement = false;\r
+ Print( "<!%s>", value );\r
+}\r
+\r
+\r
+bool XMLPrinter::VisitEnter( const XMLDocument& doc )\r
+{\r
+ _processEntities = doc.ProcessEntities();\r
+ if ( doc.HasBOM() ) {\r
+ PushHeader( true, false );\r
+ }\r
+ return true;\r
+}\r
+\r
+\r
+bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )\r
+{\r
+ const XMLElement* parentElem = 0;\r
+ if ( element.Parent() ) {\r
+ parentElem = element.Parent()->ToElement();\r
+ }\r
+ const bool compactMode = parentElem ? CompactMode( *parentElem ) : _compactMode;\r
+ OpenElement( element.Name(), compactMode );\r
+ while ( attribute ) {\r
+ PushAttribute( attribute->Name(), attribute->Value() );\r
+ attribute = attribute->Next();\r
+ }\r
+ return true;\r
+}\r
+\r
+\r
+bool XMLPrinter::VisitExit( const XMLElement& element )\r
+{\r
+ CloseElement( CompactMode(element) );\r
+ return true;\r
+}\r
+\r
+\r
+bool XMLPrinter::Visit( const XMLText& text )\r
+{\r
+ PushText( text.Value(), text.CData() );\r
+ return true;\r
+}\r
+\r
+\r
+bool XMLPrinter::Visit( const XMLComment& comment )\r
+{\r
+ PushComment( comment.Value() );\r
+ return true;\r
+}\r
+\r
+bool XMLPrinter::Visit( const XMLDeclaration& declaration )\r
+{\r
+ PushDeclaration( declaration.Value() );\r
+ return true;\r
+}\r
+\r
+\r
+bool XMLPrinter::Visit( const XMLUnknown& unknown )\r
+{\r
+ PushUnknown( unknown.Value() );\r
+ return true;\r
+}\r
+\r
+} // namespace tinyxml2\r
+\r
--- /dev/null
+<?xml version="1.0"?>
+<World>
+ <Background>0</Background>
+ <BGM>assets/music/embark.wav</BGM>
+ <GenerationType>Random</GenerationType>
+ <GenerationWidth>500</GenerationsWidth>
+</World>