1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
/*******************************************************************************
**
** nyamp: yet another media player
** Copyright (C) 2017 Aqua-sama
**
** 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 3 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/>.
**
******************************************************************************/
#include "configuration.h"
#include <libconfig.h++>
#include <sstream>
using namespace libconfig;
Configuration::Configuration()
{
m_userCfg = new Config();
#ifdef C_LIKE_CONFIG
m_userCfg->setOptions(Config::OptionSemicolonSeparators | Config::OptionOpenBraceOnSeparateLine);
#endif
// fsync after writing and before closing
m_userCfg->setOption(Config::OptionFsync, true);
m_defaultCfg = new Config();
}
Configuration::~Configuration()
{
if(!m_userCfgPath.empty()) {
m_userCfg->writeFile(m_userCfgPath.c_str());
}
delete m_userCfg;
delete m_defaultCfg;
}
bool Configuration::readUserConfiguration(const std::string &path)
{
m_userCfgPath = path;
try {
m_userCfg->readFile(path.c_str());
} catch (FileIOException) {
return false;
} catch (ParseException) {
return false;
}
return true;
}
bool Configuration::writeUserConfiguration(const std::string &path)
{
m_userCfgPath = path;
try {
m_userCfg->writeFile(path.c_str());
} catch (FileIOException) {
return false;
}
return true;
}
bool Configuration::readDefaultConfiguration(const std::string &data)
{
try {
m_defaultCfg->readString(data);
} catch(ParseException) {
return false;
}
return true;
}
std::vector<std::string> Configuration::children(const char* name)
{
std::vector<std::string> groupNames;
const Setting &root = m_defaultCfg->lookup(name);
for(auto i = root.begin(); i != root.end(); ++i) {
groupNames.push_back(std::string(i->getName()));
}
return groupNames;
}
template<typename T>
std::optional<T> Configuration::value(const char* path) const
{
Config *conf = getConfig(path);
if(conf == nullptr) {
return std::nullopt;
}
// setting was found
const Setting &setting = conf->lookup(path);
std::optional<T> r;
// cast depending on type
// type checks are done during compile time
switch (setting.getType()) {
case Setting::TypeNone:
r = std::nullopt;
break;
case Setting::TypeInt:
// int, unsigned int, long, unsigned long
if constexpr(std::is_same_v<T, std::string>) {
r = std::optional<std::string>(std::to_string(static_cast<int>(setting)));
} else if constexpr(std::is_same_v<T, int> || std::is_same_v<T, unsigned int> || std::is_same_v<T, long> || std::is_same_v<T, unsigned long>) {
r = std::optional<T>(static_cast<T>(setting));
} else {
r = std::nullopt;
}
break;
case Setting::TypeInt64:
// int, unsigned int; long long, unsigned long long
if constexpr(std::is_same_v<T, std::string>) {
r = std::optional<std::string>(std::to_string(static_cast<long long>(setting)));
} else if constexpr(std::is_same_v<T, int> || std::is_same_v<T, unsigned int> || std::is_same_v<T, long long> || std::is_same_v<T, unsigned long long>) {
r = std::optional<T>(static_cast<T>(setting));
} else {
r = std::nullopt;
}
break;
case Setting::TypeFloat:
// float, double
if constexpr(std::is_same_v<T, std::string>) {
r = std::optional<std::string>(std::to_string(static_cast<float>(setting)));
} else if constexpr(std::is_same_v<T, float> || std::is_same_v<T, double>) {
r = std::optional<T>(static_cast<T>(setting));
} else {
r = std::nullopt;
}
break;
case Setting::TypeString:
// const char*, std::string
if constexpr(std::is_same<T, std::string>::value) {
r = std::optional<std::string>(static_cast<const char*>(setting));
} else {
r = std::nullopt;
}
break;
case Setting::TypeBoolean:
// bool
if constexpr(std::is_same<T, std::string>::value) {
r = std::optional<std::string>(static_cast<bool>(setting) ? "true" : "false");
} else if constexpr(std::is_same<T, bool>::value) {
r = std::optional<bool>(static_cast<bool>(setting));
} else {
r = std::nullopt;
}
break;
case Setting::TypeGroup:
r = std::nullopt;
break;
case Setting::TypeArray:
r = std::nullopt;
break;
case Setting::TypeList:
r = std::nullopt;
break;
}
return r;
}
// tell the compiler to export these functions, otherwise you get linking errors
template std::optional<int> Configuration::value<int>(const char* path) const;
template std::optional<unsigned int> Configuration::value<unsigned int>(const char* path) const;
template std::optional<long> Configuration::value<long>(const char* path) const;
template std::optional<unsigned long> Configuration::value<unsigned long>(const char* path) const;
template std::optional<long long> Configuration::value<long long>(const char* path) const;
template std::optional<unsigned long long> Configuration::value<unsigned long long>(const char* path) const;
template std::optional<float> Configuration::value<float>(const char* path) const;
template std::optional<double> Configuration::value<double>(const char* path) const;
template std::optional<bool> Configuration::value<bool>(const char* path) const;
template std::optional<std::string> Configuration::value<std::string>(const char* path) const;
template<typename T>
void Configuration::setValue(std::string path, const T &val)
{
if(m_userCfg->exists(path)) {
Setting &setting = m_userCfg->lookup(path);
// compiler complained about operator= not taking unsinged ints, longs and long longs
if constexpr(std::is_unsigned_v<T> && !std::is_same_v<T, bool>) {
setting = static_cast<typename std::make_signed_t<T>>(val);
} else {
setting = val;
}
return;
}
// the entry doesn't exist, so we need to create it
// libconfig can't create an entry from a path, we need to get its root, and then add it
// this will error out if entry being added is not in the default config
std::istringstream p(path);
Setting *userSetting = &m_userCfg->getRoot();
Setting *defaultSetting = &m_defaultCfg->getRoot();
std::string i;
while (std::getline(p, i, '.')) {
defaultSetting = &defaultSetting->lookup(i);
// check if user setting exists, if not, create it
if(!userSetting->exists(i)) {
userSetting = &userSetting->add(i, defaultSetting->getType());
}
}
if constexpr(std::is_unsigned_v<T> && !std::is_same_v<T, bool>) {
*userSetting = static_cast<typename std::make_signed_t<T>>(val);
} else {
*userSetting = val;
}
}
template void Configuration::setValue<int>(std::string path, const int &val);
template void Configuration::setValue<unsigned int>(std::string path, const unsigned int &val);
template void Configuration::setValue<long>(std::string path, const long &val);
template void Configuration::setValue<unsigned long>(std::string path, const unsigned long &val);
template void Configuration::setValue<long long>(std::string path, const long long &val);
template void Configuration::setValue<unsigned long long>(std::string path, const unsigned long long &val);
template void Configuration::setValue<float>(std::string path, const float &val);
template void Configuration::setValue<double>(std::string path, const double &val);
template void Configuration::setValue<bool>(std::string path, const bool &val);
template void Configuration::setValue<std::string>(std::string path, const std::string &val);
Config *Configuration::getConfig(const char* path) const
{
if(m_userCfg->exists(path)) {
return m_userCfg;
} else if(m_defaultCfg->exists(path)) {
return m_defaultCfg;
} else {
return nullptr;
}
}
std::string &patchHome(std::string &path, const std::string &home)
{
const size_t location = path.find("~");
if(location != std::string::npos) {
return path.replace(location, 1, home);
}
return path;
}
|