forked from vermagav/config-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
98 lines (73 loc) · 2.24 KB
/
main.cc
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
#include <iostream>
#include <iterator>
#include <sstream>
#include "config/handler.h"
// Helper function to print settings of heterogeneous types.
void printSetting(config::Item* setting) {
if (setting == NULL) {
std::cout << "[NONE]:\tSetting not found.\n";
} else {
switch (setting->GetValueType()) {
case config::ValueType::STRING:
std::cout << "[STR]:\t" << setting->GetString() << "\n";
break;
case config::ValueType::BOOLEAN:
std::cout << "[BOOL]:\t" << setting->GetBoolean() << "\n";
break;
case config::ValueType::INTEGER:
std::cout << "[INT]:\t" << setting->GetInteger() << "\n";
break;
case config::ValueType::DOUBLE:
std::cout << "[DBL]:\t" << setting->GetDouble() << "\n";
break;
case config::ValueType::LIST:
std::cout << "[LIST]:\t{ ";
std::vector<std::string> list = setting->GetList();
unsigned int i = 0;
for (; i < list.size()-1; i++) {
std::cout << list[i] << ", ";
}
std::cout << list[i] << " }\n";
break;
}
}
}
int main() {
try {
// Get a config handler
config::Handler handler;
// Load a config file
handler.Load("sample.ini", {"production", "ubuntu"});
// Output some settings to standard output
config::Item* setting;
setting = handler.Get("common.paid_users_size_limit");
std::cout << "\n\nTest print directly: " << setting->GetInteger() << "\n";
setting = handler.Get("common.paid_users_size_limit");
printSetting(setting);
setting = handler.Get("ftp.name");
printSetting(setting);
setting = handler.Get("http.params");
printSetting(setting);
setting = handler.Get("ftp.foobar123");
printSetting(setting);
setting = handler.Get("ftp.enabled");
printSetting(setting);
setting = handler.Get("ftp.path");
printSetting(setting);
// Add more here...
std::unordered_map<std::string, config::Item>* sectionMap = handler.GetSection("common");
if (sectionMap == NULL) {
std::cout << "Section not found.\n";
} else {
std::cout << "\n\nPrinting ALL keys and values via GetSection():\n";
for (auto kv : *sectionMap) {
std::cout << "[KEY]:\t" << kv.first << ":\n";
printSetting(&kv.second);
}
}
// ...or here
} catch (std::exception e) {
std::cout << "Encountered error: " << e.what() << "\n";
}
return 0;
}