Explorar o código

test cases in progress

Lee Thomason %!s(int64=14) %!d(string=hai) anos
pai
achega
dadcdfad4a
Modificáronse 4 ficheiros con 54 adicións e 10 borrados
  1. 43 5
      tinyxml2.cpp
  2. 2 0
      tinyxml2.h
  3. BIN=BIN
      tinyxml2.suo
  4. 9 5
      xmltest.cpp

+ 43 - 5
tinyxml2.cpp

@@ -143,16 +143,22 @@ char* XMLBase::Identify( XMLDocument* document, char* p, XMLNode** node )
 	static const char* commentHeader	= { "<!--" };
 	static const char* commentHeader	= { "<!--" };
 	static const char* dtdHeader		= { "<!" };
 	static const char* dtdHeader		= { "<!" };
 	static const char* cdataHeader		= { "<![CDATA[" };
 	static const char* cdataHeader		= { "<![CDATA[" };
+	static const char* elementHeader	= { "<" };	// and a header for everything else; check last.
 
 
 	static const int xmlHeaderLen		= 5;
 	static const int xmlHeaderLen		= 5;
 	static const int commentHeaderLen	= 4;
 	static const int commentHeaderLen	= 4;
 	static const int dtdHeaderLen		= 2;
 	static const int dtdHeaderLen		= 2;
 	static const int cdataHeaderLen		= 9;
 	static const int cdataHeaderLen		= 9;
+	static const int elementHeaderLen	= 1;
 
 
-	if ( XMLNode::StringEqual( p, commentHeader, commentHeaderLen ) ) {
+	if ( StringEqual( p, commentHeader, commentHeaderLen ) ) {
 		returnNode = new XMLComment( document );
 		returnNode = new XMLComment( document );
 		p += commentHeaderLen;
 		p += commentHeaderLen;
 	}
 	}
+	else if ( StringEqual( p, elementHeader, elementHeaderLen ) ) {
+		returnNode = new XMLElement( document );
+		p += elementHeaderLen;
+	}
 	else {
 	else {
 		TIXMLASSERT( 0 );
 		TIXMLASSERT( 0 );
 	}
 	}
@@ -222,6 +228,7 @@ void XMLNode::Print( FILE* fp, int depth )
 	}
 	}
 }
 }
 
 
+
 void XMLNode::PrintSpace( FILE* fp, int depth ) 
 void XMLNode::PrintSpace( FILE* fp, int depth ) 
 {
 {
 	for( int i=0; i<depth; ++i ) {
 	for( int i=0; i<depth; ++i ) {
@@ -270,6 +277,12 @@ char* XMLAttribute::ParseDeep( char* p )
 }
 }
 
 
 
 
+void XMLAttribute::Print( FILE* cfile )
+{
+	fprintf( cfile, "\"%s\"", value );
+}
+
+
 // --------- XMLElement ---------- //
 // --------- XMLElement ---------- //
 XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
 XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
 	name( 0 ),
 	name( 0 ),
@@ -385,6 +398,29 @@ char* XMLElement::ParseDeep( char* p )
 }
 }
 
 
 
 
+void XMLElement::Print( FILE* cfile, int depth )
+{
+	PrintSpace( cfile, depth );
+	fprintf( cfile, "<%s", Name() );
+
+	for( XMLAttribute* attrib=rootAttribute; attrib; attrib=attrib->next ) {
+		fprintf( cfile, " " );
+		attrib->Print( cfile );
+	}
+
+	if ( firstChild ) {
+		fprintf( cfile, ">/n" );
+		for( XMLNode* node=firstChild; node; node=node->next ) {
+			node->Print( cfile, depth+1 );
+		}
+		fprintf( cfile, "</%s>", Name() );
+	}
+	else {
+		fprintf( cfile, "/>\n" );
+	}
+}
+
+
 // --------- XMLDocument ----------- //
 // --------- XMLDocument ----------- //
 XMLDocument::XMLDocument() : 
 XMLDocument::XMLDocument() : 
 	charBuffer( 0 )
 	charBuffer( 0 )
@@ -407,10 +443,12 @@ bool XMLDocument::Parse( const char* p )
 	XMLNode* node = 0;
 	XMLNode* node = 0;
 	
 	
 	char* q = Identify( this, charBuffer->mem, &node );
 	char* q = Identify( this, charBuffer->mem, &node );
-	root->InsertEndChild( node );
-	node->ParseDeep( q );
-
-	return true;
+	if ( node ) {
+		root->InsertEndChild( node );
+		node->ParseDeep( q );
+		return true;
+	}
+	return false;
 }
 }
 
 
 
 

+ 2 - 0
tinyxml2.h

@@ -133,6 +133,7 @@ class XMLAttribute : public XMLBase
 public:
 public:
 	XMLAttribute( XMLElement* element ) : value( 0 ), next( 0 ) {}
 	XMLAttribute( XMLElement* element ) : value( 0 ), next( 0 ) {}
 	virtual ~XMLAttribute()	{}
 	virtual ~XMLAttribute()	{}
+	virtual void Print( FILE* cfile );
 
 
 private:
 private:
 	char* ParseDeep( char* p );
 	char* ParseDeep( char* p );
@@ -149,6 +150,7 @@ public:
 	virtual ~XMLElement();
 	virtual ~XMLElement();
 
 
 	const char* Name() const { return name; }
 	const char* Name() const { return name; }
+	virtual void Print( FILE* cfile, int depth );
 
 
 	virtual XMLElement* ToElement() { return this; }
 	virtual XMLElement* ToElement() { return this; }
 	bool Closing() const			{ return closing; }
 	bool Closing() const			{ return closing; }

BIN=BIN
tinyxml2.suo


+ 9 - 5
xmltest.cpp

@@ -28,11 +28,15 @@ int main( int argc, const char* argv )
 	}
 	}
 #endif
 #endif
 	{
 	{
-		static const char* test = "<element />";
-
-		XMLDocument doc;
-		doc.Parse( test );
-		doc.Print( stdout );
+		static const char* test[] = {	"<element />",
+									    "<element></element>",
+										0
+		};
+		for( const char* t=test[0]; *t; ++t ) {
+			XMLDocument doc;
+			doc.Parse( t );
+			doc.Print( stdout );
+		}
 	}
 	}
 	return 0;
 	return 0;
 }
 }