Jelajahi Sumber

ability to set bool write values

Lee Thomason 9 tahun lalu
induk
melakukan
ce667c9233
3 mengubah file dengan 46 tambahan dan 2 penghapusan
  1. 20 1
      tinyxml2.cpp
  2. 14 1
      tinyxml2.h
  3. 12 0
      xmltest.cpp

+ 20 - 1
tinyxml2.cpp

@@ -368,6 +368,25 @@ const char* StrPair::GetStr()
 
 
 // --------- XMLUtil ----------- //
 // --------- XMLUtil ----------- //
 
 
+char* XMLUtil::writeBoolTrue  = "true";
+char* XMLUtil::writeBoolFalse = "false";
+
+void XMLUtil::SetBool(const char* writeTrue, const char* writeFalse)
+{
+	static const char* defTrue = "true";
+	static const char* defFalse = "false";
+	if (writeTrue)
+		writeBoolTrue = (char*) writeTrue;
+	else
+		writeBoolTrue = (char*) defTrue;
+
+	if (writeFalse)
+		writeBoolFalse = (char*) writeFalse;
+	else
+		writeBoolFalse = (char*) defFalse;
+}
+
+
 const char* XMLUtil::ReadBOM( const char* p, bool* bom )
 const char* XMLUtil::ReadBOM( const char* p, bool* bom )
 {
 {
     TIXMLASSERT( p );
     TIXMLASSERT( p );
@@ -545,7 +564,7 @@ void XMLUtil::ToStr( unsigned v, char* buffer, int bufferSize )
 
 
 void XMLUtil::ToStr( bool v, char* buffer, int bufferSize )
 void XMLUtil::ToStr( bool v, char* buffer, int bufferSize )
 {
 {
-    TIXML_SNPRINTF( buffer, bufferSize, "%s", v ? "true" : "false" );
+    TIXML_SNPRINTF( buffer, bufferSize, "%s", v ? writeBoolTrue : writeBoolFalse);
 }
 }
 
 
 /*
 /*

+ 14 - 1
tinyxml2.h

@@ -527,7 +527,7 @@ enum XMLError {
 /*
 /*
 	Utility functionality.
 	Utility functionality.
 */
 */
-class XMLUtil
+class TINYXML2_LIB XMLUtil
 {
 {
 public:
 public:
     static const char* SkipWhiteSpace( const char* p, int* curLineNumPtr )	{
     static const char* SkipWhiteSpace( const char* p, int* curLineNumPtr )	{
@@ -605,6 +605,19 @@ public:
     static bool	ToFloat( const char* str, float* value );
     static bool	ToFloat( const char* str, float* value );
     static bool ToDouble( const char* str, double* value );
     static bool ToDouble( const char* str, double* value );
 	static bool ToInt64(const char* str, int64_t* value);
 	static bool ToInt64(const char* str, int64_t* value);
+
+	// Default to "true" and "false". If you
+	// need different values, can assign them. (For instance
+	// if you need "true" to be "1".)
+	// Be careful: static, global, & not thread safe.
+	// Be sure to set static const memory as parameters.
+	// Shouldn't be set unless you have a special reason 
+	// such as back-compatibility or testing.
+	static void SetBool(const char* writeTrue, const char* writeFalse);
+
+private:
+	static char* writeBoolTrue;
+	static char* writeBoolFalse;
 };
 };
 
 
 
 

+ 12 - 0
xmltest.cpp

@@ -752,6 +752,18 @@ int main( int argc, const char ** argv )
 			XMLTest("Attribute: bool", true, v, true);
 			XMLTest("Attribute: bool", true, v, true);
 			XMLTest("Attribute: bool", true, element->BoolAttribute("attrib"), true);
 			XMLTest("Attribute: bool", true, element->BoolAttribute("attrib"), true);
 		}
 		}
+		{
+			element->SetAttribute("attrib", true);
+			const char* result = element->Attribute("attrib");
+			XMLTest("Bool true is 'true'", "true", result);
+
+			XMLUtil::SetBool("1", "0");
+			element->SetAttribute("attrib", true);
+			result = element->Attribute("attrib");
+			XMLTest("Bool true is '1'", "1", result);
+
+			XMLUtil::SetBool(0, 0);
+		}
 		{
 		{
 			element->SetAttribute("attrib", 100.0);
 			element->SetAttribute("attrib", 100.0);
 			double v = 0;
 			double v = 0;