49 #define DELETE_NODE( node ) { \ 51 MemPool* pool = node->_memPool; \ 56 #define DELETE_ATTRIBUTE( attrib ) { \ 58 MemPool* pool = attrib->_memPool; \ 59 attrib->~XMLAttribute(); \ 60 pool->Free( attrib ); \ 91 if ( _flags & NEEDS_DELETE ) {
103 size_t len = strlen( str );
104 _start =
new char[ len+1 ];
105 memcpy( _start, str, len+1 );
107 _flags = flags | NEEDS_DELETE;
116 char endChar = *endTag;
117 size_t length = strlen( endTag );
121 if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
122 Set( start, p, strFlags );
135 if ( !start || !(*start) ) {
156 if ( _start && *_start ) {
180 if ( _flags & NEEDS_FLUSH ) {
182 _flags ^= NEEDS_FLUSH;
189 if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p ==
CR ) {
193 if ( *(p+1) ==
LF ) {
201 else if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p ==
LF ) {
202 if ( *(p+1) ==
CR ) {
210 else if ( (_flags & NEEDS_ENTITY_PROCESSING) && *p ==
'&' ) {
216 if ( *(p+1) ==
'#' ) {
217 char buf[10] = { 0 };
220 for(
int i=0; i<len; ++i ) {
228 if ( strncmp( p+1, entities[i].
pattern, entities[i].
length ) == 0
229 && *(p+entities[i].length+1) ==
';' ) {
231 *q = entities[i].
value;
233 p += entities[i].
length + 2;
237 if ( i == NUM_ENTITIES ) {
255 CollapseWhitespace();
257 _flags = (_flags & NEEDS_DELETE);
270 const unsigned char* pu =
reinterpret_cast<const unsigned char*
>(p);
284 const unsigned long BYTE_MASK = 0xBF;
285 const unsigned long BYTE_MARK = 0x80;
286 const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
291 else if ( input < 0x800 ) {
294 else if ( input < 0x10000 ) {
297 else if ( input < 0x200000 ) {
311 *output = (char)((input | BYTE_MARK) & BYTE_MASK);
315 *output = (char)((input | BYTE_MARK) & BYTE_MASK);
319 *output = (char)((input | BYTE_MARK) & BYTE_MASK);
323 *output = (char)(input | FIRST_BYTE_MARK[*length]);
335 if ( *(p+1) ==
'#' && *(p+2) ) {
336 unsigned long ucs = 0;
340 if ( *(p+2) ==
'x' ) {
347 q = strchr( q,
';' );
356 while ( *q !=
'x' ) {
357 if ( *q >=
'0' && *q <=
'9' ) {
358 ucs += mult * (*q -
'0');
360 else if ( *q >=
'a' && *q <=
'f' ) {
361 ucs += mult * (*q -
'a' + 10);
363 else if ( *q >=
'A' && *q <=
'F' ) {
364 ucs += mult * (*q -
'A' + 10 );
380 q = strchr( q,
';' );
389 while ( *q !=
'#' ) {
390 if ( *q >=
'0' && *q <=
'9' ) {
391 ucs += mult * (*q -
'0');
401 ConvertUTF32ToUTF8( ucs, value, length );
402 return p + delta + 1;
466 if ( ToInt( str, &ival )) {
467 *value = (ival==0) ?
false :
true;
470 if ( StringEqual( str,
"true" ) ) {
474 else if ( StringEqual( str,
"false" ) ) {
515 static const char* xmlHeader = {
"<?" };
516 static const char* commentHeader = {
"<!--" };
517 static const char* dtdHeader = {
"<!" };
518 static const char* cdataHeader = {
"<![CDATA[" };
519 static const char* elementHeader = {
"<" };
521 static const int xmlHeaderLen = 2;
522 static const int commentHeaderLen = 4;
523 static const int dtdHeaderLen = 2;
524 static const int cdataHeaderLen = 9;
525 static const int elementHeaderLen = 1;
527 #if defined(_MSC_VER) 528 #pragma warning ( push ) 529 #pragma warning ( disable : 4127 ) 533 #if defined(_MSC_VER) 534 #pragma warning (pop) 538 returnNode->
_memPool = &_commentPool;
542 returnNode =
new (_commentPool.Alloc())
XMLComment(
this );
543 returnNode->
_memPool = &_commentPool;
544 p += commentHeaderLen;
554 returnNode =
new (_commentPool.Alloc())
XMLUnknown(
this );
555 returnNode->
_memPool = &_commentPool;
559 returnNode =
new (_elementPool.Alloc())
XMLElement(
this );
560 returnNode->
_memPool = &_elementPool;
561 p += elementHeaderLen;
564 returnNode =
new (_textPool.Alloc())
XMLText(
this );
578 if ( !node->Accept( visitor ) ) {
592 _firstChild( 0 ), _lastChild( 0 ),
593 _prev( 0 ), _next( 0 ),
641 if ( child->
_prev ) {
644 if ( child->
_next ) {
710 if ( afterThis->
_parent !=
this ) {
714 if ( afterThis->
_next == 0 ) {
718 addThis->
_prev = afterThis;
721 afterThis->
_next = addThis;
761 if ( element->ToElement()
773 if ( element->ToElement()
805 if ( p == 0 || node == 0 ) {
842 else if ( !endTag.
Empty() ) {
863 const char* start = p;
864 if ( this->CData() ) {
908 return visitor->
Visit( *
this );
927 const char* start = p;
954 return visitor->
Visit( *
this );
974 const char* start = p;
1002 return visitor->
Visit( *
this );
1020 const char* start = p;
1048 return visitor->
Visit( *
this );
1055 p = _name.ParseName( p );
1062 if ( !p || *p !=
'=' ) {
1068 if ( *p !=
'\"' && *p !=
'\'' ) {
1072 char endTag[2] = { *p, 0 };
1320 last = attrib, attrib = attrib->
_next ) {
1329 last->
_next = attrib;
1335 attrib->_memPool->SetTracked();
1362 const char* start = p;
1368 if ( !p || !(*p) ) {
1390 if ( prevAttribute ) {
1391 prevAttribute->
_next = attrib;
1396 prevAttribute = attrib;
1399 else if ( *p ==
'/' && *(p+1) ==
'>' ) {
1404 else if ( *p ==
'>' ) {
1495 if ( !node->Accept( visitor ) ) {
1508 _processEntities( processEntities ),
1510 _whitespace( whitespace ),
1532 if (
Error() ==
false ) {
1586 dec->
SetValue( str ? str :
"xml version=\"1.0\" encoding=\"UTF-8\"" );
1605 #if defined(_MSC_VER) && (_MSC_VER >= 1400 ) 1606 errno_t err = fopen_s(&fp, filename,
"rb" );
1609 fp = fopen( filename,
"rb" );
1625 fseek( fp, 0, SEEK_END );
1626 size_t size = ftell( fp );
1627 fseek( fp, 0, SEEK_SET );
1636 if ( read != size ) {
1659 #if defined(_MSC_VER) && (_MSC_VER >= 1400 ) 1660 errno_t err = fopen_s(&fp, filename,
"w" );
1663 fp = fopen( filename,
"w" );
1685 const char* start = p;
1692 if ( len == (
size_t)(-1) ) {
1706 ptrdiff_t
delta = p - start;
1716 streamer = &stdStreamer;
1733 static const int LEN = 20;
1734 char buf1[LEN] = { 0 };
1735 char buf2[LEN] = { 0 };
1744 printf(
"XMLDocument error id=%d str1=%s str2=%s\n",
1751 _elementJustOpened( false ),
1752 _firstElement( true ),
1757 _compactMode( compact )
1765 if ( entities[i].value < ENTITY_RANGE ) {
1779 va_start( va, format );
1782 vfprintf(
_fp, format, va );
1791 len = vsnprintf_s( _accumulator.Mem(), _accumulator.Capacity(), _TRUNCATE, format, va );
1794 _accumulator.PushArr( expand );
1798 memcpy( p, _accumulator.Mem(), len+1 );
1800 int len = vsnprintf( 0, 0, format, va );
1803 va_start( va, format );
1805 vsnprintf( p, len+1, format, va );
1814 for(
int i=0; i<depth; ++i ) {
1833 if ( flag[(
unsigned)(*q)] ) {
1839 if ( entities[i].value == *q ) {
1840 Print(
"&%s;", entities[i].pattern );
1884 Print(
"<%s", name );
1894 Print(
" %s=\"", name );
1945 Print(
"</%s>", name );
1973 Print(
"<![CDATA[" );
1974 Print(
"%s", text );
2039 Print(
"<!--%s-->", comment );
2053 Print(
"<?%s?>", value );
2067 Print(
"<!%s>", value );
2084 while ( attribute ) {
2086 attribute = attribute->
Next();
virtual XMLElement * ToElement()
Safely cast to an Element, or null.
XMLText * NewText(const char *text)
Create a new Text associated with this Document.
virtual bool Visit(const XMLText &text)
Visit a text node.
void SetError(XMLError error, const char *str1, const char *str2)
void OpenElement(const char *name)
If streaming, start writing an element.
void CollapseWhitespace()
void PushText(const char *text, bool cdata=false, bool specrow=false)
Add a text node.
bool Error() const
Return true if there was an error parsing the document.
static const char LINE_FEED
static void ToStr(int v, char *buffer, int bufferSize)
XMLAttribute * FindOrCreateAttribute(const char *name)
XMLError QueryIntValue(int *value) const
See QueryIntValue.
XMLError Parse(const char *xml, size_t nBytes=(size_t)(-1))
Parse an XML file from a character string.
static const char * GetCharacterRef(const char *p, char *value, int *length)
static const unsigned char TIXML_UTF_LEAD_0
XMLError LoadFile(const char *filename)
Load an XML file from disk.
void PushComment(const char *comment)
Add a comment.
char * ParseDeep(char *p, StrPair *endTag)
XMLError QueryFloatValue(float *value) const
See QueryIntValue.
static const Entity entities[NUM_ENTITIES]
virtual bool Accept(XMLVisitor *visitor) const
Accept a hierarchical visit of the nodes in the TinyXML-2 DOM.
XMLAttribute * _rootAttribute
MemPoolT< sizeof(XMLAttribute) > _attributePool
static const char * SkipWhiteSpace(const char *p)
static bool ToFloat(const char *str, float *value)
virtual XMLComment * ToComment()
Safely cast to a Comment, or null.
char * ParseDeep(char *, StrPair *endTag)
XMLError QueryBoolText(bool *bval) const
See QueryIntText()
void PushHeader(bool writeBOM, bool writeDeclaration)
If streaming, write the BOM and declaration.
bool CData() const
Returns true if this is a CDATA text element.
void Unlink(XMLNode *child)
void PushDeclaration(const char *value)
void PrintError() const
If there is an error, print it to stdout.
static bool IsNameChar(unsigned char ch)
static bool IsWhiteSpace(char p)
static const char * ReadBOM(const char *p, bool *hasBOM)
char * ParseDeep(char *p, bool processEntities)
const XMLAttribute * FindAttribute(const char *name) const
Query a specific attribute in the list.
static const char DOUBLE_QUOTE
const XMLNode * FirstChild() const
Get the first child node, or null if none exists.
const char * Name() const
The name of the attribute.
The element is a container class.
static bool ToBool(const char *str, bool *value)
virtual bool VisitExit(const XMLDocument &)
Visit a document.
char * ParseName(char *in)
void SetAttribute(const char *value)
Set the attribute to a string value.
#define DELETE_NODE(node)
void Print(const char *format,...)
virtual bool ShallowEqual(const XMLNode *compare) const
Test if 2 nodes are the same, but don't test children.
XMLPrinter(FILE *file=0, bool compact=false, int depth=0)
Construct the printer.
const char * Value() const
The value of the attribute.
void Clear()
Clear the document, resetting it to the initial state.
void SetAttribute(const char *name, const char *value)
Sets the named attribute to value.
virtual char * ParseDeep(char *, StrPair *)
A Document binds together all the functionality.
XMLError QueryDoubleText(double *dval) const
See QueryIntText()
void DeleteChildren()
Delete all the children of this node.
XMLElement * NewElement(const char *name)
Create a new Element associated with this Document.
virtual XMLNode * ShallowClone(XMLDocument *document) const
Make a copy of this node, but not its children.
XMLError QueryDoubleValue(double *value) const
See QueryIntValue.
static bool ToLong(const char *str, long *value)
bool ProcessEntities() const
DynArray< char, 20 > _buffer
#define DELETE_ATTRIBUTE(attrib)
static const unsigned char TIXML_UTF_LEAD_2
char * ParseDeep(char *, StrPair *endTag)
static bool ToInt(const char *str, int *value)
void SetName(const char *name)
XMLError SaveFile(const char *filename, bool compact=false)
Save the XML file to disk.
Any tag that TinyXML-2 doesn't recognize is saved as an unknown.
static bool ToUnsigned(const char *str, unsigned *value)
const XMLAttribute * Next() const
The next attribute in the list.
const XMLElement * LastChildElement(const char *value=0) const
Get the last child element or optionally the last child element with the specified name...
static const char CARRIAGE_RETURN
void SetName(const char *str, bool staticMem=false)
Set the name of the element.
XMLComment * NewComment(const char *comment)
Create a new Comment associated with this Document.
const XMLElement * FirstChildElement(const char *value=0) const
Get the first child element, or optionally the first child element with the specified name...
XMLNode is a base class for every object that is in the XML Document Object Model (DOM)...
const char * Value() const
The meaning of 'value' changes for the specific type.
virtual void SetTracked()=0
const char * Attribute(const char *name, const char *value=0) const
Given an attribute name, Attribute() returns the value for the attribute of that name, or null if none exists.
virtual bool VisitEnter(const XMLDocument &)
Visit a document.
static bool ToDouble(const char *str, double *value)
MemPoolT< sizeof(XMLComment) > _commentPool
const char * GetText() const
Convenience function for easy access to the text inside an element.
XMLError QueryIntText(int *ival) const
Convenience method to query the value of a child text node.
An attribute is a name-value pair.
static const char SINGLE_QUOTE
virtual bool VisitExit(const XMLDocument &)
Visit a document.
virtual ~XMLDeclaration()
void SetStr(const char *str, int flags=0)
virtual XMLNode * ShallowClone(XMLDocument *document) const
Make a copy of this node, but not its children.
void SetCData(bool isCData)
Declare whether this should be CDATA or standard text.
virtual bool Accept(XMLVisitor *visitor) const
Accept a hierarchical visit of the nodes in the TinyXML-2 DOM.
XMLDeclaration(XMLDocument *doc)
XMLNode * InsertFirstChild(XMLNode *addThis)
Add a child node as the first (left) child.
XMLElement(XMLDocument *doc)
char * ParseAttributes(char *p)
XMLDocument(bool processEntities=true, Whitespace=PRESERVE_WHITESPACE)
constructor
virtual XMLUnknown * ToUnknown()
Safely cast to an Unknown, or null.
char * Identify(char *p, XMLNode **node)
static bool StringEqual(const char *p, const char *q, int nChar=INT_MAX)
virtual bool ShallowEqual(const XMLNode *compare) const
Test if 2 nodes are the same, but don't test children.
virtual XMLNode * ShallowClone(XMLDocument *document) const
Make a copy of this node, but not its children.
XMLError QueryFloatText(float *fval) const
See QueryIntText()
void SetValue(const char *val, bool staticMem=false)
Set the Value of an XML node.
virtual void CloseElement()
If streaming, close the Element.
XMLError QueryUnsignedText(unsigned *uval) const
See QueryIntText()
In correct XML the declaration is the first entry in the file.
static const int NUM_ENTITIES
void PushUnknown(const char *value)
virtual bool Accept(XMLVisitor *visitor) const
Accept a hierarchical visit of the nodes in the TinyXML-2 DOM.
void PushAttribute(const char *name, const char *value)
If streaming, add an attribute to an open element.
const XMLElement * NextSiblingElement(const char *value=0) const
Get the next (right) sibling element of this node, with an optionally supplied name.
DynArray< const char *, 10 > _stack
static void ConvertUTF32ToUTF8(unsigned long input, char *output, int *length)
Implements the interface to the "Visitor pattern" (see the Accept() method.) If you call the Accept()...
void DeleteAttribute(const char *name)
Delete an attribute.
void PrintString(const char *, bool restrictedEntitySet)
XMLUnknown(XMLDocument *doc)
XMLError QueryUnsignedValue(unsigned int *value) const
See QueryIntValue.
static bool IsNameStartChar(unsigned char ch)
void DeleteChild(XMLNode *node)
Delete a child of this node.
virtual XMLText * ToText()
Safely cast to Text, or null.
virtual XMLNode * ShallowClone(XMLDocument *document) const
Make a copy of this node, but not its children.
bool HasBOM() const
Returns true if this document has a leading Byte Order Mark of UTF8.
virtual bool Accept(XMLVisitor *visitor) const
Accept a hierarchical visit of the nodes in the TinyXML-2 DOM.
void PrintSpace(int depth)
XMLUnknown * NewUnknown(const char *text)
Create a new Unknown associated with this Document.
MemPoolT< sizeof(XMLText) > _textPool
char * ParseDeep(char *, StrPair *endTag)
XMLDeclaration * NewDeclaration(const char *text=0)
Create a new Declaration associated with this Document.
MemPoolT< sizeof(XMLElement) > _elementPool
const XMLAttribute * FirstAttribute() const
Return the first attribute in the list.
virtual bool ShallowEqual(const XMLNode *compare) const
Test if 2 nodes are the same, but don't test children.
virtual bool Visit(const XMLDeclaration &)
Visit a declaration.
bool _restrictedEntityFlag[ENTITY_RANGE]
virtual bool VisitEnter(const XMLDocument &)
Visit a document.
void Print(XMLPrinter *streamer=0) const
Print the Document.
char * ParseText(char *in, const char *endTag, int strFlags)
XMLError QueryBoolValue(bool *value) const
See QueryIntValue.
const XMLElement * PreviousSiblingElement(const char *value=0) const
Get the previous (left) sibling element of this node, with an optionally supplied name...
Whitespace WhitespaceMode() const
XMLNode * InsertAfterChild(XMLNode *afterThis, XMLNode *addThis)
Add a node after the specified child node.
virtual XMLDeclaration * ToDeclaration()
Safely cast to a Declaration, or null.
static const unsigned char TIXML_UTF_LEAD_1
virtual XMLElement * ToElement()
Safely cast to an Element, or null.
virtual bool Accept(XMLVisitor *visitor) const
Accept a hierarchical visit of the nodes in the TinyXML-2 DOM.
XMLNode * InsertEndChild(XMLNode *addThis)
Add a child node as the last (right) child.
const XMLNode * NextSibling() const
Get the next (right) sibling node of this node.
XMLError QueryLongValue(long *value) const
QueryIntValue interprets the attribute as an integer, and returns the value in the provided parameter...
bool _entityFlag[ENTITY_RANGE]
const char * Name() const
Get the name of an element (which is the Value() of the node.)
virtual bool ShallowEqual(const XMLNode *compare) const
Test if 2 nodes are the same, but don't test children.
void SetInternedStr(const char *str)