35static const char LINE_FEED = (char)0x0a;
36static const char LF = LINE_FEED;
37static const char CARRIAGE_RETURN = (char)0x0d;
38static const char CR = CARRIAGE_RETURN;
39static const char SINGLE_QUOTE =
'\'';
40static const char DOUBLE_QUOTE =
'\"';
46static const unsigned char TIXML_UTF_LEAD_0 = 0xefU;
47static const unsigned char TIXML_UTF_LEAD_1 = 0xbbU;
48static const unsigned char TIXML_UTF_LEAD_2 = 0xbfU;
51#define DELETE_NODE( node ) { \
53 MemPool* pool = node->_memPool; \
58#define DELETE_ATTRIBUTE( attrib ) { \
60 MemPool* pool = attrib->_memPool; \
61 attrib->~XMLAttribute(); \
62 pool->Free( attrib ); \
75static const int NUM_ENTITIES = 5;
76static const Entity entities[NUM_ENTITIES] = {
77 {
"quot", 4, DOUBLE_QUOTE },
79 {
"apos", 4, SINGLE_QUOTE },
93 if ( _flags & NEEDS_DELETE ) {
105 size_t len = strlen( str );
106 _start =
new char[ len+1 ];
107 memcpy( _start, str, len+1 );
109 _flags = flags | NEEDS_DELETE;
118 char endChar = *endTag;
119 size_t length = strlen( endTag );
123 if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
124 Set( start, p, strFlags );
137 if ( !start || !(*start) ) {
145 || (*p ==
'-' && p>start )
146 || (*p ==
'.' && p>start ) )) {
158void StrPair::CollapseWhitespace()
163 if ( _start && *_start ) {
187 if ( _flags & NEEDS_FLUSH ) {
189 _flags ^= NEEDS_FLUSH;
200 if ( *(p+1) == LF ) {
209 if ( *(p+1) == CR ) {
223 if ( *(p+1) ==
'#' ) {
224 char buf[10] = { 0 };
227 for(
int i=0; i<len; ++i ) {
234 for(; i<NUM_ENTITIES; ++i ) {
235 if ( strncmp( p+1, entities[i].pattern, entities[i].length ) == 0
236 && *(p+entities[i].length+1) ==
';' ) {
238 *q = entities[i].value;
240 p += entities[i].length + 2;
244 if ( i == NUM_ENTITIES ) {
262 CollapseWhitespace();
264 _flags = (_flags & NEEDS_DELETE);
277 const unsigned char* pu =
reinterpret_cast<const unsigned char*
>(p);
279 if ( *(pu+0) == TIXML_UTF_LEAD_0
280 && *(pu+1) == TIXML_UTF_LEAD_1
281 && *(pu+2) == TIXML_UTF_LEAD_2 ) {
291 const unsigned long BYTE_MASK = 0xBF;
292 const unsigned long BYTE_MARK = 0x80;
293 const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
298 else if ( input < 0x800 ) {
301 else if ( input < 0x10000 ) {
304 else if ( input < 0x200000 ) {
318 *output = (char)((input | BYTE_MARK) & BYTE_MASK);
322 *output = (char)((input | BYTE_MARK) & BYTE_MASK);
326 *output = (char)((input | BYTE_MARK) & BYTE_MASK);
330 *output = (char)(input | FIRST_BYTE_MARK[*length]);
340 if ( *(p+1) ==
'#' && *(p+2) ) {
341 unsigned long ucs = 0;
345 if ( *(p+2) ==
'x' ) {
352 q = strchr( q,
';' );
361 while ( *q !=
'x' ) {
362 if ( *q >=
'0' && *q <=
'9' ) {
363 ucs += mult * (*q -
'0');
365 else if ( *q >=
'a' && *q <=
'f' ) {
366 ucs += mult * (*q -
'a' + 10);
368 else if ( *q >=
'A' && *q <=
'F' ) {
369 ucs += mult * (*q -
'A' + 10 );
385 q = strchr( q,
';' );
394 while ( *q !=
'#' ) {
395 if ( *q >=
'0' && *q <=
'9' ) {
396 ucs += mult * (*q -
'0');
407 return p + delta + 1;
462 if (
ToInt( str, &ival )) {
463 *value = (ival==0) ?
false :
true;
511 static const char* xmlHeader = {
"<?" };
512 static const char* commentHeader = {
"<!--" };
513 static const char* dtdHeader = {
"<!" };
514 static const char* cdataHeader = {
"<![CDATA[" };
515 static const char* elementHeader = {
"<" };
517 static const int xmlHeaderLen = 2;
518 static const int commentHeaderLen = 4;
519 static const int dtdHeaderLen = 2;
520 static const int cdataHeaderLen = 9;
521 static const int elementHeaderLen = 1;
524#pragma warning ( push )
525#pragma warning ( disable : 4127 )
534 returnNode->_memPool = &_commentPool;
538 returnNode =
new (_commentPool.Alloc())
XMLComment(
this );
539 returnNode->_memPool = &_commentPool;
540 p += commentHeaderLen;
545 returnNode->_memPool = &_textPool;
550 returnNode =
new (_commentPool.Alloc())
XMLUnknown(
this );
551 returnNode->_memPool = &_commentPool;
555 returnNode =
new (_elementPool.Alloc())
XMLElement(
this );
556 returnNode->_memPool = &_elementPool;
557 p += elementHeaderLen;
560 returnNode =
new (_textPool.Alloc())
XMLText(
this );
561 returnNode->_memPool = &_textPool;
574 if ( !node->Accept( visitor ) ) {
588 _firstChild( 0 ), _lastChild( 0 ),
589 _prev( 0 ), _next( 0 )
626void XMLNode::Unlink(
XMLNode* child )
636 if ( child->
_prev ) {
639 if ( child->
_next ) {
703 if ( afterThis->
_parent !=
this ) {
707 if ( afterThis->
_next == 0 ) {
711 addThis->
_prev = afterThis;
714 afterThis->
_next = addThis;
753 if ( element->ToElement()
765 if ( element->ToElement()
797 if ( p == 0 || node == 0 ) {
833 else if ( !endTag.
Empty() ) {
854 const char* start = p;
855 if ( this->
CData() ) {
899 return visitor->
Visit( *
this );
918 const char* start = p;
945 return visitor->
Visit( *
this );
965 const char* start = p;
993 return visitor->
Visit( *
this );
1011 const char* start = p;
1039 return visitor->
Visit( *
this );
1043char* XMLAttribute::ParseDeep(
char* p,
bool processEntities )
1053 if ( !p || *p !=
'=' ) {
1059 if ( *p !=
'\"' && *p !=
'\'' ) {
1063 char endTag[2] = { *p, 0 };
1071void XMLAttribute::SetName(
const char* n )
1174XMLElement::~XMLElement()
1176 while( _rootAttribute ) {
1177 XMLAttribute* next = _rootAttribute->_next;
1179 _rootAttribute = next;
1186 XMLAttribute* a = 0;
1187 for( a=_rootAttribute; a; a = a->_next ) {
1199 for( a=_rootAttribute; a; a = a->_next ) {
1296XMLAttribute* XMLElement::FindOrCreateAttribute(
const char* name )
1300 for( attrib = _rootAttribute;
1302 last = attrib, attrib = attrib->_next ) {
1308 attrib =
new (
_document->_attributePool.Alloc() ) XMLAttribute();
1309 attrib->_memPool = &
_document->_attributePool;
1311 last->_next = attrib;
1314 _rootAttribute = attrib;
1316 attrib->SetName( name );
1328 prev->_next = a->_next;
1331 _rootAttribute = a->_next;
1341char* XMLElement::ParseAttributes(
char* p )
1343 const char* start = p;
1349 if ( !p || !(*p) ) {
1356 XMLAttribute* attrib =
new (
_document->_attributePool.Alloc() ) XMLAttribute();
1357 attrib->_memPool = &
_document->_attributePool;
1360 if ( !p ||
Attribute( attrib->Name() ) ) {
1370 if ( prevAttribute ) {
1371 prevAttribute->_next = attrib;
1374 _rootAttribute = attrib;
1376 prevAttribute = attrib;
1379 else if ( *p ==
'/' && *(p+1) ==
'>' ) {
1384 else if ( *p ==
'>' ) {
1422 p = ParseAttributes( p );
1423 if ( !p || !*p || _closingType ) {
1473 if ( visitor->
VisitEnter( *
this, _rootAttribute ) ) {
1475 if ( !node->Accept( visitor ) ) {
1488 _processEntities( processEntities ),
1490 _whitespace( whitespace ),
1502 delete [] _charBuffer;
1505 textPool.Trace(
"text" );
1506 elementPool.Trace(
"element" );
1507 commentPool.Trace(
"comment" );
1508 attributePool.Trace(
"attribute" );
1514 TIXMLASSERT( _attributePool.CurrentAllocs() == 0 );
1518void XMLDocument::InitDocument()
1524 delete [] _charBuffer;
1532 ele->_memPool = &_elementPool;
1541 comment->_memPool = &_commentPool;
1550 text->_memPool = &_textPool;
1559 dec->_memPool = &_commentPool;
1560 dec->
SetValue( str ? str :
"xml version=\"1.0\" encoding=\"UTF-8\"" );
1568 unk->_memPool = &_commentPool;
1580 MMechostr(MSKDEBUG,
"XMLDocument::LoadFile START\n");
1582#if defined(_MSC_VER) && (_MSC_VER >= 1400 )
1583 errno_t err = fopen_s(&fp, filename,
"rb" );
1586 fp = fopen( filename,
"rb" );
1589 MMechostr(MSKDEBUG,
"XMLDocument::LoadFile XML_ERROR_FILE_NOT_FOUND\n");
1604 fseek( fp, 0, SEEK_END );
1605 size_t size = ftell( fp );
1606 fseek( fp, 0, SEEK_SET );
1612 _charBuffer =
new char[size+1];
1613 size_t read = fread( _charBuffer, 1, size, fp );
1614 if ( read != size ) {
1619 _charBuffer[size] = 0;
1621 const char* p = _charBuffer;
1629 ParseDeep( _charBuffer + (p-_charBuffer), 0 );
1637#if defined(_MSC_VER) && (_MSC_VER >= 1400 )
1638 errno_t err = fopen_s(&fp, filename,
"w" );
1641 fp = fopen( filename,
"w" );
1670 if ( len == (
size_t)(-1) ) {
1673 _charBuffer =
new char[ len+1 ];
1674 memcpy( _charBuffer, p, len );
1675 _charBuffer[len] = 0;
1693 streamer = &stdStreamer;
1710 static const int LEN = 20;
1711 char buf1[LEN] = { 0 };
1712 char buf2[LEN] = { 0 };
1721 printf(
"XMLDocument error id=%d str1=%s str2=%s\n",
1722 _errorID, buf1, buf2 );
1728 _elementJustOpened( false ),
1729 _firstElement( true ),
1733 _processEntities( true ),
1734 _compactMode( compact )
1736 for(
int i=0; i<ENTITY_RANGE; ++i ) {
1737 _entityFlag[i] =
false;
1738 _restrictedEntityFlag[i] =
false;
1740 for(
int i=0; i<NUM_ENTITIES; ++i ) {
1742 if ( entities[i].value < ENTITY_RANGE ) {
1743 _entityFlag[ (int)entities[i].value ] =
true;
1746 _restrictedEntityFlag[(int)
'&'] =
true;
1747 _restrictedEntityFlag[(int)
'<'] =
true;
1748 _restrictedEntityFlag[(int)
'>'] =
true;
1753void XMLPrinter::Print(
const char* format, ... )
1756 va_start( va, format );
1759 vfprintf( _fp, format, va );
1768 len = vsnprintf_s( _accumulator.Mem(), _accumulator.Capacity(), _TRUNCATE, format, va );
1771 _accumulator.PushArr( expand );
1774 char* p = _buffer.
PushArr( len ) - 1;
1775 memcpy( p, _accumulator.Mem(), len+1 );
1777 int len = vsnprintf( 0, 0, format, va );
1780 va_start( va, format );
1781 char* p = _buffer.
PushArr( len ) - 1;
1782 vsnprintf( p, len+1, format, va );
1789void XMLPrinter::PrintSpace(
int depth )
1791 for(
int i=0; i<depth; ++i ) {
1797void XMLPrinter::PrintString(
const char* p,
bool restricted )
1801 const bool* flag = restricted ? _restrictedEntityFlag : _entityFlag;
1803 if ( _processEntities ) {
1806 if ( *q > 0 && *q < ENTITY_RANGE ) {
1810 if ( flag[(
unsigned)(*q)] ) {
1815 for(
int i=0; i<NUM_ENTITIES; ++i ) {
1816 if ( entities[i].value == *q ) {
1817 Print(
"&%s;", entities[i].pattern );
1829 if ( !_processEntities || (q-p > 0) ) {
1837 static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 };
1849 if ( _elementJustOpened ) {
1852 _stack.
Push( name );
1854 if ( _textDepth < 0 && !_firstElement && !_compactMode ) {
1856 PrintSpace( _depth );
1859 Print(
"<%s", name );
1860 _elementJustOpened =
true;
1861 _firstElement =
false;
1869 Print(
" %s=\"", name );
1870 PrintString( value,
false );
1910 const char* name = _stack.
Pop();
1912 if ( _elementJustOpened ) {
1916 if ( _textDepth < 0 && !_compactMode) {
1918 PrintSpace( _depth );
1920 Print(
"</%s>", name );
1923 if ( _textDepth == _depth ) {
1926 if ( _depth == 0 && !_compactMode) {
1929 _elementJustOpened =
false;
1933void XMLPrinter::SealElement()
1935 _elementJustOpened =
false;
1942 _textDepth = _depth-1;
1944 if ( _elementJustOpened ) {
1948 Print(
"<![CDATA[" );
1949 Print(
"%s", text );
1953 PrintString( text,
true );
1999 if ( _elementJustOpened ) {
2002 if ( _textDepth < 0 && !_firstElement && !_compactMode) {
2004 PrintSpace( _depth );
2006 _firstElement =
false;
2007 Print(
"<!--%s-->", comment );
2013 if ( _elementJustOpened ) {
2016 if ( _textDepth < 0 && !_firstElement && !_compactMode) {
2018 PrintSpace( _depth );
2020 _firstElement =
false;
2021 Print(
"<?%s?>", value );
2027 if ( _elementJustOpened ) {
2030 if ( _textDepth < 0 && !_firstElement && !_compactMode) {
2032 PrintSpace( _depth );
2034 _firstElement =
false;
2035 Print(
"<!%s>", value );
2052 while ( attribute ) {
2054 attribute = attribute->
Next();
@ NEEDS_ENTITY_PROCESSING
@ NEEDS_NEWLINE_NORMALIZATION
@ TEXT_ELEMENT_LEAVE_ENTITIES
void SetStr(const char *str, int flags=0)
void SetInternedStr(const char *str)
void Set(char *start, char *end, int flags)
char * ParseName(char *in)
char * ParseText(char *in, const char *endTag, int strFlags)
XMLError QueryFloatValue(float *value) const
See QueryIntAttribute.
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.
const char * Name() const
The name of the attribute.
XMLError QueryBoolValue(bool *value) const
See QueryIntAttribute.
XMLError QueryIntValue(int *value) const
const XMLAttribute * Next() const
The next attribute in the list.
const char * Value() const
The value of the attribute.
char * ParseDeep(char *, StrPair *endTag)
virtual ~XMLDeclaration()
virtual bool Accept(XMLVisitor *visitor) const
virtual XMLNode * ShallowClone(XMLDocument *document) const
virtual bool ShallowEqual(const XMLNode *compare) const
XMLDeclaration(XMLDocument *doc)
XMLError Parse(const char *xml, size_t nBytes=(size_t)(-1))
void PrintError() const
If there is an error, print it to stdout.
char * Identify(char *p, XMLNode **node)
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)
bool ProcessEntities() const
XMLError SaveFile(const char *filename, bool compact=false)
Whitespace WhitespaceMode() const
virtual bool Accept(XMLVisitor *visitor) const
XMLText * NewText(const char *text)
void SetError(XMLError error, const char *str1, const char *str2)
XMLDeclaration * NewDeclaration(const char *text=0)
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.
const XMLAttribute * FindAttribute(const char *name) const
Query a specific attribute in the list.
XMLError QueryUnsignedText(unsigned *_value) const
See QueryIntText()
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
virtual bool ShallowEqual(const XMLNode *compare) 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()
char * ParseDeep(char *p, StrPair *endTag)
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.)
void DeleteAttribute(const char *name)
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.
const XMLElement * FirstChildElement(const char *value=0) const
virtual XMLComment * ToComment()
Safely cast to a Comment, or null.
virtual char * ParseDeep(char *, StrPair *)
XMLNode * InsertAfterChild(XMLNode *afterThis, XMLNode *addThis)
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.
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 PushDeclaration(const char *value)
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)
void PushUnknown(const char *value)
XMLPrinter(std::FILE *file=0, bool compact=false)
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
char * ParseDeep(char *, StrPair *endTag)
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.
char * ParseDeep(char *, StrPair *endTag)
virtual bool Accept(XMLVisitor *visitor) const
XMLUnknown(XMLDocument *doc)
virtual XMLNode * ShallowClone(XMLDocument *document) const
virtual bool ShallowEqual(const XMLNode *compare) const
static int IsAlpha(unsigned char anyByte)
static const char * SkipWhiteSpace(const char *p)
static bool ToUnsigned(const char *str, unsigned *value)
static void ConvertUTF32ToUTF8(unsigned long input, char *output, int *length)
static bool IsWhiteSpace(char p)
static bool ToFloat(const char *str, float *value)
static void ToStr(int v, char *buffer, int bufferSize)
static const char * GetCharacterRef(const char *p, char *value, int *length)
static int IsAlphaNum(unsigned char anyByte)
static bool StringEqual(const char *p, const char *q, int nChar=INT_MAX)
static bool ToInt(const char *str, int *value)
static bool ToDouble(const char *str, double *value)
static bool ToBool(const char *str, bool *value)
static const char * ReadBOM(const char *p, bool *hasBOM)
virtual bool VisitExit(const XMLDocument &)
Visit a document.
virtual bool VisitEnter(const XMLDocument &)
Visit a document.
virtual bool Visit(const XMLDeclaration &)
Visit a declaration.
@ XML_ERROR_MISMATCHED_ELEMENT
@ XML_ERROR_EMPTY_DOCUMENT
@ XML_ERROR_PARSING_ATTRIBUTE
@ XML_ERROR_FILE_NOT_FOUND
@ XML_ERROR_PARSING_COMMENT
@ XML_ERROR_FILE_READ_ERROR
@ XML_ERROR_PARSING_UNKNOWN
@ XML_ERROR_PARSING_CDATA
@ XML_ERROR_PARSING_DECLARATION
@ XML_WRONG_ATTRIBUTE_TYPE
@ XML_ERROR_PARSING_ELEMENT
@ XML_ERROR_FILE_COULD_NOT_BE_OPENED
@ XML_CAN_NOT_CONVERT_TEXT
#define DELETE_ATTRIBUTE(attrib)
#define DELETE_NODE(node)