forked from Prof-Butts/Hook_XWACockpitLook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.cpp
169 lines (132 loc) · 3.11 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
#include "config.h"
#include <fstream>
std::string Trim(const std::string& str)
{
const char* ws = " \t\n\r\f\v";
std::string s = str;
s.erase(str.find_last_not_of(ws) + 1);
s.erase(0, s.find_first_not_of(ws));
return s;
}
std::string GetStringWithoutExtension(const std::string& str)
{
auto b = str.find_last_of('.');
return str.substr(0, b);
}
std::vector<std::string> GetFileLines(const std::string& path, const std::string& section)
{
std::vector<std::string> values;
std::ifstream file(path);
if (file)
{
std::string line;
bool readSection = section.empty();
while (std::getline(file, line))
{
line = Trim(line);
if (!line.length())
{
continue;
}
if (line[0] == '#' || line[0] == ';' || (line[0] == '/' && line[1] == '/'))
{
continue;
}
if (line[0] == '[' && line[line.length() - 1] == ']')
{
std::string name = line.substr(1, line.length() - 2);
if (_stricmp(name.c_str(), section.c_str()) == 0)
{
readSection = true;
}
else
{
readSection = false;
}
}
else
{
if (readSection)
{
values.push_back(line);
}
}
}
}
return values;
}
std::string GetFileKeyValue(const std::vector<std::string>& lines, const std::string& key)
{
for (const auto& line : lines)
{
int pos = line.find("=");
if (pos == -1)
{
continue;
}
std::string name = Trim(line.substr(0, pos));
if (!name.length())
{
continue;
}
if (_stricmp(name.c_str(), key.c_str()) == 0)
{
std::string value = Trim(line.substr(pos + 1));
return value;
}
}
return std::string();
}
int GetFileKeyValueInt(const std::vector<std::string>& lines, const std::string& key, int defaultValue)
{
std::string value = GetFileKeyValue(lines, key);
if (value.empty())
{
return defaultValue;
}
return std::stoi(value, 0, 0);
}
std::vector<std::string> Tokennize(const std::string& str)
{
const char* delimiters = ",;";
std::vector<std::string> tokens;
std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
std::string::size_type pos = str.find_first_of(delimiters, lastPos);
while (std::string::npos != pos || std::string::npos != lastPos)
{
std::string value = Trim(str.substr(lastPos, pos - lastPos));
tokens.push_back(value);
lastPos = str.find_first_not_of(delimiters, pos);
pos = str.find_first_of(delimiters, lastPos);
}
return tokens;
}
std::vector<std::vector<std::string>> GetFileListValues(const std::vector<std::string>& lines)
{
std::vector<std::vector<std::string>> values;
for (const std::string& line : lines)
{
values.push_back(Tokennize(line));
}
return values;
}
std::vector<int> GetFileListIntValues(const std::vector<std::string>& lines)
{
std::vector<int> values;
for (const std::string& line : lines)
{
int value = std::stoi(Trim(line));
values.push_back(value);
}
return values;
}
std::vector<unsigned short> GetFileListUnsignedShortValues(const std::vector<std::string>& lines)
{
std::vector<unsigned short> values;
for (const std::string& line : lines)
{
unsigned short value = (unsigned short)std::stoi(Trim(line));
values.push_back(value);
}
return values;
}