فهرست منبع

To bring BoolFirstChild() more in line with the other methods, reimplemented it in terms of a new QueryBoolFirstChild().

Uli Kusterer 12 سال پیش
والد
کامیت
ff8e2041dd
2فایلهای تغییر یافته به همراه30 افزوده شده و 9 حذف شده
  1. 18 6
      tinyxml2.cpp
  2. 12 3
      tinyxml2.h

+ 18 - 6
tinyxml2.cpp

@@ -1336,13 +1336,25 @@ void	XMLElement::SetBoolFirstChild( bool inBool )
 }
 
 
-bool	XMLElement::BoolFirstChild()
-{
-	if ( FirstChild() && FirstChild()->ToElement() ) {
-		return strcmp( FirstChild()->Value(), "true" ) == 0;
+XMLError	XMLElement::QueryBoolFirstChild( bool *outBool )
+{
+	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;
 }
 
 

+ 12 - 3
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