From ce667c923324d277e10da40455ade87f199a7292 Mon Sep 17 00:00:00 2001 From: Lee Thomason Date: Mon, 26 Dec 2016 16:45:30 -0800 Subject: [PATCH 1/3] ability to set bool write values --- tinyxml2.cpp | 21 ++++++++++++++++++++- tinyxml2.h | 15 ++++++++++++++- xmltest.cpp | 12 ++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 018f9a9..7f5641e 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -368,6 +368,25 @@ const char* StrPair::GetStr() // --------- 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 ) { TIXMLASSERT( p ); @@ -545,7 +564,7 @@ void XMLUtil::ToStr( unsigned 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); } /* diff --git a/tinyxml2.h b/tinyxml2.h index de589bd..53f411a 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -527,7 +527,7 @@ enum XMLError { /* Utility functionality. */ -class XMLUtil +class TINYXML2_LIB XMLUtil { public: static const char* SkipWhiteSpace( const char* p, int* curLineNumPtr ) { @@ -605,6 +605,19 @@ public: static bool ToFloat( const char* str, float* value ); static bool ToDouble( const char* str, double* 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; }; diff --git a/xmltest.cpp b/xmltest.cpp index b6bb76f..27c9665 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -752,6 +752,18 @@ int main( int argc, const char ** argv ) XMLTest("Attribute: bool", true, v, 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); double v = 0; From f458d265c1697b9c473d97ebf68f00b1fd4d98cc Mon Sep 17 00:00:00 2001 From: Lee Thomason Date: Mon, 26 Dec 2016 22:47:25 -0800 Subject: [PATCH 2/3] fix const. hopefully. --- tinyxml2.cpp | 12 ++++++------ tinyxml2.h | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 7f5641e..40f6d82 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -368,22 +368,22 @@ const char* StrPair::GetStr() // --------- XMLUtil ----------- // -char* XMLUtil::writeBoolTrue = "true"; -char* XMLUtil::writeBoolFalse = "false"; +const char* XMLUtil::writeBoolTrue = "true"; +const 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; + writeBoolTrue = writeTrue; else - writeBoolTrue = (char*) defTrue; + writeBoolTrue = defTrue; if (writeFalse) - writeBoolFalse = (char*) writeFalse; + writeBoolFalse = writeFalse; else - writeBoolFalse = (char*) defFalse; + writeBoolFalse = defFalse; } diff --git a/tinyxml2.h b/tinyxml2.h index 53f411a..8030e7d 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -616,8 +616,8 @@ public: static void SetBool(const char* writeTrue, const char* writeFalse); private: - static char* writeBoolTrue; - static char* writeBoolFalse; + static const char* writeBoolTrue; + static const char* writeBoolFalse; }; From c5c99c2ba0ce403154467049d7914364b797f230 Mon Sep 17 00:00:00 2001 From: Lee Thomason Date: Thu, 29 Dec 2016 11:19:17 -0800 Subject: [PATCH 3/3] tweaks to bool serialization --- tinyxml2.cpp | 14 ++++---------- tinyxml2.h | 10 ++++------ xmltest.cpp | 4 ++-- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 40f6d82..1f7a918 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -371,19 +371,13 @@ const char* StrPair::GetStr() const char* XMLUtil::writeBoolTrue = "true"; const char* XMLUtil::writeBoolFalse = "false"; -void XMLUtil::SetBool(const char* writeTrue, const char* writeFalse) +void XMLUtil::SetBoolSerialization(const char* writeTrue, const char* writeFalse) { - static const char* defTrue = "true"; + static const char* defTrue = "true"; static const char* defFalse = "false"; - if (writeTrue) - writeBoolTrue = writeTrue; - else - writeBoolTrue = defTrue; - if (writeFalse) - writeBoolFalse = writeFalse; - else - writeBoolFalse = defFalse; + writeBoolTrue = (writeTrue) ? writeTrue : defTrue; + writeBoolFalse = (writeFalse) ? writeFalse : defFalse; } diff --git a/tinyxml2.h b/tinyxml2.h index 8030e7d..00f6883 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -606,14 +606,12 @@ public: static bool ToDouble( const char* str, double* 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".) + // Changes what is serialized for a boolean value. + // Default to "true" and "false". Shouldn't be changed + // unless you have a special testing or compatibility need. // 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); + static void SetBoolSerialization(const char* writeTrue, const char* writeFalse); private: static const char* writeBoolTrue; diff --git a/xmltest.cpp b/xmltest.cpp index 27c9665..7c11389 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -757,12 +757,12 @@ int main( int argc, const char ** argv ) const char* result = element->Attribute("attrib"); XMLTest("Bool true is 'true'", "true", result); - XMLUtil::SetBool("1", "0"); + XMLUtil::SetBoolSerialization("1", "0"); element->SetAttribute("attrib", true); result = element->Attribute("attrib"); XMLTest("Bool true is '1'", "1", result); - XMLUtil::SetBool(0, 0); + XMLUtil::SetBoolSerialization(0, 0); } { element->SetAttribute("attrib", 100.0);