diff options
author | Yohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer> | 2010-09-08 22:43:00 +0200 |
---|---|---|
committer | Yohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer> | 2010-09-08 22:43:00 +0200 |
commit | d6d215e2ab53322c769792b4aa53396ecce96422 (patch) | |
tree | ca1f73f1156f7a3eeccf2430a00d7bcdbd39d149 /src/utils | |
parent | de803e103f5317856d4eadf15661ef7516cfc72a (diff) | |
download | manaserv-d6d215e2ab53322c769792b4aa53396ecce96422.tar.gz manaserv-d6d215e2ab53322c769792b4aa53396ecce96422.tar.xz manaserv-d6d215e2ab53322c769792b4aa53396ecce96422.zip |
Centralized stringToBool conversion.
Also moved the trim() function into the utils namespace
where it belongs more, and made some random code cleanups.
Reviewed-by: Thorbjorn.
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/string.cpp | 36 | ||||
-rw-r--r-- | src/utils/string.hpp | 37 | ||||
-rw-r--r-- | src/utils/trim.hpp | 50 | ||||
-rw-r--r-- | src/utils/xml.cpp | 8 |
4 files changed, 70 insertions, 61 deletions
diff --git a/src/utils/string.cpp b/src/utils/string.cpp index 3a2bb25..785a092 100644 --- a/src/utils/string.cpp +++ b/src/utils/string.cpp @@ -26,12 +26,18 @@ namespace utils { -std::string toupper(std::string s) +std::string toUpper(std::string s) { std::transform(s.begin(), s.end(), s.begin(), (int(*)(int)) std::toupper); return s; } +std::string toLower(std::string s) +{ + std::transform(s.begin(), s.end(), s.begin(), (int(*)(int)) std::tolower); + return s; +} + bool isNumeric(const std::string &s) { for (unsigned int i = 0; i < s.size(); ++i) @@ -73,4 +79,32 @@ int compareStrI(const std::string &a, const std::string &b) return 0; } +bool stringToBool(const std::string &s, bool defaultValue) +{ + std::string a = toLower(s); + 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; + + return defaultValue; +} + +void trim(std::string &str) +{ + std::string::size_type pos = str.find_last_not_of(" \n\t"); + if (pos != std::string::npos) + { + str.erase(pos + 1); + pos = str.find_first_not_of(" \n\t"); + if (pos != std::string::npos) + str.erase(0, pos); + } + else + { + // There is nothing else but whitespace in the string + str.clear(); + } +} + } // namespace utils diff --git a/src/utils/string.hpp b/src/utils/string.hpp index e09e9ab..9f2b4ac 100644 --- a/src/utils/string.hpp +++ b/src/utils/string.hpp @@ -18,15 +18,32 @@ * along with The Mana Server. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef UTILS_STRING_HPP -#define UTILS_STRING_HPP +#ifndef UTILS_STRING_H +#define UTILS_STRING_H #include <string> namespace utils { - std::string toupper(std::string); + /** + * Returns an upper-cased copy of the string. + */ + std::string toUpper(std::string); + + /** + * Returns an lower-cased copy of the string. + */ + std::string toLower(std::string); + + /** + * Tells whether the string is a numerical representation. + */ bool isNumeric(const std::string &); + + /** + * Turns a string representing a numerical representation + * into an integer value. + */ int stringToInt(const std::string &); /** @@ -38,6 +55,18 @@ namespace utils * negative if the second is greater */ int compareStrI(const std::string &a, const std::string &b); + + /** + * Returns the boolean value represented in a string, or default. + */ + bool stringToBool(const std::string &s, bool defaultValue); + + /** + * Trims spaces off the end and the beginning of the given string. + * + * @param str the string to trim spaces off + */ + void trim(std::string &str); } -#endif // UTILS_STRING_HPP +#endif // UTILS_STRING_H diff --git a/src/utils/trim.hpp b/src/utils/trim.hpp deleted file mode 100644 index ff93f93..0000000 --- a/src/utils/trim.hpp +++ /dev/null @@ -1,50 +0,0 @@ -/* - * The Mana Server - * Copyright (C) 2007-2010 The Mana World Development Team - * - * This file is part of The Mana Server. - * - * The Mana Server is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * The Mana Server is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with The Mana Server. If not, see <http://www.gnu.org/licenses/>. - */ - -#ifndef UTILS_TRIM_HPP -#define UTILS_TRIM_HPP - -#include <string> - -/** - * Trims spaces off the end and the beginning of the given string. - * - * @param str the string to trim spaces off - */ -inline void trim(std::string &str) -{ - std::string::size_type pos = str.find_last_not_of(" \n\t"); - if (pos != std::string::npos) - { - str.erase(pos + 1); - pos = str.find_first_not_of(" \n\t"); - if (pos != std::string::npos) - { - str.erase(0, pos); - } - } - else - { - // There is nothing else but whitespace in the string - str.clear(); - } -} - -#endif diff --git a/src/utils/xml.cpp b/src/utils/xml.cpp index 8bc9ebe..2920227 100644 --- a/src/utils/xml.cpp +++ b/src/utils/xml.cpp @@ -23,6 +23,7 @@ #include "common/resourcemanager.hpp" #include "utils/logger.h" +#include "utils/string.hpp" #include <iostream> #include <fstream> @@ -114,12 +115,7 @@ namespace XML xmlChar *prop = xmlGetProp(node, BAD_CAST name); if (prop) { - if (xmlStrEqual(prop, BAD_CAST "true") - ||xmlStrEqual(prop, BAD_CAST "yes")) - ret = true; - if (xmlStrEqual(prop, BAD_CAST "false") - ||xmlStrEqual(prop, BAD_CAST "no")) - ret = false; + ret = utils::stringToBool((char*) prop, def); xmlFree(prop); } return ret; |