-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiniLoader.cpp
More file actions
87 lines (80 loc) · 2.51 KB
/
iniLoader.cpp
File metadata and controls
87 lines (80 loc) · 2.51 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
#include "iniLoader.h"
void iniLoader::loadIni(INI_STRUCT* ini, std::string path) {
std::map<std::string, std::map<std::string, std::string>> section_keyVaule;
std::ifstream file(path);
std::string line;
std::string contextSection;
if (file.is_open()) {
while (std::getline(file, line)) {
line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
if (line[0] == '[') {
std::string section = line.substr(1, line.length() - 2);
std::map<std::string, std::string> key_value;
section_keyVaule.insert(std::make_pair(section, key_value));
contextSection = section;
}
if (line.find("=") != std::string::npos) {
std::string key, value;
key = line.substr(0, line.find("="));
value = line.substr(line.find("=") + 1, line.length() - line.find("=") - 1);
auto it = section_keyVaule.find(contextSection);
it->second.insert(std::make_pair(key, value));
}
}
file.close();
}
*ini = section_keyVaule;
}
void iniLoader::editKey(INI_STRUCT* ini, std::string section, std::string key, std::string value)
{
if ((*ini).find(section) == (*ini).end()) {
std::map<std::string, std::string> key_value;
(*ini).insert(std::make_pair(section, key_value));
}
else {
if ((*ini).find(section)->second.find(key) == (*ini).find(section)->second.end()) {
(*ini).find(section)->second.insert(std::make_pair(key, value));
}
else {
(*ini).find(section)->second.find(key)->second = value;
}
}
}
void iniLoader::deleteKey(INI_STRUCT* ini, std::string section, std::string key)
{
auto it_section = (*ini).find(section);
if (it_section != (*ini).end()) {
auto it_key = it_section->second.find(key);
if (it_key != it_section->second.end()) {
it_section->second.erase(key);
if (it_section->second.empty() == true) {
(*ini).erase(section);
}
}
}
}
std::string iniLoader::readKey(INI_STRUCT ini, std::string section, std::string key)
{
auto it_section = ini.find(section);
if (it_section != ini.end()) {
auto it_key = it_section->second.find(key);
if (it_key != it_section->second.end()) {
return it_key->second;
}
}
return std::string();
}
void iniLoader::writeIni(INI_STRUCT ini, std::string path)
{
std::ofstream outfile(path);
std::string iniContent = std::string();
for (const auto& section_keyValue : ini) {
iniContent += "[" + section_keyValue.first + "]" + "\n";
for (const auto& key_value : section_keyValue.second) {
iniContent += key_value.first + "=" + key_value.second + "\n";
}
iniContent += "\n";
}
outfile << iniContent;
outfile.close();
}