diff options
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/Configuration/Config.cpp | 11 | ||||
| -rw-r--r-- | src/common/Utilities/StringConvert.h | 179 | ||||
| -rw-r--r-- | src/common/Utilities/Util.cpp | 5 | ||||
| -rw-r--r-- | src/common/Utilities/Util.h | 12 |
4 files changed, 199 insertions, 8 deletions
diff --git a/src/common/Configuration/Config.cpp b/src/common/Configuration/Config.cpp index fa0e546ccc3..29508c7b4c2 100644 --- a/src/common/Configuration/Config.cpp +++ b/src/common/Configuration/Config.cpp @@ -17,6 +17,7 @@ #include "Config.h" #include "Log.h" +#include "StringConvert.h" #include "Util.h" #include <boost/property_tree/ini_parser.hpp> #include <algorithm> @@ -137,7 +138,15 @@ bool ConfigMgr::GetBoolDefault(std::string const& name, bool def, bool quiet) co { std::string val = GetValueDefault(name, std::string(def ? "1" : "0"), quiet); val.erase(std::remove(val.begin(), val.end(), '"'), val.end()); - return StringToBool(val); + Optional<bool> boolVal = Trinity::StringTo<bool>(val); + if (boolVal) + return *boolVal; + else + { + TC_LOG_ERROR("server.loading", "Bad value defined for name %s in config file %s, going to use '%s' instead", + name.c_str(), _filename.c_str(), def ? "true" : "false"); + return def; + } } int ConfigMgr::GetIntDefault(std::string const& name, int def, bool quiet) const diff --git a/src/common/Utilities/StringConvert.h b/src/common/Utilities/StringConvert.h new file mode 100644 index 00000000000..7af791b91a1 --- /dev/null +++ b/src/common/Utilities/StringConvert.h @@ -0,0 +1,179 @@ +/* + * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information + * + * This program 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 (at your + * option) any later version. + * + * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef TRINITY_STRINGCONVERT_H +#define TRINITY_STRINGCONVERT_H + +#include "Define.h" +#include "Errors.h" +#include "Optional.h" +#include "Util.h" +#include <charconv> +#include <string> +#include <string_view> +#include <type_traits> + +namespace Trinity::Impl::StringConvertImpl +{ + template <typename T, typename = void> struct For + { + static_assert(Trinity::dependant_false_v<T>, "Unsupported type used for ToString or StringTo"); + /* + static Optional<T> FromString(std::string_view str, ...); + static std::string ToString(T&& val, ...); + */ + }; + + // @todo relax this once proper std::from_chars support exists + template <typename T> + struct For<T, std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, bool>>> + { + static Optional<T> FromString(std::string_view str, int base = 10) + { + if (base == 0) + { + if (StringEqualI(str.substr(0, 2), "0x")) + { + base = 16; + str.remove_prefix(2); + } + else if (StringEqualI(str.substr(0, 2), "0b")) + { + base = 2; + str.remove_prefix(2); + } + else + base = 10; + + if (str.empty()) + return std::nullopt; + } + + char const* const start = str.data(); + char const* const end = (start + str.length()); + + T val; + std::from_chars_result const res = std::from_chars(start, end, val, base); + if (res.ptr == end) + return val; + else + return std::nullopt; + } + + static std::string ToString(T val) + { + std::string buf(20,'\0'); /* 2^64 is 20 decimal characters, -(2^63) is 20 including the sign */ + char* const start = buf.data(); + char* const end = (start + buf.length()); + std::to_chars_result const res = std::to_chars(start, end, val); + ASSERT(res.ec == std::errc()); + buf.resize(res.ptr - start); + return buf; + } + }; + +#ifdef TRINITY_NEED_CHARCONV_WORKAROUND + /* + If this is defined, std::from_chars will cause linkage errors for 64-bit types. + (This is a bug in clang-7.) + + If the clang requirement is bumped to >= clang-8, remove this ifdef block and its + associated check in cmake/compiler/clang/settings.cmake + */ + template <> + struct For<uint64, void> + { + static Optional<uint64> FromString(std::string_view str, int base = 10) + { + if (str.empty()) + return std::nullopt; + try + { + size_t n; + uint64 val = std::stoull(std::string(str), &n, base); + if (n != str.length()) + return std::nullopt; + return val; + } + catch (...) { return std::nullopt; } + } + + static std::string ToString(uint64 val) + { + return std::to_string(val); + } + }; + + template <> + struct For<int64, void> + { + static Optional<int64> FromString(std::string_view str, int base = 10) + { + try { + if (str.empty()) + return std::nullopt; + size_t n; + int64 val = std::stoll(std::string(str), &n, base); + if (n != str.length()) + return std::nullopt; + return val; + } + catch (...) { return std::nullopt; } + } + + static std::string ToString(int64 val) + { + return std::to_string(val); + } + }; +#endif + + template <> + struct For<bool, void> + { + static Optional<bool> FromString(std::string_view str) + { + if ((str == "1") || StringEqualI(str, "y") || StringEqualI(str, "on") || StringEqualI(str, "yes") || StringEqualI(str, "true")) + return true; + if ((str == "0") || StringEqualI(str, "n") || StringEqualI(str, "off") || StringEqualI(str, "no") || StringEqualI(str, "false")) + return false; + return std::nullopt; + } + + static std::string ToString(bool val) + { + return (val ? "1" : "0"); + } + }; +} + +namespace Trinity +{ + template <typename Result, typename... Params> + Optional<Result> StringTo(std::string_view str, Params&&... params) + { + return Trinity::Impl::StringConvertImpl::For<Result>::FromString(str, std::forward<Params>(params)...); + } + + template <typename Type, typename... Params> + std::string ToString(Type&& val, Params&&... params) + { + return Trinity::Impl::StringConvertImpl::For<std::decay_t<Type>>::ToString(std::forward<Type>(val), std::forward<Params>(params)...); + } +} + +#endif diff --git a/src/common/Utilities/Util.cpp b/src/common/Utilities/Util.cpp index da086c6b6cc..49cac60b924 100644 --- a/src/common/Utilities/Util.cpp +++ b/src/common/Utilities/Util.cpp @@ -651,11 +651,6 @@ void Trinity::Impl::HexStrToByteArray(std::string_view str, uint8* out, size_t o } } -bool StringToBool(std::string_view str) -{ - return ((str == "1") || StringEqualI(str, "true") || StringEqualI(str, "yes")); -} - bool StringEqualI(std::string_view str1, std::string_view str2) { return std::equal(str1.begin(), str1.end(), str2.begin(), str2.end(), diff --git a/src/common/Utilities/Util.h b/src/common/Utilities/Util.h index cd8765b309f..c22584d970d 100644 --- a/src/common/Utilities/Util.h +++ b/src/common/Utilities/Util.h @@ -20,6 +20,7 @@ #include "Define.h" #include "Errors.h" +#include "Optional.h" #include <array> #include <string> @@ -345,8 +346,6 @@ inline std::vector<uint8> HexStrToByteVector(std::string_view str, bool reverse return buf; } -TC_COMMON_API bool StringToBool(std::string_view str); - TC_COMMON_API bool StringEqualI(std::string_view str1, std::string_view str2); TC_COMMON_API bool StringStartsWith(std::string_view haystack, std::string_view needle); TC_COMMON_API bool StringContainsStringI(std::string_view haystack, std::string_view needle); @@ -560,4 +559,13 @@ Ret* Coalesce(T1* first, T*... rest) return static_cast<Ret*>(first); } +namespace Trinity +{ + template <typename T> + struct dependant_false { static constexpr bool value = false; }; + + template <typename T> + constexpr bool dependant_false_v = dependant_false<T>::value; +} + #endif |
