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
|
// Copyright (C) 2003 Dolphin Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
// see IniFile.h
#include "IniFile.h"
template <typename S>
void StripChars(std::string& str, const S space)
{
const size_t start = str.find_first_not_of(space);
if (str.npos == start)
str.clear();
else
str = str.substr(start, str.find_last_not_of(space) - start + 1);
}
bool Section::Get(const std::string& key, std::string* const val, const std::string& def) const
{
const const_iterator f = find(key);
if (f != end())
{
*val = f->second;
return true;
}
if (false == def.empty())
*val = def;
return false;
}
void Section::Set(const std::string& key, const std::string& val, const std::string& def)
{
if (val != def)
operator[](key) = val;
else
{
iterator f = find(key);
if (f != end())
erase(f);
}
}
bool IniFile::Save(const std::string& filename) const
{
return Save(filename.c_str());
}
bool IniFile::Save(const char filename[]) const
{
std::ofstream file;
file.open(filename);
if (file.is_open())
{
Save(file);
file.close();
return true;
}
else
return false;
}
void IniFile::Save(std::ostream& file) const
{
const_iterator
si = begin(),
se = end();
for ( ; si != se; ++si )
{
// skip a line at new sections
file << "\n[" << si->first << "]\n";
si->second.Save(file);
}
}
void Section::Save(std::ostream& file) const
{
if (m_use_lines) // this is used when GetLines or SetLines has been called
{
std::vector<std::string>::const_iterator
i = m_lines.begin(),
e = m_lines.end();
for ( ; i!=e; ++i)
file << *i << '\n';
}
else
{
Section::const_iterator
vi = begin(),
ve = end();
for ( ; vi!=ve; ++vi)
{
file << vi->first << " = ";
// if value has quotes or whitespace, surround it with quotes
if (vi->second.find_first_of("\"\t ") != std::string::npos)
file << '"' << vi->second << '"';
else
file << vi->second;
file << '\n';
}
}
}
bool IniFile::Load(const std::string& filename)
{
return Load(filename.c_str());
}
bool IniFile::Load(const char filename[])
{
std::ifstream file;
file.open(filename);
if (file.is_open())
{
Load(file);
file.close();
return true;
}
else
return false;
}
void IniFile::Load(std::istream& file)
{
std::vector<std::string> lines;
Section sectmp;
Section* section = §mp;
std::string line;
while (std::getline(file, line)) // read a line
{
if (line.size())
{
switch (line[0])
{
// section
case '[' :
section->m_lines = lines;
// kinda odd trimming
StripChars(line, "][\t\r ");
section = &(*this)[line];
lines.clear();
break;
// key/value
default :
{
std::istringstream ss(line);
std::string key; std::getline(ss, key, '=');
std::string val; std::getline(ss, val);
StripChars(val, "\t\r ");
// handle quote surrounded values
if (val.length() > 1)
if ('"' == val[0])
val.assign(val.begin()+1, val.end()-1);
StripChars(key, "\t\r ");
(*section)[key] = val;
}
//break; // no break
// comment
case '#' :
case ';' :
lines.push_back(line);
break;
}
}
}
//Clean();
}
//
// IniFile :: Clean
//
// remove empty key/values and sections
// after trying to access ini sections/values with the [] operator, they are automatically allocated
// this deletes the empty stuff
//
void IniFile::Clean()
{
iterator
i = begin(),
e = end();
for ( ; i != e; )
{
Section::iterator
si = i->second.begin(),
se = i->second.end();
for ( ; si != se; )
{
if (si->second.empty())
i->second.erase( si++ );
else
++si;
}
if (i->second.empty() && i->second.m_lines.empty())
erase( i++ );
else
++i;
}
}
bool IniFile::Exists(const std::string& section) const
{
return find(section) != end();
}
void IniFile::Delete(const std::string& section)
{
const iterator f = find(section);
if (end() != f)
erase(f);
}
bool Section::Exists(const std::string& key) const
{
return find(key) != end();
}
void Section::Delete(const std::string& key)
{
const iterator f = find(key);
if (end() != f)
erase(f);
}
void Section::SetLines(const std::vector<std::string>& lines)
{
m_lines = lines;
m_use_lines = true;
}
void Section::GetLines(std::vector<std::string>& lines)
{
lines = m_lines;
m_use_lines = true;
}
|