|
|
@@ -31,6 +31,137 @@ static const char CR = CARRIAGE_RETURN;
|
|
|
}
|
|
|
|
|
|
|
|
|
+// --------- XMLBase ----------- //
|
|
|
+const char* XMLBase::ParseText( char* p, const char* endTag, char** next )
|
|
|
+{
|
|
|
+ TIXMLASSERT( endTag && *endTag );
|
|
|
+
|
|
|
+ char* start = p;
|
|
|
+ char* q = p; // q (target) <= p (src) in same buffer.
|
|
|
+ char endChar = *endTag;
|
|
|
+ int length = strlen( endTag );
|
|
|
+ char* nextTag = 0;
|
|
|
+ *next = 0;
|
|
|
+
|
|
|
+ // Inner loop of text parsing.
|
|
|
+ while ( *p ) {
|
|
|
+ if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
|
|
|
+ *q = 0;
|
|
|
+ nextTag = p + length;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ else if ( *p == CR ) {
|
|
|
+ // CR-LF pair becomes LF
|
|
|
+ // CR alone becomes LF
|
|
|
+ // LF-CR becomes LF
|
|
|
+ if ( *(p+1) == LF ) {
|
|
|
+ p += 2;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ ++p;
|
|
|
+ }
|
|
|
+ *q = LF;
|
|
|
+ }
|
|
|
+ else if ( *p == LF ) {
|
|
|
+ if ( *(p+1) == CR ) {
|
|
|
+ p += 2;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ ++p;
|
|
|
+ }
|
|
|
+ *q = LF;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ *q = *p;
|
|
|
+ ++p;
|
|
|
+ }
|
|
|
+ ++q;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Error? If we don't have a text tag, something went wrong. (Although
|
|
|
+ // what the nextTag points at may be null.)
|
|
|
+ if ( nextTag == 0 ) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ *next = nextTag;
|
|
|
+ return start;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+const char* XMLBase::ParseName( char* p, char** next )
|
|
|
+{
|
|
|
+ char* start = p;
|
|
|
+ char* nextTag = 0;
|
|
|
+ *next = 0;
|
|
|
+
|
|
|
+ start = p;
|
|
|
+ if ( !start || !(*start) ) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ( !IsAlpha( *p ) ) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ while( *p && (
|
|
|
+ IsAlphaNum( (unsigned char) *p )
|
|
|
+ || *p == '_'
|
|
|
+ || *p == '-'
|
|
|
+ || *p == '.'
|
|
|
+ || *p == ':' ))
|
|
|
+ {
|
|
|
+ ++p;
|
|
|
+ }
|
|
|
+ *p = 0;
|
|
|
+
|
|
|
+ if ( p > start ) {
|
|
|
+ *next = p;
|
|
|
+ return start;
|
|
|
+ }
|
|
|
+ return p+1;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+char* XMLBase::Identify( XMLDocument* document, char* p, XMLNode** node )
|
|
|
+{
|
|
|
+ XMLNode* returnNode = 0;
|
|
|
+
|
|
|
+ p = XMLNode::SkipWhiteSpace( p );
|
|
|
+ if( !p || !*p || *p != '<' )
|
|
|
+ {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // What is this thing?
|
|
|
+ // - Elements start with a letter or underscore, but xml is reserved.
|
|
|
+ // - Comments: <!--
|
|
|
+ // - Decleration: <?xml
|
|
|
+ // - Everthing else is unknown to tinyxml.
|
|
|
+ //
|
|
|
+
|
|
|
+ static const char* xmlHeader = { "<?xml" };
|
|
|
+ static const char* commentHeader = { "<!--" };
|
|
|
+ static const char* dtdHeader = { "<!" };
|
|
|
+ static const char* cdataHeader = { "<![CDATA[" };
|
|
|
+
|
|
|
+ static const int xmlHeaderLen = 5;
|
|
|
+ static const int commentHeaderLen = 4;
|
|
|
+ static const int dtdHeaderLen = 2;
|
|
|
+ static const int cdataHeaderLen = 9;
|
|
|
+
|
|
|
+ if ( XMLNode::StringEqual( p, commentHeader, commentHeaderLen ) ) {
|
|
|
+ returnNode = new XMLComment( document );
|
|
|
+ p += commentHeaderLen;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ TIXMLASSERT( 0 );
|
|
|
+ }
|
|
|
+
|
|
|
+ *node = returnNode;
|
|
|
+ return p;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
// --------- XMLNode ----------- //
|
|
|
|
|
|
XMLNode::XMLNode( XMLDocument* doc ) :
|
|
|
@@ -51,6 +182,12 @@ XMLNode::~XMLNode()
|
|
|
delete node;
|
|
|
node = temp;
|
|
|
}
|
|
|
+ if ( prev ) {
|
|
|
+ prev->next = next;
|
|
|
+ }
|
|
|
+ if ( next ) {
|
|
|
+ next->prev = prev;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -93,71 +230,17 @@ void XMLNode::PrintSpace( FILE* fp, int depth )
|
|
|
}
|
|
|
|
|
|
|
|
|
-const char* XMLNode::ParseText( char* p, const char* endTag, char** next )
|
|
|
-{
|
|
|
- TIXMLASSERT( endTag && *endTag );
|
|
|
-
|
|
|
- char* start = p;
|
|
|
- char* q = p; // q (target) <= p (src) in same buffer.
|
|
|
- char endChar = *endTag;
|
|
|
- int length = strlen( endTag );
|
|
|
- char* nextTag = 0;
|
|
|
-
|
|
|
- // Inner loop of text parsing.
|
|
|
- while ( *p ) {
|
|
|
- if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
|
|
|
- *q = 0;
|
|
|
- nextTag = p + length;
|
|
|
- break;
|
|
|
- }
|
|
|
- else if ( *p == CR ) {
|
|
|
- // CR-LF pair becomes LF
|
|
|
- // CR alone becomes LF
|
|
|
- // LF-CR becomes LF
|
|
|
- if ( *(p+1) == LF ) {
|
|
|
- p += 2;
|
|
|
- }
|
|
|
- else {
|
|
|
- ++p;
|
|
|
- }
|
|
|
- *q = LF;
|
|
|
- }
|
|
|
- else if ( *p == LF ) {
|
|
|
- if ( *(p+1) == CR ) {
|
|
|
- p += 2;
|
|
|
- }
|
|
|
- else {
|
|
|
- ++p;
|
|
|
- }
|
|
|
- *q = LF;
|
|
|
- }
|
|
|
- else {
|
|
|
- *q = *p;
|
|
|
- ++p;
|
|
|
- }
|
|
|
- ++q;
|
|
|
- }
|
|
|
-
|
|
|
- // Error? If we don't have a text tag, something went wrong. (Although
|
|
|
- // what the nextTag points at may be null.)
|
|
|
- if ( nextTag == 0 ) {
|
|
|
- return 0;
|
|
|
- }
|
|
|
- *next = nextTag;
|
|
|
- return start;
|
|
|
-}
|
|
|
|
|
|
|
|
|
// --------- XMLComment ---------- //
|
|
|
|
|
|
-XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
|
|
|
+XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc ), value( 0 )
|
|
|
{
|
|
|
}
|
|
|
|
|
|
|
|
|
XMLComment::~XMLComment()
|
|
|
{
|
|
|
-
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -176,6 +259,132 @@ char* XMLComment::ParseDeep( char* p )
|
|
|
}
|
|
|
|
|
|
|
|
|
+// --------- XMLAttribute ---------- //
|
|
|
+char* XMLAttribute::ParseDeep( char* p )
|
|
|
+{
|
|
|
+ char endTag[2] = { *p, 0 };
|
|
|
+ ++p;
|
|
|
+ value = ParseText( p, endTag, &p );
|
|
|
+ if ( !value ) return 0;
|
|
|
+ return p;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+// --------- XMLElement ---------- //
|
|
|
+XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
|
|
|
+ name( 0 ),
|
|
|
+ closing( false ),
|
|
|
+ rootAttribute( 0 ),
|
|
|
+ lastAttribute( 0 )
|
|
|
+{
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+XMLElement::~XMLElement()
|
|
|
+{
|
|
|
+ XMLAttribute* attribute = rootAttribute;
|
|
|
+ while( attribute ) {
|
|
|
+ XMLAttribute* next = attribute->next;
|
|
|
+ delete attribute;
|
|
|
+ attribute = next;
|
|
|
+ }
|
|
|
+
|
|
|
+ XMLNode* child = firstChild;
|
|
|
+ while( child ) {
|
|
|
+ XMLNode* next = child->next;
|
|
|
+ delete child;
|
|
|
+ child = next;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+char* XMLElement::ParseDeep( char* p )
|
|
|
+{
|
|
|
+ // Read the element name.
|
|
|
+ p = SkipWhiteSpace( p );
|
|
|
+ if ( !p ) return 0;
|
|
|
+ const char* start = p;
|
|
|
+
|
|
|
+ // The closing element is the </element> form. It is
|
|
|
+ // parsed just like a regular element then deleted from
|
|
|
+ // the DOM.
|
|
|
+ if ( *p == '/' ) {
|
|
|
+ closing = true;
|
|
|
+ ++p;
|
|
|
+ }
|
|
|
+
|
|
|
+ name = ParseName( p, &p );
|
|
|
+ if ( !name ) return 0;
|
|
|
+
|
|
|
+ // Read the attributes.
|
|
|
+ while( p ) {
|
|
|
+ p = SkipWhiteSpace( p );
|
|
|
+ if ( !p || !(*p) ) {
|
|
|
+ document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, name );
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ const char* saveP = p;
|
|
|
+
|
|
|
+ // attribute.
|
|
|
+ if ( *p == '\'' || *p == '\"' ) {
|
|
|
+ XMLAttribute* attrib = new XMLAttribute( this );
|
|
|
+ p = attrib->ParseDeep( p );
|
|
|
+ if ( !p ) {
|
|
|
+ delete attrib;
|
|
|
+ document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, saveP );
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ if ( rootAttribute ) {
|
|
|
+ TIXMLASSERT( lastAttribute );
|
|
|
+ lastAttribute->next = attrib;
|
|
|
+ lastAttribute = attrib;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ rootAttribute = lastAttribute = attrib;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if ( *p == '/' && *(p+1) == '>' ) {
|
|
|
+ // end tag.
|
|
|
+ if ( closing ) {
|
|
|
+ document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ return p+2; // done; sealed element.
|
|
|
+ }
|
|
|
+ else if ( *p == '>' ) {
|
|
|
+ ++p;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ while( p && *p ) {
|
|
|
+ XMLNode* node = 0;
|
|
|
+ p = Identify( document, p, &node );
|
|
|
+ if ( p && node ) {
|
|
|
+ node->ParseDeep( p );
|
|
|
+
|
|
|
+ XMLElement* element = node->ToElement();
|
|
|
+ if ( element && element->Closing() ) {
|
|
|
+ if ( StringEqual( element->Name(), this->Name() ) ) {
|
|
|
+ // All good, this is closing tag.
|
|
|
+ delete node;
|
|
|
+ p = 0;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
|
|
|
+ delete node;
|
|
|
+ }
|
|
|
+ return p;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ this->InsertEndChild( node );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
// --------- XMLDocument ----------- //
|
|
|
XMLDocument::XMLDocument() :
|
|
|
charBuffer( 0 )
|
|
|
@@ -197,7 +406,7 @@ bool XMLDocument::Parse( const char* p )
|
|
|
charBuffer = CharBuffer::Construct( p );
|
|
|
XMLNode* node = 0;
|
|
|
|
|
|
- char* q = Identify( charBuffer->mem, &node );
|
|
|
+ char* q = Identify( this, charBuffer->mem, &node );
|
|
|
root->InsertEndChild( node );
|
|
|
node->ParseDeep( q );
|
|
|
|
|
|
@@ -213,41 +422,3 @@ void XMLDocument::Print( FILE* fp, int depth )
|
|
|
}
|
|
|
|
|
|
|
|
|
-char* XMLDocument::Identify( char* p, XMLNode** node )
|
|
|
-{
|
|
|
- XMLNode* returnNode = 0;
|
|
|
-
|
|
|
- p = XMLNode::SkipWhiteSpace( p );
|
|
|
- if( !p || !*p || *p != '<' )
|
|
|
- {
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- // What is this thing?
|
|
|
- // - Elements start with a letter or underscore, but xml is reserved.
|
|
|
- // - Comments: <!--
|
|
|
- // - Decleration: <?xml
|
|
|
- // - Everthing else is unknown to tinyxml.
|
|
|
- //
|
|
|
-
|
|
|
- static const char* xmlHeader = { "<?xml" };
|
|
|
- static const char* commentHeader = { "<!--" };
|
|
|
- static const char* dtdHeader = { "<!" };
|
|
|
- static const char* cdataHeader = { "<![CDATA[" };
|
|
|
-
|
|
|
- static const int xmlHeaderLen = 5;
|
|
|
- static const int commentHeaderLen = 4;
|
|
|
- static const int dtdHeaderLen = 2;
|
|
|
- static const int cdataHeaderLen = 9;
|
|
|
-
|
|
|
- if ( XMLNode::StringEqual( p, commentHeader, commentHeaderLen ) ) {
|
|
|
- returnNode = new XMLComment( this );
|
|
|
- p += commentHeaderLen;
|
|
|
- }
|
|
|
- else {
|
|
|
- TIXMLASSERT( 0 );
|
|
|
- }
|
|
|
-
|
|
|
- *node = returnNode;
|
|
|
- return p;
|
|
|
-}
|