From 321072ee0b3ab0b64b1e94c2e2a2e00096892a2f Mon Sep 17 00:00:00 2001 From: Uli Kusterer Date: Tue, 21 Jan 2014 01:57:38 +0100 Subject: [PATCH 1/3] Add tests for SetText(). --- xmltest.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/xmltest.cpp b/xmltest.cpp index 2c1fece..6cde449 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -617,6 +617,38 @@ int main( int argc, const char ** argv ) } + // --------SetText()----------- + { + const char* str = ""; + XMLDocument doc; + doc.Parse( str ); + XMLElement* element = doc.RootElement(); + + element->SetText("He kept turning his head to left and right, but I could not see anything through the darkness."); + XMLTest( "SetText() normal use (open/close).", "He kept turning his head to left and right, but I could not see anything through the darkness.", element->GetText() ); + + element->SetText("Suddenly, away on our left I saw a faint flickering blue flame."); + XMLTest( "SetText() replace.", "Suddenly, away on our left I saw a faint flickering blue flame.", element->GetText() ); + + str = ""; + doc.Parse( str ); + element = doc.RootElement(); + + element->SetText("The driver saw it at the same moment."); + XMLTest( "SetText() normal use. (self-closing)", "The driver saw it at the same moment.", element->GetText() ); + + element->SetText("He at once checked the horses, and, jumping to the ground, disappeared into the darkness."); + XMLTest( "SetText() replace with tag-like text.", "He at once checked the horses, and, jumping to the ground, disappeared into the darkness.", element->GetText() ); + + str = "Text in nested element"; + doc.Parse( str ); + element = doc.RootElement(); + + element->SetText("I did not know what to do, the less as the howling of the wolves grew closer."); + XMLTest( "SetText() prefix to nested non-text children.", "I did not know what to do, the less as the howling of the wolves grew closer.", element->GetText() ); + } + + // ---------- CDATA --------------- { const char* str = "" From 9c0678a01d5e0ab4b369f9ba6661790e948427bd Mon Sep 17 00:00:00 2001 From: Lee Thomason Date: Fri, 24 Jan 2014 10:18:27 -0800 Subject: [PATCH 2/3] trim the test cases and review --- xmltest.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/xmltest.cpp b/xmltest.cpp index 6cde449..8af4e32 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -624,28 +624,29 @@ int main( int argc, const char ** argv ) doc.Parse( str ); XMLElement* element = doc.RootElement(); - element->SetText("He kept turning his head to left and right, but I could not see anything through the darkness."); - XMLTest( "SetText() normal use (open/close).", "He kept turning his head to left and right, but I could not see anything through the darkness.", element->GetText() ); + element->SetText("darkness."); + XMLTest( "SetText() normal use (open/close).", "darkness.", element->GetText() ); - element->SetText("Suddenly, away on our left I saw a faint flickering blue flame."); - XMLTest( "SetText() replace.", "Suddenly, away on our left I saw a faint flickering blue flame.", element->GetText() ); + element->SetText("blue flame."); + XMLTest( "SetText() replace.", "blue flame.", element->GetText() ); str = ""; doc.Parse( str ); element = doc.RootElement(); - element->SetText("The driver saw it at the same moment."); - XMLTest( "SetText() normal use. (self-closing)", "The driver saw it at the same moment.", element->GetText() ); + element->SetText("The driver"); + XMLTest( "SetText() normal use. (self-closing)", "The driver", element->GetText() ); - element->SetText("He at once checked the horses, and, jumping to the ground, disappeared into the darkness."); - XMLTest( "SetText() replace with tag-like text.", "He at once checked the horses, and, jumping to the ground, disappeared into the darkness.", element->GetText() ); + element->SetText("horses"); + XMLTest( "SetText() replace with tag-like text.", "horses", element->GetText() ); + //doc.Print(); str = "Text in nested element"; doc.Parse( str ); element = doc.RootElement(); - element->SetText("I did not know what to do, the less as the howling of the wolves grew closer."); - XMLTest( "SetText() prefix to nested non-text children.", "I did not know what to do, the less as the howling of the wolves grew closer.", element->GetText() ); + element->SetText("wolves"); + XMLTest( "SetText() prefix to nested non-text children.", "wolves", element->GetText() ); } From 5bb2d8079bad8ed2e22659326d8c77fadf4e6e4b Mon Sep 17 00:00:00 2001 From: Lee Thomason Date: Fri, 24 Jan 2014 10:42:57 -0800 Subject: [PATCH 3/3] add variants of SetText() to support types --- CMakeLists.txt | 2 +- dox | 2 +- tinyxml2.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ tinyxml2.h | 15 +++++++++++++-- xmltest.cpp | 22 ++++++++++++++++++++++ 5 files changed, 78 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 399c4c2..12cac92 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ include(GNUInstallDirs) ################################ # set lib version here -set(GENERIC_LIB_VERSION "1.0.13") +set(GENERIC_LIB_VERSION "1.0.14") set(GENERIC_LIB_SOVERSION "1") diff --git a/dox b/dox index 2413b2e..10878f4 100755 --- a/dox +++ b/dox @@ -32,7 +32,7 @@ PROJECT_NAME = "TinyXML-2" # This could be handy for archiving the generated documentation or # if some version control system is used. -PROJECT_NUMBER = 1.0.13 +PROJECT_NUMBER = 1.0.14 # Using the PROJECT_BRIEF tag one can provide an optional one line description # for a project that appears at the top of each page and should give viewer diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 0338beb..cb26e00 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -1270,6 +1270,47 @@ void XMLElement::SetText( const char* inText ) } } + +void XMLElement::SetText( int v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + SetText( buf ); +} + + +void XMLElement::SetText( unsigned v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + SetText( buf ); +} + + +void XMLElement::SetText( bool v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + SetText( buf ); +} + + +void XMLElement::SetText( float v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + SetText( buf ); +} + + +void XMLElement::SetText( double v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + SetText( buf ); +} + + XMLError XMLElement::QueryIntText( int* ival ) const { if ( FirstChild() && FirstChild()->ToText() ) { diff --git a/tinyxml2.h b/tinyxml2.h index 2330ab9..41f1c70 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -118,7 +118,7 @@ inline int TIXML_SNPRINTF( char* buffer, size_t size, const char* format, ... ) static const int TIXML2_MAJOR_VERSION = 1; static const int TIXML2_MINOR_VERSION = 0; -static const int TIXML2_PATCH_VERSION = 13; +static const int TIXML2_PATCH_VERSION = 14; namespace tinyxml2 { @@ -1403,7 +1403,17 @@ public: Hullaballoo! @endverbatim */ - void SetText( const char* inText ); + void SetText( const char* inText ); + /// Convenince method for setting text inside and element. See SetText() for important limitations. + void SetText( int value ); + /// Convenince method for setting text inside and element. See SetText() for important limitations. + void SetText( unsigned value ); + /// Convenince method for setting text inside and element. See SetText() for important limitations. + void SetText( bool value ); + /// Convenince method for setting text inside and element. See SetText() for important limitations. + void SetText( double value ); + /// Convenince method for setting text inside and element. See SetText() for important limitations. + void SetText( float value ); /** Convenience method to query the value of a child text node. This is probably best @@ -1465,6 +1475,7 @@ private: //void LinkAttribute( XMLAttribute* attrib ); char* ParseAttributes( char* p ); + enum { BUF_SIZE = 200 }; int _closingType; // The attribute list is ordered; there is no 'lastAttribute' // because the list needs to be scanned for dupes before adding diff --git a/xmltest.cpp b/xmltest.cpp index 8af4e32..6fdc162 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -647,6 +647,28 @@ int main( int argc, const char ** argv ) element->SetText("wolves"); XMLTest( "SetText() prefix to nested non-text children.", "wolves", element->GetText() ); + + str = ""; + doc.Parse( str ); + element = doc.RootElement(); + + element->SetText( "str" ); + XMLTest( "SetText types", "str", element->GetText() ); + + element->SetText( 1 ); + XMLTest( "SetText types", "1", element->GetText() ); + + element->SetText( 1U ); + XMLTest( "SetText types", "1", element->GetText() ); + + element->SetText( true ); + XMLTest( "SetText types", "1", element->GetText() ); // TODO: should be 'true'? + + element->SetText( 1.5f ); + XMLTest( "SetText types", "1.5", element->GetText() ); + + element->SetText( 1.5 ); + XMLTest( "SetText types", "1.5", element->GetText() ); }