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(); } //if ( !(x)) WinDebugBreak() 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 ) {} 135 void Set(
char* start,
char* end,
int flags ) {
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 );
184 template <
class T,
int INITIAL_SIZE>
190 _allocated = INITIAL_SIZE;
195 if ( _mem != _pool ) {
206 EnsureCapacity( _size+1 );
213 EnsureCapacity( _size+count );
214 T* ret = &_mem[_size];
221 return _mem[--_size];
245 return _mem[ _size - 1];
274 if ( cap > _allocated ) {
276 int newAllocated = cap * 2;
277 T* newMem =
new T[newAllocated];
278 memcpy( newMem, _mem,
sizeof(T)*_size );
279 if ( _mem != _pool ) {
283 _allocated = newAllocated;
288 T _pool[INITIAL_SIZE];
304 virtual int ItemSize()
const = 0;
305 virtual void* Alloc() = 0;
306 virtual void Free(
void* ) = 0;
307 virtual void SetTracked() = 0;
308 virtual void Clear() = 0;
319 MemPoolT() : _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {}
326 while( !_blockPtrs.Empty()) {
327 Block* b = _blockPtrs.Pop();
341 return _currentAllocs;
347 Block* block =
new Block();
348 _blockPtrs.Push( block );
350 for(
int i=0; i<COUNT-1; ++i ) {
351 block->chunk[i].next = &block->chunk[i+1];
353 block->chunk[COUNT-1].next = 0;
354 _root = block->chunk;
356 void* result = _root;
360 if ( _currentAllocs > _maxAllocs ) {
361 _maxAllocs = _currentAllocs;
368 virtual void Free(
void* mem ) {
373 Chunk* chunk =
static_cast<Chunk*
>( mem );
375 memset( chunk, 0xfe,
sizeof(Chunk) );
381 printf(
"Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
382 name, _maxAllocs, _maxAllocs*SIZE/1024, _currentAllocs, SIZE, _nAllocs, _blockPtrs.Size() );
402 enum { COUNT = (4*1024)/SIZE };
522 while( IsWhiteSpace(*p) ) {
529 return const_cast<char*
>( SkipWhiteSpace( const_cast<const char*>(p) ) );
535 return !IsUTF8Continuation(p) && isspace( static_cast<unsigned char>(p) );
543 if ( isalpha( ch ) ) {
546 return ch ==
':' || ch ==
'_';
550 return IsNameStartChar( ch )
556 inline static bool StringEqual(
const char* p,
const char* q,
int nChar=INT_MAX ) {
561 while( *p && *q && *p == *q && n<nChar ) {
566 if ( (n == nChar) || ( *p == 0 && *q == 0 ) ) {
573 return ( p & 0x80 ) != 0;
576 static const char* ReadBOM(
const char* p,
bool* hasBOM );
579 static const char* GetCharacterRef(
const char* p,
char* value,
int* length );
580 static void ConvertUTF32ToUTF8(
unsigned long input,
char* output,
int* length );
583 static void ToStr(
int v,
char* buffer,
int bufferSize );
584 static void ToStr(
unsigned v,
char* buffer,
int bufferSize );
585 static void ToStr(
bool v,
char* buffer,
int bufferSize );
586 static void ToStr(
float v,
char* buffer,
int bufferSize );
587 static void ToStr(
double v,
char* buffer,
int bufferSize );
590 static bool ToInt(
const char* str,
int* value );
591 static bool ToUnsigned(
const char* str,
unsigned* value );
592 static bool ToBool(
const char* str,
bool* value );
593 static bool ToFloat(
const char* str,
float* value );
594 static bool ToDouble(
const char* str,
double* value );
693 const char* Value()
const;
698 void SetValue(
const char* val,
bool staticMem=
false );
726 const XMLElement* FirstChildElement(
const char* name = 0 )
const;
729 return const_cast<XMLElement*
>(
const_cast<const XMLNode*
>(
this)->FirstChildElement( name ));
744 const XMLElement* LastChildElement(
const char* name = 0 )
const;
747 return const_cast<XMLElement*
>(
const_cast<const XMLNode*
>(
this)->LastChildElement(name) );
760 const XMLElement* PreviousSiblingElement(
const char* name = 0 )
const ;
763 return const_cast<XMLElement*
>(
const_cast<const XMLNode*
>(
this)->PreviousSiblingElement( name ) );
776 const XMLElement* NextSiblingElement(
const char* name = 0 )
const;
779 return const_cast<XMLElement*
>(
const_cast<const XMLNode*
>(
this)->NextSiblingElement( name ) );
792 return InsertEndChild( addThis );
815 void DeleteChildren();
820 void DeleteChild(
XMLNode* node );
839 virtual bool ShallowEqual(
const XMLNode* compare )
const = 0;
863 virtual bool Accept(
XMLVisitor* visitor )
const = 0;
869 virtual char* ParseDeep(
char*,
StrPair* );
884 static void DeleteNode(
XMLNode* node );
885 void InsertChildPreamble(
XMLNode* insertThis )
const;
906 friend class XMLBase;
909 virtual bool Accept(
XMLVisitor* visitor )
const;
928 virtual bool ShallowEqual(
const XMLNode* compare )
const;
934 char* ParseDeep(
char*,
StrPair* endTag );
956 virtual bool Accept(
XMLVisitor* visitor )
const;
959 virtual bool ShallowEqual(
const XMLNode* compare )
const;
965 char* ParseDeep(
char*,
StrPair* endTag );
995 virtual bool Accept(
XMLVisitor* visitor )
const;
998 virtual bool ShallowEqual(
const XMLNode* compare )
const;
1004 char* ParseDeep(
char*,
StrPair* endTag );
1030 virtual bool Accept(
XMLVisitor* visitor )
const;
1033 virtual bool ShallowEqual(
const XMLNode* compare )
const;
1039 char* ParseDeep(
char*,
StrPair* endTag );
1059 const char* Name()
const;
1062 const char* Value()
const;
1075 QueryIntValue( &i );
1081 QueryUnsignedValue( &i );
1087 QueryBoolValue( &b );
1093 QueryDoubleValue( &d );
1099 QueryFloatValue( &f );
1107 XMLError QueryIntValue(
int* value )
const;
1109 XMLError QueryUnsignedValue(
unsigned int* value )
const;
1111 XMLError QueryBoolValue(
bool* value )
const;
1113 XMLError QueryDoubleValue(
double* value )
const;
1115 XMLError QueryFloatValue(
float* value )
const;
1118 void SetAttribute(
const char* value );
1120 void SetAttribute(
int value );
1122 void SetAttribute(
unsigned value );
1124 void SetAttribute(
bool value );
1126 void SetAttribute(
double value );
1128 void SetAttribute(
float value );
1131 enum { BUF_SIZE = 200 };
1138 void SetName(
const char* name );
1140 char* ParseDeep(
char* p,
bool processEntities );
1155 friend class XMLBase;
1163 void SetName(
const char* str,
bool staticMem=
false ) {
1164 SetValue( str, staticMem );
1173 virtual bool Accept(
XMLVisitor* visitor )
const;
1198 const char* Attribute(
const char* name,
const char* value=0 )
const;
1207 QueryIntAttribute( name, &i );
1213 QueryUnsignedAttribute( name, &i );
1219 QueryBoolAttribute( name, &b );
1225 QueryDoubleAttribute( name, &d );
1231 QueryFloatAttribute( name, &f );
1307 return QueryIntAttribute( name, value );
1311 return QueryUnsignedAttribute( name, value );
1315 return QueryBoolAttribute( name, value );
1319 return QueryDoubleAttribute( name, value );
1323 return QueryFloatAttribute( name, value );
1360 void DeleteAttribute(
const char* name );
1364 return _rootAttribute;
1367 const XMLAttribute* FindAttribute(
const char* name )
const;
1397 const char* GetText()
const;
1433 void SetText(
const char* inText );
1435 void SetText(
int value );
1437 void SetText(
unsigned value );
1439 void SetText(
bool value );
1441 void SetText(
double value );
1443 void SetText(
float value );
1471 XMLError QueryIntText(
int* ival )
const;
1473 XMLError QueryUnsignedText(
unsigned* uval )
const;
1475 XMLError QueryBoolText(
bool* bval )
const;
1477 XMLError QueryDoubleText(
double* dval )
const;
1479 XMLError QueryFloatText(
float* fval )
const;
1488 return _closingType;
1491 virtual bool ShallowEqual(
const XMLNode* compare )
const;
1494 char* ParseDeep(
char* p,
StrPair* endTag );
1505 XMLAttribute* FindOrCreateAttribute(
const char* name );
1507 char* ParseAttributes(
char* p );
1508 static void DeleteAttribute(
XMLAttribute* attribute );
1510 enum { BUF_SIZE = 200 };
1557 XMLError Parse(
const char* xml,
size_t nBytes=(
size_t)(-1) );
1584 XMLError SaveFile(
const char* filename,
bool compact =
false );
1593 XMLError SaveFile( FILE* fp,
bool compact =
false );
1596 return _processEntities;
1618 return FirstChildElement();
1621 return FirstChildElement();
1639 virtual bool Accept(
XMLVisitor* visitor )
const;
1652 XMLComment* NewComment(
const char* comment );
1658 XMLText* NewText(
const char* text );
1682 void DeleteNode(
XMLNode* node );
1684 void SetError(
XMLError error,
const char* str1,
const char* str2 );
1694 const char* ErrorName()
const;
1705 void PrintError()
const;
1711 char* Identify(
char* p,
XMLNode** node );
1821 return XMLHandle( _node ? _node->FirstChild() : 0 );
1825 return XMLHandle( _node ? _node->FirstChildElement( name ) : 0 );
1829 return XMLHandle( _node ? _node->LastChild() : 0 );
1833 return XMLHandle( _node ? _node->LastChildElement( name ) : 0 );
1837 return XMLHandle( _node ? _node->PreviousSibling() : 0 );
1841 return XMLHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
1845 return XMLHandle( _node ? _node->NextSibling() : 0 );
1849 return XMLHandle( _node ? _node->NextSiblingElement( name ) : 0 );
1858 return ( ( _node == 0 ) ? 0 : _node->ToElement() );
1862 return ( ( _node == 0 ) ? 0 : _node->ToText() );
1866 return ( ( _node == 0 ) ? 0 : _node->ToUnknown() );
1870 return ( ( _node == 0 ) ? 0 : _node->ToDeclaration() );
1904 return XMLConstHandle( _node ? _node->FirstChildElement( name ) : 0 );
1910 return XMLConstHandle( _node ? _node->LastChildElement( name ) : 0 );
1916 return XMLConstHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
1922 return XMLConstHandle( _node ? _node->NextSiblingElement( name ) : 0 );
1930 return ( ( _node == 0 ) ? 0 : _node->ToElement() );
1933 return ( ( _node == 0 ) ? 0 : _node->ToText() );
1936 return ( ( _node == 0 ) ? 0 : _node->ToUnknown() );
1939 return ( ( _node == 0 ) ? 0 : _node->ToDeclaration() );
1998 XMLPrinter( FILE* file=0,
bool compact =
false,
int depth = 0 );
2002 void PushHeader(
bool writeBOM,
bool writeDeclaration );
2006 void OpenElement(
const char* name,
bool compactMode=
false );
2008 void PushAttribute(
const char* name,
const char* value );
2009 void PushAttribute(
const char* name,
int value );
2010 void PushAttribute(
const char* name,
unsigned value );
2011 void PushAttribute(
const char* name,
bool value );
2012 void PushAttribute(
const char* name,
double value );
2014 virtual void CloseElement(
bool compactMode=
false );
2017 void PushText(
const char* text,
bool cdata=
false );
2019 void PushText(
int value );
2021 void PushText(
unsigned value );
2023 void PushText(
bool value );
2025 void PushText(
float value );
2027 void PushText(
double value );
2030 void PushComment(
const char* comment );
2032 void PushDeclaration(
const char* value );
2033 void PushUnknown(
const char* value );
2041 virtual bool VisitExit(
const XMLElement& element );
2043 virtual bool Visit(
const XMLText& text );
2044 virtual bool Visit(
const XMLComment& comment );
2046 virtual bool Visit(
const XMLUnknown& unknown );
2053 return _buffer.Mem();
2061 return _buffer.Size();
2078 virtual void PrintSpace(
int depth );
2079 void Print(
const char* format, ... );
2081 void SealElementIfJustOpened();
2086 void PrintString(
const char*,
bool restrictedEntitySet );
2099 bool _entityFlag[ENTITY_RANGE];
2100 bool _restrictedEntityFlag[ENTITY_RANGE];
2108 #if defined(_MSC_VER) 2109 # pragma warning(pop) 2112 #endif // TINYXML2_INCLUDED virtual XMLElement * ToElement()
Safely cast to an Element, or null.
double DoubleAttribute(const char *name) const
See IntAttribute()
int QueryAttribute(const char *name, int *value) const
int QueryAttribute(const char *name, bool *value) const
const XMLDeclaration * ToDeclaration() const
void CollapseWhitespace()
bool BoolValue() const
Query as a boolean. See IntValue()
bool Error() const
Return true if there was an error parsing the document.
const XMLConstHandle FirstChild() const
const XMLConstHandle LastChild() const
XMLError QueryIntValue(int *value) const
const XMLText * ToText() const
virtual void Free(void *mem)
static const int TIXML2_PATCH_VERSION
XMLError QueryUnsignedAttribute(const char *name, unsigned int *value) const
See QueryIntAttribute()
virtual const XMLDeclaration * ToDeclaration() const
virtual XMLDocument * ToDocument()
Safely cast to a Document, or null.
XMLHandle(XMLNode *node)
Create a handle from any node (at any depth of the tree.) This can be a null pointer.
virtual const XMLComment * ToComment() const
XMLElement * PreviousSiblingElement(const char *name=0)
XMLError QueryFloatValue(float *value) const
See QueryIntValue.
const T & operator[](int i) const
XMLElement * LastChildElement(const char *name=0)
XMLAttribute * _rootAttribute
MemPoolT< sizeof(XMLAttribute) > _attributePool
static const char * SkipWhiteSpace(const char *p)
int CurrentAllocs() const
void SetAttribute(const char *name, unsigned value)
Sets the named attribute to value.
virtual XMLComment * ToComment()
Safely cast to a Comment, or null.
XMLConstHandle & operator=(const XMLConstHandle &ref)
virtual const XMLText * ToText() const
virtual bool VisitEnter(const XMLElement &, const XMLAttribute *)
Visit an element.
bool CData() const
Returns true if this is a CDATA text element.
void Set(char *start, char *end, int flags)
DynArray< Block *, 10 > _blockPtrs
static bool IsNameChar(unsigned char ch)
static bool IsWhiteSpace(char p)
void TransferTo(StrPair *other)
const XMLElement * RootElement() const
virtual XMLUnknown * ToUnknown()
Safely cast to an Unknown, or null.
virtual bool Visit(const XMLText &)
Visit a text node.
const XMLNode * FirstChild() const
Get the first child node, or null if none exists.
virtual bool ShallowEqual(const XMLNode *) const
static char * SkipWhiteSpace(char *p)
double DoubleValue() const
Query as a double. See IntValue()
XMLConstHandle(const XMLNode *node)
XMLNode * PreviousSibling()
virtual bool VisitExit(const XMLDocument &)
Visit a document.
char * ParseName(char *in)
const T & PeekTop() const
XMLConstHandle(const XMLNode &node)
int QueryAttribute(const char *name, double *value) const
void SetAttribute(const char *value)
Set the attribute to a string value.
virtual const XMLText * ToText() const
XMLError QueryFloatAttribute(const char *name, float *value) const
See QueryIntAttribute()
virtual const XMLUnknown * ToUnknown() const
const XMLNode * ToNode() const
const XMLConstHandle PreviousSibling() const
virtual XMLText * ToText()
Safely cast to Text, or null.
virtual bool Visit(const XMLComment &)
Visit a comment node.
int IntAttribute(const char *name) const
XMLHandle LastChild()
Get the last child of this handle.
virtual XMLNode * ShallowClone(XMLDocument *) const
void SetAttribute(const char *name, const char *value)
Sets the named attribute to value.
const char * GetErrorStr2() const
Return a possibly helpful secondary diagnostic location or string.
XMLHandle FirstChildElement(const char *name=0)
Get the first child element of this handle.
static const int TIXML2_MINOR_VERSION
const XMLUnknown * ToUnknown() const
XMLHandle & operator=(const XMLHandle &ref)
Assignment.
const XMLConstHandle NextSibling() const
XMLError QueryDoubleValue(double *value) const
See QueryIntValue.
bool ProcessEntities() const
DynArray< char, 20 > _buffer
XMLError ErrorID() const
Return the errorID.
XMLElement * RootElement()
XMLHandle NextSiblingElement(const char *name=0)
Get the next sibling element of this handle.
static bool IsUTF8Continuation(char p)
const XMLNode * Parent() const
Get the parent of this node on the DOM.
const XMLAttribute * Next() const
The next attribute in the list.
XMLError QueryDoubleAttribute(const char *name, double *value) const
See QueryIntAttribute()
void SetName(const char *str, bool staticMem=false)
Set the name of the element.
virtual int ItemSize() const
void SetAttribute(const char *name, double value)
Sets the named attribute to value.
int QueryAttribute(const char *name, float *value) const
const XMLConstHandle PreviousSiblingElement(const char *name=0) const
static const int TIXML2_MAJOR_VERSION
MemPoolT< sizeof(XMLComment) > _commentPool
bool BoolAttribute(const char *name) const
See IntAttribute()
XMLHandle LastChildElement(const char *name=0)
Get the last child element of this handle.
void EnsureCapacity(int cap)
virtual bool VisitExit(const XMLDocument &)
Visit a document.
void SetStr(const char *str, int flags=0)
XMLHandle(XMLNode &node)
Create a handle from a node.
void SetCData(bool isCData)
Declare whether this should be CDATA or standard text.
XMLDocument * GetDocument()
Get the XMLDocument that owns this XMLNode.
const XMLDocument * GetDocument() const
Get the XMLDocument that owns this XMLNode.
virtual XMLDeclaration * ToDeclaration()
Safely cast to a Declaration, or null.
virtual XMLUnknown * ToUnknown()
Safely cast to an Unknown, or null.
static bool StringEqual(const char *p, const char *q, int nChar=INT_MAX)
unsigned UnsignedAttribute(const char *name) const
See IntAttribute()
virtual bool VisitExit(const XMLElement &)
Visit an element.
virtual const XMLElement * ToElement() const
virtual bool Visit(const XMLUnknown &)
Visit an unknown node.
XMLConstHandle(const XMLConstHandle &ref)
XMLElement * NextSiblingElement(const char *name=0)
const XMLNode * PreviousSibling() const
Get the previous (left) sibling node of this node.
virtual const XMLDeclaration * ToDeclaration() const
XMLNode * LinkEndChild(XMLNode *addThis)
XMLAttribute * FindAttribute(const char *name)
XMLElement * FirstChildElement(const char *name=0)
void SetAttribute(const char *name, bool value)
Sets the named attribute to value.
void SetAttribute(const char *name, float value)
Sets the named attribute to value.
const char * GetErrorStr1() const
Return a possibly helpful diagnostic location or string.
const XMLConstHandle FirstChildElement(const char *name=0) const
DynArray< const char *, 10 > _stack
XMLError QueryIntAttribute(const char *name, int *value) const
XMLNode * ToNode()
Safe cast to XMLNode. This can return null.
void Trace(const char *name)
XMLHandle NextSibling()
Get the next sibling of this handle.
unsigned UnsignedValue() const
Query as an unsigned integer. See IntValue()
XMLError QueryUnsignedValue(unsigned int *value) const
See QueryIntValue.
XMLText(XMLDocument *doc)
static bool IsNameStartChar(unsigned char ch)
const XMLElement * ToElement() const
virtual XMLText * ToText()
Safely cast to Text, or null.
void SetAttribute(const char *name, int value)
Sets the named attribute to value.
XMLError QueryBoolAttribute(const char *name, bool *value) const
See QueryIntAttribute()
XMLText * ToText()
Safe cast to XMLText. This can return null.
void operator=(StrPair &other)
int QueryAttribute(const char *name, unsigned int *value) const
const XMLNode * LastChild() const
Get the last child node, or null if none exists.
const XMLConstHandle LastChildElement(const char *name=0) const
const XMLConstHandle NextSiblingElement(const char *name=0) const
virtual bool CompactMode(const XMLElement &)
XMLHandle PreviousSibling()
Get the previous sibling of this handle.
MemPoolT< sizeof(XMLText) > _textPool
XMLDeclaration * ToDeclaration()
Safe cast to XMLDeclaration. This can return null.
bool NoChildren() const
Returns true if this node has no children.
virtual const XMLDocument * ToDocument() const
MemPoolT< sizeof(XMLElement) > _elementPool
const XMLAttribute * FirstAttribute() const
Return the first attribute in the list.
XMLUnknown * ToUnknown()
Safe cast to XMLUnknown. This can return null.
XMLHandle FirstChild()
Get the first child of this handle.
virtual const XMLUnknown * ToUnknown() const
XMLElement * ToElement()
Safe cast to XMLElement. This can return null.
XMLHandle PreviousSiblingElement(const char *name=0)
Get the previous sibling element of this handle.
float FloatAttribute(const char *name) const
See IntAttribute()
virtual bool Visit(const XMLDeclaration &)
Visit a declaration.
virtual bool VisitEnter(const XMLDocument &)
Visit a document.
virtual const XMLElement * ToElement() const
char * ParseText(char *in, const char *endTag, int strFlags)
XMLError QueryBoolValue(bool *value) const
See QueryIntValue.
virtual XMLDocument * ToDocument()
Safely cast to a Document, or null.
Whitespace WhitespaceMode() const
float FloatValue() const
Query as a float. See IntValue()
virtual XMLDeclaration * ToDeclaration()
Safely cast to a Declaration, or null.
virtual XMLElement * ToElement()
Safely cast to an Element, or null.
const XMLNode * NextSibling() const
Get the next (right) sibling node of this node.
XMLHandle(const XMLHandle &ref)
Copy constructor.
virtual const XMLDocument * ToDocument() const
const char * CStr() const
const char * Name() const
Get the name of an element (which is the Value() of the node.)
void SetInternedStr(const char *str)