Forráskód Böngészése

Bools should really be implemented as self-closing <true /> and <false /> tags, not as 0/1 inside a tag, so removing those SetText variants again, in favor of SetBoolFirstChild() and BoolFirstChild() methods.

Uli Kusterer 12 éve
szülő
commit
c1c20bb196
2 módosított fájl, 25 hozzáadás és 13 törlés
  1. 20 10
      tinyxml2.cpp
  2. 5 3
      tinyxml2.h

+ 20 - 10
tinyxml2.cpp

@@ -1297,10 +1297,10 @@ void	XMLElement::SetText( unsigned inNum )
 }
 
 
-void	XMLElement::SetText( bool inBool )
+void	XMLElement::SetText( double inNum )
 {
     char buf[BUF_SIZE];
-    XMLUtil::ToStr( inBool, buf, BUF_SIZE );
+    XMLUtil::ToStr( inNum, buf, BUF_SIZE );
 	if ( FirstChild() && FirstChild()->ToText() )
 		FirstChild()->SetValue( buf );
 	else {
@@ -1310,7 +1310,7 @@ void	XMLElement::SetText( bool inBool )
 }
 
 
-void	XMLElement::SetText( double inNum )
+void	XMLElement::SetText( float inNum )
 {
     char buf[BUF_SIZE];
     XMLUtil::ToStr( inNum, buf, BUF_SIZE );
@@ -1323,19 +1323,29 @@ void	XMLElement::SetText( double inNum )
 }
 
 
-void	XMLElement::SetText( float inNum )
+void	XMLElement::SetBoolFirstChild( bool inBool )
 {
-    char buf[BUF_SIZE];
-    XMLUtil::ToStr( inNum, buf, BUF_SIZE );
-	if ( FirstChild() && FirstChild()->ToText() )
-		FirstChild()->SetValue( buf );
-	else {
-		XMLText*	theText = GetDocument()->NewText( buf );
+	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() ) {

+ 5 - 3
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: