| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_PARSE_PARSE_HPP |
| 4 |
#define MPT_PARSE_PARSE_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/detect.hpp" |
| 9 |
#include "mpt/base/namespace.hpp" |
| 10 |
#include "mpt/string/types.hpp" |
| 11 |
#include "mpt/string_transcode/transcode.hpp" |
| 12 |
|
| 13 |
#include <ios> |
| 14 |
#include <locale> |
| 15 |
#include <sstream> |
| 16 |
#include <string> |
| 17 |
#include <type_traits> |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace mpt { |
| 22 |
inline namespace MPT_INLINE_NS { |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
inline std::string parse_as_internal_string_type(const std::string & s) { |
| 27 |
return s; |
| 28 |
} |
| 29 |
|
| 30 |
#if !defined(MPT_COMPILER_QUIRK_NO_WCHAR) |
| 31 |
inline std::wstring parse_as_internal_string_type(const std::wstring & s) { |
| 32 |
return s; |
| 33 |
} |
| 34 |
#endif // !MPT_COMPILER_QUIRK_NO_WCHAR |
| 35 |
|
| 36 |
#if MPT_USTRING_MODE_WIDE |
| 37 |
template <typename Tstring> |
| 38 |
inline std::wstring parse_as_internal_string_type(const Tstring & s) { |
| 39 |
return mpt::transcode<std::wstring>(s); |
| 40 |
} |
| 41 |
#else // !MPT_USTRING_MODE_WIDE |
| 42 |
template <typename Tstring> |
| 43 |
inline std::string parse_as_internal_string_type(const Tstring & s) { |
| 44 |
return mpt::transcode<std::string>(mpt::common_encoding::utf8, s); |
| 45 |
} |
| 46 |
#endif // MPT_USTRING_MODE_WIDE |
| 47 |
|
| 48 |
|
| 49 |
template <typename T, typename Tstring> |
| 50 |
inline bool parse_into(T & dst, const Tstring & str) { |
| 51 |
std::basic_istringstream<typename decltype(mpt::parse_as_internal_string_type(mpt::as_string(str)))::value_type> stream(mpt::parse_as_internal_string_type(mpt::as_string(str))); |
| 52 |
stream.imbue(std::locale::classic()); |
| 53 |
if constexpr (std::is_same<T, bool>::value) { |
| 54 |
signed int tmp; |
| 55 |
if (!(stream >> tmp)) { |
| 56 |
return false; |
| 57 |
} |
| 58 |
dst = tmp ? true : false; |
| 59 |
} else if constexpr (std::is_same<T, signed char>::value) { |
| 60 |
signed int tmp; |
| 61 |
if (!(stream >> tmp)) { |
| 62 |
return false; |
| 63 |
} |
| 64 |
dst = static_cast<T>(tmp); |
| 65 |
} else if constexpr (std::is_same<T, unsigned char>::value) { |
| 66 |
unsigned int tmp; |
| 67 |
if (!(stream >> tmp)) { |
| 68 |
return false; |
| 69 |
} |
| 70 |
dst = static_cast<T>(tmp); |
| 71 |
} else { |
| 72 |
if (!(stream >> dst)) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
} |
| 76 |
return true; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
template <typename T, typename Tstring> |
| 81 |
inline T parse_or(const Tstring & str, T def) { |
| 82 |
std::basic_istringstream<typename decltype(mpt::parse_as_internal_string_type(mpt::as_string(str)))::value_type> stream(mpt::parse_as_internal_string_type(mpt::as_string(str))); |
| 83 |
stream.imbue(std::locale::classic()); |
| 84 |
T value = def; |
| 85 |
if constexpr (std::is_same<T, bool>::value) { |
| 86 |
int tmp; |
| 87 |
if (!(stream >> tmp)) { |
| 88 |
return def; |
| 89 |
} |
| 90 |
value = tmp ? true : false; |
| 91 |
} else if constexpr (std::is_same<T, signed char>::value) { |
| 92 |
signed int tmp; |
| 93 |
if (!(stream >> tmp)) { |
| 94 |
return def; |
| 95 |
} |
| 96 |
value = static_cast<T>(tmp); |
| 97 |
} else if constexpr (std::is_same<T, unsigned char>::value) { |
| 98 |
unsigned int tmp; |
| 99 |
if (!(stream >> tmp)) { |
| 100 |
return def; |
| 101 |
} |
| 102 |
value = static_cast<T>(tmp); |
| 103 |
} else { |
| 104 |
if (!(stream >> value)) { |
| 105 |
return def; |
| 106 |
} |
| 107 |
} |
| 108 |
return value; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
template <typename T, typename Tstring> |
| 113 |
inline T parse(const Tstring & str) { |
| 114 |
return mpt::parse_or<T>(str, T{}); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
template <typename T, typename Tstring> |
| 119 |
inline bool locale_parse_into(const std::locale & loc, T & dst, const Tstring & str) { |
| 120 |
std::basic_istringstream<typename decltype(mpt::parse_as_internal_string_type(mpt::as_string(str)))::value_type> stream(mpt::parse_as_internal_string_type(mpt::as_string(str))); |
| 121 |
stream.imbue(loc); |
| 122 |
if constexpr (std::is_same<T, bool>::value) { |
| 123 |
int tmp; |
| 124 |
if (!(stream >> tmp)) { |
| 125 |
return false; |
| 126 |
} |
| 127 |
dst = tmp ? true : false; |
| 128 |
} else if constexpr (std::is_same<T, signed char>::value) { |
| 129 |
signed int tmp; |
| 130 |
if (!(stream >> tmp)) { |
| 131 |
return false; |
| 132 |
} |
| 133 |
dst = static_cast<T>(tmp); |
| 134 |
} else if constexpr (std::is_same<T, unsigned char>::value) { |
| 135 |
unsigned int tmp; |
| 136 |
if (!(stream >> tmp)) { |
| 137 |
return false; |
| 138 |
} |
| 139 |
dst = static_cast<T>(tmp); |
| 140 |
} else { |
| 141 |
if (!(stream >> dst)) { |
| 142 |
return false; |
| 143 |
} |
| 144 |
} |
| 145 |
return true; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
template <typename T, typename Tstring> |
| 150 |
inline T locale_parse_or(const std::locale & loc, const Tstring & str, T def) { |
| 151 |
std::basic_istringstream<typename decltype(mpt::parse_as_internal_string_type(mpt::as_string(str)))::value_type> stream(mpt::parse_as_internal_string_type(mpt::as_string(str))); |
| 152 |
stream.imbue(loc); |
| 153 |
T value = def; |
| 154 |
if constexpr (std::is_same<T, signed char>::value) { |
| 155 |
signed int tmp; |
| 156 |
if (!(stream >> tmp)) { |
| 157 |
return def; |
| 158 |
} |
| 159 |
value = tmp ? true : false; |
| 160 |
} else if constexpr (std::is_same<T, signed char>::value) { |
| 161 |
signed int tmp; |
| 162 |
if (!(stream >> tmp)) { |
| 163 |
return def; |
| 164 |
} |
| 165 |
value = static_cast<T>(tmp); |
| 166 |
} else if constexpr (std::is_same<T, unsigned char>::value) { |
| 167 |
unsigned int tmp; |
| 168 |
if (!(stream >> tmp)) { |
| 169 |
return def; |
| 170 |
} |
| 171 |
value = static_cast<T>(tmp); |
| 172 |
} else { |
| 173 |
if (!(stream >> value)) { |
| 174 |
return def; |
| 175 |
} |
| 176 |
} |
| 177 |
return value; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
template <typename T, typename Tstring> |
| 182 |
inline T locale_parse(const std::locale & loc, const Tstring & str) { |
| 183 |
return mpt::parse_or<T>(loc, str, T{}); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
template <typename T, typename Tstring> |
| 188 |
inline T parse_hex(const Tstring & str) { |
| 189 |
std::basic_istringstream<typename decltype(mpt::parse_as_internal_string_type(mpt::as_string(str)))::value_type> stream(mpt::parse_as_internal_string_type(mpt::as_string(str))); |
| 190 |
stream.imbue(std::locale::classic()); |
| 191 |
T value; |
| 192 |
if constexpr (std::is_same<T, signed char>::value) { |
| 193 |
signed int tmp; |
| 194 |
if (!(stream >> std::hex >> tmp)) { |
| 195 |
return T{}; |
| 196 |
} |
| 197 |
value = static_cast<T>(tmp); |
| 198 |
} else if constexpr (std::is_same<T, unsigned char>::value) { |
| 199 |
unsigned int tmp; |
| 200 |
if (!(stream >> std::hex >> tmp)) { |
| 201 |
return T{}; |
| 202 |
} |
| 203 |
value = static_cast<T>(tmp); |
| 204 |
} else { |
| 205 |
if (!(stream >> std::hex >> value)) { |
| 206 |
return T{}; |
| 207 |
} |
| 208 |
} |
| 209 |
return value; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
} // namespace MPT_INLINE_NS |
| 215 |
} // namespace mpt |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
#endif // MPT_PARSE_PARSE_HPP |