-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiniFile.cpp
More file actions
271 lines (240 loc) · 7.27 KB
/
Copy pathiniFile.cpp
File metadata and controls
271 lines (240 loc) · 7.27 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
#include "IniFile.h"
#include <string>
#include <vector>
#include <map>
#include <fstream>
#include <iostream>
#include <cctype>
std::string del(const std::string& str){
std::string result;
for (char c : str){
if (c != ' '){
result += c;
}
}
return result;
}
IniFile::IniFile(const std::string &path) : filePath(path){
std::ifstream file(path);
if (!file.is_open()){
throw "Error";
}
std::string line;
std::string sec;
std::string key;
std::string value;
while (getline(file, line)){
line = del(line);
int rav = 0;
int countr = 0;
int countl = 0;
// удаление коментария
if (line.find(";") != std::string::npos){
line.erase(line.find(";"), line.length());
}
// обработка исключений
if (line.find(']') < line.find('[')) {
line.erase(0, line.length());
}
if ((line[0] == '=') || (line[line.length() - 1] == '=')) {
line.erase(0, line.length());
}
for (int i = 0; i < line.length(); ++i){
if (line[i] == '='){
rav++;
}
if (line[i] == '['){
countr++;
}
if (line[i] == ']'){
countl++;
}
}
if (rav > 1 || countr > 1 || countl > 1){
line.erase(0, line.length());
}
if (line.empty()){
continue;
}
//
// обработка ключа и значения
if (line.find("=") != std::string::npos){
key = line.substr(0, line.find("="));
value = line.substr(line.find("=") + 1);
if (!key.empty() && !value.empty()){
data[sec][key] = value;
}
}
// обработка секций
if (line.find("[") != std::string::npos && line.find("]") != std::string::npos){
sec = line.substr(line.find("[") + 1, line.find("]") - line.find("[") - 1);
if (!sec.empty()){
if (data.find(sec) != data.end()){
for (const auto& entry : data[sec]){
data[sec][entry.first] = entry.second;
}
}
else{
data[sec] = KeysMap();
}
}
}
}
}
IniFile::~IniFile(){
std::ofstream file(filePath);
for (auto &sec : data){
std::ofstream file(filePath, std::ios::app);
file << "[" << sec.first << "]" << std::endl;
for (auto &key : sec.second){
file << key.first << "=" << key.second << std::endl;
}
file.close();
}
}
void IniFile::save() {
std::ofstream file(filePath);
if (file.is_open()){
// цикл по секциям
for (auto &sec : data){
std::ofstream file(filePath, std::ios::app);
//записывается секция в конец файла
file << "[" << sec.first << "]" << std::endl;
//цикл по ключам в секции
for (auto &key: sec.second){
//хаписывается ключ и значение в конец файла
file << key.first << "=" << key.second << std::endl;
}
file.close();
}
}
else
throw "Error";
}
int IniFile::readInt(const std::string& section, const std::string& key, int def) {
// если секция не найдена вернем значение по умолчанию
if (data.find(section) == data.end()){
return def;
}
// если ключ не найден в секции вернем значение по умолчанию
if (data[section].find(key) == data[section].end()){
return def;
}
// если секция и ключ найдены вернем значение ключа
return std::stoi(data[section][key]);
}
double IniFile::readDouble(const std::string& section, const std::string& key, double def){
if (data.find(section) == data.end()){
return def;
}
if (data[section].find(key) == data[section].end()){
return def;
}
return std::stod(data[section][key]);
}
std::string IniFile::readString(const std::string& section, const std::string &key, const std::string& def){
if (data.find(section) == data.end()){
return def;
}
if (data[section].find(key) == data[section].end()){
return def;
}
return data[section][key];
}
bool IniFile::readBool(const std::string& section, const std::string& key, bool def){
if (data.find(section) == data.end()){
return def;
}
if (data[section].find(key) == data[section].end()){
return def;
}
// переводим значение ключа в нижний регистр
std::string value = data[section][key];
for (char &c : value) {
c = std::tolower(c);
}
bool isTrueValue = false;
// проходмся по всем значениям возможного true
for (const std::string& trueVal : trueValues) {
// если значение совпадает с одним из true значений
if (value == trueVal) {
isTrueValue = true;
break;
}
}
return isTrueValue;
}
void IniFile::writeInt(const std::string& section, const std::string& key, int value){
//Проверка существет ли секция, если нет делаем новую
if (data.find(section) == data.end()){
data[section] = KeysMap();
}
// записываем строковое значение в ключ
data[section][key] = std::to_string(value);
}
void IniFile::writeDouble(const std::string& section, const std::string& key, double value){
if (data.find(section) == data.end()){
data[section] = KeysMap();
}
data[section][key] = std::to_string(value);
}
void IniFile::writeString(const std::string§ion, const std::string& key, const std::string& value){
if (data.find(section) == data.end()){
data[section] = KeysMap();
}
data[section][key] = value;
}
void IniFile::writeBool(const std::string§ion, const std::string& key, bool value){
if (data.find(section) == data.end()){
data[section] = KeysMap();
}
if (value){
// устанавливается значение в true для данной секции и ключа
data[section][key] = "true";
}
else{
data[section][key] = "false";
}
}
bool IniFile::isSectionExist(const std::string& section) {
return data.find(section) != data.end();
}
bool IniFile::isKeysExist(const std::string& section, const std::string& key) {
if (data.find(section) != data.end()) {
return data[section].find(key) != data[section].end();
}
return false;
}
size_t IniFile::getSectionsCount() {
return data.size();
}
size_t IniFile::getKeysCount(const std::string& section){
return data[section].size();
}
SectionsMap IniFile::getSections() const{
return data;
}
bool IniFile::deleteSection(const std::string& section){
if (data.find(section) != data.end()){
data.erase(data.find(section));
return true;
}
return false;
}
bool IniFile::deleteKey(const std::string& section, const std::string& key){
if (data.find(section) != data.end()){
auto& KeysMap = data.find(section)->second;
if (KeysMap.find(key) != KeysMap.end()){
KeysMap.erase(key);
return true;
}
}
return false;
}
bool IniFile::addNewSection(const std::string& section){
if (data.find(section) == data.end()){
data[section] = KeysMap();
return true;
}
return false;
}