From a9d42b0dd30c12b73e7b31853a5012a72b339af5 Mon Sep 17 00:00:00 2001 From: Martinsh Shaiters Date: Wed, 30 Jan 2013 11:14:27 +0200 Subject: [PATCH 1/2] Replace InitDocument() with Clear() and make it part of the API. --- tinyxml2.cpp | 13 ++++++------- tinyxml2.h | 4 +++- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 2c06a46..6b349f0 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -1520,8 +1520,10 @@ XMLDocument::~XMLDocument() } -void XMLDocument::InitDocument() +void XMLDocument::Clear() { + DeleteChildren(); + _errorID = XML_NO_ERROR; _errorStr1 = 0; _errorStr2 = 0; @@ -1578,8 +1580,7 @@ XMLUnknown* XMLDocument::NewUnknown( const char* str ) XMLError XMLDocument::LoadFile( const char* filename ) { - DeleteChildren(); - InitDocument(); + Clear(); FILE* fp = 0; #if defined(_MSC_VER) && (_MSC_VER >= 1400 ) @@ -1600,8 +1601,7 @@ XMLError XMLDocument::LoadFile( const char* filename ) XMLError XMLDocument::LoadFile( FILE* fp ) { - DeleteChildren(); - InitDocument(); + Clear(); fseek( fp, 0, SEEK_END ); size_t size = ftell( fp ); @@ -1662,8 +1662,7 @@ XMLError XMLDocument::SaveFile( FILE* fp, bool compact ) XMLError XMLDocument::Parse( const char* p, size_t len ) { - DeleteChildren(); - InitDocument(); + Clear(); if ( !p || !*p ) { SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 ); diff --git a/tinyxml2.h b/tinyxml2.h index d04c5e4..cf60a58 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -1532,6 +1532,9 @@ public: } /// If there is an error, print it to stdout. void PrintError() const; + + /// Clear the document, resetting it to the initial state. + void Clear(); // internal char* Identify( char* p, XMLNode** node ); @@ -1546,7 +1549,6 @@ public: private: XMLDocument( const XMLDocument& ); // not supported void operator=( const XMLDocument& ); // not supported - void InitDocument(); bool _writeBOM; bool _processEntities; From 53ab79afad55608e4b4b5a59ee1e5965d89ffbc4 Mon Sep 17 00:00:00 2001 From: Martinsh Shaiters Date: Wed, 30 Jan 2013 11:21:36 +0200 Subject: [PATCH 2/2] Add unit test for XMLDocument::Clear(). --- xmltest.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/xmltest.cpp b/xmltest.cpp index 64217f3..5f7cb30 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -1072,6 +1072,13 @@ int main( int argc, const char ** argv ) XMLTest( "Set length of incoming data", doc.Error(), false ); } + { + XMLDocument doc; + doc.LoadFile( "resources/dream.xml" ); + doc.Clear(); + XMLTest( "Document Clear()'s", doc.NoChildren(), true ); + } + // ----------- Whitespace ------------ { const char* xml = ""