27 #if defined(ANDROID_NDK) || defined(__QNXNTO__) 35 #if defined(_MSC_VER) && (_MSC_VER >= 1400 ) && (!defined WINCE) 44 static inline int TIXML_SNPRINTF(
char* buffer,
size_t size,
const char* format, ... )
47 va_start( va, format );
48 int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va );
53 static inline int TIXML_VSNPRINTF(
char* buffer,
size_t size,
const char* format, va_list va )
55 int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va );
59 #define TIXML_VSCPRINTF _vscprintf 60 #define TIXML_SSCANF sscanf_s 61 #elif defined _MSC_VER 63 #define TIXML_SNPRINTF _snprintf 64 #define TIXML_VSNPRINTF _vsnprintf 65 #define TIXML_SSCANF sscanf 66 #if (_MSC_VER < 1400 ) && (!defined WINCE) 68 #define TIXML_VSCPRINTF _vscprintf // VS2003's C runtime has this, but VC6 C runtime or WinCE SDK doesn't have. 76 char* str =
new char[len]();
77 const int required = _vsnprintf(str, len, format, va);
79 if ( required != -1 ) {
92 #define TIXML_SNPRINTF snprintf 93 #define TIXML_VSNPRINTF vsnprintf 96 int len = vsnprintf( 0, 0, format, va );
100 #define TIXML_SSCANF sscanf 130 {
"quot", 4, DOUBLE_QUOTE },
132 {
"apos", 4, SINGLE_QUOTE },
146 if (
this == other ) {
169 if ( _flags & NEEDS_DELETE ) {
181 size_t len = strlen( str );
183 _start =
new char[ len+1 ];
184 memcpy( _start, str, len+1 );
186 _flags = flags | NEEDS_DELETE;
195 char endChar = *endTag;
196 size_t length = strlen( endTag );
200 if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
201 Set( start, p, strFlags );
219 char*
const start = p;
263 if ( _flags & NEEDS_FLUSH ) {
265 _flags ^= NEEDS_FLUSH;
272 if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == CR ) {
276 if ( *(p+1) == LF ) {
284 else if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == LF ) {
285 if ( *(p+1) == CR ) {
293 else if ( (_flags & NEEDS_ENTITY_PROCESSING) && *p ==
'&' ) {
299 if ( *(p+1) ==
'#' ) {
300 const int buflen = 10;
301 char buf[buflen] = { 0 };
304 if ( adjusted == 0 ) {
313 memcpy( q, buf, len );
318 bool entityFound =
false;
320 const Entity& entity = entities[i];
322 && *( p + entity.
length + 1 ) ==
';' ) {
331 if ( !entityFound ) {
348 if ( _flags & NEEDS_WHITESPACE_COLLAPSING ) {
349 CollapseWhitespace();
351 _flags = (_flags & NEEDS_DELETE);
367 const unsigned char* pu =
reinterpret_cast<const unsigned char*
>(p);
369 if ( *(pu+0) == TIXML_UTF_LEAD_0
370 && *(pu+1) == TIXML_UTF_LEAD_1
382 const unsigned long BYTE_MASK = 0xBF;
383 const unsigned long BYTE_MARK = 0x80;
384 const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
389 else if ( input < 0x800 ) {
392 else if ( input < 0x10000 ) {
395 else if ( input < 0x200000 ) {
409 *output = (char)((input | BYTE_MARK) & BYTE_MASK);
413 *output = (char)((input | BYTE_MARK) & BYTE_MASK);
417 *output = (char)((input | BYTE_MARK) & BYTE_MASK);
421 *output = (char)(input | FIRST_BYTE_MARK[*length]);
434 if ( *(p+1) ==
'#' && *(p+2) ) {
435 unsigned long ucs = 0;
439 static const char SEMICOLON =
';';
441 if ( *(p+2) ==
'x' ) {
448 q = strchr( q, SEMICOLON );
458 while ( *q !=
'x' ) {
459 unsigned int digit = 0;
461 if ( *q >=
'0' && *q <=
'9' ) {
464 else if ( *q >=
'a' && *q <=
'f' ) {
465 digit = *q -
'a' + 10;
467 else if ( *q >=
'A' && *q <=
'F' ) {
468 digit = *q -
'A' + 10;
474 TIXMLASSERT( digit == 0 || mult <= UINT_MAX / digit );
475 const unsigned int digitScaled = mult * digit;
490 q = strchr( q, SEMICOLON );
500 while ( *q !=
'#' ) {
501 if ( *q >=
'0' && *q <=
'9' ) {
502 const unsigned int digit = *q -
'0';
504 TIXMLASSERT( digit == 0 || mult <= UINT_MAX / digit );
505 const unsigned int digitScaled = mult * digit;
518 ConvertUTF32ToUTF8( ucs, value, length );
519 return p + delta + 1;
577 if ( ToInt( str, &ival )) {
578 *value = (ival==0) ?
false :
true;
581 if ( StringEqual( str,
"true" ) ) {
585 else if ( StringEqual( str,
"false" ) ) {
614 char*
const start = p;
623 static const char* xmlHeader = {
"<?" };
624 static const char* commentHeader = {
"<!--" };
625 static const char* cdataHeader = {
"<![CDATA[" };
626 static const char* dtdHeader = {
"<!" };
627 static const char* elementHeader = {
"<" };
629 static const int xmlHeaderLen = 2;
630 static const int commentHeaderLen = 4;
631 static const int cdataHeaderLen = 9;
632 static const int dtdHeaderLen = 2;
633 static const int elementHeaderLen = 1;
641 returnNode->_memPool = &_commentPool;
646 returnNode =
new (_commentPool.Alloc())
XMLComment(
this );
647 returnNode->_memPool = &_commentPool;
648 p += commentHeaderLen;
656 text->SetCData(
true );
660 returnNode =
new (_commentPool.Alloc())
XMLUnknown(
this );
661 returnNode->_memPool = &_commentPool;
666 returnNode =
new (_elementPool.Alloc())
XMLElement(
this );
667 returnNode->_memPool = &_elementPool;
668 p += elementHeaderLen;
672 returnNode =
new (_textPool.Alloc())
XMLText(
this );
673 returnNode->_memPool = &_textPool;
689 if ( !node->Accept( visitor ) ) {
703 _firstChild( 0 ), _lastChild( 0 ),
704 _prev( 0 ), _next( 0 ),
763 if ( child->
_prev ) {
766 if ( child->
_next ) {
853 if ( afterThis->
_parent !=
this ) {
858 if ( afterThis->
_next == 0 ) {
863 addThis->
_prev = afterThis;
866 afterThis->
_next = addThis;
990 bool mismatch =
false;
991 if ( endTag.
Empty() ) {
1039 const char*
start = p;
1040 if ( this->CData() ) {
1086 return visitor->
Visit( *
this );
1105 const char*
start = p;
1135 return visitor->
Visit( *
this );
1155 const char*
start = p;
1186 return visitor->
Visit( *
this );
1204 const char*
start = p;
1235 return visitor->
Visit( *
this );
1242 return _name.GetStr();
1253 p = _name.ParseName( p );
1266 if ( *p !=
'\"' && *p !=
'\'' ) {
1270 char endTag[2] = { *p, 0 };
1547 last = attrib, attrib = attrib->
_next ) {
1557 last->
_next = attrib;
1563 attrib->_memPool->SetTracked();
1590 const char*
start = p;
1606 attrib->_memPool->SetTracked();
1609 if ( !p ||
Attribute( attrib->Name() ) ) {
1619 if ( prevAttribute ) {
1620 prevAttribute->
_next = attrib;
1625 prevAttribute = attrib;
1628 else if ( *p ==
'>' ) {
1633 else if ( *p ==
'/' && *(p+1) ==
'>' ) {
1647 if ( attribute == 0 ) {
1652 pool->
Free( attribute );
1732 if ( !node->Accept( visitor ) ) {
1747 "XML_WRONG_ATTRIBUTE_TYPE",
1748 "XML_ERROR_FILE_NOT_FOUND",
1749 "XML_ERROR_FILE_COULD_NOT_BE_OPENED",
1750 "XML_ERROR_FILE_READ_ERROR",
1751 "XML_ERROR_ELEMENT_MISMATCH",
1752 "XML_ERROR_PARSING_ELEMENT",
1753 "XML_ERROR_PARSING_ATTRIBUTE",
1754 "XML_ERROR_IDENTIFYING_TAG",
1755 "XML_ERROR_PARSING_TEXT",
1756 "XML_ERROR_PARSING_CDATA",
1757 "XML_ERROR_PARSING_COMMENT",
1758 "XML_ERROR_PARSING_DECLARATION",
1759 "XML_ERROR_PARSING_UNKNOWN",
1760 "XML_ERROR_EMPTY_DOCUMENT",
1761 "XML_ERROR_MISMATCHED_ELEMENT",
1762 "XML_ERROR_PARSING",
1763 "XML_CAN_NOT_CONVERT_TEXT",
1771 _processEntities( processEntities ),
1773 _whitespace( whitespace ),
1794 const bool hadError =
Error();
1826 ele->SetName( name );
1836 comment->SetValue( str );
1846 text->SetValue( str );
1856 dec->SetValue( str ? str :
"xml version=\"1.0\" encoding=\"UTF-8\"" );
1866 unk->SetValue( str );
1870 static FILE*
callfopen(
const char* filepath,
const char* mode )
1874 #if defined(_MSC_VER) && (_MSC_VER >= 1400 ) && (!defined WINCE) 1876 errno_t err = fopen_s( &fp, filepath, mode );
1881 FILE* fp = fopen( filepath, mode );
1924 <
bool = (
sizeof(
unsigned long) >=
sizeof(
size_t))>
1926 static bool Fits(
unsigned long value )
1928 return value < (size_t)-1;
1942 fseek( fp, 0, SEEK_SET );
1943 if ( fgetc( fp ) == EOF && ferror( fp ) != 0 ) {
1948 fseek( fp, 0, SEEK_END );
1949 const long filelength = ftell( fp );
1950 fseek( fp, 0, SEEK_SET );
1951 if ( filelength == -1L ) {
1962 if ( filelength == 0 ) {
1967 const size_t size = filelength;
1971 if ( read != size ) {
2011 if ( len == 0 || !p || !*p ) {
2015 if ( len == (
size_t)(-1) ) {
2045 Accept( &stdoutStreamer );
2069 static const int LEN = 20;
2070 char buf1[LEN] = { 0 };
2071 char buf2[LEN] = { 0 };
2083 printf(
"XMLDocument error id=%d '%s' str1=%s str2=%s\n",
2103 _elementJustOpened( false ),
2104 _firstElement( true ),
2109 _compactMode( compact )
2116 const char entityValue = entities[i].
value;
2117 TIXMLASSERT( 0 <= entityValue && entityValue < ENTITY_RANGE );
2130 va_start( va, format );
2133 vfprintf(
_fp, format, va );
2140 va_start( va, format );
2151 for(
int i=0; i<depth; ++i ) {
2171 if ( flag[(
unsigned char)(*q)] ) {
2173 const size_t delta = q - p;
2175 const int toPrint = ( INT_MAX < delta ) ? INT_MAX : (
int)delta;
2176 Print(
"%.*s", toPrint, p );
2179 bool entityPatternPrinted =
false;
2181 if ( entities[i].value == *q ) {
2182 Print(
"&%s;", entities[i].pattern );
2183 entityPatternPrinted =
true;
2187 if ( !entityPatternPrinted ) {
2227 if ( !compactMode ) {
2231 Print(
"<%s", name );
2241 Print(
" %s=\"", name );
2292 Print(
"</%s>", name );
2298 if (
_depth == 0 && !compactMode) {
2321 Print(
"<![CDATA[%s]]>", text );
2376 Print(
"<!--%s-->", comment );
2388 Print(
"<?%s?>", value );
2400 Print(
"<!%s>", value );
2417 if ( element.
Parent() ) {
2422 while ( attribute ) {
2424 attribute = attribute->
Next();
virtual XMLElement * ToElement()
Safely cast to an Element, or null.
XMLText * NewText(const char *text)
virtual bool Visit(const XMLText &text)
Visit a text node.
void SetError(XMLError error, const char *str1, const char *str2)
void CollapseWhitespace()
bool Error() const
Return true if there was an error parsing the document.
static const char LINE_FEED
void SetText(const char *inText)
void SealElementIfJustOpened()
const XMLElement * FirstChildElement(const char *name=0) const
static void ToStr(int v, char *buffer, int bufferSize)
XMLAttribute * FindOrCreateAttribute(const char *name)
XMLError QueryIntValue(int *value) const
static const char * GetCharacterRef(const char *p, char *value, int *length)
virtual XMLDocument * ToDocument()
Safely cast to a Document, or null.
static const unsigned char TIXML_UTF_LEAD_0
XMLError LoadFile(const char *filename)
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
XMLAttribute * _rootAttribute
MemPoolT< sizeof(XMLAttribute) > _attributePool
static const char * SkipWhiteSpace(const char *p)
static bool ToFloat(const char *str, float *value)
const XMLElement * NextSiblingElement(const char *name=0) const
Get the next (right) sibling element of this node, with an optionally supplied name.
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)
bool CData() const
Returns true if this is a CDATA text element.
void Unlink(XMLNode *child)
void PushDeclaration(const char *value)
virtual void Free(void *)=0
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)
void TransferTo(StrPair *other)
char * ParseDeep(char *p, bool processEntities)
const XMLElement * PreviousSiblingElement(const char *name=0) const
Get the previous (left) sibling element of this node, with an optionally supplied name...
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.
static const char * _errorNames[XML_ERROR_COUNT]
static bool ToBool(const char *str, bool *value)
virtual bool VisitExit(const XMLDocument &)
Visit a document.
char * ParseName(char *in)
const char * Value() const
The value of the attribute.
void SetAttribute(const char *value)
Set the attribute to a string value.
void Print(const char *format,...)
virtual bool ShallowEqual(const XMLNode *compare) const
XMLPrinter(FILE *file=0, bool compact=false, int depth=0)
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 *)
XMLError QueryDoubleText(double *dval) const
See QueryIntText()
XMLElement * NewElement(const char *name)
virtual XMLNode * ShallowClone(XMLDocument *document) const
XMLError QueryDoubleValue(double *value) const
See QueryIntValue.
bool ProcessEntities() const
DynArray< char, 20 > _buffer
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)
static bool ToUnsigned(const char *str, unsigned *value)
const XMLNode * Parent() const
Get the parent of this node on the DOM.
void PushText(const char *text, bool cdata=false)
Add a text node.
const XMLAttribute * Next() const
The next attribute in the list.
static int TIXML_VSCPRINTF(const char *format, va_list va)
static const char CARRIAGE_RETURN
XMLComment * NewComment(const char *comment)
virtual void CloseElement(bool compactMode=false)
If streaming, close the Element.
void DeleteNode(XMLNode *node)
virtual void SetTracked()=0
const char * Attribute(const char *name, const char *value=0) const
virtual bool VisitEnter(const XMLDocument &)
Visit a document.
static bool ToDouble(const char *str, double *value)
MemPoolT< sizeof(XMLComment) > _commentPool
const char * Name() const
The name of the attribute.
const char * GetText() const
XMLError QueryIntText(int *ival) const
static bool Fits(unsigned long value)
static const char SINGLE_QUOTE
virtual bool VisitExit(const XMLDocument &)
Visit a document.
virtual ~XMLDeclaration()
void OpenElement(const char *name, bool compactMode=false)
void SetStr(const char *str, int flags=0)
virtual XMLNode * ShallowClone(XMLDocument *document) const
void SetCData(bool isCData)
Declare whether this should be CDATA or standard text.
virtual bool Accept(XMLVisitor *visitor) const
XMLDeclaration(XMLDocument *doc)
XMLNode * InsertFirstChild(XMLNode *addThis)
const XMLDocument * GetDocument() const
Get the XMLDocument that owns this XMLNode.
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
virtual XMLNode * ShallowClone(XMLDocument *document) const
void InsertChildPreamble(XMLNode *insertThis) const
const char * Value() const
static void DeleteNode(XMLNode *node)
XMLError QueryFloatText(float *fval) const
See QueryIntText()
void SetValue(const char *val, bool staticMem=false)
XMLError QueryUnsignedText(unsigned *uval) const
See QueryIntText()
static const int NUM_ENTITIES
void PushUnknown(const char *value)
virtual bool Accept(XMLVisitor *visitor) const
void PushAttribute(const char *name, const char *value)
If streaming, add an attribute to an open element.
DynArray< const char *, 10 > _stack
static void ConvertUTF32ToUTF8(unsigned long input, char *output, int *length)
void DeleteAttribute(const char *name)
void PrintString(const char *, bool restrictedEntitySet)
const XMLElement * LastChildElement(const char *name=0) const
XMLUnknown(XMLDocument *doc)
XMLError QueryUnsignedValue(unsigned int *value) const
See QueryIntValue.
static bool IsNameStartChar(unsigned char ch)
void DeleteChild(XMLNode *node)
virtual XMLText * ToText()
Safely cast to Text, or null.
virtual XMLNode * ShallowClone(XMLDocument *document) const
virtual bool Accept(XMLVisitor *visitor) const
virtual void PrintSpace(int depth)
XMLUnknown * NewUnknown(const char *text)
virtual bool CompactMode(const XMLElement &)
MemPoolT< sizeof(XMLText) > _textPool
char * ParseDeep(char *, StrPair *endTag)
XMLDeclaration * NewDeclaration(const char *text=0)
bool NoChildren() const
Returns true if this node has no children.
MemPoolT< sizeof(XMLElement) > _elementPool
const XMLAttribute * FirstAttribute() const
Return the first attribute in the list.
virtual bool ShallowEqual(const XMLNode *compare) const
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
char * ParseText(char *in, const char *endTag, int strFlags)
XMLError QueryBoolValue(bool *value) const
See QueryIntValue.
Whitespace WhitespaceMode() const
XMLNode * InsertAfterChild(XMLNode *afterThis, XMLNode *addThis)
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.
const char * ErrorName() const
virtual bool Accept(XMLVisitor *visitor) const
static FILE * callfopen(const char *filepath, const char *mode)
XMLNode * InsertEndChild(XMLNode *addThis)
const XMLNode * NextSibling() const
Get the next (right) sibling node of this node.
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
void SetInternedStr(const char *str)