From ff8e2041ddf52e1bbe7e2695c35a77d223aca175 Mon Sep 17 00:00:00 2001 From: Uli Kusterer Date: Tue, 21 Jan 2014 02:53:47 +0100 Subject: [PATCH] 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