summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorYohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer>2010-09-27 22:40:40 +0200
committerYohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer>2010-09-27 22:40:40 +0200
commit6f25f5d9390ae247970ad886dc51d55435285831 (patch)
treee5c748d1e5598cdd658741a4b35890cec636d37a /src
parent661d16e98c62dfff40f481177bf3f1a0c58c2124 (diff)
downloadmana-6f25f5d9390ae247970ad886dc51d55435285831.tar.gz
mana-6f25f5d9390ae247970ad886dc51d55435285831.tar.bz2
mana-6f25f5d9390ae247970ad886dc51d55435285831.tar.xz
mana-6f25f5d9390ae247970ad886dc51d55435285831.zip
Centralized String to bool conversion into the client.
The former XML::getBoolProperty() had a potential memleak and was unsafe when dealing with unknown values. Reviewed-by: CodyMartin. Resolves: Mana-Mantis #213.
Diffstat (limited to 'src')
-rw-r--r--src/configuration.cpp2
-rw-r--r--src/utils/stringutils.cpp17
-rw-r--r--src/utils/stringutils.h5
-rw-r--r--src/utils/xml.cpp12
4 files changed, 21 insertions, 15 deletions
diff --git a/src/configuration.cpp b/src/configuration.cpp
index 6ccef401..50d76bfb 100644
--- a/src/configuration.cpp
+++ b/src/configuration.cpp
@@ -221,7 +221,7 @@ bool Configuration::getBoolValue(const std::string &key) const
}
else
{
- defaultValue = getBoolFromString(iter->second);
+ return getBoolFromString(iter->second, defaultValue);
}
return defaultValue;
diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp
index 4726f8a7..ca03791f 100644
--- a/src/utils/stringutils.cpp
+++ b/src/utils/stringutils.cpp
@@ -155,7 +155,8 @@ bool isWordSeparator(char chr)
return (chr == ' ' || chr == ',' || chr == '.' || chr == '"');
}
-const std::string findSameSubstring(const std::string &str1, const std::string &str2)
+const std::string findSameSubstring(const std::string &str1,
+ const std::string &str2)
{
int minLength = str1.length() > str2.length() ? str2.length() : str1.length();
for (int f = 0; f < minLength; f ++)
@@ -176,16 +177,16 @@ const char* getSafeUtf8String(std::string text)
return buf;
}
-bool getBoolFromString(const std::string &text)
+bool getBoolFromString(const std::string &text, bool def)
{
- std::string txt = text;
- toLower(trim(txt));
- if (txt == "true" || txt == "yes" || txt == "on" || txt == "1")
- return true;
- else if (txt == "false" || txt == "no" || txt == "off" || txt == "0")
+ std::string a = text;
+ toLower(trim(a));
+ if (a == "true" || a == "1" || a == "on" || a == "yes" || a == "y")
+ return true;
+ if (a == "false" || a == "0" || a == "off" || a == "no" || a == "n")
return false;
else
- return (bool) atoi(txt.c_str());
+ return def;
}
std::string autocomplete(std::vector<std::string> &candidates,
diff --git a/src/utils/stringutils.h b/src/utils/stringutils.h
index 012ae98f..f032733d 100644
--- a/src/utils/stringutils.h
+++ b/src/utils/stringutils.h
@@ -122,7 +122,8 @@ int compareStrI(const std::string &a, const std::string &b);
bool isWordSeparator(char chr);
-const std::string findSameSubstring(const std::string &str1, const std::string &str2);
+const std::string findSameSubstring(const std::string &str1,
+ const std::string &str2);
const char* getSafeUtf8String(std::string text);
@@ -132,7 +133,7 @@ const char* getSafeUtf8String(std::string text);
* @param text the string used to get the bool value
* @return a boolean value..
*/
-bool getBoolFromString(const std::string &text);
+bool getBoolFromString(const std::string &text, bool def = false);
std::string autocomplete(std::vector<std::string> &candidates,
std::string base);
diff --git a/src/utils/xml.cpp b/src/utils/xml.cpp
index 2bcb6f24..65eb1370 100644
--- a/src/utils/xml.cpp
+++ b/src/utils/xml.cpp
@@ -32,6 +32,7 @@
#include "resources/resourcemanager.h"
+#include "utils/stringutils.h"
#include "utils/zlib.h"
namespace XML
@@ -135,11 +136,14 @@ namespace XML
bool getBoolProperty(xmlNodePtr node, const char* name, bool def)
{
+ bool ret = def;
xmlChar *prop = xmlGetProp(node, BAD_CAST name);
-
- if (xmlStrEqual(prop, BAD_CAST "true" ) ) return true;
- if (xmlStrEqual(prop, BAD_CAST "false") ) return false;
- return def;
+ if (prop)
+ {
+ ret = getBoolFromString((char*) prop, def);
+ xmlFree(prop);
+ }
+ return ret;
}
xmlNodePtr findFirstChildByName(xmlNodePtr parent, const char *name)