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;
#define DELETE_NODE( node ) { MemPool* pool = node->memPool; node->~XMLNode(); pool->Free( node ); }
#define DELETE_ATTRIBUTE( attrib ) { MemPool* pool = attrib->memPool; attrib->~XMLAttribute(); pool->Free( attrib ); }
#define DELETE_NODE( node ) { \
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 {
const char* pattern;
@ -397,6 +409,11 @@ char* XMLDocument::Identify( char* p, XMLNode** node )
returnNode = new (elementPool.Alloc()) XMLElement( this );
returnNode->memPool = &elementPool;
p += elementHeaderLen;
p = XMLUtil::SkipWhiteSpace( p );
if ( p && *p == '/' ) {
((XMLElement*)returnNode)->closingType = XMLElement::CLOSING;
}
}
else {
returnNode = new (textPool.Alloc()) XMLText( this );
@ -587,22 +604,77 @@ const XMLElement* XMLNode::LastChildElement( const char* value ) const
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 ) {
XMLNode* node = 0;
p = document->Identify( p, &node );
if ( p && node ) {
p = node->ParseDeep( p );
char* mark = p;
if ( node->IsClosingElement() ) {
if ( !XMLUtil::StringEqual( Value(), node->Value() )) {
document->SetError( ERROR_MISMATCHED_ELEMENT, Value(), 0 );
p = document->Identify( p, &node );
if ( p == 0 ) {
break;
}
// We read the end tag. Back up and return.
if ( node && node->ToElement() && node->ToElement()->ClosingType() == XMLElement::CLOSING ) {
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 );
}
}
}
return 0;
}
@ -736,7 +808,7 @@ char* XMLAttribute::ParseDeep( char* p )
char endTag[2] = { *p, 0 };
++p;
p = value.ParseText( p, endTag, StrPair::ATTRIBUTE_VALUE );
if ( value.Empty() ) return 0;
//if ( value.Empty() ) return 0;
return p;
}
@ -842,9 +914,8 @@ void XMLAttribute::SetAttribute( float v )
// --------- XMLElement ---------- //
XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
closing( false ),
closingType( 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;
*closedElement = false;
// Read the attributes.
while( p ) {
@ -965,11 +1035,7 @@ char* XMLElement::ParseAttributes( char* p, bool* closedElement )
}
// end of the tag
else if ( *p == '/' && *(p+1) == '>' ) {
if ( closing ) {
document->SetError( ERROR_PARSING_ELEMENT, start, p );
return 0;
}
*closedElement = true;
closingType = CLOSED;
return p+2; // done; sealed element.
}
// end of the tag
@ -1001,7 +1067,7 @@ char* XMLElement::ParseDeep( char* p )
// parsed just like a regular element then deleted from
// the DOM.
if ( *p == '/' ) {
closing = true;
closingType = CLOSING;
++p;
}
@ -1009,8 +1075,8 @@ char* XMLElement::ParseDeep( char* p )
if ( value.Empty() ) return 0;
bool elementClosed=false;
p = ParseAttributes( p, &elementClosed );
if ( !p || !*p || elementClosed || closing )
p = ParseAttributes( p );
if ( !p || !*p || closingType )
return p;
p = XMLNode::ParseDeep( p );

View File

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

View File

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