-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTConfig.hpp
More file actions
443 lines (429 loc) · 14.5 KB
/
TConfig.hpp
File metadata and controls
443 lines (429 loc) · 14.5 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/**
* @file TConfig.hpp
* @author Yongjun Choi (chldydwns2014@gmail.com)
* @brief
* @version 1.0
* @date 17-05-2025
*
* @copyright Copyright (c) 2025
*
*/
#ifndef __TCONFIG_H__
#define __TCONFIG_H__
#include<filesystem>
#include<string>
#include<vector>
#include<fstream>
#include<iostream>
#include<array>
#include<sstream>
#include<algorithm>
namespace KEI {
class TDictionary;
class TConfig;
class TConfigFile;
class TConfigFile {
public: // 생성자 & 소멸자
TConfigFile() = default;
TConfigFile(std::filesystem::path path);
public: // 외부에서 사용하는 함수
bool hasConfig(std::string_view title) const;
TConfig getConfig(std::string_view title) const;
TConfig& modifyConfig(std::string_view title);
private:
std::filesystem::path mPath;
std::vector<TConfig> mConfigSet;
private:
void read();
void removeFrontBlank(std::string& line);
void removeBackBlank(std::string& line);
bool isBlankOrComment(std::string_view line) const;
void removeQuoteQue(std::string& line);
std::string isEndComment(std::string_view line) const;
bool isTitle(std::string_view line) const;
};
class TConfig {
public:
TConfig(std::string_view title);
public:
void addConfig(const TConfig& config);
void addDictionary(const TDictionary& dict);
std::string getTitle() const;
TConfig& modifyConfig(std::string_view title);
template<typename T> T getValue(std::string_view key) const;
template<typename T, int N> std::array<T, N> getValue(std::string_view key) const;
template<typename T> std::vector<T> getValueVector(std::string_view key) const;
bool hasKey(std::string_view key) const;
TConfig getConfig(std::string_view title) const;
private:
std::string mTitle;
std::vector<TConfig> mConfigSet;
std::vector<TDictionary> mDictSet;
private:
TDictionary getDictionary(std::string_view key) const;
};
class TDictionary {
public:
TDictionary(std::string_view key, std::string_view value) : mKey(key), mValue(value) { }
public:
std::string getKey() const;
template <typename T> T getValue() const;
template<typename T> std::vector<T> getValueVector() const;
template<typename T, int N> std::array<T, N> getValue() const;
void setValue(const std::string& value);
private:
std::string mKey;
std::string mValue;
private:
template <typename T> static constexpr bool isInterger8();
template <typename T> static constexpr bool isInteger16();
template <typename T> static constexpr bool isInteger32();
template <typename T> static constexpr bool isInteger64();
template <typename T> static constexpr bool isUnsignedInteger8();
template <typename T> static constexpr bool isUnsignedInteger16();
template <typename T> static constexpr bool isUnsignedInteger32();
template <typename T> static constexpr bool isUnsignedInteger64();
};
inline std::string TDictionary::getKey() const { return mKey; }
inline void TDictionary::setValue(const std::string& value) { mValue = value; }
// This is
template <typename T> constexpr bool TDictionary::isInterger8() {
return std::is_same_v<T, int8_t> || std::is_same_v<T, signed char>;
}
template <typename T> constexpr bool TDictionary::isInteger16() {
return std::is_same_v<T, int16_t> || std::is_same_v<T, short> || std::is_same_v<T, short int> || std::is_same_v<T, signed short> || std::is_same_v<T, signed short int>;
}
template <typename T> constexpr bool TDictionary::isInteger32() {
return std::is_same_v<T, int> || std::is_same_v<T, signed> || std::is_same_v < T, signed int> || std::is_same_v<T, int32_t> || std::is_same_v<T, long> || std::is_same_v<T, signed long> || std::is_same_v<T, signed long int>;
}
template <typename T> constexpr bool TDictionary::isInteger64() {
return std::is_same_v<T, long long> || std::is_same_v<T, signed long long> || std::is_same_v<T, signed long long int> || std::is_same_v<T, int64_t>;
}
template <typename T> constexpr bool TDictionary::isUnsignedInteger8() {
return std::is_same_v<T, uint8_t> || std::is_same_v<T, unsigned char>;
}
template <typename T> constexpr bool TDictionary::isUnsignedInteger16() {
return std::is_same_v<T, uint16_t> || std::is_same_v<T, unsigned short> || std::is_same_v<T, unsigned short int>;
}
template <typename T> constexpr bool TDictionary::isUnsignedInteger32() {
return std::is_same_v<T, unsigned int> || std::is_same_v<T, unsigned> || std::is_same_v<T, uint32_t> || std::is_same_v<T, unsigned long> || std::is_same_v<T, unsigned long int>;
}
template <typename T> constexpr bool TDictionary::isUnsignedInteger64() {
return std::is_same_v<T, unsigned long long> || std::is_same_v<T, unsigned long long int> || std::is_same_v<T, uint64_t>;
}
template <typename T> T TDictionary::getValue() const {
if constexpr ( isInteger32<T>() ) {
return std::stoi(mValue);
} else if constexpr ( isUnsignedInteger32<T>() ) {
return static_cast<unsigned int>(std::stoul(mValue));
} else if constexpr ( isInterger8<T>() ) {
return static_cast<int8_t>(mValue.at(0));
} else if constexpr ( isUnsignedInteger8<T>() ) {
return static_cast<uint8_t>(mValue.at(0));
} else if constexpr ( isInteger16<T>() ) {
return static_cast<int16_t>(std::stoi(mValue));
} else if constexpr ( isUnsignedInteger16<T>() ) {
return static_cast<uint16_t>(std::stoul(mValue));
} else if constexpr ( isInteger64<T>() ) {
return static_cast<int64_t>(std::stoll(mValue));
} else if constexpr ( isUnsignedInteger64<T>() ) {
return static_cast<uint64_t>(std::stoull(mValue));
} else if constexpr ( std::is_same_v<T, bool> ) {
if ( mValue == "true" || mValue == "1" || mValue == "True" || mValue == "TRUE" ) {
return true;
} else if ( mValue == "false" || mValue == "0" || mValue == "False" || mValue == "FALSE" ) {
return false;
}
} else if constexpr ( std::is_same_v<T, char> ) {
return mValue.at(0);
} else if constexpr ( std::is_same_v<T, float> ) {
return static_cast<float>(std::stof(mValue));
} else if constexpr ( std::is_same_v<T, double> ) {
return static_cast<double>(std::stod(mValue));
} else if constexpr ( std::is_same_v<T, long double> ) {
return static_cast<long double>(std::stold(mValue));
} else if constexpr ( std::is_same_v<T, std::string> ) {
return mValue;
} else {
static_assert(!std::is_same_v<T, T>, "Unsupported type");
}
return T();
}
template<typename T> std::vector<T> TDictionary::getValueVector() const {
std::vector<T> vec;
std::istringstream valueList(mValue);
std::string value;
while ( valueList >> value ) {
if constexpr ( std::is_same_v<T, int> ) {
vec.push_back(std::stoi(value));
} else if constexpr ( std::is_same_v<T, unsigned int> ) {
vec.push_back(static_cast<unsigned int>(std::stoul(value)));
} else if constexpr ( std::is_same_v<T, float> ) {
vec.push_back(static_cast<float>(std::stof(value)));
} else if constexpr ( std::is_same_v<T, double> ) {
vec.push_back(static_cast<double>(std::stod(value)));
} else if constexpr ( std::is_same_v<T, long double> ) {
vec.push_back(static_cast<long double>(std::stold(value)));
} else if constexpr ( std::is_same_v<T, char> ) {
vec.push_back(value.at(0));
} else if constexpr ( std::is_same_v<T, std::string> ) {
vec.push_back(value);
} else {
static_assert(!std::is_same_v<T, T>, "Unsupported type");
}
}
return vec;
}
template<typename T, int N> std::array<T, N> TDictionary::getValue() const {
static_assert(N > 0, "N must be greater than 0");
std::array<T, N> arr;
std::istringstream valueList(mValue);
std::string value;
int iValue = 0;
while ( valueList >> value, ' ' ) {
if constexpr ( std::is_same_v<T, int> ) {
arr[iValue] = std::stoi(value);
} else if constexpr ( std::is_same_v<T, unsigned int> ) {
arr[iValue] = static_cast<unsigned int>(std::stoul(value));
} else if constexpr ( std::is_same_v<T, float> ) {
arr[iValue] = static_cast<float>(std::stof(value));
} else if constexpr ( std::is_same_v<T, double> ) {
arr[iValue] = static_cast<double>(std::stod(value));
} else if constexpr ( std::is_same_v<T, long double> ) {
arr[iValue] = static_cast<long double>(std::stold(value));
} else if constexpr ( std::is_same_v<T, char> ) {
arr[iValue] = value.at(0);
} else if constexpr ( std::is_same_v<T, std::string> ) {
arr[iValue] = value;
} else {
static_assert(!std::is_same_v<T, T>, "Unsupported type");
}
iValue++;
if ( iValue == N ) {
break;
}
}
if ( arr.size() != N ) {
throw std::runtime_error("Array size mismatch");
}
return arr;
}
inline TConfig::TConfig(std::string_view title) : mTitle(title) { }
inline void TConfig::addConfig(const TConfig& config) {
mConfigSet.push_back(config);
}
inline void TConfig::addDictionary(const TDictionary& dict) {
bool isExist = false;
for ( auto& dic : mDictSet ) {
if ( dic.getKey() == dict.getKey() ) {
dic.setValue(dict.getValue<std::string>());
isExist = true;
}
}
if ( !isExist ) {
mDictSet.push_back(dict);
}
}
inline std::string TConfig::getTitle() const {
return mTitle;
}
template<typename T> T TConfig::getValue(std::string_view key) const {
for ( const auto& dict : mDictSet ) {
if ( dict.getKey() == key ) {
return dict.getValue<T>();
}
}
throw std::runtime_error("Key " + std::string(key) + " not found");
}
template<typename T, int N> std::array<T, N> TConfig::getValue(std::string_view key) const {
for ( const auto& dict : mDictSet ) {
if ( dict.getKey() == key ) {
return dict.getValue<T, N>();
}
}
throw std::runtime_error("Key not found");
}
template<typename T> std::vector<T> TConfig::getValueVector(std::string_view key) const {
for ( const auto& dict : mDictSet ) {
if ( dict.getKey() == key ) {
return dict.getValueVector<T>();
}
}
throw std::runtime_error("Key not found");
}
inline bool TConfig::hasKey(std::string_view key) const {
for ( const auto& dict : mDictSet ) {
if ( dict.getKey() == key ) {
return true;
}
}
return false;
}
inline TConfig TConfig::getConfig(std::string_view title) const {
for ( const auto& config : mConfigSet ) {
if ( config.mTitle == title ) {
return config;
}
}
throw std::runtime_error("Config " + std::string(title) + " not found");
}
inline TDictionary TConfig::getDictionary(std::string_view key) const {
for ( const auto& dict : mDictSet ) {
if ( dict.getKey() == key ) {
return dict;
}
}
throw std::runtime_error("Dictionary " + std::string(key) + " not found");
}
inline TConfig& TConfig::modifyConfig(std::string_view title) {
for ( auto& config : mConfigSet ) {
if ( config.mTitle == title ) {
return config;
}
}
throw std::runtime_error("Config not found");
}
inline TConfigFile::TConfigFile(std::filesystem::path path) {
try {
if ( !std::filesystem::exists(path) ) {
throw std::runtime_error("File does not exist");
} else if ( path.extension() != ".conf" ) {
throw std::runtime_error("File is not a configuration file");
} else {
mPath = path;
read();
}
} catch ( const std::exception& e ) {
std::cerr << "Error: " << e.what() << std::endl;
// Handle the error
}
}
inline bool TConfigFile::hasConfig(std::string_view title) const {
for ( const auto& config : mConfigSet ) {
if ( config.getTitle() == title ) {
return true;
}
}
return false;
}
inline void TConfigFile::read() {
std::ifstream mFile(mPath);
if ( !mFile.is_open() ) {
throw std::runtime_error("Failed to open file");
}
std::string line;
std::vector<TConfig> subConfig;
while ( std::getline(mFile, line) ) {
removeFrontBlank(line);
if ( isBlankOrComment(line) ) {
continue;
}
if ( isTitle(line) ) {
std::string title = line.substr(1, line.find(']') - 1);
mConfigSet.push_back(TConfig(title));
continue;
}
if ( line.find('=') != std::string::npos ) {
std::string key = line.substr(0, line.find('='));
removeBackBlank(key);
std::string value = line.substr(line.find('=') + 1);
removeFrontBlank(value);
removeBackBlank(value);
value = isEndComment(value);
removeQuoteQue(value);
if ( value.at(0) != '{' ) {
if ( subConfig.size() > 0 ) {
subConfig.back().addDictionary(TDictionary(key, value));
} else {
mConfigSet.back().addDictionary(TDictionary(key, value));
}
} else {
subConfig.push_back(TConfig(key));
}
} else if ( line.at(0) == '}' ) {
if ( subConfig.size() > 1 ) {
(subConfig.end() - 2)->addConfig(subConfig.back());
subConfig.pop_back();
} else {
mConfigSet.back().addConfig(subConfig[0]);
subConfig.pop_back();
}
}
}
}
inline TConfig TConfigFile::getConfig(std::string_view title) const {
for ( const auto& config : mConfigSet ) {
if ( config.getTitle() == title ) {
return config;
}
}
throw std::runtime_error("Config " + std::string(title) + " not found");
}
inline TConfig& TConfigFile::modifyConfig(std::string_view title) {
for ( auto& config : mConfigSet ) {
if ( config.getTitle() == title ) {
return config;
}
}
throw std::runtime_error("Config " + std::string(title) + " not found");
}
inline void TConfigFile::removeFrontBlank(std::string& line) {
auto it = line.begin();
while ( it != line.end() && std::isspace(*it) ) {
++it;
}
line.erase(line.begin(), it);
}
inline void TConfigFile::removeBackBlank(std::string& line) {
auto it = line.end();
while ( it != line.begin() && std::isspace(*(it - 1)) ) {
--it;
}
line.erase(it, line.end());
}
inline bool TConfigFile::isBlankOrComment(std::string_view line) const {
if ( line.empty() || line.at(0) == '#' ) {
return true;
} else {
return false;
}
}
inline void TConfigFile::removeQuoteQue(std::string& line) {
if ( line.at(0) == '"' ) {
line.erase(line.begin());
}
if ( line.back() == '"' ) {
line.pop_back();
}
}
inline std::string TConfigFile::isEndComment(std::string_view line) const {
std::string temp = std::string(line);
if ( temp.find("#") == std::string::npos ) {
return temp;
} else {
int nQuote = 0;
for ( size_t i = 0; i < temp.size(); i++ ) {
if ( temp[i] == '"' ) {
nQuote++;
}
if ( nQuote % 2 == 0 && temp[i] == '#' ) {
return temp.substr(0, i - 1);
}
}
}
return temp;
}
inline bool TConfigFile::isTitle(std::string_view line) const {
if ( line.at(0) == '[' && line.at(line.size() - 1) == ']' ) {
if ( std::count(line.begin(), line.end(), '[') > 1 || std::count(line.begin(), line.end(), ']') > 1 ) {
std::cerr << "Invalide title format. Title should be in the format [TITLE]" << std::endl;
return false;
} else {
return true;
}
} else {
return false;
}
}
}
#endif // __TCONFIG_H__