new element loop

This commit is contained in:
Lee Thomason (grinliz) 2012-02-23 22:27:28 -08:00
parent d627776dd3
commit 46a14cfec7
3 changed files with 481 additions and 409 deletions

View File

@ -27,8 +27,20 @@ static const unsigned char TIXML_UTF_LEAD_1 = 0xbbU;
static const unsigned char TIXML_UTF_LEAD_2 = 0xbfU; static const unsigned char TIXML_UTF_LEAD_2 = 0xbfU;
#define DELETE_NODE( node ) { MemPool* pool = node->memPool; node->~XMLNode(); pool->Free( node ); } #define DELETE_NODE( node ) { \
#define DELETE_ATTRIBUTE( attrib ) { MemPool* pool = attrib->memPool; attrib->~XMLAttribute(); pool->Free( attrib ); } if ( node ) { \
MemPool* pool = node->memPool; \
node->~XMLNode(); \
pool->Free( node ); \
} \
}
#define DELETE_ATTRIBUTE( attrib ) { \
if ( attrib ) { \
MemPool* pool = attrib->memPool; \
attrib->~XMLAttribute(); \
pool->Free( attrib ); \
} \
}
struct Entity { struct Entity {
const char* pattern; const char* pattern;
@ -397,6 +409,11 @@ char* XMLDocument::Identify( char* p, XMLNode** node )
returnNode = new (elementPool.Alloc()) XMLElement( this ); returnNode = new (elementPool.Alloc()) XMLElement( this );
returnNode->memPool = &elementPool; returnNode->memPool = &elementPool;
p += elementHeaderLen; p += elementHeaderLen;
p = XMLUtil::SkipWhiteSpace( p );
if ( p && *p == '/' ) {
((XMLElement*)returnNode)->closingType = XMLElement::CLOSING;
}
} }
else { else {
returnNode = new (textPool.Alloc()) XMLText( this ); returnNode = new (textPool.Alloc()) XMLText( this );
@ -587,22 +604,77 @@ const XMLElement* XMLNode::LastChildElement( const char* value ) const
char* XMLNode::ParseDeep( char* p ) char* XMLNode::ParseDeep( char* p )
{ {
// This is a recursive method, but thinking about it "at the current level"
// it is a pretty simple flat list:
// <foo/>
// <!-- comment -->
//
// With a special case:
// <foo>
// </foo>
// <!-- comment -->
//
// Where the closing element (/foo) *must* be the next thing after the opening
// element, and the names must match. BUT the tricky bit is that the closing
// element will be read by the child.
while( p && *p ) { while( p && *p ) {
XMLNode* node = 0; XMLNode* node = 0;
p = document->Identify( p, &node ); char* mark = p;
if ( p && node ) {
p = node->ParseDeep( p );
if ( node->IsClosingElement() ) { p = document->Identify( p, &node );
if ( !XMLUtil::StringEqual( Value(), node->Value() )) { if ( p == 0 ) {
document->SetError( ERROR_MISMATCHED_ELEMENT, Value(), 0 ); break;
} }
// We read the end tag. Back up and return.
if ( node && node->ToElement() && node->ToElement()->ClosingType() == XMLElement::CLOSING ) {
DELETE_NODE( node ); DELETE_NODE( node );
return p; return mark;
} }
if ( node ) {
p = node->ParseDeep( p );
if ( !p ) {
DELETE_NODE( node );
node = 0;
break;
}
XMLElement* ele = node->ToElement();
if ( ele && ele->ClosingType() == XMLElement::OPEN ) {
XMLNode* closingNode = 0;
p = document->Identify( p, &closingNode );
XMLElement* closingEle = closingNode ? closingNode->ToElement() : 0;
if ( closingEle == 0 ) {
document->SetError( ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
p = 0;
}
else if ( closingEle->ClosingType() != XMLElement::CLOSING ) {
document->SetError( ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
p = 0;
}
else
{
p = closingEle->ParseDeep( p );
if ( !XMLUtil::StringEqual( closingEle->Value(), node->Value() )) {
document->SetError( ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
p = 0;
}
}
// Else everything is fine, but we need to throw away the node.
DELETE_NODE( closingNode );
if ( p == 0 ) {
DELETE_NODE( node );
node = 0;
}
}
if ( node ) {
this->InsertEndChild( node ); this->InsertEndChild( node );
} }
} }
}
return 0; return 0;
} }
@ -736,7 +808,7 @@ char* XMLAttribute::ParseDeep( char* p )
char endTag[2] = { *p, 0 }; char endTag[2] = { *p, 0 };
++p; ++p;
p = value.ParseText( p, endTag, StrPair::ATTRIBUTE_VALUE ); p = value.ParseText( p, endTag, StrPair::ATTRIBUTE_VALUE );
if ( value.Empty() ) return 0; //if ( value.Empty() ) return 0;
return p; return p;
} }
@ -842,9 +914,8 @@ void XMLAttribute::SetAttribute( float v )
// --------- XMLElement ---------- // // --------- XMLElement ---------- //
XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ), XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
closing( false ), closingType( 0 ),
rootAttribute( 0 ) rootAttribute( 0 )
//lastAttribute( 0 )
{ {
} }
@ -937,10 +1008,9 @@ void XMLElement::DeleteAttribute( const char* name )
} }
char* XMLElement::ParseAttributes( char* p, bool* closedElement ) char* XMLElement::ParseAttributes( char* p )
{ {
const char* start = p; const char* start = p;
*closedElement = false;
// Read the attributes. // Read the attributes.
while( p ) { while( p ) {
@ -965,11 +1035,7 @@ char* XMLElement::ParseAttributes( char* p, bool* closedElement )
} }
// end of the tag // end of the tag
else if ( *p == '/' && *(p+1) == '>' ) { else if ( *p == '/' && *(p+1) == '>' ) {
if ( closing ) { closingType = CLOSED;
document->SetError( ERROR_PARSING_ELEMENT, start, p );
return 0;
}
*closedElement = true;
return p+2; // done; sealed element. return p+2; // done; sealed element.
} }
// end of the tag // end of the tag
@ -1001,7 +1067,7 @@ char* XMLElement::ParseDeep( char* p )
// parsed just like a regular element then deleted from // parsed just like a regular element then deleted from
// the DOM. // the DOM.
if ( *p == '/' ) { if ( *p == '/' ) {
closing = true; closingType = CLOSING;
++p; ++p;
} }
@ -1009,8 +1075,8 @@ char* XMLElement::ParseDeep( char* p )
if ( value.Empty() ) return 0; if ( value.Empty() ) return 0;
bool elementClosed=false; bool elementClosed=false;
p = ParseAttributes( p, &elementClosed ); p = ParseAttributes( p );
if ( !p || !*p || elementClosed || closing ) if ( !p || !*p || closingType )
return p; return p;
p = XMLNode::ParseDeep( p ); p = XMLNode::ParseDeep( p );

View File

@ -463,7 +463,6 @@ public:
virtual bool Accept( XMLVisitor* visitor ) const = 0; virtual bool Accept( XMLVisitor* visitor ) const = 0;
virtual char* ParseDeep( char* ); virtual char* ParseDeep( char* );
virtual bool IsClosingElement() const { return false; }
protected: protected:
XMLNode( XMLDocument* ); XMLNode( XMLDocument* );
@ -681,7 +680,12 @@ public:
const char* GetText() const; const char* GetText() const;
// internal: // internal:
virtual bool IsClosingElement() const { return closing; } enum {
OPEN, // <foo>
CLOSED, // <foo/>
CLOSING // </foo>
};
int ClosingType() const { return closingType; }
char* ParseDeep( char* p ); char* ParseDeep( char* p );
private: private:
@ -693,9 +697,9 @@ private:
XMLAttribute* FindAttribute( const char* name ); XMLAttribute* FindAttribute( const char* name );
XMLAttribute* FindOrCreateAttribute( const char* name ); XMLAttribute* FindOrCreateAttribute( const char* name );
void LinkAttribute( XMLAttribute* attrib ); void LinkAttribute( XMLAttribute* attrib );
char* ParseAttributes( char* p, bool *closedElement ); char* ParseAttributes( char* p );
bool closing; int closingType;
XMLAttribute* rootAttribute; XMLAttribute* rootAttribute;
}; };

View File

@ -129,7 +129,7 @@ int main( int argc, const char* argv )
printf( "----------------------------------------------\n" ); printf( "----------------------------------------------\n" );
} }
} }
#if 1
{ {
static const char* test = "<!--hello world\n" static const char* test = "<!--hello world\n"
" line 2\r" " line 2\r"
@ -597,6 +597,8 @@ int main( int argc, const char* argv )
"<!-- With this comment, child2 will not be parsed! -->" "<!-- With this comment, child2 will not be parsed! -->"
"<child2 att=''/>" "<child2 att=''/>"
"</Parent>" ); "</Parent>" );
xml.Print();
int count = 0; int count = 0;
for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild(); for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
@ -634,7 +636,7 @@ int main( int argc, const char* argv )
xml.Parse( "<infinite>loop" ); xml.Parse( "<infinite>loop" );
XMLTest( "Infinite loop test.", true, true ); XMLTest( "Infinite loop test.", true, true );
} }
#endif
#if defined( WIN32 ) #if defined( WIN32 )
_CrtMemCheckpoint( &endMemState ); _CrtMemCheckpoint( &endMemState );
//_CrtMemDumpStatistics( &endMemState ); //_CrtMemDumpStatistics( &endMemState );