24#ifndef TINYXML2_INCLUDED
25#define TINYXML2_INCLUDED
27#include <scolPlugin.h>
48#if defined( _DEBUG ) || defined( DEBUG ) || defined (__DEBUG__)
57# define TIXMLASSERT( x ) if ( !(x)) { __debugbreak(); }
58# elif defined (ANDROID_NDK)
59# include <android/log.h>
60# define TIXMLASSERT( x ) if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); }
63# define TIXMLASSERT assert
66# define TIXMLASSERT( x ) {}
70#if defined(_MSC_VER) && (_MSC_VER >= 1400 )
79inline int TIXML_SNPRINTF(
char* buffer,
size_t size,
const char* format, ... )
82 va_start( va, format );
83 int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va );
87#define TIXML_SSCANF sscanf_s
91#define TIXML_SNPRINTF snprintf
92#define TIXML_SSCANF sscanf
95static const int TIXML2_MAJOR_VERSION = 1;
96static const int TIXML2_MINOR_VERSION = 0;
97static const int TIXML2_PATCH_VERSION = 9;
122 NEEDS_ENTITY_PROCESSING = 0x01,
123 NEEDS_NEWLINE_NORMALIZATION = 0x02,
124 COLLAPSE_WHITESPACE = 0x04,
126 TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
127 TEXT_ELEMENT_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
129 ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
130 ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
131 COMMENT = NEEDS_NEWLINE_NORMALIZATION
134 StrPair() : _flags( 0 ), _start( 0 ), _end( 0 ) {}
137 void Set(
char* start,
char* end,
int flags ) {
141 _flags = flags | NEEDS_FLUSH;
144 const char* GetStr();
147 return _start == _end;
150 void SetInternedStr(
const char* str ) {
152 _start =
const_cast<char*
>(str);
155 void SetStr(
const char* str,
int flags=0 );
157 char* ParseText(
char* in,
const char* endTag,
int strFlags );
158 char* ParseName(
char* in );
162 void CollapseWhitespace();
181template <
class T,
int INIT>
192 if ( _mem != _pool ) {
198 EnsureCapacity( _size+1 );
203 EnsureCapacity( _size+
count );
204 T*
ret = &_mem[_size];
210 return _mem[--_size];
213 void PopArr(
int count ) {
214 TIXMLASSERT( _size >=
count );
222 T& operator[](
int i) {
223 TIXMLASSERT(
i>= 0 &&
i < _size );
227 const T& operator[](
int i)
const {
228 TIXMLASSERT(
i>= 0 &&
i < _size );
236 int Capacity()
const {
240 const T* Mem()
const {
249 void EnsureCapacity(
int cap ) {
250 if (
cap > _allocated ) {
254 if ( _mem != _pool ) {
279 virtual int ItemSize()
const = 0;
280 virtual void* Alloc() = 0;
281 virtual void Free(
void* ) = 0;
292 MemPoolT() : _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0) {}
295 for(
int i=0;
i<_blockPtrs.Size(); ++
i ) {
296 delete _blockPtrs[
i];
300 virtual int ItemSize()
const {
303 int CurrentAllocs()
const {
304 return _currentAllocs;
307 virtual void* Alloc() {
310 Block*
block =
new Block();
311 _blockPtrs.Push(
block );
313 for(
int i=0;
i<COUNT-1; ++
i ) {
316 block->chunk[COUNT-1].next = 0;
317 _root =
block->chunk;
323 if ( _currentAllocs > _maxAllocs ) {
324 _maxAllocs = _currentAllocs;
329 virtual void Free(
void* mem ) {
334 Chunk* chunk = (Chunk*)mem;
336 memset( chunk, 0xfe,
sizeof(Chunk) );
341 void Trace(
const char*
name ) {
342 printf(
"Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
343 name, _maxAllocs, _maxAllocs*
SIZE/1024, _currentAllocs,
SIZE, _nAllocs, _blockPtrs.Size() );
346 enum { COUNT = 1024/
SIZE };
435 static const char* SkipWhiteSpace(
const char*
p ) {
436 while( !IsUTF8Continuation(*
p) && std::isspace( *
reinterpret_cast<const unsigned char*
>(
p) ) ) {
441 static char* SkipWhiteSpace(
char*
p ) {
442 while( !IsUTF8Continuation(*
p) && std::isspace( *
reinterpret_cast<unsigned char*
>(
p) ) ) {
447 static bool IsWhiteSpace(
char p ) {
448 return !IsUTF8Continuation(
p) && std::isspace(
static_cast<unsigned char>(
p) );
451 inline static bool StringEqual(
const char*
p,
const char*
q,
int nChar=
INT_MAX ) {
461 if ( (
n ==
nChar) || ( *
p == 0 && *
q == 0 ) ) {
466 inline static int IsUTF8Continuation(
const char p ) {
469 inline static int IsAlphaNum(
unsigned char anyByte ) {
472 inline static int IsAlpha(
unsigned char anyByte ) {
476 static const char* ReadBOM(
const char*
p,
bool*
hasBOM );
479 static const char* GetCharacterRef(
const char*
p,
char* value,
int* length );
480 static void ConvertUTF32ToUTF8(
unsigned long input,
char*
output,
int* length );
490 static bool ToInt(
const char*
str,
int* value );
491 static bool ToUnsigned(
const char*
str,
unsigned* value );
492 static bool ToBool(
const char*
str,
bool* value );
493 static bool ToFloat(
const char*
str,
float* value );
494 static bool ToDouble(
const char*
str,
double* value );
566 virtual const XMLText*
ToText()
const {
569 virtual const XMLComment*
ToComment()
const {
572 virtual const XMLDocument*
ToDocument()
const {
578 virtual const XMLUnknown*
ToUnknown()
const {
592 return _value.GetStr();
629 return const_cast<XMLElement*
>(
const_cast<const XMLNode*
>(
this)->
FirstChildElement( value ));
647 return const_cast<XMLElement*
>(
const_cast<const XMLNode*
>(
this)->
LastChildElement(value) );
679 return const_cast<XMLElement*
>(
const_cast<const XMLNode*
>(
this)->
NextSiblingElement( value ) );
687 XMLNode* LinkEndChild( XMLNode* addThis ) {
753 virtual char* ParseDeep(
char*,
StrPair* );
791 friend class XMLBase;
818 virtual ~XMLText() {}
819 XMLText(
const XMLText& );
820 XMLText& operator=(
const XMLText& );
839 virtual bool Accept( XMLVisitor* visitor )
const;
841 char* ParseDeep(
char*, StrPair* endTag );
842 virtual XMLNode*
ShallowClone( XMLDocument* document )
const;
843 virtual bool ShallowEqual(
const XMLNode* compare )
const;
846 XMLComment( XMLDocument* doc );
847 virtual ~XMLComment();
848 XMLComment(
const XMLComment& );
849 XMLComment& operator=(
const XMLComment& );
877 virtual bool Accept( XMLVisitor* visitor )
const;
879 char* ParseDeep(
char*, StrPair* endTag );
880 virtual XMLNode*
ShallowClone( XMLDocument* document )
const;
881 virtual bool ShallowEqual(
const XMLNode* compare )
const;
884 XMLDeclaration( XMLDocument* doc );
885 virtual ~XMLDeclaration();
886 XMLDeclaration(
const XMLDeclaration& );
887 XMLDeclaration& operator=(
const XMLDeclaration& );
909 virtual bool Accept( XMLVisitor* visitor )
const;
911 char* ParseDeep(
char*, StrPair* endTag );
912 virtual XMLNode*
ShallowClone( XMLDocument* document )
const;
913 virtual bool ShallowEqual(
const XMLNode* compare )
const;
916 XMLUnknown( XMLDocument* doc );
917 virtual ~XMLUnknown();
918 XMLUnknown(
const XMLUnknown& );
919 XMLUnknown& operator=(
const XMLUnknown& );
928 XML_WRONG_ATTRIBUTE_TYPE,
930 XML_ERROR_FILE_NOT_FOUND,
931 XML_ERROR_FILE_COULD_NOT_BE_OPENED,
932 XML_ERROR_FILE_READ_ERROR,
933 XML_ERROR_ELEMENT_MISMATCH,
934 XML_ERROR_PARSING_ELEMENT,
935 XML_ERROR_PARSING_ATTRIBUTE,
936 XML_ERROR_IDENTIFYING_TAG,
937 XML_ERROR_PARSING_TEXT,
938 XML_ERROR_PARSING_CDATA,
939 XML_ERROR_PARSING_COMMENT,
940 XML_ERROR_PARSING_DECLARATION,
941 XML_ERROR_PARSING_UNKNOWN,
942 XML_ERROR_EMPTY_DOCUMENT,
943 XML_ERROR_MISMATCHED_ELEMENT,
946 XML_CAN_NOT_CONVERT_TEXT,
963 return _name.GetStr();
967 return _value.GetStr();
1036 enum { BUF_SIZE = 200 };
1038 XMLAttribute() : _next( 0 ) {}
1039 virtual ~XMLAttribute() {}
1041 XMLAttribute(
const XMLAttribute& );
1042 void operator=(
const XMLAttribute& );
1043 void SetName(
const char* name );
1045 char* ParseDeep(
char* p,
bool processEntities );
1047 mutable StrPair _name;
1048 mutable StrPair _value;
1049 XMLAttribute* _next;
1060 friend class XMLBase;
1078 virtual bool Accept( XMLVisitor* visitor )
const;
1103 const char*
Attribute(
const char* name,
const char* value=0 )
const;
1156 return XML_NO_ATTRIBUTE;
1158 return a->QueryIntValue( value );
1164 return XML_NO_ATTRIBUTE;
1166 return a->QueryUnsignedValue( value );
1172 return XML_NO_ATTRIBUTE;
1174 return a->QueryBoolValue( value );
1180 return XML_NO_ATTRIBUTE;
1182 return a->QueryDoubleValue( value );
1188 return XML_NO_ATTRIBUTE;
1190 return a->QueryFloatValue( value );
1196 a->SetAttribute( value );
1201 a->SetAttribute( value );
1206 a->SetAttribute( value );
1211 a->SetAttribute( value );
1216 a->SetAttribute( value );
1226 return _rootAttribute;
1303 int ClosingType()
const {
1304 return _closingType;
1306 char* ParseDeep(
char* p, StrPair* endTag );
1307 virtual XMLNode*
ShallowClone( XMLDocument* document )
const;
1308 virtual bool ShallowEqual(
const XMLNode* compare )
const;
1311 XMLElement( XMLDocument* doc );
1312 virtual ~XMLElement();
1313 XMLElement(
const XMLElement& );
1314 void operator=(
const XMLElement& );
1317 XMLAttribute* FindOrCreateAttribute(
const char* name );
1319 char* ParseAttributes(
char* p );
1325 XMLAttribute* _rootAttribute;
1330 PRESERVE_WHITESPACE,
1365 XMLError
Parse(
const char* xml,
size_t nBytes=(
size_t)(-1) );
1372 XMLError
LoadFile(
const char* filename );
1399 bool ProcessEntities()
const {
1400 return _processEntities;
1402 Whitespace WhitespaceMode()
const {
1442 void Print( XMLPrinter* streamer=0 );
1443 virtual bool Accept( XMLVisitor* visitor )
const;
1456 XMLComment*
NewComment(
const char* comment );
1462 XMLText*
NewText(
const char* text );
1487 node->_parent->DeleteChild(
node );
1490 void SetError( XMLError
error,
const char*
str1,
const char*
str2 );
1494 return _errorID != XML_NO_ERROR;
1524 void InitDocument();
1527 bool _processEntities;
1529 Whitespace _whitespace;
1530 const char* _errorStr1;
1531 const char* _errorStr2;
1660 return ( ( _node && _node->
ToText() ) ? _node->
ToText() : 0 );
1701 const XMLConstHandle FirstChildElement(
const char* value=0 )
const {
1702 return XMLConstHandle( _node ? _node->FirstChildElement( value ) : 0 );
1707 const XMLConstHandle LastChildElement(
const char* _value=0 )
const {
1708 return XMLConstHandle( _node ? _node->LastChildElement( _value ) : 0 );
1713 const XMLConstHandle PreviousSiblingElement(
const char* _value=0 )
const {
1714 return XMLConstHandle( _node ? _node->PreviousSiblingElement( _value ) : 0 );
1719 const XMLConstHandle NextSiblingElement(
const char* _value=0 )
const {
1720 return XMLConstHandle( _node ? _node->NextSiblingElement( _value ) : 0 );
1724 const XMLNode* ToNode()
const {
1728 return ( ( _node && _node->ToElement() ) ? _node->ToElement() : 0 );
1730 const XMLText* ToText()
const {
1731 return ( ( _node && _node->ToText() ) ? _node->ToText() : 0 );
1734 return ( ( _node && _node->ToUnknown() ) ? _node->ToUnknown() : 0 );
1737 return ( ( _node && _node->ToDeclaration() ) ? _node->ToDeclaration() : 0 );
1830 void PushDeclaration(
const char* value );
1831 void PushUnknown(
const char* value );
1851 return _buffer.Mem();
1859 return _buffer.Size();
1864 void PrintSpace(
int depth );
1866 void Print(
const char*
format, ... );
1868 bool _elementJustOpened;
1873 bool _processEntities;
1880 bool _entityFlag[ENTITY_RANGE];
1881 bool _restrictedEntityFlag[ENTITY_RANGE];
1883 DynArray< const char*, 10 > _stack;
XMLError QueryFloatValue(float *value) const
See QueryIntAttribute.
unsigned UnsignedValue() const
Query as an unsigned integer. See IntAttribute()
float FloatValue() const
Query as a float. See IntAttribute()
XMLError QueryDoubleValue(double *value) const
See QueryIntAttribute.
void SetAttribute(const char *value)
Set the attribute to a string value.
XMLError QueryUnsignedValue(unsigned int *value) const
See QueryIntAttribute.
double DoubleValue() const
Query as a double. See IntAttribute()
const char * Name() const
The name of the attribute.
XMLError QueryBoolValue(bool *value) const
See QueryIntAttribute.
XMLError QueryIntValue(int *value) const
bool BoolValue() const
Query as a boolean. See IntAttribute()
const XMLAttribute * Next() const
The next attribute in the list.
const char * Value() const
The value of the attribute.
virtual XMLDeclaration * ToDeclaration()
Safely cast to a Declaration, or null.
virtual bool Accept(XMLVisitor *visitor) const
virtual XMLNode * ShallowClone(XMLDocument *document) const
virtual bool ShallowEqual(const XMLNode *compare) const
XMLElement * RootElement()
XMLError Parse(const char *xml, size_t nBytes=(size_t)(-1))
void PrintError() const
If there is an error, print it to stdout.
XMLError LoadFile(std::FILE *)
void Print(XMLPrinter *streamer=0)
XMLError LoadFile(const char *filename)
bool Error() const
Return true if there was an error parsing the document.
XMLComment * NewComment(const char *comment)
XMLElement * NewElement(const char *name)
XMLUnknown * NewUnknown(const char *text)
XMLError SaveFile(std::FILE *fp, bool compact=false)
virtual bool ShallowEqual(const XMLNode *) const
XMLError SaveFile(const char *filename, bool compact=false)
virtual XMLDocument * ToDocument()
Safely cast to a Document, or null.
const char * GetErrorStr1() const
Return a possibly helpful diagnostic location or string.
virtual bool Accept(XMLVisitor *visitor) const
void DeleteNode(XMLNode *node)
const char * GetErrorStr2() const
Return a possibly helpful secondary diagnostic location or string.
XMLText * NewText(const char *text)
XMLDeclaration * NewDeclaration(const char *text=0)
virtual XMLNode * ShallowClone(XMLDocument *) const
XMLError ErrorID() const
Return the errorID.
bool BoolAttribute(const char *name) const
See IntAttribute()
XMLError QueryDoubleText(double *_value) const
See QueryIntText()
const char * GetText() const
void SetAttribute(const char *name, const char *value)
Sets the named attribute to value.
XMLError QueryBoolAttribute(const char *name, bool *value) const
See QueryIntAttribute()
const XMLAttribute * FindAttribute(const char *name) const
Query a specific attribute in the list.
XMLError QueryUnsignedText(unsigned *_value) const
See QueryIntText()
void SetAttribute(const char *name, double value)
Sets the named attribute to value.
XMLError QueryUnsignedAttribute(const char *name, unsigned int *value) const
See QueryIntAttribute()
unsigned UnsignedAttribute(const char *name) const
See IntAttribute()
XMLError QueryFloatText(float *_value) const
See QueryIntText()
const char * Attribute(const char *name, const char *value=0) const
const XMLAttribute * FirstAttribute() const
Return the first attribute in the list.
XMLError QueryIntText(int *_value) const
XMLError QueryDoubleAttribute(const char *name, double *value) const
See QueryIntAttribute()
virtual bool ShallowEqual(const XMLNode *compare) const
float FloatAttribute(const char *name) const
See IntAttribute()
double DoubleAttribute(const char *name) const
See IntAttribute()
XMLError QueryIntAttribute(const char *name, int *value) const
void SetName(const char *str, bool staticMem=false)
Set the name of the element.
virtual bool Accept(XMLVisitor *visitor) const
XMLError QueryBoolText(bool *_value) const
See QueryIntText()
void SetAttribute(const char *name, bool value)
Sets the named attribute to value.
void SetAttribute(const char *name, int value)
Sets the named attribute to value.
virtual XMLNode * ShallowClone(XMLDocument *document) const
virtual XMLElement * ToElement()
Safely cast to an Element, or null.
const char * Name() const
Get the name of an element (which is the Value() of the node.)
XMLError QueryFloatAttribute(const char *name, float *value) const
See QueryIntAttribute()
int IntAttribute(const char *name) const
void SetAttribute(const char *name, unsigned value)
Sets the named attribute to value.
void DeleteAttribute(const char *name)
XMLHandle PreviousSiblingElement(const char *_value=0)
Get the previous sibling element of this handle.
XMLHandle LastChildElement(const char *_value=0)
Get the last child element of this handle.
XMLHandle PreviousSibling()
Get the previous sibling of this handle.
XMLHandle NextSiblingElement(const char *_value=0)
Get the next sibling element of this handle.
XMLHandle FirstChild()
Get the first child of this handle.
XMLNode * ToNode()
Safe cast to XMLNode. This can return null.
XMLDeclaration * ToDeclaration()
Safe cast to XMLDeclaration. This can return null.
XMLHandle FirstChildElement(const char *value=0)
Get the first child element of this handle.
XMLHandle(XMLNode *node)
Create a handle from any node (at any depth of the tree.) This can be a null pointer.
XMLHandle LastChild()
Get the last child of this handle.
XMLHandle & operator=(const XMLHandle &ref)
Assignment.
XMLHandle(XMLNode &node)
Create a handle from a node.
XMLHandle NextSibling()
Get the next sibling of this handle.
XMLElement * ToElement()
Safe cast to XMLElement. This can return null.
XMLText * ToText()
Safe cast to XMLText. This can return null.
XMLUnknown * ToUnknown()
Safe cast to XMLUnknown. This can return null.
XMLHandle(const XMLHandle &ref)
Copy constructor.
const char * Value() const
void SetValue(const char *val, bool staticMem=false)
virtual XMLText * ToText()
Safely cast to Text, or null.
const XMLElement * LastChildElement(const char *value=0) const
virtual XMLDeclaration * ToDeclaration()
Safely cast to a Declaration, or null.
void DeleteChild(XMLNode *node)
const XMLElement * NextSiblingElement(const char *value=0) const
Get the next (right) sibling element of this node, with an opitionally supplied name.
XMLDocument * GetDocument()
Get the XMLDocument that owns this XMLNode.
const XMLNode * Parent() const
Get the parent of this node on the DOM.
const XMLElement * FirstChildElement(const char *value=0) const
virtual XMLComment * ToComment()
Safely cast to a Comment, or null.
virtual XMLDocument * ToDocument()
Safely cast to a Document, or null.
const XMLNode * LastChild() const
Get the last child node, or null if none exists.
const XMLDocument * GetDocument() const
Get the XMLDocument that owns this XMLNode.
virtual bool ShallowEqual(const XMLNode *compare) const =0
virtual bool Accept(XMLVisitor *visitor) const =0
virtual XMLNode * ShallowClone(XMLDocument *document) const =0
XMLNode * InsertAfterChild(XMLNode *afterThis, XMLNode *addThis)
const XMLNode * PreviousSibling() const
Get the previous (left) sibling node of this node.
virtual XMLElement * ToElement()
Safely cast to an Element, or null.
virtual XMLUnknown * ToUnknown()
Safely cast to an Unknown, or null.
const XMLElement * PreviousSiblingElement(const char *value=0) const
Get the previous (left) sibling element of this node, with an opitionally supplied name.
const XMLNode * FirstChild() const
Get the first child node, or null if none exists.
bool NoChildren() const
Returns true if this node has no children.
XMLNode * InsertFirstChild(XMLNode *addThis)
XMLNode * InsertEndChild(XMLNode *addThis)
const XMLNode * NextSibling() const
Get the next (right) sibling node of this node.
virtual bool VisitExit(const XMLDocument &)
Visit a document.
void PushHeader(bool writeBOM, bool writeDeclaration)
void PushText(const char *text, bool cdata=false)
Add a text node.
void PushAttribute(const char *name, const char *value)
If streaming, add an attribute to an open element.
virtual bool VisitEnter(const XMLDocument &)
Visit a document.
void OpenElement(const char *name)
const char * CStr() const
virtual bool Visit(const XMLText &text)
Visit a text node.
void CloseElement()
If streaming, close the Element.
void PushComment(const char *comment)
Add a comment.
virtual bool Accept(XMLVisitor *visitor) const
virtual XMLNode * ShallowClone(XMLDocument *document) const
virtual bool ShallowEqual(const XMLNode *compare) const
virtual XMLText * ToText()
Safely cast to Text, or null.
bool CData() const
Returns true if this is a CDATA text element.
void SetCData(bool isCData)
Declare whether this should be CDATA or standard text.
virtual XMLUnknown * ToUnknown()
Safely cast to an Unknown, or null.
virtual bool Accept(XMLVisitor *visitor) const
virtual XMLNode * ShallowClone(XMLDocument *document) const
virtual bool ShallowEqual(const XMLNode *compare) const
virtual bool Visit(const XMLUnknown &)
Visit an unknown node.
virtual bool VisitExit(const XMLDocument &)
Visit a document.
virtual bool VisitExit(const XMLElement &)
Visit an element.
virtual bool VisitEnter(const XMLDocument &)
Visit a document.
virtual bool Visit(const XMLComment &)
Visit a comment node.
virtual bool Visit(const XMLDeclaration &)
Visit a declaration.
virtual bool Visit(const XMLText &)
Visit a text node.
virtual bool VisitEnter(const XMLElement &, const XMLAttribute *)
Visit an element.