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/string.cpp | |
parent | de803e103f5317856d4eadf15661ef7516cfc72a (diff) | |
download | manaserv-d6d215e2ab53322c769792b4aa53396ecce96422.tar.gz manaserv-d6d215e2ab53322c769792b4aa53396ecce96422.tar.bz2 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/string.cpp')
-rw-r--r-- | src/utils/string.cpp | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/src/utils/string.cpp b/src/utils/string.cpp index 3a2bb25e..785a0921 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 |