-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cc
116 lines (101 loc) · 2.6 KB
/
Config.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "Config.h"
#include <fstream>
#include <vector>
#include <string>
bool Config::set_file(CSR filename)
{
std::ofstream ofs(filename, std::ios::app);
bool valid = ofs.is_open();
if (valid == true)
this->filename = filename;
return valid;
}
Config::STR Config::get_str(CSR key, CSR def)
{
STR ret;
{
std::ifstream ifs(filename);
if (ifs.is_open() == true)
{
for (STR line; std::getline(ifs, line);)
{
auto space = line.find_first_of(" \t");
if (space != std::string::npos &&
line.substr(0, space) == key)
{
auto val = line.find_first_not_of(" \t", space);
if (val != std::string::npos)
{
ret = line.substr(val);
break;
}
}
}
}
}
if (ret.empty() == true)
{
ret = def;
set_value(key, ret);
}
return ret;
}
int Config::get_int(Config::CSR key, int def)
{
int ret;
try {
ret = std::stoi(get_str(key, std::to_string(def)));
} catch(...) {
ret = def;
set_value(key, std::to_string(def));
}
return ret;
}
bool Config::get_bool(Config::CSR key, bool def)
{
return 0 != get_int(key, (int) def);
}
bool Config::set_value(CSR key, CSR value)
{
std::vector<std::pair<STR, STR>> map;
// read all
{
std::ifstream ifs(filename);
if (ifs.is_open() == false)
return false;
for (STR line; std::getline(ifs, line);)
{
auto space = line.find_first_of(" \t");
if (space == std::string::npos)
continue;
auto val = line.find_first_not_of(" \t", space);
if (val != std::string::npos)
map.emplace_back(line.substr(0, space),
line.substr(val));
}
} // file closed
// replace or add
{
auto it = std::begin(map);
auto end = std::end(map);
for (; it != end; ++it)
{
if (it->first == key)
{
it->second = value;
break;
}
}
if (it == end)
map.emplace_back(key, value);
}
// rewrite
{
std::ofstream ofs(filename, std::ios::trunc);
if (ofs.is_open() == false)
return false;
for (const auto &pair : map)
ofs << pair.first << ' ' << pair.second << '\n';
} // file closed
return true;
}