annotate include/core/ini.h @ 268:382b50754fe4

dep/animone: make osx code a bit less hacky it would be nice if macos actually provided a real API for getting window titles (outside of the accessibility api). the accessibility API is a real mess to work with; the user has to give permission to access it under newer versions.
author Paper <paper@paper.us.eu.org>
date Fri, 12 Apr 2024 05:21:45 -0400
parents 3ec7804abf17
children ec0a2b5493f8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
261
3ec7804abf17 include: make header guards more sane
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
1 #ifndef MINORI_CORE_INI_H_
3ec7804abf17 include: make header guards more sane
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
2 #define MINORI_CORE_INI_H_
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
3
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
4 #define MINI_CASE_SENSITIVE
120
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
5 #include "core/strings.h"
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
6 #include "gui/translate/anime.h"
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
7 #include "gui/translate/config.h"
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 221
diff changeset
8 #include "mini/ini.h"
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 221
diff changeset
9 #include <string>
120
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
10 #include <type_traits>
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
11
116
254b1d2b7096 settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
12 namespace INI {
254b1d2b7096 settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
13
120
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
14 /* very simple tutorial on how to give anyone who reads
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
15 your code an aneurysm */
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 221
diff changeset
16 template<typename T, typename = void>
120
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
17 struct is_toutf8string_available : std::false_type {};
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
18
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
19 template<typename T>
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 221
diff changeset
20 struct is_toutf8string_available<T, std::void_t<decltype(Strings::ToUtf8String(std::declval<T>()))>> : std::true_type {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 221
diff changeset
21 };
120
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
22
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 221
diff changeset
23 template<typename T, typename = void>
120
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
24 struct is_translation_available : std::false_type {};
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
25
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
26 template<typename T>
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 221
diff changeset
27 struct is_translation_available<T, std::void_t<decltype(Translate::ToString(std::declval<T>()))>> : std::true_type {};
120
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
28
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
29 template<typename T>
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 221
diff changeset
30 T GetIniValue(const mINI::INIStructure& ini, const std::string& section, const std::string& value, const T& def) {
120
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
31 if (!ini.has(section) || !ini.get(section).has(value))
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
32 return def;
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
33
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
34 const std::string val = ini.get(section).get(value);
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
35
221
53211cb1e7f5 library: add initial library stuff
Paper <paper@paper.us.eu.org>
parents: 136
diff changeset
36 if constexpr (std::is_integral<T>::value) {
120
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
37 /* Integer? */
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
38 if constexpr (std::is_same<T, bool>::value) {
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
39 /* Boolean? */
136
7d3ad9529c4c ini: fix bool getters to provide default vals for bools and ints
Paper <mrpapersonic@gmail.com>
parents: 122
diff changeset
40 return Strings::ToBool(val, def);
120
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
41 } else {
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
42 /* Always fall back to long long */
136
7d3ad9529c4c ini: fix bool getters to provide default vals for bools and ints
Paper <mrpapersonic@gmail.com>
parents: 122
diff changeset
43 return Strings::ToInt<T>(val, def);
120
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
44 }
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
45 } else {
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
46 return val;
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
47 }
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
48 }
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
49
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
50 /* this should be able to handle most of our custom types */
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
51 template<typename T>
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 221
diff changeset
52 void SetIniValue(mINI::INIStructure& ini, const std::string& section, const std::string& key, const T& value) {
120
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
53 auto& ini_key = ini[section][key];
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
54
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
55 if constexpr (is_translation_available<T>::value) {
221
53211cb1e7f5 library: add initial library stuff
Paper <paper@paper.us.eu.org>
parents: 136
diff changeset
56 /* prioritize translation */
120
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
57 ini_key = Translate::ToString(value);
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
58 } else if constexpr (std::is_same<T, std::string>::value) {
221
53211cb1e7f5 library: add initial library stuff
Paper <paper@paper.us.eu.org>
parents: 136
diff changeset
59 /* lmfao */
120
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
60 ini_key = value;
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
61 } else if constexpr (is_toutf8string_available<T>::value) {
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
62 ini_key = Strings::ToUtf8String(value);
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
63 }
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
64 }
116
254b1d2b7096 settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
65
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 221
diff changeset
66 } // namespace INI
116
254b1d2b7096 settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
67
120
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
68 #endif