Răsfoiți Sursa

clean up Value of XMLDocument

Lee Thomason 10 ani în urmă
părinte
comite
8549202800
3 a modificat fișierele cu 45 adăugiri și 23 ștergeri
  1. 3 0
      tinyxml2.cpp
  2. 4 4
      tinyxml2.h
  3. 38 19
      xmltest.cpp

+ 3 - 0
tinyxml2.cpp

@@ -645,6 +645,9 @@ XMLNode::~XMLNode()
 
 const char* XMLNode::Value() const 
 {
+    // Catch an edge case: XMLDocuments don't have a a Value. Carefully return nullptr.
+    if ( this->ToDocument() )
+        return 0;
     return _value.GetStr();
 }
 

+ 4 - 4
tinyxml2.h

@@ -122,9 +122,9 @@ inline int TIXML_SNPRINTF( char* buffer, size_t size, const char* format, ... )
 /* Versioning, past 1.0.14:
 	http://semver.org/
 */
-static const int TIXML2_MAJOR_VERSION = 3;
-static const int TIXML2_MINOR_VERSION = 0;
-static const int TIXML2_PATCH_VERSION = 0;
+static const int TIXML2_MAJOR_VERSION = 3;
+static const int TIXML2_MINOR_VERSION = 0;
+static const int TIXML2_PATCH_VERSION = 0;
 
 namespace tinyxml2
 {
@@ -708,7 +708,7 @@ public:
 
     /** The meaning of 'value' changes for the specific type.
     	@verbatim
-    	Document:	empty
+    	Document:	empty (NULL is returned, not an empty string)
     	Element:	name of the element
     	Comment:	the comment text
     	Unknown:	the tag contents

+ 38 - 19
xmltest.cpp

@@ -30,7 +30,13 @@ int gFail = 0;
 
 bool XMLTest (const char* testString, const char* expected, const char* found, bool echo=true, bool extraNL=false )
 {
-	bool pass = !strcmp( expected, found );
+	bool pass;
+	if ( !expected && !found )
+		pass = true;
+	else if ( !expected || !found )
+		pass = false;
+	else 
+		pass = !strcmp( expected, found );
 	if ( pass )
 		printf ("[pass]");
 	else
@@ -1462,24 +1468,37 @@ int main( int argc, const char ** argv )
 
 	{
 		// Check that declarations are parsed only as the FirstChild
-                const char* xml0 = "<?xml version=\"1.0\" ?>"
-                                   "   <!-- xml version=\"1.1\" -->"
-                                   "<first />";
-                const char* xml1 = "<?xml version=\"1.0\" ?>"
-                                   "   <?xml version=\"1.1\" ?>"
-                                   "<first />";
-                const char* xml2 = "<first />"
-                                   "<?xml version=\"1.0\" ?>";
-                XMLDocument doc;
-                doc.Parse(xml0);
-                XMLTest("Test that the code changes do not affect normal parsing", doc.Error(), false);
-                doc.Parse(xml1);
-                XMLTest("Test that the second declaration throws an error", doc.ErrorID(), XML_ERROR_PARSING_DECLARATION);
-                doc.Parse(xml2);
-                XMLTest("Test that declaration after a child throws an error", doc.ErrorID(), XML_ERROR_PARSING_DECLARATION);
-	}
-
-	// ----------- Performance tracking --------------
+	    const char* xml0 = "<?xml version=\"1.0\" ?>"
+	                       "   <!-- xml version=\"1.1\" -->"
+	                       "<first />";
+	    const char* xml1 = "<?xml version=\"1.0\" ?>"
+	                       "   <?xml version=\"1.1\" ?>"
+	                       "<first />";
+	    const char* xml2 = "<first />"
+	                       "<?xml version=\"1.0\" ?>";
+	    XMLDocument doc;
+	    doc.Parse(xml0);
+	    XMLTest("Test that the code changes do not affect normal parsing", doc.Error(), false);
+	    doc.Parse(xml1);
+	    XMLTest("Test that the second declaration throws an error", doc.ErrorID(), XML_ERROR_PARSING_DECLARATION);
+	    doc.Parse(xml2);
+	    XMLTest("Test that declaration after a child throws an error", doc.ErrorID(), XML_ERROR_PARSING_DECLARATION);
+	}
+
+    {
+	    // No matter - before or after successfully parsing a text -
+	    // calling XMLDocument::Value() causes an assert in debug.
+	    const char* validXml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
+	                           "<first />"
+	                           "<second />";
+	    XMLDocument* doc = new XMLDocument();
+	    XMLTest( "XMLDocument::Value() returns null?", NULL, doc->Value() );
+	    doc->Parse( validXml );
+	    XMLTest( "XMLDocument::Value() returns null?", NULL, doc->Value() );
+	    delete doc;
+    }
+
+    // ----------- Performance tracking --------------
 	{
 #if defined( _MSC_VER )
 		__int64 start, end, freq;