-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.cpp
199 lines (162 loc) · 4.14 KB
/
config.cpp
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
#include "config.h"
#include <locale>
#include <codecvt>
#include <fstream>
#include <regex>
#include <winerror.h>
#include <ShlObj_core.h>
#include <Shlwapi.h>
#pragma comment (lib, "shlwapi.lib")
#pragma warning( disable: 4984 )
namespace ets2dc_config {
#pragma region this should be private to ets2dc_config namespace
namespace {
static constexpr char config_regex[] = "^([^=]*)=(.*)$";
static bool initialized = false;
static bool dirty = false;
static std::wstring config_file;
static std::map<std::string, std::string> config_options;
static bool autoSave = true;
void load()
{
config_options = std::map<std::string, std::string>();
std::ifstream ifs;
ifs.open(config_file, std::ifstream::in);
std::regex expression(config_regex);
std::cmatch cm;
for (std::string line; std::getline(ifs, line); ) {
std::string id;
std::string val;
bool error = true;
if (std::regex_match(line.c_str(), cm, expression)) {
if (cm.size() == 3) {
id = cm[1];
val = cm[2];
error = false;
}
}
if (error) continue;
if (id[0] == '#') continue;
config_options[id] = val;
}
ifs.close();
}
void set_auto_save(bool value)
{
autoSave = value;
}
template<typename T> T get(std::string key, const T& default_value)
{
auto config_option = config_options.find(key);
if (config_option == config_options.end()) {
return default_value;
}
std::string value = config_option->second;
if constexpr (std::is_integral<T>::value) {
return std::stoi(value);
}
else if constexpr (std::is_same<float, T>::value) {
return std::stof(value);
}
else if constexpr (std::is_same<double, T>::value) {
return std::stod(value);
}
else if constexpr (std::is_base_of<std::string, T>::value) {
return value;
}
else if constexpr (std::is_base_of<std::wstring, T>::value) {
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> conv1;
// std::wstring w(config_option->second.begin(), config_option->second.end());
std::wstring v = conv1.from_bytes(value.data());
return v;
}
return default_value;
}
template<typename T> void set(std::string key, T value) {
std::string string_value;
if constexpr (std::is_same<int, T>::value ||
std::is_same<float, T>::value ||
std::is_same<double, T >::value) {
string_value = std::to_string(value);
}
else if constexpr (std::is_base_of<std::string, T>::value) {
string_value = value;
}
else if constexpr (std::is_base_of<std::wstring, T>::value) {
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv1;
string_value = conv1.to_bytes(value);
}
dirty = true;
config_options[key] = string_value;
save();
}
}
#pragma endregion
void init(std::wstring path, std::wstring file) {
config_file = path + file;
load();
}
void set(std::string key, std::string value)
{
set<std::string>(key, value);
}
void set(std::string key, std::wstring value)
{
set<std::wstring>(key, value);
}
void set(std::string key, int value)
{
set<int>(key, value);
}
void set(std::string key, float value)
{
set<float>(key, value);
}
void set(std::string key, double value)
{
set<double>(key, value);
}
bool is_dirty()
{
bool d = dirty;
dirty = false;
return d;
}
std::string get(std::string key, std::string default_value)
{
return get<std::string>(key, default_value);
}
std::wstring get(std::string key, std::wstring default_value)
{
return get<std::wstring>(key, default_value);
}
int get(std::string key, int default_value)
{
return get<int>(key, default_value);
}
float get(std::string key, float default_value)
{
return get<float>(key, default_value);
}
double get(std::string key, double default_value)
{
return get<double>(key, default_value);
}
void save() {
std::ofstream ofs;
ofs.open(config_file, std::ofstream::out | std::ofstream::trunc);
for (const auto& config_option : config_options) {
ofs << config_option.first << "=" << config_option.second << std::endl;
}
ofs.close();
}
void begin_save_session()
{
autoSave = false;
}
void end_save_session()
{
save();
autoSave = true;
}
}