-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfigfile.cpp
330 lines (291 loc) · 7.64 KB
/
configfile.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/***************************************************************************
configfile.cpp - description
----------------------------------------------------
begin : 2003
copyright : (C) 2007 by Andreas Lindenau
email : [email protected]
configfile.cpp - description
----------------------------------------------------
begin : 2013
copyright : (C) 2014 by Andrey Sobol
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "configfile.h"
//#define ldebug
ConfigFile::ConfigFile(){
sl.clear(); // Liste vorsichthalber noch loeschen
speichern=false;
}
ConfigFile::~ConfigFile(){
close();
}
void ConfigFile::open( const QString &name ){
QString qs;
bool binsert = true;
QFile *f1;
//qDebug("open");
//qDebug()<<sl.count();
fname = name;
f1 = new QFile(fname);
if(f1->open(QIODevice::ReadOnly)){
QTextStream ts( f1);
while(!ts.atEnd()){
qs = ts.readLine();
if(qs.isEmpty())binsert = false;
if(!qs.contains("="))binsert = false;
if(binsert)sl.append(qs);
//qDebug()<<sl.count();
}
f1->close();
delete(f1);
sl.sort();
//qDebug()<<sl.count();
}
}
void ConfigFile::close(){
QString qs;
QStringList::Iterator it;
QFile *f1;
//qDebug("close");
//qDebug()<<sl.count();
if(speichern){
sl.sort();
f1 = new QFile(fname);
if(f1->open(QIODevice::WriteOnly)){
QTextStream ts(f1);
for(it = sl.begin() ; it != sl.end(); it++){
qs = (*it).toLocal8Bit().constData();
//qDebug(qPrintable(qs));
ts << qs << endl;
}
f1->close();
delete(f1);
}
}
speichern = false;
}
bool ConfigFile::clearItem(const QString &name){
QString qs1;
QStringList::Iterator it;
//qDebug("clearItem");
//qDebug()<<sl.count();
speichern = true;
for(it = sl.begin() ; it < sl.end(); it++){
qs1 = (*it).toLocal8Bit().constData();
//qDebug(qPrintable(qs1));
if(qs1.contains(name)){
//qDebug()<<sl.count();
sl.removeOne(qs1);
//qDebug()<<sl.count();
return true;
}
}
return false;
}
double ConfigFile::readDouble(const QString &name, double def){
QString qs1;
QStringList::Iterator it;
//qDebug("readDouble");
//qDebug()<<sl.count();
for(it = sl.begin() ; it < sl.end(); it++){
qs1 = (*it).toLocal8Bit().constData();
if(qs1.indexOf(name+"=",0)==0){
//qDebug(name);
//qDebug(qs1);
qs1.remove(0, name.length()+1);
return qs1.toDouble();
}
}
return def;
}
int ConfigFile::readInteger(const QString &name, int def){
QString qs1;
QStringList::Iterator it;
//qDebug("readInteger");
//qDebug()<<sl.count();
for(it = sl.begin() ; it < sl.end(); it++){
qs1 = (*it).toLocal8Bit().constData();
if(qs1.indexOf(name+"=",0)==0){
qs1.remove(0, name.length()+1);
return qs1.toInt();
}
}
return def;
}
long ConfigFile::readLong(const QString &name, long def){
QString qs1;
QStringList::Iterator it;
//qDebug("readLong");
//qDebug()<<sl.count();
for(it = sl.begin() ; it < sl.end(); it++){
qs1 = (*it).toLocal8Bit().constData();
if(qs1.indexOf(name+"=",0)==0){
qs1.remove(0, name.length()+1);
return qs1.toLong();
}
}
return def;
}
qlonglong ConfigFile::readLongLong(const QString &name, qlonglong def){
QString qs1;
QStringList::Iterator it;
//qDebug("readLong");
//qDebug()<<sl.count();
for(it = sl.begin() ; it < sl.end(); it++){
qs1 = (*it).toLocal8Bit().constData();
if(qs1.indexOf(name+"=",0)==0){
qs1.remove(0, name.length()+1);
return qs1.toLongLong();
}
}
return def;
}
QString ConfigFile::readString(const QString &name, const QString &def){
QString qs1;
QStringList::Iterator it;
//qDebug("readString");
//qDebug()<<sl.count();
for(it = sl.begin() ; it < sl.end(); it++){
qs1 = (*it).toLocal8Bit().constData();
if(qs1.indexOf(name+"=",0)==0){
qs1.remove(0, name.length()+1);
return qs1;
}
}
return def;
}
bool ConfigFile::readBool(const QString &name, bool def){
QString qs1;
QStringList::Iterator it;
//qDebug("readBool");
//qDebug()<<sl.count();
for(it = sl.begin() ; it < sl.end(); it++){
qs1 = (*it).toLocal8Bit().constData();
if(qs1.indexOf(name+"=",0)==0){
qs1.remove(0, name.length()+1);
return bool(qs1.toInt());
}
}
return def;
}
void ConfigFile::writeDouble(const QString &name, double wert){
QString qs1, qs2;
QStringList::Iterator it;
//qDebug("writeDouble");
//qDebug()<<sl.count();
speichern=true;
for(it = sl.begin() ; it < sl.end(); it++){
qs1 = (*it).toLocal8Bit().constData();
if(qs1.indexOf(name+"=",0)==0){
sl.erase(it);
break;
}
}
qs1 = name + "=";
qs2.setNum(wert,'g',10);
qs1.append(qs2);
sl.append(qs1);
return;
}
void ConfigFile::writeInteger(const QString &name, int wert){
QString qs1, qs2;
QStringList::Iterator it;
//qDebug("writeInteger");
//qDebug()<<sl.count();
speichern=true;
for(it = sl.begin() ; it < sl.end(); it++){
qs1 = (*it).toLocal8Bit().constData();
if(qs1.indexOf(name+"=",0)==0){
sl.erase(it);
break;
}
}
qs1 = name + "=";
qs2.setNum(wert);
qs1.append(qs2);
sl.append(qs1);
return;
}
void ConfigFile::writeLong(const QString &name, long wert){
QString qs1, qs2;
QStringList::Iterator it;
//qDebug("writeLong");
//qDebug()<<sl.count();
speichern=true;
for(it = sl.begin() ; it < sl.end(); it++){
qs1 = (*it).toLocal8Bit().constData();
if(qs1.indexOf(name+"=",0)==0){
sl.erase(it);
break;
}
}
qs1 = name + "=";
qs2.setNum(wert);
qs1.append(qs2);
sl.append(qs1);
return;
}
void ConfigFile::writeLongLong(const QString &name, qlonglong wert){
QString qs1, qs2;
QStringList::Iterator it;
//qDebug("writeLongLong");
//qDebug()<<sl.count();
speichern=true;
for(it = sl.begin() ; it < sl.end(); it++){
qs1 = (*it).toLocal8Bit().constData();
if(qs1.indexOf(name+"=",0)==0){
sl.erase(it);
break;
}
}
qs1 = name + "=";
qs2.setNum(wert);
//qDebug()<<qs2;
qs1.append(qs2);
sl.append(qs1);
return;
}
void ConfigFile::writeString(const QString &name, const QString &wert){
QString qs1;
QStringList::Iterator it;
//qDebug("writeString");
//qDebug()<<sl.count();
speichern=true;
for(it = sl.begin() ; it < sl.end(); it++){
qs1 = (*it).toLocal8Bit().constData();
if(qs1.indexOf(name+"=",0)==0){
sl.erase(it);
break;
}
}
qs1 = (name + "=" + wert);
sl.append(qs1);
return;
}
void ConfigFile::writeBool(const QString &name, bool wert){
QString qs1, qs2;
QStringList::Iterator it;
//qDebug("writeBool");
//qDebug()<<sl.count();
speichern=true;
for(it = sl.begin() ; it < sl.end(); it++){
qs1 = (*it).toLocal8Bit().constData();
if(qs1.indexOf(name+"=",0)==0){
sl.erase(it);
break;
}
}
qs1 = name + "=";
qs2.setNum(wert);
qs1.append(qs2);
sl.append(qs1);
return;
}