forked from crosire/reshade
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathini_file.cpp
170 lines (136 loc) · 3.01 KB
/
ini_file.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
170
/**
* Copyright (C) 2014 Patrick Mours. All rights reserved.
* License: https://github.com/crosire/reshade#license
*/
#include "ini_file.hpp"
#include <fstream>
namespace reshade
{
static inline void trim(std::string &str, const char *chars = " \t")
{
str.erase(0, str.find_first_not_of(chars));
str.erase(str.find_last_not_of(chars) + 1);
}
static inline std::string trim(const std::string &str, const char *chars = " \t")
{
std::string res(str);
trim(res, chars);
return res;
}
ini_file::ini_file(const filesystem::path &path) : _path(path)
{
load();
}
ini_file::~ini_file()
{
save();
}
void ini_file::load()
{
std::string line, section;
std::ifstream file(_path.wstring());
while (std::getline(file, line))
{
trim(line);
if (line.empty() || line[0] == ';' || line[0] == '/')
{
continue;
}
// Read section name
if (line[0] == '[')
{
section = trim(line.substr(0, line.find(']')), " \t[]");
continue;
}
// Read section content
const auto assign_index = line.find('=');
if (assign_index != std::string::npos)
{
const auto key = trim(line.substr(0, assign_index));
const auto value = trim(line.substr(assign_index + 1));
std::vector<std::string> value_splitted;
for (size_t i = 0, len = value.size(), found; i < len; i = found + 1)
{
found = value.find_first_of(',', i);
if (found == std::string::npos)
found = len;
value_splitted.push_back(value.substr(i, found - i));
}
_sections[section][key] = value_splitted;
}
else
{
_sections[section][line] = 0;
}
}
}
void ini_file::save() const
{
if (!_modified)
{
return;
}
std::ofstream file(_path.wstring());
const auto it = _sections.find("");
if (it != _sections.end())
{
for (const auto §ion_line : it->second)
{
file << section_line.first << '=';
size_t i = 0;
for (const auto &item : section_line.second.data())
{
if (i++ != 0)
{
file << ',';
}
file << item;
}
file << std::endl;
}
file << std::endl;
}
for (const auto §ion : _sections)
{
if (section.first.empty())
{
continue;
}
file << '[' << section.first << ']' << std::endl;
for (const auto §ion_line : section.second)
{
file << section_line.first << '=';
size_t i = 0;
for (const auto &item : section_line.second.data())
{
if (i++ != 0)
{
file << ',';
}
file << item;
}
file << std::endl;
}
file << std::endl;
}
}
variant ini_file::get(const std::string §ion, const std::string &key, const variant &default) const
{
const auto it1 = _sections.find(section);
if (it1 == _sections.end())
{
return default;
}
const auto it2 = it1->second.find(key);
if (it2 == it1->second.end())
{
return default;
}
return it2->second;
}
void ini_file::set(const std::string §ion, const std::string &key, const variant &value)
{
_modified = true;
_sections[section][key] = value;
}
}