40 static const char LINE_FEED = (char)0x0a;
41 static const char LF = LINE_FEED;
42 static const char CARRIAGE_RETURN = (char)0x0d;
43 static const char CR = CARRIAGE_RETURN;
44 static const char SINGLE_QUOTE =
'\'';
45 static const char DOUBLE_QUOTE =
'\"';
51 static const unsigned char TIXML_UTF_LEAD_0 = 0xefU;
52 static const unsigned char TIXML_UTF_LEAD_1 = 0xbbU;
53 static const unsigned char TIXML_UTF_LEAD_2 = 0xbfU;
56 #define DELETE_NODE( node ) { \
58 MemPool* pool = node->memPool; \
63 #define DELETE_ATTRIBUTE( attrib ) { \
65 MemPool* pool = attrib->memPool; \
66 attrib->~XMLAttribute(); \
67 pool->Free( attrib ); \
77 static const int NUM_ENTITIES = 5;
78 static const Entity entities[NUM_ENTITIES] =
80 {
"quot", 4, DOUBLE_QUOTE },
82 {
"apos", 4, SINGLE_QUOTE },
111 size_t len = strlen( str );
112 start =
new char[ len+1 ];
113 memcpy(
start, str, len+1 );
125 char endChar = *endTag;
126 size_t length = strlen( endTag );
130 if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
131 Set( start, p, strFlags );
145 if ( !start || !(*start) ) {
153 || (*p ==
'-' && p>start )
154 || (*p ==
'.' && p>start ) ))
210 if ( *(p+1) == LF ) {
218 else if ( (
flags & NEEDS_NEWLINE_NORMALIZATION) && *p == LF ) {
219 if ( *(p+1) == CR ) {
233 if ( *(p+1) ==
'#' ) {
234 char buf[10] = { 0 };
237 for(
int i=0; i<len; ++i ) {
244 for(; i<NUM_ENTITIES; ++i ) {
245 if ( strncmp( p+1, entities[i].pattern, entities[i].length ) == 0
246 && *(p+entities[i].length+1) ==
';' )
249 *q = entities[i].
value;
251 p += entities[i].
length + 2;
255 if ( i == NUM_ENTITIES ) {
289 const unsigned char* pu =
reinterpret_cast<const unsigned char*
>(p);
291 if ( *(pu+0) == TIXML_UTF_LEAD_0
292 && *(pu+1) == TIXML_UTF_LEAD_1
293 && *(pu+2) == TIXML_UTF_LEAD_2 )
305 const unsigned long BYTE_MASK = 0xBF;
306 const unsigned long BYTE_MARK = 0x80;
307 const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
311 else if ( input < 0x800 )
313 else if ( input < 0x10000 )
315 else if ( input < 0x200000 )
318 { *length = 0;
return; }
327 *output = (char)((input | BYTE_MARK) & BYTE_MASK);
331 *output = (char)((input | BYTE_MARK) & BYTE_MASK);
335 *output = (char)((input | BYTE_MARK) & BYTE_MASK);
339 *output = (char)(input | FIRST_BYTE_MARK[*length]);
350 if ( *(p+1) ==
'#' && *(p+2) )
352 unsigned long ucs = 0;
359 if ( !*(p+3) )
return 0;
362 q = strchr( q,
';' );
364 if ( !q || !*q )
return 0;
371 if ( *q >=
'0' && *q <=
'9' )
372 ucs += mult * (*q -
'0');
373 else if ( *q >=
'a' && *q <=
'f' )
374 ucs += mult * (*q -
'a' + 10);
375 else if ( *q >=
'A' && *q <=
'F' )
376 ucs += mult * (*q -
'A' + 10 );
386 if ( !*(p+2) )
return 0;
389 q = strchr( q,
';' );
391 if ( !q || !*q )
return 0;
398 if ( *q >=
'0' && *q <=
'9' )
399 ucs += mult * (*q -
'0');
408 return p + delta + 1;
469 if (
ToInt( str, &ival )) {
470 *value = (ival==0) ?
false :
true;
522 static const char* xmlHeader = {
"<?" };
523 static const char* commentHeader = {
"<!--" };
524 static const char* dtdHeader = {
"<!" };
525 static const char* cdataHeader = {
"<![CDATA[" };
526 static const char* elementHeader = {
"<" };
528 static const int xmlHeaderLen = 2;
529 static const int commentHeaderLen = 4;
530 static const int dtdHeaderLen = 2;
531 static const int cdataHeaderLen = 9;
532 static const int elementHeaderLen = 1;
534 #if defined(_MSC_VER)
535 #pragma warning ( push )
536 #pragma warning ( disable : 4127 )
540 #if defined(_MSC_VER)
541 #pragma warning (pop)
551 p += commentHeaderLen;
568 p += elementHeaderLen;
588 if ( !node->Accept( visitor ) )
602 firstChild( 0 ), lastChild( 0 ),
721 if ( afterThis->
parent !=
this )
724 if ( afterThis->
next == 0 ) {
728 addThis->
prev = afterThis;
731 afterThis->
next = addThis;
773 if ( element->ToElement()
787 if ( element->ToElement()
821 if ( p == 0 || node == 0 ) {
857 else if ( !endTag.
Empty() ) {
879 const char* start = p;
880 if ( this->
CData() ) {
926 return visitor->
Visit( *
this );
949 const char* start = p;
979 return visitor->
Visit( *
this );
1002 const char* start = p;
1033 return visitor->
Visit( *
this );
1054 const char* start = p;
1085 return visitor->
Visit( *
this );
1094 if ( !p || !*p )
return 0;
1098 if ( !p || *p !=
'=' )
return 0;
1102 if ( *p !=
'\"' && *p !=
'\'' )
return 0;
1104 char endTag[2] = { *p, 0 };
1359 last = attrib, attrib = attrib->
next )
1369 last->
next = attrib;
1403 const char* start = p;
1409 if ( !p || !(*p) ) {
1430 if ( prevAttribute ) {
1431 prevAttribute->
next = attrib;
1436 prevAttribute = attrib;
1439 else if ( *p ==
'/' && *(p+1) ==
'>' ) {
1444 else if ( *p ==
'>' ) {
1536 if ( !node->Accept( visitor ) )
1549 processEntities( _processEntities ),
1551 whitespace( _whitespace ),
1628 dec->
SetValue( str ? str :
"xml version=\"1.0\" encoding=\"UTF-8\"" );
1650 #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
1651 errno_t err = fopen_s(&fp, filename,
"rb" );
1654 fp = fopen( filename,
"rb" );
1672 fseek( fp, 0, SEEK_END );
1673 unsigned size = ftell( fp );
1674 fseek( fp, 0, SEEK_SET );
1681 size_t read = fread(
charBuffer, 1, size, fp );
1682 if ( read != size ) {
1706 #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
1707 errno_t err = fopen_s(&fp, filename,
"w" );
1710 fp = fopen( filename,
"w" );
1741 if ( len == (
size_t)(-1) ) {
1765 streamer = &stdStreamer;
1783 static const int LEN = 20;
1784 char buf1[LEN] = { 0 };
1785 char buf2[LEN] = { 0 };
1794 printf(
"XMLDocument error id=%d str1=%s str2=%s\n",
1802 elementJustOpened( false ),
1803 firstElement( true ),
1808 compactMode( compact )
1814 for(
int i=0; i<NUM_ENTITIES; ++i ) {
1816 if ( entities[i].value < ENTITY_RANGE ) {
1831 va_start( va, format );
1834 vfprintf( fp, format, va );
1843 len = vsnprintf_s( accumulator.Mem(), accumulator.Capacity(), _TRUNCATE, format, va );
1846 accumulator.PushArr( expand );
1850 memcpy( p, accumulator.Mem(), len+1 );
1852 int len = vsnprintf( 0, 0, format, va );
1855 va_start( va, format );
1857 vsnprintf( p, len+1, format, va );
1867 for(
int i=0; i<
depth; ++i ) {
1887 if ( flag[(
unsigned)(*q)] ) {
1892 for(
int i=0; i<NUM_ENTITIES; ++i ) {
1893 if ( entities[i].value == *q ) {
1894 Print(
"&%s;", entities[i].pattern );
1915 static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 };
1938 Print(
"<%s", name );
1949 Print(
" %s=\"", name );
2005 Print(
"</%s>", name );
2033 Print(
"<![CDATA[" );
2034 Print(
"%s", text );
2099 Print(
"<!--%s-->", comment );
2114 Print(
"<?%s?>", value );
2129 Print(
"<!%s>", value );
2148 while ( attribute ) {
2150 attribute = attribute->
Next();
void PushComment(const char *comment)
Add a comment.
const char * Attribute(const char *name, const char *value=0) const
#define DELETE_NODE(node)
XMLNode * InsertFirstChild(XMLNode *addThis)
virtual bool Accept(XMLVisitor *visitor) const
static bool ToFloat(const char *str, float *value)
virtual bool ShallowEqual(const XMLNode *compare) const
XMLDeclaration(XMLDocument *doc)
int QueryUnsignedValue(unsigned int *value) const
See QueryIntAttribute.
int QueryBoolValue(bool *value) const
See QueryIntAttribute.
void SetName(const char *str, bool staticMem=false)
Set the name of the element.
const XMLElement * PreviousSiblingElement(const char *value=0) const
Get the previous (left) sibling element of this node, with an opitionally supplied name...
void Unlink(XMLNode *child)
DynArray< char, 20 > buffer
virtual ~XMLDeclaration()
MemPoolT< sizeof(XMLText) > textPool
const char * Value() const
The value of the attribute.
const XMLAttribute * Next() const
The next attribute in the list.
static void ConvertUTF32ToUTF8(unsigned long input, char *output, int *length)
char * ParseDeep(char *, StrPair *endTag)
void DeleteChild(XMLNode *node)
void Set(char *_start, char *_end, int _flags)
static bool StringEqual(const char *p, const char *q, int nChar=INT_MAX)
char * ParseDeep(char *, StrPair *endTag)
XMLElement(XMLDocument *doc)
MemPoolT< sizeof(XMLElement) > elementPool
virtual XMLText * ToText()
Safely cast to Text, or null.
#define DELETE_ATTRIBUTE(attrib)
char * ParseText(char *in, const char *endTag, int strFlags)
static void ToStr(int v, char *buffer, int bufferSize)
XMLComment * NewComment(const char *comment)
bool CData() const
Returns true if this is a CDATA text element.
const XMLElement * LastChildElement(const char *value=0) const
static bool IsWhiteSpace(char p)
virtual XMLUnknown * ToUnknown()
Safely cast to an Unknown, or null.
char * ParseDeep(char *p, StrPair *endTag)
XMLAttribute * rootAttribute
virtual XMLNode * ShallowClone(XMLDocument *document) const
virtual bool Accept(XMLVisitor *visitor) const
int QueryFloatText(float *_value) const
See QueryIntText()
int Parse(const char *xml, size_t nBytes=(size_t)(-1))
const char * Name() const
Get the name of an element (which is the Value() of the node.)
int LoadFile(const char *filename)
void PushUnknown(const char *value)
XMLElement * NewElement(const char *name)
bool Error() const
Return true if there was an error parsing the document.
const XMLNode * NextSibling() const
Get the next (right) sibling node of this node.
static bool ToDouble(const char *str, double *value)
XMLNode * InsertAfterChild(XMLNode *afterThis, XMLNode *addThis)
int QueryIntValue(int *value) const
void DeleteAttribute(const char *name)
virtual bool ShallowEqual(const XMLNode *compare) const
char * ParseName(char *in)
virtual XMLNode * ShallowClone(XMLDocument *document) const
virtual bool Accept(XMLVisitor *visitor) const
void Print(const char *format,...)
char * ParseDeep(char *, StrPair *endTag)
virtual bool VisitEnter(const XMLDocument &)
Visit a document.
virtual XMLElement * ToElement()
Safely cast to an Element, or null.
const XMLElement * NextSiblingElement(const char *value=0) const
Get the next (right) sibling element of this node, with an opitionally supplied name.
static int IsAlpha(unsigned char anyByte)
const XMLAttribute * FirstAttribute() const
Return the first attribute in the list.
XMLDeclaration * NewDeclaration(const char *text=0)
virtual XMLDeclaration * ToDeclaration()
Safely cast to a Declaration, or null.
void SetName(const char *name)
virtual bool Visit(const XMLDeclaration &)
Visit a declaration.
char * ParseAttributes(char *p)
virtual bool Accept(XMLVisitor *visitor) const
XMLAttribute * FindOrCreateAttribute(const char *name)
int QueryIntText(int *_value) const
virtual bool ShallowEqual(const XMLNode *compare) const
void PushAttribute(const char *name, const char *value)
If streaming, add an attribute to an open element.
void PushDeclaration(const char *value)
char * ParseDeep(char *, StrPair *endTag)
XMLPrinter(FILE *file=0, bool compact=false)
static const char * GetCharacterRef(const char *p, char *value, int *length)
virtual bool Accept(XMLVisitor *visitor) const
XMLNode * InsertEndChild(XMLNode *addThis)
static const char * SkipWhiteSpace(const char *p)
void SetError(int error, const char *str1, const char *str2)
void SetCData(bool _isCData)
Declare whether this should be CDATA or standard text.
void SetStr(const char *str, int flags=0)
void Print(XMLPrinter *streamer=0)
const XMLAttribute * FindAttribute(const char *name) const
Query a specific attribute in the list.
const XMLElement * FirstChildElement(const char *value=0) const
virtual XMLNode * ShallowClone(XMLDocument *document) const
void SetInternedStr(const char *str)
void SetValue(const char *val, bool staticMem=false)
bool restrictedEntityFlag[ENTITY_RANGE]
static int IsAlphaNum(unsigned char anyByte)
MemPoolT< sizeof(XMLComment) > commentPool
XMLComment(XMLDocument *doc)
int QueryDoubleText(double *_value) const
See QueryIntText()
virtual bool Visit(const XMLText &text)
Visit a text node.
virtual char * ParseDeep(char *, StrPair *)
virtual XMLElement * ToElement()
Safely cast to an Element, or null.
void PushHeader(bool writeBOM, bool writeDeclaration)
virtual bool VisitExit(const XMLDocument &)
Visit a document.
const char * Value() const
virtual XMLNode * ShallowClone(XMLDocument *document) const
int SaveFile(const char *filename, bool compact=false)
XMLUnknown(XMLDocument *doc)
void OpenElement(const char *name)
const char * Name() const
The name of the attribute.
char * Identify(char *p, XMLNode **node)
XMLText * NewText(const char *text)
char * ParseDeep(char *p, bool processEntities)
void SetAttribute(const char *name, const char *_value)
Sets the named attribute to value.
static bool ToUnsigned(const char *str, unsigned *value)
static bool ToBool(const char *str, bool *value)
static const char * ReadBOM(const char *p, bool *hasBOM)
DynArray< const char *, 10 > stack
MemPoolT< sizeof(XMLAttribute) > attributePool
const XMLNode * FirstChild() const
Get the first child node, or null if none exists.
XMLUnknown * NewUnknown(const char *text)
void CloseElement()
If streaming, close the Element.
const char * GetText() const
virtual XMLNode * ShallowClone(XMLDocument *document) const
int QueryFloatValue(float *value) const
See QueryIntAttribute.
virtual bool ShallowEqual(const XMLNode *compare) const
virtual bool VisitEnter(const XMLDocument &)
Visit a document.
int QueryDoubleValue(double *value) const
See QueryIntAttribute.
virtual bool Accept(XMLVisitor *visitor) const
int QueryBoolText(bool *_value) const
See QueryIntText()
XMLDocument(bool processEntities=true, Whitespace=PRESERVE_WHITESPACE)
constructor
int QueryUnsignedText(unsigned *_value) const
See QueryIntText()
void PrintString(const char *, bool restrictedEntitySet)
void PrintError() const
If there is an error, print it to stdout.
void CollapseWhitespace()
static bool ToInt(const char *str, int *value)
Whitespace WhitespaceMode() const
void PrintSpace(int depth)
virtual bool ShallowEqual(const XMLNode *compare) const
virtual bool VisitExit(const XMLDocument &)
Visit a document.
bool entityFlag[ENTITY_RANGE]
bool ProcessEntities() const
void SetAttribute(const char *value)
Set the attribute to a string value.
void PushText(const char *text, bool cdata=false)
Add a text node.
virtual XMLComment * ToComment()
Safely cast to a Comment, or null.