24 #ifndef TINYXML2_INCLUDED
25 #define TINYXML2_INCLUDED
27 #if defined(ANDROID_NDK) || defined(__BORLANDC__) || defined(__QNXNTO__)
52 #if defined( _DEBUG ) || defined( DEBUG ) || defined (__DEBUG__)
59 # pragma warning(push)
60 # pragma warning(disable: 4251)
64 # ifdef TINYXML2_EXPORT
65 # define TINYXML2_LIB __declspec(dllexport)
66 # elif defined(TINYXML2_IMPORT)
67 # define TINYXML2_LIB __declspec(dllimport)
77 # if defined(_MSC_VER)
78 # // "(void)0," is for suppressing C4127 warning in "assert(false)", "assert(true)" and the like
79 # define TIXMLASSERT( x ) if ( !((void)0,(x))) { __debugbreak(); }
80 # elif defined (ANDROID_NDK)
81 # include <android/log.h>
82 # define TIXMLASSERT( x ) if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); }
85 # define TIXMLASSERT assert
88 # define TIXMLASSERT( x ) {}
95 static const int TIXML2_MAJOR_VERSION = 3;
96 static const int TIXML2_MINOR_VERSION = 0;
97 static const int TIXML2_PATCH_VERSION = 0;
106 class XMLDeclaration;
132 StrPair() : _flags( 0 ), _start( 0 ), _end( 0 ) {}
135 void Set(
char* start,
char* end,
int flags ) {
139 _flags = flags | NEEDS_FLUSH;
145 return _start == _end;
150 _start =
const_cast<char*
>(str);
153 void SetStr(
const char* str,
int flags=0 );
155 char*
ParseText(
char* in,
const char* endTag,
int strFlags );
162 void CollapseWhitespace();
174 void operator=(
StrPair& other );
183 template <
class T,
int INITIAL_SIZE>
189 _allocated = INITIAL_SIZE;
194 if ( _mem != _pool ) {
205 EnsureCapacity( _size+1 );
212 EnsureCapacity( _size+count );
213 T* ret = &_mem[_size];
220 return _mem[--_size];
244 return _mem[ _size - 1];
271 void EnsureCapacity(
int cap ) {
273 if ( cap > _allocated ) {
275 int newAllocated = cap * 2;
276 T* newMem =
new T[newAllocated];
277 memcpy( newMem, _mem,
sizeof(T)*_size );
278 if ( _mem != _pool ) {
282 _allocated = newAllocated;
287 T _pool[INITIAL_SIZE];
304 virtual void*
Alloc() = 0;
305 virtual void Free(
void* ) = 0;
307 virtual void Clear() = 0;
318 MemPoolT() : _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {}
325 while( !_blockPtrs.
Empty()) {
326 Block* b = _blockPtrs.
Pop();
340 return _currentAllocs;
346 Block* block =
new Block();
347 _blockPtrs.
Push( block );
349 for(
int i=0; i<
COUNT-1; ++i ) {
350 block->chunk[i].next = &block->chunk[i+1];
352 block->chunk[COUNT-1].next = 0;
353 _root = block->chunk;
355 void* result = _root;
359 if ( _currentAllocs > _maxAllocs ) {
360 _maxAllocs = _currentAllocs;
367 virtual void Free(
void* mem ) {
372 Chunk* chunk =
static_cast<Chunk*
>( mem );
374 memset( chunk, 0xfe,
sizeof(Chunk) );
380 printf(
"Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
381 name, _maxAllocs, _maxAllocs*SIZE/1024, _currentAllocs, SIZE, _nAllocs, _blockPtrs.
Size() );
521 while( IsWhiteSpace(*p) ) {
528 return const_cast<char*
>( SkipWhiteSpace( const_cast<const char*>(p) ) );
534 return !IsUTF8Continuation(p) && isspace( static_cast<unsigned char>(p) );
542 if ( isalpha( ch ) ) {
545 return ch ==
':' || ch ==
'_';
549 return IsNameStartChar( ch )
555 inline static bool StringEqual(
const char* p,
const char* q,
int nChar=INT_MAX ) {
559 return strncmp( p, q, nChar ) == 0;
563 return ( p & 0x80 ) != 0;
566 static const char* ReadBOM(
const char* p,
bool* hasBOM );
569 static const char* GetCharacterRef(
const char* p,
char* value,
int* length );
570 static void ConvertUTF32ToUTF8(
unsigned long input,
char* output,
int* length );
573 static void ToStr(
int v,
char* buffer,
int bufferSize );
574 static void ToStr(
unsigned v,
char* buffer,
int bufferSize );
575 static void ToStr(
bool v,
char* buffer,
int bufferSize );
576 static void ToStr(
float v,
char* buffer,
int bufferSize );
577 static void ToStr(
double v,
char* buffer,
int bufferSize );
580 static bool ToInt(
const char* str,
int* value );
581 static bool ToUnsigned(
const char* str,
unsigned* value );
582 static bool ToBool(
const char* str,
bool* value );
583 static bool ToFloat(
const char* str,
float* value );
584 static bool ToDouble(
const char* str,
double* value );
683 const char* Value()
const;
688 void SetValue(
const char* val,
bool staticMem=
false );
716 const XMLElement* FirstChildElement(
const char* name = 0 )
const;
719 return const_cast<XMLElement*
>(
const_cast<const XMLNode*
>(
this)->FirstChildElement( name ));
734 const XMLElement* LastChildElement(
const char* name = 0 )
const;
737 return const_cast<XMLElement*
>(
const_cast<const XMLNode*
>(
this)->LastChildElement(name) );
750 const XMLElement* PreviousSiblingElement(
const char* name = 0 )
const ;
753 return const_cast<XMLElement*
>(
const_cast<const XMLNode*
>(
this)->PreviousSiblingElement( name ) );
766 const XMLElement* NextSiblingElement(
const char* name = 0 )
const;
769 return const_cast<XMLElement*
>(
const_cast<const XMLNode*
>(
this)->NextSiblingElement( name ) );
782 return InsertEndChild( addThis );
805 void DeleteChildren();
810 void DeleteChild(
XMLNode* node );
829 virtual bool ShallowEqual(
const XMLNode* compare )
const = 0;
853 virtual bool Accept(
XMLVisitor* visitor )
const = 0;
859 virtual char* ParseDeep(
char*,
StrPair* );
874 static void DeleteNode(
XMLNode* node );
875 void InsertChildPreamble(
XMLNode* insertThis )
const;
896 friend class XMLBase;
899 virtual bool Accept(
XMLVisitor* visitor )
const;
918 virtual bool ShallowEqual(
const XMLNode* compare )
const;
924 char* ParseDeep(
char*,
StrPair* endTag );
946 virtual bool Accept(
XMLVisitor* visitor )
const;
949 virtual bool ShallowEqual(
const XMLNode* compare )
const;
955 char* ParseDeep(
char*,
StrPair* endTag );
985 virtual bool Accept(
XMLVisitor* visitor )
const;
988 virtual bool ShallowEqual(
const XMLNode* compare )
const;
994 char* ParseDeep(
char*,
StrPair* endTag );
1020 virtual bool Accept(
XMLVisitor* visitor )
const;
1023 virtual bool ShallowEqual(
const XMLNode* compare )
const;
1029 char* ParseDeep(
char*,
StrPair* endTag );
1049 const char* Name()
const;
1052 const char* Value()
const;
1065 QueryIntValue( &i );
1071 QueryUnsignedValue( &i );
1077 QueryBoolValue( &b );
1083 QueryDoubleValue( &d );
1089 QueryFloatValue( &f );
1097 XMLError QueryIntValue(
int* value )
const;
1099 XMLError QueryUnsignedValue(
unsigned int* value )
const;
1101 XMLError QueryBoolValue(
bool* value )
const;
1103 XMLError QueryDoubleValue(
double* value )
const;
1105 XMLError QueryFloatValue(
float* value )
const;
1108 void SetAttribute(
const char* value );
1110 void SetAttribute(
int value );
1112 void SetAttribute(
unsigned value );
1114 void SetAttribute(
bool value );
1116 void SetAttribute(
double value );
1118 void SetAttribute(
float value );
1121 enum { BUF_SIZE = 200 };
1123 XMLAttribute() : _next( 0 ), _memPool( 0 ) {}
1124 virtual ~XMLAttribute() {}
1126 XMLAttribute(
const XMLAttribute& );
1127 void operator=(
const XMLAttribute& );
1128 void SetName(
const char* name );
1130 char* ParseDeep(
char* p,
bool processEntities );
1132 mutable StrPair _name;
1133 mutable StrPair _value;
1134 XMLAttribute* _next;
1145 friend class XMLBase;
1153 void SetName(
const char* str,
bool staticMem=
false ) {
1154 SetValue( str, staticMem );
1163 virtual bool Accept(
XMLVisitor* visitor )
const;
1188 const char* Attribute(
const char* name,
const char* value=0 )
const;
1197 QueryIntAttribute( name, &i );
1203 QueryUnsignedAttribute( name, &i );
1209 QueryBoolAttribute( name, &b );
1215 QueryDoubleAttribute( name, &d );
1221 QueryFloatAttribute( name, &f );
1297 return QueryIntAttribute( name, value );
1301 return QueryUnsignedAttribute( name, value );
1305 return QueryBoolAttribute( name, value );
1309 return QueryDoubleAttribute( name, value );
1313 return QueryFloatAttribute( name, value );
1350 void DeleteAttribute(
const char* name );
1354 return _rootAttribute;
1357 const XMLAttribute* FindAttribute(
const char* name )
const;
1387 const char* GetText()
const;
1423 void SetText(
const char* inText );
1425 void SetText(
int value );
1427 void SetText(
unsigned value );
1429 void SetText(
bool value );
1431 void SetText(
double value );
1433 void SetText(
float value );
1461 XMLError QueryIntText(
int* ival )
const;
1463 XMLError QueryUnsignedText(
unsigned* uval )
const;
1465 XMLError QueryBoolText(
bool* bval )
const;
1467 XMLError QueryDoubleText(
double* dval )
const;
1469 XMLError QueryFloatText(
float* fval )
const;
1478 return _closingType;
1481 virtual bool ShallowEqual(
const XMLNode* compare )
const;
1484 char* ParseDeep(
char* p,
StrPair* endTag );
1495 XMLAttribute* FindOrCreateAttribute(
const char* name );
1497 char* ParseAttributes(
char* p );
1498 static void DeleteAttribute( XMLAttribute* attribute );
1500 enum { BUF_SIZE = 200 };
1505 XMLAttribute* _rootAttribute;
1547 XMLError Parse(
const char* xml,
size_t nBytes=(
size_t)(-1) );
1554 XMLError LoadFile(
const char* filename );
1574 XMLError SaveFile(
const char* filename,
bool compact =
false );
1583 XMLError SaveFile( FILE* fp,
bool compact =
false );
1586 return _processEntities;
1608 return FirstChildElement();
1611 return FirstChildElement();
1629 virtual bool Accept(
XMLVisitor* visitor )
const;
1642 XMLComment* NewComment(
const char* comment );
1648 XMLText* NewText(
const char* text );
1672 void DeleteNode(
XMLNode* node );
1674 void SetError(
XMLError error,
const char* str1,
const char* str2 );
1684 const char* ErrorName()
const;
1695 void PrintError()
const;
1701 char* Identify(
char* p,
XMLNode** node );
1715 bool _processEntities;
1718 const char* _errorStr1;
1719 const char* _errorStr2;
1811 return XMLHandle( _node ? _node->FirstChild() : 0 );
1815 return XMLHandle( _node ? _node->FirstChildElement( name ) : 0 );
1819 return XMLHandle( _node ? _node->LastChild() : 0 );
1823 return XMLHandle( _node ? _node->LastChildElement( name ) : 0 );
1827 return XMLHandle( _node ? _node->PreviousSibling() : 0 );
1831 return XMLHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
1835 return XMLHandle( _node ? _node->NextSibling() : 0 );
1839 return XMLHandle( _node ? _node->NextSiblingElement( name ) : 0 );
1848 return ( ( _node == 0 ) ? 0 : _node->ToElement() );
1852 return ( ( _node == 0 ) ? 0 : _node->ToText() );
1856 return ( ( _node == 0 ) ? 0 : _node->ToUnknown() );
1860 return ( ( _node == 0 ) ? 0 : _node->ToDeclaration() );
1894 return XMLConstHandle( _node ? _node->FirstChildElement( name ) : 0 );
1900 return XMLConstHandle( _node ? _node->LastChildElement( name ) : 0 );
1906 return XMLConstHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
1912 return XMLConstHandle( _node ? _node->NextSiblingElement( name ) : 0 );
1920 return ( ( _node == 0 ) ? 0 : _node->ToElement() );
1923 return ( ( _node == 0 ) ? 0 : _node->ToText() );
1926 return ( ( _node == 0 ) ? 0 : _node->ToUnknown() );
1929 return ( ( _node == 0 ) ? 0 : _node->ToDeclaration() );
1988 XMLPrinter( FILE* file=0,
bool compact =
false,
int depth = 0 );
1992 void PushHeader(
bool writeBOM,
bool writeDeclaration );
1996 void OpenElement(
const char* name,
bool compactMode=
false );
1998 void PushAttribute(
const char* name,
const char* value );
1999 void PushAttribute(
const char* name,
int value );
2000 void PushAttribute(
const char* name,
unsigned value );
2001 void PushAttribute(
const char* name,
bool value );
2002 void PushAttribute(
const char* name,
double value );
2004 virtual void CloseElement(
bool compactMode=
false );
2007 void PushText(
const char* text,
bool cdata=
false );
2009 void PushText(
int value );
2011 void PushText(
unsigned value );
2013 void PushText(
bool value );
2015 void PushText(
float value );
2017 void PushText(
double value );
2020 void PushComment(
const char* comment );
2022 void PushDeclaration(
const char* value );
2023 void PushUnknown(
const char* value );
2031 virtual bool VisitExit(
const XMLElement& element );
2033 virtual bool Visit(
const XMLText& text );
2034 virtual bool Visit(
const XMLComment& comment );
2036 virtual bool Visit(
const XMLUnknown& unknown );
2043 return _buffer.Mem();
2051 return _buffer.Size();
2068 virtual void PrintSpace(
int depth );
2069 void Print(
const char* format, ... );
2071 void SealElementIfJustOpened();
2076 void PrintString(
const char*,
bool restrictedEntitySet );
2082 bool _processEntities;
2089 bool _entityFlag[ENTITY_RANGE];
2090 bool _restrictedEntityFlag[ENTITY_RANGE];
2098 #if defined(_MSC_VER)
2099 # pragma warning(pop)
2102 #endif // TINYXML2_INCLUDED
int QueryAttribute(const char *name, float *value) const
bool CData() const
Returns true if this is a CDATA text element.
const XMLNode * Parent() const
Get the parent of this node on the DOM.
virtual bool VisitExit(const XMLDocument &)
Visit a document.
static bool IsUTF8Continuation(char p)
void SetInternedStr(const char *str)
const XMLConstHandle LastChildElement(const char *name=0) const
XMLHandle FirstChildElement(const char *name=0)
Get the first child element of this handle.
virtual bool ShallowEqual(const XMLNode *) const
XMLText * ToText()
Safe cast to XMLText. This can return null.
XMLText(XMLDocument *doc)
XMLElement * PreviousSiblingElement(const char *name=0)
virtual void Free(void *)=0
XMLError QueryBoolAttribute(const char *name, bool *value) const
See QueryIntAttribute()
const XMLConstHandle NextSiblingElement(const char *name=0) const
XMLElement * ToElement()
Safe cast to XMLElement. This can return null.
virtual bool CompactMode(const XMLElement &)
virtual XMLElement * ToElement()
Safely cast to an Element, or null.
double DoubleValue() const
Query as a double. See IntValue()
virtual XMLText * ToText()
Safely cast to Text, or null.
bool NoChildren() const
Returns true if this node has no children.
const XMLConstHandle NextSibling() const
const XMLUnknown * ToUnknown() const
int QueryAttribute(const char *name, unsigned int *value) const
XMLNode * LinkEndChild(XMLNode *addThis)
XMLError ErrorID() const
Return the errorID.
const XMLElement * ToElement() const
virtual XMLDocument * ToDocument()
Safely cast to a Document, or null.
virtual const XMLElement * ToElement() const
const XMLNode * LastChild() const
Get the last child node, or null if none exists.
XMLUnknown * ToUnknown()
Safe cast to XMLUnknown. This can return null.
int QueryAttribute(const char *name, bool *value) const
XMLHandle(const XMLHandle &ref)
Copy constructor.
XMLHandle FirstChild()
Get the first child of this handle.
void SetCData(bool isCData)
Declare whether this should be CDATA or standard text.
static char * SkipWhiteSpace(char *p)
const XMLNode * ToNode() const
XMLHandle LastChildElement(const char *name=0)
Get the last child element of this handle.
int QueryAttribute(const char *name, double *value) const
XMLHandle LastChild()
Get the last child of this handle.
static bool StringEqual(const char *p, const char *q, int nChar=INT_MAX)
virtual void Free(void *mem)
XMLError QueryUnsignedAttribute(const char *name, unsigned int *value) const
See QueryIntAttribute()
XMLElement * RootElement()
int CurrentAllocs() const
virtual void SetTracked()=0
const char * Name() const
Get the name of an element (which is the Value() of the node.)
virtual XMLText * ToText()
Safely cast to Text, or null.
XMLHandle(XMLNode &node)
Create a handle from a node.
void SetName(const char *str, bool staticMem=false)
Set the name of the element.
XMLHandle(XMLNode *node)
Create a handle from any node (at any depth of the tree.) This can be a null pointer.
static const char * SkipWhiteSpace(const char *p)
char * ParseText(char *in, const char *endTag, int strFlags)
XMLError QueryIntValue(int *value) const
double DoubleAttribute(const char *name) const
See IntAttribute()
int QueryAttribute(const char *name, int *value) const
virtual XMLElement * ToElement()
Safely cast to an Element, or null.
static bool IsNameChar(unsigned char ch)
bool ProcessEntities() const
virtual int ItemSize() const
XMLNode * ToNode()
Safe cast to XMLNode. This can return null.
unsigned UnsignedAttribute(const char *name) const
See IntAttribute()
const T & PeekTop() const
virtual int ItemSize() const =0
const XMLDeclaration * ToDeclaration() const
bool BoolAttribute(const char *name) const
See IntAttribute()
bool BoolValue() const
Query as a boolean. See IntValue()
virtual XMLDeclaration * ToDeclaration()
Safely cast to a Declaration, or null.
XMLError QueryFloatAttribute(const char *name, float *value) const
See QueryIntAttribute()
virtual bool Visit(const XMLDeclaration &)
Visit a declaration.
virtual const XMLUnknown * ToUnknown() const
virtual bool Visit(const XMLUnknown &)
Visit an unknown node.
DynArray< const char *, 10 > _stack
void SetAttribute(const char *name, unsigned value)
Sets the named attribute to value.
XMLHandle NextSibling()
Get the next sibling of this handle.
virtual const XMLDocument * ToDocument() const
XMLError QueryUnsignedValue(unsigned int *value) const
See QueryIntValue.
void Set(char *start, char *end, int flags)
virtual XMLUnknown * ToUnknown()
Safely cast to an Unknown, or null.
XMLError QueryFloatValue(float *value) const
See QueryIntValue.
XMLHandle PreviousSibling()
Get the previous sibling of this handle.
XMLHandle & operator=(const XMLHandle &ref)
Assignment.
const XMLConstHandle PreviousSibling() const
XMLConstHandle(const XMLNode &node)
virtual bool VisitExit(const XMLDocument &)
Visit a document.
char * ParseName(char *in)
virtual bool VisitEnter(const XMLElement &, const XMLAttribute *)
Visit an element.
const XMLDocument * GetDocument() const
Get the XMLDocument that owns this XMLNode.
virtual XMLNode * ShallowClone(XMLDocument *) const
const XMLConstHandle FirstChild() const
virtual bool VisitEnter(const XMLDocument &)
Visit a document.
virtual const XMLUnknown * ToUnknown() const
virtual const XMLDeclaration * ToDeclaration() const
const XMLNode * NextSibling() const
Get the next (right) sibling node of this node.
virtual XMLDocument * ToDocument()
Safely cast to a Document, or null.
XMLElement * LastChildElement(const char *name=0)
float FloatAttribute(const char *name) const
See IntAttribute()
const char * GetErrorStr2() const
Return a possibly helpful secondary diagnostic location or string.
void SetAttribute(const char *name, bool value)
Sets the named attribute to value.
const XMLConstHandle PreviousSiblingElement(const char *name=0) const
const XMLConstHandle LastChild() const
XMLDeclaration * ToDeclaration()
Safe cast to XMLDeclaration. This can return null.
unsigned UnsignedValue() const
Query as an unsigned integer. See IntValue()
void SetAttribute(const char *value)
Set the attribute to a string value.
void SetAttribute(const char *name, const char *value)
Sets the named attribute to value.
static bool IsWhiteSpace(char p)
virtual bool VisitExit(const XMLElement &)
Visit an element.
const char * CStr() const
XMLError QueryIntAttribute(const char *name, int *value) const
const XMLElement * RootElement() const
void SetAttribute(const char *name, double value)
Sets the named attribute to value.
void TransferTo(StrPair *other)
virtual bool Visit(const XMLComment &)
Visit a comment node.
const XMLAttribute * Next() const
The next attribute in the list.
static bool IsNameStartChar(unsigned char ch)
const char * GetErrorStr1() const
Return a possibly helpful diagnostic location or string.
void Trace(const char *name)
virtual const XMLComment * ToComment() const
XMLError QueryDoubleValue(double *value) const
See QueryIntValue.
XMLHandle NextSiblingElement(const char *name=0)
Get the next sibling element of this handle.
XMLConstHandle(const XMLConstHandle &ref)
Whitespace WhitespaceMode() const
bool Error() const
Return true if there was an error parsing the document.
const XMLText * ToText() const
XMLElement * NextSiblingElement(const char *name=0)
const XMLNode * PreviousSibling() const
Get the previous (left) sibling node of this node.
float FloatValue() const
Query as a float. See IntValue()
XMLElement * FirstChildElement(const char *name=0)
XMLError QueryBoolValue(bool *value) const
See QueryIntValue.
XMLConstHandle(const XMLNode *node)
const T & operator[](int i) const
virtual const XMLElement * ToElement() const
virtual bool Visit(const XMLText &)
Visit a text node.
const XMLConstHandle FirstChildElement(const char *name=0) const
virtual const XMLDocument * ToDocument() const
XMLDocument * GetDocument()
Get the XMLDocument that owns this XMLNode.
virtual XMLUnknown * ToUnknown()
Safely cast to an Unknown, or null.
XMLError QueryDoubleAttribute(const char *name, double *value) const
See QueryIntAttribute()
XMLConstHandle & operator=(const XMLConstHandle &ref)
void SetAttribute(const char *name, float value)
Sets the named attribute to value.
const XMLNode * FirstChild() const
Get the first child node, or null if none exists.
virtual const XMLText * ToText() const
virtual XMLDeclaration * ToDeclaration()
Safely cast to a Declaration, or null.
const XMLAttribute * FirstAttribute() const
Return the first attribute in the list.
virtual const XMLText * ToText() const
void SetStr(const char *str, int flags=0)
int IntAttribute(const char *name) const
XMLHandle PreviousSiblingElement(const char *name=0)
Get the previous sibling element of this handle.
XMLNode * PreviousSibling()
void SetAttribute(const char *name, int value)
Sets the named attribute to value.
virtual const XMLDeclaration * ToDeclaration() const
virtual XMLComment * ToComment()
Safely cast to a Comment, or null.