Mercurial > minori
comparison include/core/ini.h @ 280:9b6e12c14a1e
chore: merge
| author | Paper <paper@paper.us.eu.org> |
|---|---|
| date | Mon, 06 May 2024 17:23:30 -0400 |
| parents | ec0a2b5493f8 |
| children | b1f625b0227c |
comparison
equal
deleted
inserted
replaced
| 279:657fda1b9cac | 280:9b6e12c14a1e |
|---|---|
| 1 #ifndef MINORI_CORE_INI_H_ | 1 #ifndef MINORI_CORE_INI_H_ |
| 2 #define MINORI_CORE_INI_H_ | 2 #define MINORI_CORE_INI_H_ |
| 3 | 3 |
| 4 #define MINI_CASE_SENSITIVE | 4 #define MINI_CASE_SENSITIVE |
| 5 #include "core/strings.h" | 5 #include "core/strings.h" |
| 6 #include "gui/translate/anime.h" | |
| 7 #include "gui/translate/config.h" | |
| 8 #include "mini/ini.h" | 6 #include "mini/ini.h" |
| 9 #include <string> | 7 #include <string> |
| 10 #include <type_traits> | |
| 11 | 8 |
| 12 namespace INI { | 9 namespace INI { |
| 13 | 10 |
| 14 /* very simple tutorial on how to give anyone who reads | 11 std::string GetIniString(const mINI::INIStructure& ini, const std::string& section, const std::string& key, const std::string& def); |
| 15 your code an aneurysm */ | 12 bool GetIniBool(const mINI::INIStructure& ini, const std::string& section, const std::string& key, bool def); |
| 16 template<typename T, typename = void> | |
| 17 struct is_toutf8string_available : std::false_type {}; | |
| 18 | 13 |
| 19 template<typename T> | 14 template<typename T> |
| 20 struct is_toutf8string_available<T, std::void_t<decltype(Strings::ToUtf8String(std::declval<T>()))>> : std::true_type { | 15 T GetIniInteger(const mINI::INIStructure& ini, const std::string& section, const std::string& key, T def) { |
| 21 }; | 16 return Strings::ToInt<T>(GetIniString(ini, section, key, ""), def); |
| 22 | |
| 23 template<typename T, typename = void> | |
| 24 struct is_translation_available : std::false_type {}; | |
| 25 | |
| 26 template<typename T> | |
| 27 struct is_translation_available<T, std::void_t<decltype(Translate::ToString(std::declval<T>()))>> : std::true_type {}; | |
| 28 | |
| 29 template<typename T> | |
| 30 T GetIniValue(const mINI::INIStructure& ini, const std::string& section, const std::string& value, const T& def) { | |
| 31 if (!ini.has(section) || !ini.get(section).has(value)) | |
| 32 return def; | |
| 33 | |
| 34 const std::string val = ini.get(section).get(value); | |
| 35 | |
| 36 if constexpr (std::is_integral<T>::value) { | |
| 37 /* Integer? */ | |
| 38 if constexpr (std::is_same<T, bool>::value) { | |
| 39 /* Boolean? */ | |
| 40 return Strings::ToBool(val, def); | |
| 41 } else { | |
| 42 /* Always fall back to long long */ | |
| 43 return Strings::ToInt<T>(val, def); | |
| 44 } | |
| 45 } else { | |
| 46 return val; | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 /* this should be able to handle most of our custom types */ | |
| 51 template<typename T> | |
| 52 void SetIniValue(mINI::INIStructure& ini, const std::string& section, const std::string& key, const T& value) { | |
| 53 auto& ini_key = ini[section][key]; | |
| 54 | |
| 55 if constexpr (is_translation_available<T>::value) { | |
| 56 /* prioritize translation */ | |
| 57 ini_key = Translate::ToString(value); | |
| 58 } else if constexpr (std::is_same<T, std::string>::value) { | |
| 59 /* lmfao */ | |
| 60 ini_key = value; | |
| 61 } else if constexpr (is_toutf8string_available<T>::value) { | |
| 62 ini_key = Strings::ToUtf8String(value); | |
| 63 } | |
| 64 } | 17 } |
| 65 | 18 |
| 66 } // namespace INI | 19 } // namespace INI |
| 67 | 20 |
| 68 #endif | 21 #endif |
