From dcefa0e43fffba687c7868df2eb0bbb34d2851dd Mon Sep 17 00:00:00 2001 From: Uli Kusterer Date: Tue, 21 Jan 2014 01:36:41 +0100 Subject: [PATCH 01/16] Add typed convenience setters for SetText(). --- tinyxml2.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tinyxml2.h | 18 ++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 0338beb..5e3f4aa 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -1270,6 +1270,72 @@ void XMLElement::SetText( const char* inText ) } } + +void XMLElement::SetText( int inNum ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( inNum, buf, BUF_SIZE ); + if ( FirstChild() && FirstChild()->ToText() ) + FirstChild()->SetValue( buf ); + else { + XMLText* theText = GetDocument()->NewText( buf ); + InsertFirstChild( theText ); + } +} + + +void XMLElement::SetText( unsigned inNum ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( inNum, buf, BUF_SIZE ); + if ( FirstChild() && FirstChild()->ToText() ) + FirstChild()->SetValue( buf ); + else { + XMLText* theText = GetDocument()->NewText( buf ); + InsertFirstChild( theText ); + } +} + + +void XMLElement::SetText( bool inBool ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( inBool, buf, BUF_SIZE ); + if ( FirstChild() && FirstChild()->ToText() ) + FirstChild()->SetValue( buf ); + else { + XMLText* theText = GetDocument()->NewText( buf ); + InsertFirstChild( theText ); + } +} + + +void XMLElement::SetText( double inNum ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( inNum, buf, BUF_SIZE ); + if ( FirstChild() && FirstChild()->ToText() ) + FirstChild()->SetValue( buf ); + else { + XMLText* theText = GetDocument()->NewText( buf ); + InsertFirstChild( theText ); + } +} + + +void XMLElement::SetText( float inNum ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( inNum, buf, BUF_SIZE ); + if ( FirstChild() && FirstChild()->ToText() ) + FirstChild()->SetValue( buf ); + else { + XMLText* theText = GetDocument()->NewText( buf ); + InsertFirstChild( theText ); + } +} + + XMLError XMLElement::QueryIntText( int* ival ) const { if ( FirstChild() && FirstChild()->ToText() ) { diff --git a/tinyxml2.h b/tinyxml2.h index 2330ab9..1c60766 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -1405,6 +1405,22 @@ public: */ void SetText( const char* inText ); + /// Sets the text to the given number. + void SetText( int inNum ); + + /// Sets the text to the given number. + void SetText( unsigned inNum ); + + /// Sets the text to the given boolean. + void SetText( bool inBool ); + + /// Sets the text to the given double. + void SetText( double inNum ); + + /// Sets the text to the given float. + void SetText( float inNum ); + + /** Convenience method to query the value of a child text node. This is probably best shown by example. Given you have a document is this form: @@ -1455,6 +1471,8 @@ public: virtual bool ShallowEqual( const XMLNode* compare ) const; private: + enum { BUF_SIZE = 200 }; + XMLElement( XMLDocument* doc ); virtual ~XMLElement(); XMLElement( const XMLElement& ); // not supported From 7411c08e90c31d267ce2976f8d8ed191e45db93d Mon Sep 17 00:00:00 2001 From: Uli Kusterer Date: Tue, 21 Jan 2014 01:57:38 +0100 Subject: [PATCH 02/16] 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 c1c20bb1965e01a4e97ab206b30a0e3e2ca6ab7b Mon Sep 17 00:00:00 2001 From: Uli Kusterer Date: Tue, 21 Jan 2014 02:23:20 +0100 Subject: [PATCH 03/16] Bools should really be implemented as self-closing and tags, not as 0/1 inside a tag, so removing those SetText variants again, in favor of SetBoolFirstChild() and BoolFirstChild() methods. --- tinyxml2.cpp | 36 +++++++++++++++++++++++------------- tinyxml2.h | 8 +++++--- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 5e3f4aa..efcc8c7 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -1297,19 +1297,6 @@ void XMLElement::SetText( unsigned inNum ) } -void XMLElement::SetText( bool inBool ) -{ - char buf[BUF_SIZE]; - XMLUtil::ToStr( inBool, buf, BUF_SIZE ); - if ( FirstChild() && FirstChild()->ToText() ) - FirstChild()->SetValue( buf ); - else { - XMLText* theText = GetDocument()->NewText( buf ); - InsertFirstChild( theText ); - } -} - - void XMLElement::SetText( double inNum ) { char buf[BUF_SIZE]; @@ -1336,6 +1323,29 @@ void XMLElement::SetText( float inNum ) } +void XMLElement::SetBoolFirstChild( bool inBool ) +{ + if( FirstChild() && FirstChild()->ToElement() + && (strcmp(FirstChild()->Value(),"true") == 0 || strcmp(FirstChild()->Value(),"false") == 0) ) { + FirstChild()->SetValue( inBool ? "true" : "false" ); + } + else if( !FirstChild() ) { + XMLElement* theText = GetDocument()->NewElement( inBool ? "true" : "false" ); + InsertFirstChild( theText ); + } +} + + +bool XMLElement::BoolFirstChild() +{ + if ( FirstChild() && FirstChild()->ToElement() ) { + return strcmp( FirstChild()->Value(), "true" ) == 0; + } + + return false; +} + + XMLError XMLElement::QueryIntText( int* ival ) const { if ( FirstChild() && FirstChild()->ToText() ) { diff --git a/tinyxml2.h b/tinyxml2.h index 1c60766..0492a29 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -1411,9 +1411,6 @@ public: /// Sets the text to the given number. void SetText( unsigned inNum ); - /// Sets the text to the given boolean. - void SetText( bool inBool ); - /// Sets the text to the given double. void SetText( double inNum ); @@ -1421,6 +1418,11 @@ public: void SetText( float inNum ); + /// Adds a sub-element equivalent to the given boolean. + void SetBoolFirstChild( bool inBool ); + + bool BoolFirstChild(); + /** Convenience method to query the value of a child text node. This is probably best shown by example. Given you have a document is this form: From 4af5573f42a02c038e2ed3b65d07f2a1120d8faf Mon Sep 17 00:00:00 2001 From: Uli Kusterer Date: Tue, 21 Jan 2014 02:36:47 +0100 Subject: [PATCH 04/16] Tests for SetBoolFirstChild and BoolFirstChild. --- xmltest.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/xmltest.cpp b/xmltest.cpp index 6cde449..9fb021a 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -649,6 +649,61 @@ int main( int argc, const char ** argv ) } + // --------SetBoolFirstChild()----------- + { + const char* str = ""; + XMLDocument doc; + doc.Parse( str ); + XMLElement* element = doc.RootElement(); + + element->SetBoolFirstChild(true); + XMLTest( "SetBoolFirstChild() normal use (open/close).", "true", element->FirstChild()->ToElement()->Value() ); + + element->SetBoolFirstChild(false); + XMLTest( "SetBoolFirstChild() replace.", "false", element->FirstChild()->ToElement()->Value() ); + + str = ""; + doc.Parse( str ); + element = doc.RootElement(); + + element->SetBoolFirstChild(false); + XMLTest( "SetBoolFirstChild() normal use (self-closing).", "false", element->FirstChild()->ToElement()->Value() ); + } + + + // --------BoolFirstChild()----------- + { + const char* str = ""; + XMLDocument doc; + doc.Parse( str ); + XMLElement* element = doc.RootElement(); + + XMLTest( "BoolFirstChild() normal use (open/close).", false, element->BoolFirstChild() ); + + str = ""; + doc.Parse( str ); + element = doc.RootElement(); + XMLTest( "BoolFirstChild() normal use (open/close).", true, element->BoolFirstChild() ); + + str = ""; + doc.Parse( str ); + element = doc.RootElement(); + + element->SetBoolFirstChild(true); + XMLTest( "BoolFirstChild() after SetBoolFirstChild().", true, element->BoolFirstChild() ); + + element->SetBoolFirstChild(false); + XMLTest( "BoolFirstChild() after SetBoolFirstChild() replace.", false, element->BoolFirstChild() ); + + str = ""; + doc.Parse( str ); + + element = doc.RootElement(); + element->SetBoolFirstChild(false); + XMLTest( "BoolFirstChild() (self-closing) after SetBoolFirstChild() replace.", false, element->BoolFirstChild() ); + } + + // ---------- CDATA --------------- { const char* str = "" From ff8e2041ddf52e1bbe7e2695c35a77d223aca175 Mon Sep 17 00:00:00 2001 From: Uli Kusterer Date: Tue, 21 Jan 2014 02:53:47 +0100 Subject: [PATCH 05/16] To bring BoolFirstChild() more in line with the other methods, reimplemented it in terms of a new QueryBoolFirstChild(). --- tinyxml2.cpp | 22 +++++++++++++++++----- tinyxml2.h | 15 ++++++++++++--- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index efcc8c7..c00f0e3 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -1336,13 +1336,25 @@ void XMLElement::SetBoolFirstChild( bool inBool ) } -bool XMLElement::BoolFirstChild() +XMLError XMLElement::QueryBoolFirstChild( bool *outBool ) { - if ( FirstChild() && FirstChild()->ToElement() ) { - return strcmp( FirstChild()->Value(), "true" ) == 0; + if ( FirstChild() ) + { + if ( FirstChild()->ToElement() ) + { + bool isTrue = strcmp( FirstChild()->Value(), "true" ) == 0; + bool isFalse = strcmp( FirstChild()->Value(), "false" ) == 0; + if( !isTrue && !isFalse ) + return XML_CAN_NOT_CONVERT_TEXT; + + *outBool = isTrue; + return XML_SUCCESS; + } + else + return XML_NO_ELEMENT_NODE; } - - return false; + else + return XML_NO_ELEMENT_NODE; } diff --git a/tinyxml2.h b/tinyxml2.h index 0492a29..0eae819 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -1014,7 +1014,8 @@ enum XMLError { XML_ERROR_PARSING, XML_CAN_NOT_CONVERT_TEXT, - XML_NO_TEXT_NODE + XML_NO_TEXT_NODE, + XML_NO_ELEMENT_NODE }; @@ -1419,9 +1420,17 @@ public: /// Adds a sub-element equivalent to the given boolean. - void SetBoolFirstChild( bool inBool ); + void SetBoolFirstChild( bool inBool ); - bool BoolFirstChild(); + /// Looks for a <true /> or <false /> as the first child and returns the corresponding bool. + bool BoolFirstChild() + { + bool b = false; + QueryBoolFirstChild(&b); + return b; + } + + XMLError QueryBoolFirstChild( bool *outBool ); /** Convenience method to query the value of a child text node. This is probably best From 4cd1f269c28ff2c84c454270acbb47a2c8eb0403 Mon Sep 17 00:00:00 2001 From: Uli Kusterer Date: Tue, 21 Jan 2014 02:54:36 +0100 Subject: [PATCH 06/16] Added IntText(), UnsignedText(), DoubleText() and FloatText() convenience wrappers. --- tinyxml2.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tinyxml2.h b/tinyxml2.h index 0eae819..3731562 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -1417,7 +1417,39 @@ public: /// Sets the text to the given float. void SetText( float inNum ); + + + /// Convenience for QueryIntText when you don't care if the text won't convert. + int IntText() + { + int i = 0; + QueryIntText( &i ); + return i; + } + /// Convenience for QueryUnsignedText when you don't care if the text won't convert. + unsigned UnsignedText() + { + unsigned i = 0; + QueryUnsignedText( &i ); + return i; + } + + /// Convenience for QueryDoubleText when you don't care if the text won't convert. + double DoubleText() + { + double i = 0; + QueryDoubleText( &i ); + return i; + } + + /// Convenience for QueryFloatText when you don't care if the text won't convert. + float FloatText() + { + float i = 0; + QueryFloatText( &i ); + return i; + } /// Adds a sub-element equivalent to the given boolean. void SetBoolFirstChild( bool inBool ); From 664d05662eb651e0ae15b6d1ba75cbf7d780e149 Mon Sep 17 00:00:00 2001 From: Uli Kusterer Date: Tue, 21 Jan 2014 12:24:47 +0100 Subject: [PATCH 07/16] Support for 'long long' number values. --- tinyxml2.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ tinyxml2.h | 21 +++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index c00f0e3..efaa29a 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -411,6 +411,12 @@ void XMLUtil::ToStr( int v, char* buffer, int bufferSize ) } +void XMLUtil::ToStr( long long v, char* buffer, int bufferSize ) +{ + TIXML_SNPRINTF( buffer, bufferSize, "%lld", v ); +} + + void XMLUtil::ToStr( unsigned v, char* buffer, int bufferSize ) { TIXML_SNPRINTF( buffer, bufferSize, "%u", v ); @@ -446,6 +452,14 @@ bool XMLUtil::ToInt( const char* str, int* value ) return false; } +bool XMLUtil::ToLongLong( const char* str, long long* value ) +{ + if ( TIXML_SSCANF( str, "%lld", value ) == 1 ) { + return true; + } + return false; +} + bool XMLUtil::ToUnsigned( const char* str, unsigned *value ) { if ( TIXML_SSCANF( str, "%u", value ) == 1 ) { @@ -1166,6 +1180,14 @@ void XMLAttribute::SetAttribute( int v ) } +void XMLAttribute::SetAttribute( long long v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + _value.SetStr( buf ); +} + + void XMLAttribute::SetAttribute( unsigned v ) { char buf[BUF_SIZE]; @@ -1322,6 +1344,19 @@ void XMLElement::SetText( float inNum ) } } +void XMLElement::SetText( long long inNum ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( inNum, buf, BUF_SIZE ); + if ( FirstChild() && FirstChild()->ToText() ) + FirstChild()->SetValue( buf ); + else { + XMLText* theText = GetDocument()->NewText( buf ); + InsertFirstChild( theText ); + } +} + + void XMLElement::SetBoolFirstChild( bool inBool ) { @@ -1371,6 +1406,19 @@ XMLError XMLElement::QueryIntText( int* ival ) const } +XMLError XMLElement::QueryLongLongText( long long* ival ) const +{ + if ( FirstChild() && FirstChild()->ToText() ) { + const char* t = FirstChild()->ToText()->Value(); + if ( XMLUtil::ToLongLong( t, ival ) ) { + return XML_SUCCESS; + } + return XML_CAN_NOT_CONVERT_TEXT; + } + return XML_NO_TEXT_NODE; +} + + XMLError XMLElement::QueryUnsignedText( unsigned* uval ) const { if ( FirstChild() && FirstChild()->ToText() ) { diff --git a/tinyxml2.h b/tinyxml2.h index 3731562..f1be6ca 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -540,6 +540,7 @@ public: // converts primitive types to strings static void ToStr( int v, char* buffer, int bufferSize ); + static void ToStr( long long v, char* buffer, int bufferSize ); static void ToStr( unsigned v, char* buffer, int bufferSize ); static void ToStr( bool v, char* buffer, int bufferSize ); static void ToStr( float v, char* buffer, int bufferSize ); @@ -547,6 +548,7 @@ public: // converts strings to primitive types static bool ToInt( const char* str, int* value ); + static bool ToLongLong( const char* str, long long* value ); static bool ToUnsigned( const char* str, unsigned* value ); static bool ToBool( const char* str, bool* value ); static bool ToFloat( const char* str, float* value ); @@ -1093,6 +1095,8 @@ public: /// Set the attribute to value. void SetAttribute( int value ); /// Set the attribute to value. + void SetAttribute( long long value ); + /// Set the attribute to value. void SetAttribute( unsigned value ); /// Set the attribute to value. void SetAttribute( bool value ); @@ -1308,6 +1312,11 @@ public: a->SetAttribute( value ); } /// Sets the named attribute to value. + void SetAttribute( const char* name, long long value ) { + XMLAttribute* a = FindOrCreateAttribute( name ); + a->SetAttribute( value ); + } + /// Sets the named attribute to value. void SetAttribute( const char* name, unsigned value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( value ); @@ -1418,6 +1427,8 @@ public: /// Sets the text to the given float. void SetText( float inNum ); + /// Sets the text to the given long long. + void SetText( long long inNum ); /// Convenience for QueryIntText when you don't care if the text won't convert. int IntText() @@ -1427,6 +1438,14 @@ public: return i; } + /// Convenience for QueryLongLongText when you don't care if the text won't convert. + long long LongLongText() + { + long long i = 0; + QueryLongLongText( &i ); + return i; + } + /// Convenience for QueryUnsignedText when you don't care if the text won't convert. unsigned UnsignedText() { @@ -1492,6 +1511,8 @@ public: */ XMLError QueryIntText( int* ival ) const; /// See QueryIntText() + XMLError QueryLongLongText( long long* ival ) const; + /// See QueryIntText() XMLError QueryUnsignedText( unsigned* uval ) const; /// See QueryIntText() XMLError QueryBoolText( bool* bval ) const; From 35ce309f73472c723be6a19f69ba6cffcddbf622 Mon Sep 17 00:00:00 2001 From: Uli Kusterer Date: Sat, 25 Jan 2014 03:37:16 +0100 Subject: [PATCH 08/16] Urk, forgot to build this one after merging and goofed a bit. --- tinyxml2.cpp | 10 +++++++++- tinyxml2.h | 24 +++++------------------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 6081856..fe5b437 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -1325,7 +1325,15 @@ void XMLElement::SetText( float v ) } -void XMLElement::SetText( double v ) +void XMLElement::SetText( double v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + SetText( buf ); +} + + +void XMLElement::SetText( long long v ) { char buf[BUF_SIZE]; XMLUtil::ToStr( v, buf, BUF_SIZE ); diff --git a/tinyxml2.h b/tinyxml2.h index f9269a1..734be7d 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -1414,28 +1414,16 @@ public: @endverbatim */ void SetText( const char* inText ); - /// Convenince method for setting text inside and element. See SetText() for important limitations. + /// Convenience 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. + /// Convenience 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. + /// Convenience 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. + /// Convenience 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. + /// Convenience method for setting text inside and element. See SetText() for important limitations. void SetText( float value ); - - /// Sets the text to the given number. - void SetText( int inNum ); - - /// Sets the text to the given number. - void SetText( unsigned inNum ); - - /// Sets the text to the given double. - void SetText( double inNum ); - - /// Sets the text to the given float. - void SetText( float inNum ); /// Sets the text to the given long long. void SetText( long long inNum ); @@ -1545,8 +1533,6 @@ public: virtual bool ShallowEqual( const XMLNode* compare ) const; private: - enum { BUF_SIZE = 200 }; - XMLElement( XMLDocument* doc ); virtual ~XMLElement(); XMLElement( const XMLElement& ); // not supported From 53fe47c74e16903d70f27d01f38b5196e2c8c091 Mon Sep 17 00:00:00 2001 From: Uli Kusterer Date: Mon, 27 Jan 2014 01:47:30 +0100 Subject: [PATCH 09/16] Remove long long stuff again. --- tinyxml2.cpp | 42 ------------------------------------------ tinyxml2.h | 22 ---------------------- 2 files changed, 64 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index fe5b437..58a0e4f 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -411,12 +411,6 @@ void XMLUtil::ToStr( int v, char* buffer, int bufferSize ) } -void XMLUtil::ToStr( long long v, char* buffer, int bufferSize ) -{ - TIXML_SNPRINTF( buffer, bufferSize, "%lld", v ); -} - - void XMLUtil::ToStr( unsigned v, char* buffer, int bufferSize ) { TIXML_SNPRINTF( buffer, bufferSize, "%u", v ); @@ -452,13 +446,6 @@ bool XMLUtil::ToInt( const char* str, int* value ) return false; } -bool XMLUtil::ToLongLong( const char* str, long long* value ) -{ - if ( TIXML_SSCANF( str, "%lld", value ) == 1 ) { - return true; - } - return false; -} bool XMLUtil::ToUnsigned( const char* str, unsigned *value ) { @@ -1180,14 +1167,6 @@ void XMLAttribute::SetAttribute( int v ) } -void XMLAttribute::SetAttribute( long long v ) -{ - char buf[BUF_SIZE]; - XMLUtil::ToStr( v, buf, BUF_SIZE ); - _value.SetStr( buf ); -} - - void XMLAttribute::SetAttribute( unsigned v ) { char buf[BUF_SIZE]; @@ -1333,14 +1312,6 @@ void XMLElement::SetText( double v ) } -void XMLElement::SetText( long long v ) -{ - char buf[BUF_SIZE]; - XMLUtil::ToStr( v, buf, BUF_SIZE ); - SetText( buf ); -} - - void XMLElement::SetBoolFirstChild( bool inBool ) { if( FirstChild() && FirstChild()->ToElement() @@ -1389,19 +1360,6 @@ XMLError XMLElement::QueryIntText( int* ival ) const } -XMLError XMLElement::QueryLongLongText( long long* ival ) const -{ - if ( FirstChild() && FirstChild()->ToText() ) { - const char* t = FirstChild()->ToText()->Value(); - if ( XMLUtil::ToLongLong( t, ival ) ) { - return XML_SUCCESS; - } - return XML_CAN_NOT_CONVERT_TEXT; - } - return XML_NO_TEXT_NODE; -} - - XMLError XMLElement::QueryUnsignedText( unsigned* uval ) const { if ( FirstChild() && FirstChild()->ToText() ) { diff --git a/tinyxml2.h b/tinyxml2.h index 734be7d..afb7597 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -540,7 +540,6 @@ public: // converts primitive types to strings static void ToStr( int v, char* buffer, int bufferSize ); - static void ToStr( long long v, char* buffer, int bufferSize ); static void ToStr( unsigned v, char* buffer, int bufferSize ); static void ToStr( bool v, char* buffer, int bufferSize ); static void ToStr( float v, char* buffer, int bufferSize ); @@ -548,7 +547,6 @@ public: // converts strings to primitive types static bool ToInt( const char* str, int* value ); - static bool ToLongLong( const char* str, long long* value ); static bool ToUnsigned( const char* str, unsigned* value ); static bool ToBool( const char* str, bool* value ); static bool ToFloat( const char* str, float* value ); @@ -1095,8 +1093,6 @@ public: /// Set the attribute to value. void SetAttribute( int value ); /// Set the attribute to value. - void SetAttribute( long long value ); - /// Set the attribute to value. void SetAttribute( unsigned value ); /// Set the attribute to value. void SetAttribute( bool value ); @@ -1312,11 +1308,6 @@ public: a->SetAttribute( value ); } /// Sets the named attribute to value. - void SetAttribute( const char* name, long long value ) { - XMLAttribute* a = FindOrCreateAttribute( name ); - a->SetAttribute( value ); - } - /// Sets the named attribute to value. void SetAttribute( const char* name, unsigned value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( value ); @@ -1425,9 +1416,6 @@ public: /// Convenience method for setting text inside and element. See SetText() for important limitations. void SetText( float value ); - /// Sets the text to the given long long. - void SetText( long long inNum ); - /// Convenience for QueryIntText when you don't care if the text won't convert. int IntText() { @@ -1436,14 +1424,6 @@ public: return i; } - /// Convenience for QueryLongLongText when you don't care if the text won't convert. - long long LongLongText() - { - long long i = 0; - QueryLongLongText( &i ); - return i; - } - /// Convenience for QueryUnsignedText when you don't care if the text won't convert. unsigned UnsignedText() { @@ -1509,8 +1489,6 @@ public: */ XMLError QueryIntText( int* ival ) const; /// See QueryIntText() - XMLError QueryLongLongText( long long* ival ) const; - /// See QueryIntText() XMLError QueryUnsignedText( unsigned* uval ) const; /// See QueryIntText() XMLError QueryBoolText( bool* bval ) const; From 593a33d061086bb9f62e25978fa027dd791dae41 Mon Sep 17 00:00:00 2001 From: Uli Kusterer Date: Sat, 1 Feb 2014 12:48:51 +0100 Subject: [PATCH 10/16] Added SetForceCompactMode() for overriding the compact setting on a per-node level. All sub-nodes will be printed compact as well. --- tinyxml2.cpp | 11 ++++++----- tinyxml2.h | 9 +++++++-- xmltest.cpp | 21 +++++++++++++++++++++ 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 58a0e4f..fad9f50 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -581,7 +581,8 @@ XMLNode::XMLNode( XMLDocument* doc ) : _parent( 0 ), _firstChild( 0 ), _lastChild( 0 ), _prev( 0 ), _next( 0 ), - _memPool( 0 ) + _memPool( 0 ), + _forceCompactMode( false ) { } @@ -1981,17 +1982,17 @@ void XMLPrinter::PushHeader( bool writeBOM, bool writeDec ) } -void XMLPrinter::OpenElement( const char* name ) +void XMLPrinter::OpenElement( const char* name, bool compactMode ) { if ( _elementJustOpened ) { SealElement(); } _stack.Push( name ); - if ( _textDepth < 0 && !_firstElement && !_compactMode ) { + if ( _textDepth < 0 && !_firstElement && !compactMode ) { Print( "\n" ); } - if ( !_compactMode ) { + if ( !compactMode ) { PrintSpace( _depth ); } @@ -2187,7 +2188,7 @@ bool XMLPrinter::VisitEnter( const XMLDocument& doc ) bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute ) { - OpenElement( element.Name() ); + OpenElement( element.Name(), _compactMode ? true : element.Parent()->GetForceCompactMode() ); while ( attribute ) { PushAttribute( attribute->Name(), attribute->Value() ); attribute = attribute->Next(); diff --git a/tinyxml2.h b/tinyxml2.h index afb7597..3b57935 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -821,6 +821,9 @@ public: // internal virtual char* ParseDeep( char*, StrPair* ); + + bool GetForceCompactMode() const { if( _forceCompactMode || !Parent() ) return _forceCompactMode; return Parent()->GetForceCompactMode(); }; + void SetForceCompactMode( bool b ) { _forceCompactMode = b; }; protected: XMLNode( XMLDocument* ); @@ -837,6 +840,8 @@ protected: XMLNode* _prev; XMLNode* _next; + + bool _forceCompactMode; private: MemPool* _memPool; @@ -1509,7 +1514,7 @@ public: char* ParseDeep( char* p, StrPair* endTag ); virtual XMLNode* ShallowClone( XMLDocument* document ) const; virtual bool ShallowEqual( const XMLNode* compare ) const; - + private: XMLElement( XMLDocument* doc ); virtual ~XMLElement(); @@ -2007,7 +2012,7 @@ public: /** If streaming, start writing an element. The element must be closed with CloseElement() */ - void OpenElement( const char* name ); + void OpenElement( const char* name, bool compactMode ); /// If streaming, add an attribute to an open element. void PushAttribute( const char* name, const char* value ); void PushAttribute( const char* name, int value ); diff --git a/xmltest.cpp b/xmltest.cpp index 6fdc162..fa9e1a4 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -1012,6 +1012,27 @@ int main( int argc, const char ** argv ) ele->DeleteAttribute( "attrib3" ); XMLTest( "Attribute order (empty)", false, ele->FirstAttribute() ? true : false ); } + + { + XMLDocument doc0; + XMLElement* root = doc0.NewElement("root"); + doc0.InsertEndChild(root); + XMLElement* text = doc0.NewElement("text"); + text->SetForceCompactMode(true); + root->InsertEndChild(text); + XMLText* befText = doc0.NewText("Before "); + text->InsertEndChild(befText); + XMLElement* tag = doc0.NewElement("tag"); + text->InsertEndChild(tag); + XMLText* tagText = doc0.NewText("Tag"); + tag->InsertEndChild(tagText); + XMLText* aftText = doc0.NewText(" After"); + text->InsertEndChild(aftText); + + XMLPrinter printer; + doc0.Print( &printer ); + XMLTest( "Selective text wrapping", "\n Before Tag After\n\n", printer.CStr() ); + } { // Make sure an attribute with a space in it succeeds. From d5c9e8b81d61fedd7b10e0e2627a06873022b9f6 Mon Sep 17 00:00:00 2001 From: Uli Kusterer Date: Sat, 1 Feb 2014 12:48:51 +0100 Subject: [PATCH 11/16] Added SetForceCompactMode() for overriding the compact setting on a per-node level. All sub-nodes will be printed compact as well. --- tinyxml2.cpp | 11 ++++++----- tinyxml2.h | 9 +++++++-- xmltest.cpp | 21 +++++++++++++++++++++ 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index cb26e00..91f4011 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -580,7 +580,8 @@ XMLNode::XMLNode( XMLDocument* doc ) : _parent( 0 ), _firstChild( 0 ), _lastChild( 0 ), _prev( 0 ), _next( 0 ), - _memPool( 0 ) + _memPool( 0 ), + _forceCompactMode( false ) { } @@ -1945,17 +1946,17 @@ void XMLPrinter::PushHeader( bool writeBOM, bool writeDec ) } -void XMLPrinter::OpenElement( const char* name ) +void XMLPrinter::OpenElement( const char* name, bool compactMode ) { if ( _elementJustOpened ) { SealElement(); } _stack.Push( name ); - if ( _textDepth < 0 && !_firstElement && !_compactMode ) { + if ( _textDepth < 0 && !_firstElement && !compactMode ) { Print( "\n" ); } - if ( !_compactMode ) { + if ( !compactMode ) { PrintSpace( _depth ); } @@ -2151,7 +2152,7 @@ bool XMLPrinter::VisitEnter( const XMLDocument& doc ) bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute ) { - OpenElement( element.Name() ); + OpenElement( element.Name(), _compactMode ? true : element.Parent()->GetForceCompactMode() ); while ( attribute ) { PushAttribute( attribute->Name(), attribute->Value() ); attribute = attribute->Next(); diff --git a/tinyxml2.h b/tinyxml2.h index 41f1c70..09f6dd2 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -821,6 +821,9 @@ public: // internal virtual char* ParseDeep( char*, StrPair* ); + + bool GetForceCompactMode() const { if( _forceCompactMode || !Parent() ) return _forceCompactMode; return Parent()->GetForceCompactMode(); }; + void SetForceCompactMode( bool b ) { _forceCompactMode = b; }; protected: XMLNode( XMLDocument* ); @@ -837,6 +840,8 @@ protected: XMLNode* _prev; XMLNode* _next; + + bool _forceCompactMode; private: MemPool* _memPool; @@ -1463,7 +1468,7 @@ public: char* ParseDeep( char* p, StrPair* endTag ); virtual XMLNode* ShallowClone( XMLDocument* document ) const; virtual bool ShallowEqual( const XMLNode* compare ) const; - + private: XMLElement( XMLDocument* doc ); virtual ~XMLElement(); @@ -1961,7 +1966,7 @@ public: /** If streaming, start writing an element. The element must be closed with CloseElement() */ - void OpenElement( const char* name ); + void OpenElement( const char* name, bool compactMode ); /// If streaming, add an attribute to an open element. void PushAttribute( const char* name, const char* value ); void PushAttribute( const char* name, int value ); diff --git a/xmltest.cpp b/xmltest.cpp index 6fdc162..fa9e1a4 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -1012,6 +1012,27 @@ int main( int argc, const char ** argv ) ele->DeleteAttribute( "attrib3" ); XMLTest( "Attribute order (empty)", false, ele->FirstAttribute() ? true : false ); } + + { + XMLDocument doc0; + XMLElement* root = doc0.NewElement("root"); + doc0.InsertEndChild(root); + XMLElement* text = doc0.NewElement("text"); + text->SetForceCompactMode(true); + root->InsertEndChild(text); + XMLText* befText = doc0.NewText("Before "); + text->InsertEndChild(befText); + XMLElement* tag = doc0.NewElement("tag"); + text->InsertEndChild(tag); + XMLText* tagText = doc0.NewText("Tag"); + tag->InsertEndChild(tagText); + XMLText* aftText = doc0.NewText(" After"); + text->InsertEndChild(aftText); + + XMLPrinter printer; + doc0.Print( &printer ); + XMLTest( "Selective text wrapping", "\n Before Tag After\n\n", printer.CStr() ); + } { // Make sure an attribute with a space in it succeeds. From 15354f88c39e33174f9058130d9897f8a031b7bc Mon Sep 17 00:00:00 2001 From: Uli Kusterer Date: Sat, 1 Feb 2014 13:06:39 +0100 Subject: [PATCH 12/16] Give tests a few more chances to fail, e.g. on tag sequences or attributes. --- xmltest.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/xmltest.cpp b/xmltest.cpp index fa9e1a4..3997b64 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -1026,12 +1026,17 @@ int main( int argc, const char ** argv ) text->InsertEndChild(tag); XMLText* tagText = doc0.NewText("Tag"); tag->InsertEndChild(tagText); + tag = doc0.NewElement("tagtwo"); + tag->SetAttribute("two", "2"); + text->InsertEndChild(tag); + tagText = doc0.NewText("TagTwo"); + tag->InsertEndChild(tagText); XMLText* aftText = doc0.NewText(" After"); text->InsertEndChild(aftText); XMLPrinter printer; doc0.Print( &printer ); - XMLTest( "Selective text wrapping", "\n Before Tag After\n\n", printer.CStr() ); + XMLTest( "Selective text wrapping", "\n Before TagTagTwo After\n\n", printer.CStr() ); } { From ca412e87f2815dfbbf09ea056ed2515d6cb2ae12 Mon Sep 17 00:00:00 2001 From: Uli Kusterer Date: Sat, 1 Feb 2014 13:35:05 +0100 Subject: [PATCH 13/16] SetForceCompactMode() now also handles case of a single tag inside another correctly. --- tinyxml2.cpp | 10 +++++----- tinyxml2.h | 2 +- xmltest.cpp | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 91f4011..59a149e 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -2008,7 +2008,7 @@ void XMLPrinter::PushAttribute( const char* name, double v ) } -void XMLPrinter::CloseElement() +void XMLPrinter::CloseElement( bool compactMode ) { --_depth; const char* name = _stack.Pop(); @@ -2017,7 +2017,7 @@ void XMLPrinter::CloseElement() Print( "/>" ); } else { - if ( _textDepth < 0 && !_compactMode) { + if ( _textDepth < 0 && !compactMode) { Print( "\n" ); PrintSpace( _depth ); } @@ -2027,7 +2027,7 @@ void XMLPrinter::CloseElement() if ( _textDepth == _depth ) { _textDepth = -1; } - if ( _depth == 0 && !_compactMode) { + if ( _depth == 0 && !compactMode) { Print( "\n" ); } _elementJustOpened = false; @@ -2161,9 +2161,9 @@ bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attr } -bool XMLPrinter::VisitExit( const XMLElement& ) +bool XMLPrinter::VisitExit( const XMLElement& element ) { - CloseElement(); + CloseElement( _compactMode ? true : element.GetForceCompactMode() ); return true; } diff --git a/tinyxml2.h b/tinyxml2.h index 09f6dd2..eae8cd4 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -1974,7 +1974,7 @@ public: void PushAttribute( const char* name, bool value ); void PushAttribute( const char* name, double value ); /// If streaming, close the Element. - virtual void CloseElement(); + virtual void CloseElement( bool compactMode ); /// Add a text node. void PushText( const char* text, bool cdata=false ); diff --git a/xmltest.cpp b/xmltest.cpp index 3997b64..7c24090 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -1039,6 +1039,21 @@ int main( int argc, const char ** argv ) XMLTest( "Selective text wrapping", "\n Before TagTagTwo After\n\n", printer.CStr() ); } + { + XMLDocument doc0; + XMLElement* root = doc0.NewElement("root"); + doc0.InsertEndChild(root); + XMLElement* cool = doc0.NewElement("cool"); + cool->SetForceCompactMode(true); + root->InsertEndChild(cool); + XMLElement* tag = doc0.NewElement("true"); + cool->InsertEndChild(tag); + + XMLPrinter printer; + doc0.Print( &printer ); + XMLTest( "Selective text around single tag", "\n \n\n", printer.CStr() ); + } + { // Make sure an attribute with a space in it succeeds. static const char* xml0 = ""; From 07ac762b80530c7f5aade11f6cf8f487c2fabc4b Mon Sep 17 00:00:00 2001 From: Uli Kusterer Date: Sat, 1 Feb 2014 15:06:50 +0100 Subject: [PATCH 14/16] SetBoolFirstChild now makes sure its bool is not wrapped onto its own line. --- tinyxml2.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 17dc5ec..5117986 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -1315,13 +1315,15 @@ void XMLElement::SetText( double v ) void XMLElement::SetBoolFirstChild( bool inBool ) { - if( FirstChild() && FirstChild()->ToElement() - && (strcmp(FirstChild()->Value(),"true") == 0 || strcmp(FirstChild()->Value(),"false") == 0) ) { - FirstChild()->SetValue( inBool ? "true" : "false" ); + XMLElement * theBoolElem = FirstChild() ? FirstChild()->ToElement() : NULL; + if( theBoolElem + && (strcmp(theBoolElem->Value(),"true") == 0 || strcmp(theBoolElem->Value(),"false") == 0) ) { + theBoolElem->SetValue( inBool ? "true" : "false" ); } else if( !FirstChild() ) { - XMLElement* theText = GetDocument()->NewElement( inBool ? "true" : "false" ); - InsertFirstChild( theText ); + theBoolElem = GetDocument()->NewElement( inBool ? "true" : "false" ); + InsertFirstChild( theBoolElem ); + SetForceCompactMode(true); } } From 5d1d27e88b8f7c98d8b4f59288716cb6f904ee75 Mon Sep 17 00:00:00 2001 From: Uli Kusterer Date: Thu, 20 Feb 2014 11:50:22 +0100 Subject: [PATCH 15/16] Keep indentation control fully inside the XMLPrinter. You'll have to subclass it to override its standard behaviour by overwriting CompactMode(). --- tinyxml2.cpp | 7 ++++--- tinyxml2.h | 5 ++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 5117986..beaeeb8 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -1323,7 +1323,6 @@ void XMLElement::SetBoolFirstChild( bool inBool ) else if( !FirstChild() ) { theBoolElem = GetDocument()->NewElement( inBool ? "true" : "false" ); InsertFirstChild( theBoolElem ); - SetForceCompactMode(true); } } @@ -2190,7 +2189,9 @@ bool XMLPrinter::VisitEnter( const XMLDocument& doc ) bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute ) { - OpenElement( element.Name(), _compactMode ? true : element.Parent()->GetForceCompactMode() ); + const XMLElement* parentElem = element.Parent()->ToElement(); + bool compactMode = parentElem ? CompactMode(*parentElem) : _compactMode; + OpenElement( element.Name(), compactMode ); while ( attribute ) { PushAttribute( attribute->Name(), attribute->Value() ); attribute = attribute->Next(); @@ -2201,7 +2202,7 @@ bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attr bool XMLPrinter::VisitExit( const XMLElement& element ) { - CloseElement( _compactMode ? true : element.GetForceCompactMode() ); + CloseElement( CompactMode(element) ); return true; } diff --git a/tinyxml2.h b/tinyxml2.h index 431088d..a584010 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -822,9 +822,6 @@ public: // internal virtual char* ParseDeep( char*, StrPair* ); - bool GetForceCompactMode() const { if( _forceCompactMode || !Parent() ) return _forceCompactMode; return Parent()->GetForceCompactMode(); }; - void SetForceCompactMode( bool b ) { _forceCompactMode = b; }; - protected: XMLNode( XMLDocument* ); virtual ~XMLNode(); @@ -2079,6 +2076,8 @@ public: } protected: + virtual bool CompactMode( const XMLElement& elem ) { return _compactMode; }; + void SealElement(); bool _elementJustOpened; DynArray< const char*, 10 > _stack; From e6934887423260751f898d3a5633c0fffefdb441 Mon Sep 17 00:00:00 2001 From: Lee Thomason Date: Sat, 22 Feb 2014 16:27:27 -0800 Subject: [PATCH 16/16] fix compilation warning. --- tinyxml2.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tinyxml2.h b/tinyxml2.h index 605a1ca..615afa5 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -2034,7 +2034,7 @@ public: } protected: - virtual bool CompactMode( const XMLElement& elem ) { return _compactMode; }; + virtual bool CompactMode( const XMLElement& ) { return _compactMode; }; /** Prints out the space before an element. You may override to change the space and tabs used. A PrintSpace() override should call Print().