-
Notifications
You must be signed in to change notification settings - Fork 4
/
iniedit.cpp
260 lines (223 loc) · 6.57 KB
/
iniedit.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
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
#include <errno.h>
#include "iniedit.h"
extern "C" {
#define INI_ALLOW_MULTILINE 0
#define NOTIFY_SECTION_CHANGE 1
#include "inih/ini.h"
}
#include <QString>
#include <QDir>
#include <QTextStream>
QTextStream &qStdout();
Section::Section(IniEditor *pie): pie(pie)
{
ilineLast = pie->ll.end();
}
// readline callback for inih, turn a C callback into a C++ method call
char *IniEditor::inihReader(char *szBuffer, int cbBuffer, void *user)
{
IniEditor *pie = (IniEditor *)user;
return pie->parserReader(szBuffer, cbBuffer);
}
// parser callback for inih, turn a C callback into a C++ method call
int IniEditor::inihHandler(void *user, const char *szSection, const char *szName, const char *szValue)
{
IniEditor *pie = (IniEditor *)user;
return pie->parserHandler(szSection, szName, szValue);
}
char *IniEditor::parserReader(char *szBuffer, int cbBuffer)
{
if (file.atEnd())
return NULL;
qint64 cb = file.readLine(szLineParsing = szBuffer, cbBuffer);
if (cb <= 0 || cb > cbBuffer)
return NULL;
if (szLineParsing[cb - 1] == '\n')
szLineParsing[cb - 1] = 0;
ll.push_back(Line(false, ll.size(), QString(), QString(), szLineParsing));
return szBuffer;
}
int IniEditor::parserHandler(const char *szSection, const char *szName, const char *szValue)
{
QString sSection(szSection);
LineIterator iline = ll.end();
iline--;
if (sSectionParsing != sSection) {
sSectionParsing = sSection;
si[sSectionParsing].setParent(this);
si[sSectionParsing].ilineLast = iline;
}
if (szName[0]) {
iline->fHasProperty = true;
QString s(szName);
iline->sName = s;
iline->sValue = szValue;
if (parserCallback != NULL)
parserCallback(user, sSectionParsing, *iline);
si[sSectionParsing].insert(iline->sName, iline);
}
return true;
}
void IniEditor::clear()
{
si.clear();
fe = QFileDevice::NoError;
}
void IniEditor::setFilename(const QString &sPathname)
{
file.setFileName(sPathname);
}
bool IniEditor::read(ParserCallback pc = NULL, void *user = NULL)
{
clear();
szLineParsing = NULL;
ilineParsing = 0;
sSectionParsing = ""; // fake section for front matter before first section
si[sSectionParsing].setParent(this);
parserCallback = pc;
this->user = user;
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
fe = file.error();
return false;
}
int iline = ini_parse_stream(inihReader, this, inihHandler, this);
if (iline < 0) {
return false;
}
// ignore parsing errors, allow editing of the rest, is that correct?
file.close();
return true;
}
bool IniEditor::write()
{
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return false;
QTextStream ts(&file);
for (ConstLineIterator iline = ll.cbegin(); iline != ll.cend(); iline++) {
if (!iline->fDeleted)
ts << iline->sLine << "\n";
}
file.close();
return true;
}
bool IniEditor::copyTo(QDir dir)
{
QFileInfo fi(file.fileName());
return file.copy(dir.absoluteFilePath(fi.fileName()));
}
void Section::dump() const
{
for (LineIndex::const_iterator il = li.constBegin(); il != li.constEnd(); il++) {
qStdout() << il.value()->sName << "=" << il.value()->sValue << Qt::endl;
}
}
void IniEditor::dump() const
{
for (SectionIndex::const_iterator is = si.constBegin(); is != si.constEnd(); is++) {
qStdout() << "[" << is.key() << "]" << Qt::endl;
is.value().dump();
}
for (ConstLineIterator iline = ll.cbegin(); iline != ll.cbegin(); iline++ ) {
qStdout() << iline->sLine;
}
qStdout() << Qt::endl;
}
Section &IniEditor::section(QString s)
{
Section § = si[s];
if (sect.sName.isEmpty()) {
sect.setName(s);
sect.setParent(this);
}
return sect;
}
void Section::setParent(IniEditor *pie)
{
if (pie != this->pie) {
this->pie = pie;
ilineLast = pie->ll.end();
}
}
void Section::insert(const QString &sPropertyName, LineIterator iline)
{
li.insert(sPropertyName, iline);
ilineLast = iline;
}
const QString Section::value(const QString &sPropertyName, const QString &sDefault) const
{
LineIndex::const_iterator il = li.constFind(sPropertyName);
return (il == li.constEnd()) ? sDefault : il.value()->sValue;
}
const QString Section::value(const QString &sPropertyName) const
{
return value(sPropertyName, QString());
}
int Section::intValue(const QString &sPropertyName, int iDefault) const
{
QString s = value(sPropertyName);
return s.isEmpty() ? iDefault : s.toInt();
}
double Section::value(const QString &sPropertyName, double dDefault) const
{
QString s = value(sPropertyName);
return s.isEmpty() ? dDefault : s.toDouble();
}
void Section::setValue(const QString &sPropertyName, const QString &s)
{
QString sComment = "";
LineIterator iline;
LineIndex::iterator il = li.find(sPropertyName);
if (il != li.end()) {
iline = il.value();
// preserve the comment
int ich = iline->sLine.indexOf(';');
if (ich > 0) {
while (ich > 0 && iline->sLine[ich].isSpace())
ich--;
sComment = iline->sLine.mid(ich);
}
// set the new line content
iline->sLine = QString("%1=%2%3").arg(iline->sName).arg(s).arg(sComment);
iline->sValue = s;
}
else {
iline = ilineLast;
if (iline == pie->ll.end())
pie->ll.push_back(Line(false, -1, "", "", QString("[%1]").arg(sName)));
else
iline++;
iline = pie->ll.insert(iline, Line(true, -1, sPropertyName, s,
QString("%1=%2").arg(sPropertyName).arg(s)));
insert(sPropertyName, iline);
}
}
void Section::remove(const QString &sPropertyName)
{
LineIndex::iterator il = li.find(sPropertyName);
if (il != li.end()) {
LineIterator iline = il.value();
li.erase(il);
iline->fDeleted = true;
}
}
void Section::setValue(const QString &sPropertyName, const QString &s, const QString &sDefault)
{
if (s == sDefault)
remove(sPropertyName);
else
setValue(sPropertyName, s);
}
void Section::setValue(const QString &sPropertyName, int i, int iDefault)
{
if (i == iDefault)
remove(sPropertyName);
else
setValue(sPropertyName, QString::number(i));
}
void Section::setValue(const QString &sPropertyName, double d, double dDefault, int cdecimals)
{
if (d == dDefault)
remove(sPropertyName);
else
setValue(sPropertyName, QString::number(d, 'f', cdecimals));
}