From 75a5acc6bbebf3faeb076779ed9f1fb9c8a4c4ac Mon Sep 17 00:00:00 2001 From: cugone Date: Thu, 11 Apr 2019 22:03:43 -0500 Subject: [PATCH 1/5] UnsignedInt64 support --- tinyxml2.cpp | 19 +++++++++++++++++++ tinyxml2.h | 21 ++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 1c74279..0d27eab 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -653,6 +653,16 @@ bool XMLUtil::ToInt64(const char* str, int64_t* value) } +bool XMLUtil::ToUnsigned64(const char* str, uint64_t* value) { + unsigned long long v = 0; // horrible syntax trick to make the compiler happy about %llu + if(TIXML_SSCANF(str, "%llu", &v) == 1) { + *value = (uint64_t)v; + return true; + } + return false; +} + + char* XMLDocument::Identify( char* p, XMLNode** node ) { TIXMLASSERT( node ); @@ -1414,6 +1424,15 @@ XMLError XMLAttribute::QueryInt64Value(int64_t* value) const } +XMLError XMLAttribute::QueryUnsigned64Value(uint64_t* value) const +{ + if(XMLUtil::ToUnsigned64(Value(), value)) { + return XML_SUCCESS; + } + return XML_WRONG_ATTRIBUTE_TYPE; +} + + XMLError XMLAttribute::QueryBoolValue( bool* value ) const { if ( XMLUtil::ToBool( Value(), value )) { diff --git a/tinyxml2.h b/tinyxml2.h index c7d4070..f9fd093 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -625,7 +625,7 @@ public: static bool ToFloat( const char* str, float* value ); static bool ToDouble( const char* str, double* value ); static bool ToInt64(const char* str, int64_t* value); - + static bool ToUnsigned64(const char* str, uint64_t* value); // Changes what is serialized for a boolean value. // Default to "true" and "false". Shouldn't be changed // unless you have a special testing or compatibility need. @@ -1164,6 +1164,12 @@ public: return i; } + uint64_t Unsigned64Value() const { + uint64_t i = 0; + QueryUnsigned64Value(&i); + return i; + } + /// Query as an unsigned integer. See IntValue() unsigned UnsignedValue() const { unsigned i=0; @@ -1198,6 +1204,8 @@ public: XMLError QueryUnsignedValue( unsigned int* value ) const; /// See QueryIntValue XMLError QueryInt64Value(int64_t* value) const; + /// See QueryIntValue + XMLError QueryUnsigned64Value(uint64_t* value) const; /// See QueryIntValue XMLError QueryBoolValue( bool* value ) const; /// See QueryIntValue @@ -1301,6 +1309,8 @@ public: unsigned UnsignedAttribute(const char* name, unsigned defaultValue = 0) const; /// See IntAttribute() int64_t Int64Attribute(const char* name, int64_t defaultValue = 0) const; + /// See IntAttribute() + uint64_t Unsigned64Attribute(const char* name, uint64_t defaultValue = 0) const; /// See IntAttribute() bool BoolAttribute(const char* name, bool defaultValue = false) const; /// See IntAttribute() @@ -1347,6 +1357,15 @@ public: return a->QueryInt64Value(value); } + /// See QueryIntAttribute() + XMLError QueryUnsigned64Attribute(const char* name, uint64_t* value) const { + const XMLAttribute* a = FindAttribute(name); + if(!a) { + return XML_NO_ATTRIBUTE; + } + return a->QueryUnsigned64Value(value); + } + /// See QueryIntAttribute() XMLError QueryBoolAttribute( const char* name, bool* value ) const { const XMLAttribute* a = FindAttribute( name ); From 47e229e1890b98d07a7d1501148833721a976a49 Mon Sep 17 00:00:00 2001 From: cugone Date: Fri, 12 Apr 2019 17:27:28 -0500 Subject: [PATCH 2/5] Forgot Text and XMLElement versions; --- tinyxml2.cpp | 25 ++++++++++++++++++++++++- tinyxml2.h | 7 +++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 0d27eab..5a797fc 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -582,12 +582,17 @@ void XMLUtil::ToStr( double v, char* buffer, int bufferSize ) } -void XMLUtil::ToStr(int64_t v, char* buffer, int bufferSize) +void XMLUtil::ToStr( int64_t v, char* buffer, int bufferSize ) { // horrible syntax trick to make the compiler happy about %lld TIXML_SNPRINTF(buffer, bufferSize, "%lld", (long long)v); } +void XMLUtil::ToStr( uint64_t v, char* buffer, int bufferSize ) +{ + // horrible syntax trick to make the compiler happy about %llu + TIXML_SNPRINTF(buffer, bufferSize, "%llu", (long long)v); +} bool XMLUtil::ToInt( const char* str, int* value ) { @@ -1639,6 +1644,12 @@ void XMLElement::SetText(int64_t v) SetText(buf); } +void XMLElement::SetText(uint64_t v) { + char buf[BUF_SIZE]; + XMLUtil::ToStr(v, buf, BUF_SIZE); + SetText(buf); +} + void XMLElement::SetText( bool v ) { @@ -1703,6 +1714,18 @@ XMLError XMLElement::QueryInt64Text(int64_t* ival) const } +XMLError XMLElement::QueryUnsigned64Text(uint64_t* ival) const { + if(FirstChild() && FirstChild()->ToText()) { + const char* t = FirstChild()->Value(); + if(XMLUtil::ToUnsigned64(t, ival)) { + return XML_SUCCESS; + } + return XML_CAN_NOT_CONVERT_TEXT; + } + return XML_NO_TEXT_NODE; +} + + XMLError XMLElement::QueryBoolText( bool* bval ) const { if ( FirstChild() && FirstChild()->ToText() ) { diff --git a/tinyxml2.h b/tinyxml2.h index f9fd093..f9600c2 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -617,6 +617,7 @@ public: static void ToStr( float v, char* buffer, int bufferSize ); static void ToStr( double v, char* buffer, int bufferSize ); static void ToStr(int64_t v, char* buffer, int bufferSize); + static void ToStr(uint64_t v, char* buffer, int bufferSize); // converts strings to primitive types static bool ToInt( const char* str, int* value ); @@ -1565,6 +1566,8 @@ public: void SetText( unsigned value ); /// Convenience method for setting text inside an element. See SetText() for important limitations. void SetText(int64_t value); + /// Convenience method for setting text inside an element. See SetText() for important limitations. + void SetText(uint64_t value); /// Convenience method for setting text inside an element. See SetText() for important limitations. void SetText( bool value ); /// Convenience method for setting text inside an element. See SetText() for important limitations. @@ -1604,6 +1607,8 @@ public: /// See QueryIntText() XMLError QueryInt64Text(int64_t* uval) const; /// See QueryIntText() + XMLError QueryUnsigned64Text(uint64_t* uval) const; + /// See QueryIntText() XMLError QueryBoolText( bool* bval ) const; /// See QueryIntText() XMLError QueryDoubleText( double* dval ) const; @@ -1616,6 +1621,8 @@ public: unsigned UnsignedText(unsigned defaultValue = 0) const; /// See QueryIntText() int64_t Int64Text(int64_t defaultValue = 0) const; + /// See QueryIntText() + uint64_t Unsigned64Text(uint64_t defaultValue = 0) const; /// See QueryIntText() bool BoolText(bool defaultValue = false) const; /// See QueryIntText() From 1dbfe31858f040483a686115f4e2cf9b20531d9d Mon Sep 17 00:00:00 2001 From: cugone Date: Fri, 12 Apr 2019 17:33:49 -0500 Subject: [PATCH 3/5] Missing uint64_t XMLElement::Unsigned64Text(uint64_t defaultValue) implementation --- tinyxml2.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 5a797fc..67eb00b 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -1714,7 +1714,8 @@ XMLError XMLElement::QueryInt64Text(int64_t* ival) const } -XMLError XMLElement::QueryUnsigned64Text(uint64_t* ival) const { +XMLError XMLElement::QueryUnsigned64Text(uint64_t* ival) const +{ if(FirstChild() && FirstChild()->ToText()) { const char* t = FirstChild()->Value(); if(XMLUtil::ToUnsigned64(t, ival)) { @@ -1785,6 +1786,13 @@ int64_t XMLElement::Int64Text(int64_t defaultValue) const return i; } +uint64_t XMLElement::Unsigned64Text(uint64_t defaultValue) const +{ + uint64_t i = defaultValue; + QueryUnsigned64Text(&i); + return i; +} + bool XMLElement::BoolText(bool defaultValue) const { bool b = defaultValue; From c341cea501ff10a2f8e749ecb3bd1c80339c1b9b Mon Sep 17 00:00:00 2001 From: "aaronkirkham07@gmail.com" Date: Wed, 22 May 2019 00:05:27 +0100 Subject: [PATCH 4/5] add missing uint64_t methods to XMLPrinter add missing XMLElement::Unsigned64Attribute body --- tinyxml2.cpp | 25 +++++++++++++++++++++++++ tinyxml2.h | 9 ++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 67eb00b..b1259dc 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -1580,6 +1580,13 @@ int64_t XMLElement::Int64Attribute(const char* name, int64_t defaultValue) const return i; } +uint64_t XMLElement::Unsigned64Attribute(const char* name, uint64_t defaultValue) const +{ + uint64_t i = defaultValue; + QueryUnsigned64Attribute(name, &i); + return i; +} + bool XMLElement::BoolAttribute(const char* name, bool defaultValue) const { bool b = defaultValue; @@ -2664,6 +2671,14 @@ void XMLPrinter::PushAttribute(const char* name, int64_t v) } +void XMLPrinter::PushAttribute(const char* name, uint64_t v) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr(v, buf, BUF_SIZE); + PushAttribute(name, buf); +} + + void XMLPrinter::PushAttribute( const char* name, bool v ) { char buf[BUF_SIZE]; @@ -2733,6 +2748,7 @@ void XMLPrinter::PushText( const char* text, bool cdata ) } } + void XMLPrinter::PushText( int64_t value ) { char buf[BUF_SIZE]; @@ -2740,6 +2756,15 @@ void XMLPrinter::PushText( int64_t value ) PushText( buf, false ); } + +void XMLPrinter::PushText( uint64_t value ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr(value, buf, BUF_SIZE); + PushText(buf, false); +} + + void XMLPrinter::PushText( int value ) { char buf[BUF_SIZE]; diff --git a/tinyxml2.h b/tinyxml2.h index f9600c2..6b0a758 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -2220,7 +2220,8 @@ public: void PushAttribute( const char* name, const char* value ); void PushAttribute( const char* name, int value ); void PushAttribute( const char* name, unsigned value ); - void PushAttribute(const char* name, int64_t value); + void PushAttribute( const char* name, int64_t value ); + void PushAttribute( const char* name, uint64_t value ); void PushAttribute( const char* name, bool value ); void PushAttribute( const char* name, double value ); /// If streaming, close the Element. @@ -2232,8 +2233,10 @@ public: void PushText( int value ); /// Add a text node from an unsigned. void PushText( unsigned value ); - /// Add a text node from an unsigned. - void PushText(int64_t value); + /// Add a text node from a signed 64bit integer. + void PushText( int64_t value ); + /// Add a text node from an unsigned 64bit integer. + void PushText( uint64_t value ); /// Add a text node from a bool. void PushText( bool value ); /// Add a text node from a float. From effdf95f8c3eadf97b816cecdb4d49df9b585d86 Mon Sep 17 00:00:00 2001 From: Lee Thomason Date: Sat, 10 Aug 2019 17:49:42 -0700 Subject: [PATCH 5/5] add test cases --- tinyxml2.cpp | 6 ++++++ tinyxml2.h | 18 +++++++++++++++--- xmltest.cpp | 35 +++++++++++++++++++++++++++++++---- 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index b1259dc..2622dd3 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -1494,6 +1494,12 @@ void XMLAttribute::SetAttribute(int64_t v) _value.SetStr(buf); } +void XMLAttribute::SetAttribute(uint64_t v) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr(v, buf, BUF_SIZE); + _value.SetStr(buf); +} void XMLAttribute::SetAttribute( bool v ) diff --git a/tinyxml2.h b/tinyxml2.h index 92e3d67..e61ca6a 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -1222,7 +1222,9 @@ public: void SetAttribute( unsigned value ); /// Set the attribute to value. void SetAttribute(int64_t value); - /// Set the attribute to value. + /// Set the attribute to value. + void SetAttribute(uint64_t value); + /// Set the attribute to value. void SetAttribute( bool value ); /// Set the attribute to value. void SetAttribute( double value ); @@ -1433,7 +1435,11 @@ public: return QueryInt64Attribute(name, value); } - XMLError QueryAttribute( const char* name, bool* value ) const { + XMLError QueryAttribute(const char* name, uint64_t* value) const { + return QueryUnsigned64Attribute(name, value); + } + + XMLError QueryAttribute( const char* name, bool* value ) const { return QueryBoolAttribute( name, value ); } @@ -1467,7 +1473,13 @@ public: a->SetAttribute(value); } - /// Sets the named attribute to value. + /// Sets the named attribute to value. + void SetAttribute(const char* name, uint64_t value) { + XMLAttribute* a = FindOrCreateAttribute(name); + a->SetAttribute(value); + } + + /// Sets the named attribute to value. void SetAttribute( const char* name, bool value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( value ); diff --git a/xmltest.cpp b/xmltest.cpp index a0aaee7..d2bc3ea 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -804,6 +804,7 @@ int main( int argc, const char ** argv ) // ---------- Attributes --------- { static const int64_t BIG = -123456789012345678; + static const uint64_t BIG_POS = 123456789012345678; XMLDocument doc; XMLElement* element = doc.NewElement("element"); doc.InsertFirstChild(element); @@ -864,7 +865,23 @@ int main( int argc, const char ** argv ) } XMLTest("Attribute: int64_t", BIG, element->Int64Attribute("attrib"), true); } - { + { + element->SetAttribute("attrib", BIG_POS); + { + uint64_t v = 0; + XMLError queryResult = element->QueryUnsigned64Attribute("attrib", &v); + XMLTest("Attribute: uint64_t", XML_SUCCESS, queryResult, true); + XMLTest("Attribute: uint64_t", BIG_POS, v, true); + } + { + uint64_t v = 0; + int queryResult = element->QueryAttribute("attrib", &v); + XMLTest("Attribute: uint64_t", (int)XML_SUCCESS, queryResult, true); + XMLTest("Attribute: uint64_t", BIG_POS, v, true); + } + XMLTest("Attribute: uint64_t", BIG_POS, element->Unsigned64Attribute("attrib"), true); + } + { element->SetAttribute("attrib", true); { bool v = false; @@ -931,7 +948,14 @@ int main( int argc, const char ** argv ) XMLTest("Element: int64_t", XML_SUCCESS, queryResult, true); XMLTest("Element: int64_t", BIG, v, true); } - } + { + element->SetText(BIG_POS); + uint64_t v = 0; + XMLError queryResult = element->QueryUnsigned64Text(&v); + XMLTest("Element: uint64_t", XML_SUCCESS, queryResult, true); + XMLTest("Element: uint64_t", BIG_POS, v, true); + } + } // ---------- XMLPrinter stream mode ------ { @@ -944,7 +968,8 @@ int main( int argc, const char ** argv ) printer.PushAttribute("attrib-int", int(1)); printer.PushAttribute("attrib-unsigned", unsigned(2)); printer.PushAttribute("attrib-int64", int64_t(3)); - printer.PushAttribute("attrib-bool", true); + printer.PushAttribute("attrib-uint64", uint64_t(37)); + printer.PushAttribute("attrib-bool", true); printer.PushAttribute("attrib-double", 4.0); printer.CloseElement(); fclose(printerfp); @@ -964,7 +989,9 @@ int main( int argc, const char ** argv ) XMLTest("attrib-unsigned", unsigned(2), attrib->UnsignedValue(), true); attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-int64"); XMLTest("attrib-int64", int64_t(3), attrib->Int64Value(), true); - attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-bool"); + attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-uint64"); + XMLTest("attrib-uint64", uint64_t(37), attrib->Unsigned64Value(), true); + attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-bool"); XMLTest("attrib-bool", true, attrib->BoolValue(), true); attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-double"); XMLTest("attrib-double", 4.0, attrib->DoubleValue(), true);