-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathini.cpp
More file actions
168 lines (125 loc) · 3.82 KB
/
ini.cpp
File metadata and controls
168 lines (125 loc) · 3.82 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
#include "ini.h"
#include "trim.h"
#include <iostream>
#include <fstream>
#include <sstream>
void ini::read(const std::string& fileName)
{
m_fileName = fileName;
std::ifstream stream(fileName, std::ios::binary);
if (!stream)
throw std::runtime_error("Couldn't open file: " + fileName);
std::string line;
size_t lineNumber = 0;
std::string currentSection;
while (std::getline(stream, line))
{
lineNumber++;
std::string trimmed = trim(line);
if (trimmed.empty())
continue;
// comment
if (trimmed[0] == ';' || trimmed[0] == '#' || trimmed[0] == '/')
continue;
if (trimmed[0] == '[')
{
const size_t closeBracket = trimmed.find(']');
if (closeBracket == std::string::npos)
throw std::runtime_error("Couldn't parse file");
currentSection = trimmed.substr(1, closeBracket - 1);
}
else
{
const size_t equal = trimmed.find_first_of('=');
if (equal == std::string::npos)
{
continue;
// throw std::runtime_error("Couldn't parse file: " + fileName + " line : " + std::to_string(lineNumber));
}
const std::string key = rtrim(trimmed.substr(0, equal));
std::string value = ltrim(trimmed.substr(equal + 1));
const size_t comment = value.find(';');
if (comment != std::string::npos)
value = rtrim(value.substr(0, comment));
m_sections[currentSection].insert(std::pair(key, value));
}
}
stream.close();
}
void ini::dump(std::ostream& stream) const
{
stream << "fileName: " << m_fileName << std::endl;
for (const auto& section : m_sections)
{
stream << "[" << section.first << "]" << std::endl;
for (const auto& entry : section.second)
stream << entry.first << " = " << entry.second << std::endl;
}
}
bool ini::dump(const std::string& fileName) const
{
std::ofstream of(fileName);
if (of)
{
dump(of);
of.close();
return true;
}
return false;
}
bool ini::hasValue(const std::string& section, const std::string& key) const
{
auto it = m_sections.find(section);
if (it != m_sections.end())
{
auto it2 = it->second.find(key);
if (it2 != it->second.end())
return true;
}
return false;
}
std::string ini::getValue(const std::string& section, const std::string& key) const
{
std::string value;
auto it = m_sections.find(section);
if (it != m_sections.end())
{
auto it2 = it->second.find(key);
if (it2 != it->second.end())
value = it2->second;
}
return value;
}
int ini::getIntValue(const std::string& section, const std::string& key) const
{
return std::stoi(getValue(section, key));
}
float ini::getFloatValue(const std::string& section, const std::string& key) const
{
return std::stof(getValue(section, key));
}
float ini::getFloatValue(const std::string& section, const std::string& key, const float value) const
{
auto it = m_sections.find(section);
if (it != m_sections.end())
{
auto it2 = it->second.find(key);
if (it2 != it->second.end())
return std::stof(it2->second);
}
return value;
}
std::array<float, 3> ini::getFloatArray3Value(const std::string& section, const std::string& key) const
{
std::array<float, 3> vec3{ 0, 0, 0 };
std::istringstream ss(getValue(section, key));
std::string number;
size_t index = 0;
while (index < 3 && std::getline(ss, number, ','))
vec3[index++] = std::stof(number);
return vec3;
}
bool ini::hasSection(const std::string& section) const
{
return m_sections.find(section) != m_sections.end();
}