forked from sstokic-tgm/Gladiatorcheatz-Public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cpp
162 lines (130 loc) · 3.19 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
#include "harCs.hpp"
#include "Config.hpp"
namespace ConfigMngr
{
bool Config::loadInitial(std::string const &file)
{
m_listGroupStack.push_front(this);
std::ifstream in(file, std::ifstream::in);
if (!in)
return false;
std::string buf;
while (std::getline(in, buf))
{
if ((buf.length() > 2) && (buf[0] != '#') && (buf.find(')') == std::string::npos))
{
std::string name;
std::string value;
split(buf, name, value, '=');
for (std::list<Config*>::reverse_iterator it = m_listGroupStack.rbegin(); it != m_listGroupStack.rend(); ++it)
{
(*it)->symbolExpand(value);
}
envSymbolExpand(value);
m_listGroupStack.front()->add(name, value);
}
if ((buf.length() > 0) && (buf[0] != '#') && (buf.find(')') != std::string::npos))
m_listGroupStack.pop_front();
}
in.close();
return true;
}
void Config::add(std::string name, std::string value)
{
m_mapSymbols[name] = value;
}
void Config::split(std::string in, std::string &left, std::string &right, char c)
{
size_t pos = in.find_first_of(c);
if (pos == std::string::npos)
{
left = in;
trim(left);
right = "";
}
else if (pos <= 1)
{
left = "";
right = in.substr(pos + 1, std::string::npos);
trim(right);
}
else
{
left = in.substr(0, pos - 1);
right = in.substr(pos + 1, std::string::npos);
trim(left);
trim(right);
}
}
void Config::trim(std::string &s)
{
while ((s.length() > 1) && ((s[0] == ' ') || (s[0] == '\t')))
s = s.substr(1, std::string::npos);
while ((s.length() > 1) &&
((s[s.length() - 1] == ' ') ||
(s[s.length() - 1] == '\t') ||
(s[s.length() - 1] == '\n') ||
(s[s.length() - 1] == '\r')))
s = s.substr(0, s.length() - 1);
if ((s.length() > 1) && (s[0] == '"'))
s = s.substr(1, std::string::npos);
if ((s.length() > 1) && (s[s.length() - 1] == '"'))
s = s.substr(0, s.length() - 1);
}
void Config::symbolExpand(std::string &s)
{
symbolExpand(m_mapSymbols, s);
}
void Config::envSymbolExpand(std::string &s)
{
symbolExpand(m_mapEnvSymbols, s);
}
void Config::symbolExpand(std::map<std::string, std::string> &symbols, std::string &s)
{
bool expanded;
do
{
expanded = false;
for (std::map<std::string, std::string>::iterator it = m_mapSymbols.begin(); it != m_mapSymbols.end(); ++it)
{
std::string search = "%" + it->first + "%";
std::string replace = it->second;
size_t pos = s.find(search);
if (pos != std::string::npos)
{
expanded = true;
s.replace(pos, search.length(), replace);
}
}
} while (expanded);
}
std::string Config::getString(std::string const &name)
{
auto it = m_mapSymbols.find(name);
if (it == m_mapSymbols.end())
return "0";
return it->second;
}
bool Config::getBool(std::string const &name)
{
auto res = getString(name);
if ((res == "yes") ||
(res == "Yes") ||
(res == "YES") ||
(res == "true") ||
(res == "True") ||
(res == "TRUE"))
return true;
return false;
}
int Config::getInt(std::string const &name)
{
auto res = getString(name);
return atoi(res.c_str());
}
float Config::getFloat(std::string const &name)
{
auto res = getString(name);
return std::stof(res.c_str());
}
}