From 3a621f5b6ebf69448a149aaf9b1e7c879d5d3eaf Mon Sep 17 00:00:00 2001 From: Josh Wittner Date: Mon, 12 Sep 2016 19:17:54 -0700 Subject: [PATCH 1/3] Added default values TypeText accessors --- tinyxml2.h | 66 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 54 insertions(+), 12 deletions(-) diff --git a/tinyxml2.h b/tinyxml2.h index 4379f01..cafc422 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -1231,41 +1231,41 @@ public: returned if there is an error. For a method with error checking, see QueryIntAttribute() */ - int IntAttribute( const char* name ) const { - int i=0; + int IntAttribute(const char* name, int defaultValue = 0) const { + int i=defaultValue; QueryIntAttribute( name, &i ); return i; } /// See IntAttribute() - unsigned UnsignedAttribute( const char* name ) const { - unsigned i=0; + unsigned UnsignedAttribute(const char* name , unsigned defaultValue = 0) const { + unsigned i=defaultValue; QueryUnsignedAttribute( name, &i ); return i; } /// See IntAttribute() - int64_t Int64Attribute(const char* name) const { - int64_t i = 0; + int64_t Int64Attribute(const char* name, int64_t defaultValue = 0) const { + int64_t i = defaultValue; QueryInt64Attribute(name, &i); return i; } /// See IntAttribute() - bool BoolAttribute( const char* name ) const { - bool b=false; + bool BoolAttribute( const char* name, bool defaultValue = false ) const { + bool b=defaultValue; QueryBoolAttribute( name, &b ); return b; } /// See IntAttribute() - double DoubleAttribute( const char* name ) const { - double d=0; + double DoubleAttribute( const char* name, double defaultValue=0 ) const { + double d=defaultValue; QueryDoubleAttribute( name, &d ); return d; } /// See IntAttribute() - float FloatAttribute( const char* name ) const { - float f=0; + float FloatAttribute( const char* name, float defaultValue=0 ) const { + float f=defaultValue; QueryFloatAttribute( name, &f ); return f; } @@ -1542,6 +1542,48 @@ public: /// See QueryIntText() XMLError QueryFloatText( float* fval ) const; + int IntText(int defaultValue = 0) const + { + int i = defaultValue; + QueryIntText(&i); + return i; + } + /// See QueryIntText() + unsigned UnsignedText(unsigned defaultValue = 0) const + { + unsigned i = defaultValue; + QueryUnsignedText(&i); + return i; + } + /// See QueryIntText() + int64_t Int64Text(int64_t defaultValue = 0) const + { + int64_t i = defaultValue; + QueryInt64Text(&i); + return i; + } + /// See QueryIntText() + bool BoolText(bool defaultValue = false) const + { + bool b = defaultValue; + QueryBoolText(&b); + return b; + } + /// See QueryIntText() + double DoubleText(double defaultValue = 0) const + { + double d = defaultValue; + QueryDoubleText(&d); + return d; + } + /// See QueryIntText() + float FloatText(float defaultValue = 0) const + { + float f = defaultValue; + QueryFloatText(&f); + return f; + } + // internal: enum { OPEN, // From cf3dd09b08742fbc2b136d31854581b2b5a63547 Mon Sep 17 00:00:00 2001 From: Josh Wittner Date: Tue, 11 Oct 2016 18:57:17 -0700 Subject: [PATCH 2/3] Move implementations to cpp --- tinyxml2.cpp | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tinyxml2.h | 82 +++++++++------------------------------------------- 2 files changed, 95 insertions(+), 69 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 9a8904f..f8983a1 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -1448,6 +1448,47 @@ const char* XMLElement::Attribute( const char* name, const char* value ) const return 0; } +int XMLElement::IntAttribute(const char* name, int defaultValue) const +{ + int i = defaultValue; + QueryIntAttribute(name, &i); + return i; +} + +unsigned XMLElement::UnsignedAttribute(const char* name, unsigned defaultValue) const +{ + unsigned i = defaultValue; + QueryUnsignedAttribute(name, &i); + return i; +} + +int64_t XMLElement::Int64Attribute(const char* name, int64_t defaultValue) const +{ + int64_t i = defaultValue; + QueryInt64Attribute(name, &i); + return i; +} + +bool XMLElement::BoolAttribute(const char* name, bool defaultValue) const +{ + bool b = defaultValue; + QueryBoolAttribute(name, &b); + return b; +} + +double XMLElement::DoubleAttribute(const char* name, double defaultValue) const +{ + double d = defaultValue; + QueryDoubleAttribute(name, &d); + return d; +} + +float XMLElement::FloatAttribute(const char* name, float defaultValue) const +{ + float f = defaultValue; + QueryFloatAttribute(name, &f); + return f; +} const char* XMLElement::GetText() const { @@ -1594,6 +1635,47 @@ XMLError XMLElement::QueryFloatText( float* fval ) const return XML_NO_TEXT_NODE; } +int XMLElement::IntText(int defaultValue) const +{ + int i = defaultValue; + QueryIntText(&i); + return i; +} + +unsigned XMLElement::UnsignedText(unsigned defaultValue) const +{ + unsigned i = defaultValue; + QueryUnsignedText(&i); + return i; +} + +int64_t XMLElement::Int64Text(int64_t defaultValue) const +{ + int64_t i = defaultValue; + QueryInt64Text(&i); + return i; +} + +bool XMLElement::BoolText(bool defaultValue) const +{ + bool b = defaultValue; + QueryBoolText(&b); + return b; +} + +double XMLElement::DoubleText(double defaultValue) const +{ + double d = defaultValue; + QueryDoubleText(&d); + return d; +} + +float XMLElement::FloatText(float defaultValue) const +{ + float f = defaultValue; + QueryFloatText(&f); + return f; +} XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name ) diff --git a/tinyxml2.h b/tinyxml2.h index cafc422..9f5dbf7 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -1231,44 +1231,17 @@ public: returned if there is an error. For a method with error checking, see QueryIntAttribute() */ - int IntAttribute(const char* name, int defaultValue = 0) const { - int i=defaultValue; - QueryIntAttribute( name, &i ); - return i; - } - + int IntAttribute(const char* name, int defaultValue = 0) const; /// See IntAttribute() - unsigned UnsignedAttribute(const char* name , unsigned defaultValue = 0) const { - unsigned i=defaultValue; - QueryUnsignedAttribute( name, &i ); - return i; - } - + unsigned UnsignedAttribute(const char* name, unsigned defaultValue = 0) const; /// See IntAttribute() - int64_t Int64Attribute(const char* name, int64_t defaultValue = 0) const { - int64_t i = defaultValue; - QueryInt64Attribute(name, &i); - return i; - } - + int64_t Int64Attribute(const char* name, int64_t defaultValue = 0) const; /// See IntAttribute() - bool BoolAttribute( const char* name, bool defaultValue = false ) const { - bool b=defaultValue; - QueryBoolAttribute( name, &b ); - return b; - } + bool BoolAttribute(const char* name, bool defaultValue = false) const; /// See IntAttribute() - double DoubleAttribute( const char* name, double defaultValue=0 ) const { - double d=defaultValue; - QueryDoubleAttribute( name, &d ); - return d; - } + double DoubleAttribute(const char* name, double defaultValue = 0) const; /// See IntAttribute() - float FloatAttribute( const char* name, float defaultValue=0 ) const { - float f=defaultValue; - QueryFloatAttribute( name, &f ); - return f; - } + float FloatAttribute(const char* name, float defaultValue = 0) const; /** Given an attribute name, QueryIntAttribute() returns XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion @@ -1542,47 +1515,18 @@ public: /// See QueryIntText() XMLError QueryFloatText( float* fval ) const; - int IntText(int defaultValue = 0) const - { - int i = defaultValue; - QueryIntText(&i); - return i; - } + int IntText(int defaultValue = 0) const; + /// See QueryIntText() - unsigned UnsignedText(unsigned defaultValue = 0) const - { - unsigned i = defaultValue; - QueryUnsignedText(&i); - return i; - } + unsigned UnsignedText(unsigned defaultValue = 0) const; /// See QueryIntText() - int64_t Int64Text(int64_t defaultValue = 0) const - { - int64_t i = defaultValue; - QueryInt64Text(&i); - return i; - } + int64_t Int64Text(int64_t defaultValue = 0) const; /// See QueryIntText() - bool BoolText(bool defaultValue = false) const - { - bool b = defaultValue; - QueryBoolText(&b); - return b; - } + bool BoolText(bool defaultValue = false) const; /// See QueryIntText() - double DoubleText(double defaultValue = 0) const - { - double d = defaultValue; - QueryDoubleText(&d); - return d; - } + double DoubleText(double defaultValue = 0) const; /// See QueryIntText() - float FloatText(float defaultValue = 0) const - { - float f = defaultValue; - QueryFloatText(&f); - return f; - } + float FloatText(float defaultValue = 0) const; // internal: enum { From 13cbc9a708a1e3ab8ae2fa7bd3ebbe7348ccff41 Mon Sep 17 00:00:00 2001 From: Lee Thomason Date: Thu, 27 Oct 2016 14:55:07 -0700 Subject: [PATCH 3/3] add test files. fix doc. --- tinyxml2.h | 7 ++++--- xmltest.cpp | 26 ++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/tinyxml2.h b/tinyxml2.h index d385c85..82422a4 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -1233,9 +1233,10 @@ public: const char* Attribute( const char* name, const char* value=0 ) const; /** Given an attribute name, IntAttribute() returns the value - of the attribute interpreted as an integer. 0 will be - returned if there is an error. For a method with error - checking, see QueryIntAttribute() + of the attribute interpreted as an integer. The default + value will be returned if the attribute isn't present, + or if there is an error. (For a method with error + checking, see QueryIntAttribute()). */ int IntAttribute(const char* name, int defaultValue = 0) const; /// See IntAttribute() diff --git a/xmltest.cpp b/xmltest.cpp index f474951..0b8b9e5 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -436,10 +436,12 @@ int main( int argc, const char ** argv ) element->LastChildElement()->DeleteAttribute( "attrib" ); XMLTest( "Programmatic DOM", true, doc->FirstChildElement()->FirstChildElement()->BoolAttribute( "attrib" ) ); - int value = 10; - int result = doc->FirstChildElement()->LastChildElement()->QueryIntAttribute( "attrib", &value ); + int value1 = 10; + int value2 = doc->FirstChildElement()->LastChildElement()->IntAttribute( "attrib", 10 ); + int result = doc->FirstChildElement()->LastChildElement()->QueryIntAttribute( "attrib", &value1 ); XMLTest( "Programmatic DOM", result, (int)XML_NO_ATTRIBUTE ); - XMLTest( "Programmatic DOM", value, 10 ); + XMLTest( "Programmatic DOM", value1, 10 ); + XMLTest( "Programmatic DOM", value2, 10 ); doc->Print(); @@ -517,16 +519,24 @@ int main( int argc, const char ** argv ) result = ele->QueryDoubleAttribute( "attr0", &dVal ); XMLTest( "Query attribute: int as double", result, (int)XML_SUCCESS); XMLTest( "Query attribute: int as double", (int)dVal, 1 ); + XMLTest( "Query attribute: int as double", (int)ele->DoubleAttribute("attr0"), 1); + result = ele->QueryDoubleAttribute( "attr1", &dVal ); XMLTest( "Query attribute: double as double", result, (int)XML_SUCCESS); - XMLTest( "Query attribute: double as double", (int)dVal, 2 ); + XMLTest( "Query attribute: double as double", dVal, 2.0 ); + XMLTest( "Query attribute: double as double", ele->DoubleAttribute("attr1"), 2.0 ); + result = ele->QueryIntAttribute( "attr1", &iVal ); XMLTest( "Query attribute: double as int", result, (int)XML_SUCCESS); XMLTest( "Query attribute: double as int", iVal, 2 ); + result = ele->QueryIntAttribute( "attr2", &iVal ); XMLTest( "Query attribute: not a number", result, (int)XML_WRONG_ATTRIBUTE_TYPE ); + XMLTest( "Query attribute: not a number", ele->DoubleAttribute("attr2", 4.0), 4.0 ); + result = ele->QueryIntAttribute( "bar", &iVal ); XMLTest( "Query attribute: does not exist", result, (int)XML_NO_ATTRIBUTE ); + XMLTest( "Query attribute: does not exist", ele->BoolAttribute("bar", true), true ); } { @@ -557,6 +567,8 @@ int main( int argc, const char ** argv ) XMLTest( "Attribute round trip. double.", -1, (int)dVal ); XMLTest( "Alternate query", true, iVal == iVal2 ); XMLTest( "Alternate query", true, dVal == dVal2 ); + XMLTest( "Alternate query", true, iVal == ele->IntAttribute("int") ); + XMLTest( "Alternate query", true, dVal == ele->DoubleAttribute("double") ); } { @@ -702,6 +714,7 @@ int main( int argc, const char ** argv ) XMLTest("Attribute: int", -100, v, true); element->QueryAttribute("attrib", &v); XMLTest("Attribute: int", -100, v, true); + XMLTest("Attribute: int", -100, element->IntAttribute("attrib"), true); } { element->SetAttribute("attrib", unsigned(100)); @@ -710,6 +723,7 @@ int main( int argc, const char ** argv ) XMLTest("Attribute: unsigned", unsigned(100), v, true); element->QueryAttribute("attrib", &v); XMLTest("Attribute: unsigned", unsigned(100), v, true); + XMLTest("Attribute: unsigned", unsigned(100), element->UnsignedAttribute("attrib"), true); } { element->SetAttribute("attrib", BIG); @@ -718,6 +732,7 @@ int main( int argc, const char ** argv ) XMLTest("Attribute: int64_t", BIG, v, true); element->QueryAttribute("attrib", &v); XMLTest("Attribute: int64_t", BIG, v, true); + XMLTest("Attribute: int64_t", BIG, element->Int64Attribute("attrib"), true); } { element->SetAttribute("attrib", true); @@ -726,6 +741,7 @@ int main( int argc, const char ** argv ) XMLTest("Attribute: bool", true, v, true); element->QueryAttribute("attrib", &v); XMLTest("Attribute: bool", true, v, true); + XMLTest("Attribute: bool", true, element->BoolAttribute("attrib"), true); } { element->SetAttribute("attrib", 100.0); @@ -734,6 +750,7 @@ int main( int argc, const char ** argv ) XMLTest("Attribute: double", 100.0, v, true); element->QueryAttribute("attrib", &v); XMLTest("Attribute: double", 100.0, v, true); + XMLTest("Attribute: double", 100.0, element->DoubleAttribute("attrib"), true); } { element->SetAttribute("attrib", 100.0f); @@ -742,6 +759,7 @@ int main( int argc, const char ** argv ) XMLTest("Attribute: float", 100.0f, v, true); element->QueryAttribute("attrib", &v); XMLTest("Attribute: float", 100.0f, v, true); + XMLTest("Attribute: float", 100.0f, element->FloatAttribute("attrib"), true); } { element->SetText(BIG);