-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCIniFile.cpp
More file actions
438 lines (380 loc) · 13 KB
/
CIniFile.cpp
File metadata and controls
438 lines (380 loc) · 13 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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//
// Created by samuel on 05/07/19.
//
#include <fstream>
#include <exception>
#include "CIniFile.h"
bool CIniFile::ReadFile()
{
fstream f;
string line;
string sectionName, valueName, value;
string::size_type pLeft, pRight;
int sectionID = 0;
int valueID = 0;
int count = 1;
//Apro il file
f.open(getPath().c_str(), ios::in);
if(f.fail()){
return false;
}
while(getline(f, line)) {
if (line.length()) {
//Check della stringa se esiste oppure no
if ((pLeft = line.find_first_of(";#[=")) != string::npos)
{
switch (line[pLeft]) {
case '[':
if ((pRight = line.find_last_of(']')) != string::npos && pRight > pLeft) {
sectionName = line.substr(pLeft + 1, pRight - pLeft - 1);
AddSection(sectionName);
sectionID = FindSection(sectionName);
}
break;
case '=':
valueName = line.substr(0, pLeft);
value = line.substr(pLeft + 1);
valueID = FindValue(sectionID,valueName);
//Se una chiave ha un valueID diverso da -1 al momento della lettura iniziale del file allora
//vuol dire che esiste già una chiave con lo stesso nome e quindi riscrivo il nome della
//chiave stessa in modo che non possa sovrascrivere l'altro valore già presente nel file.
if(valueID != -1){
count++;
valueName.append(std::to_string(count));
}
SetValue(sectionName, valueName, value);
break;
//Ci possono essere due tipologie di commenti che iniziano con # o ;
case ';':
case '#':
if (section.empty())
NewHeaderComment(line.substr(pLeft + 1));
else
AddKeyCommentInSection(sectionName, line.substr(pLeft + 1));
break;
}
}
}
}
f.close();
return !section.empty();
}
bool CIniFile::WriteFile()
{
int commentID, sectionID, valueID;
fstream f;
f.open(getPath().c_str(), ios::out);
if (f.fail())
return false;
// Scrive l'intestazione di commenti
for (commentID = 0; commentID < comments.size(); commentID++)
f << ';' << comments[commentID] << endl;
if (!comments.empty())
f << endl;
//Scrive su file le sezioni contenenti le varie chiavi di valori
for (sectionID = 0; sectionID < keys.size(); sectionID++) {
f << '[' << section[sectionID] << ']' << endl;
// Commenti
for (commentID = 0; commentID < keys[sectionID].comment.size(); commentID++)
f << ';' << keys[sectionID].comment[commentID] << endl;
// Valori
for (valueID = 0; valueID < keys[sectionID].names.size(); valueID++)
f << keys[sectionID].names[valueID] << '=' << keys[sectionID].value[valueID] << endl;
f << endl;
}
f.close();
return true;
}
void CIniFile::ChangeFileName(const string& putString, const string& putKeys)
{
string newFileName = putString + "." + putKeys;
setFileName(newFileName);
cout << "Nome del file: " << getFileName() << endl;
}
string CIniFile::ChangePath(const string& putPath)
{
setPath(putPath + "/" + getFileName());
cout<<getPath()<<endl;
cout<<"\nPath cambiata correttamente!"<<endl;
return putPath;
}
void CIniFile::RenameFileName(const string& putString, const string& putKeys)
{
string newFileName = putString + "." + putKeys;
size_t count = getPath().find_last_of('/');
string initPath = getPath().substr(0,count);
string newPath = initPath + "/" + newFileName;
string oldPath = initPath + "/" + getFileName();
rename(oldPath.c_str(),newPath.c_str());
setFileName(newFileName);
setPath(newPath);
cout << "Nome del file: " << getFileName() << endl;
}
int CIniFile::FindSection(string const §ionName) const
{
for(int sectionID = 0; sectionID < section.size(); sectionID++)
if (section[sectionID] == sectionName)
return sectionID;
return noID;
}
int CIniFile::AddSection(string const §ionName)
{
section.push_back(sectionName);
keys.resize(keys.size() + 1);
return section.size() - 1; //Ritorna l'ultima posizione della chiave inserita
}
string CIniFile::GetSection(int const §ionID) const
{
if(sectionID < section.size())
return section[sectionID];
else
return "";
}
void CIniFile::GetValuesInSection(int const §ionID)
{
if(sectionID == noID)
cout<<"ID non trovato!"<<endl;
cout<<"\nSezione = " << GetSection(sectionID) << endl;
for (int valueID = 0; valueID < NumKeyValuesInSection(sectionID); valueID++)
cout<<"Nome Valore = "<<GetValueName(sectionID,valueID)<<", Valore = "<<GetValue<string>(sectionID,valueID)<<endl;
}
int CIniFile::NumKeyValuesInSection(int const §ionID)
{
if(sectionID < keys.size())
return keys[sectionID].names.size();
return 0;
}
int CIniFile::NumKeyValuesInSection(string const §ionName)
{
int sectionID = FindSection(sectionName);
if(sectionID == noID)
return 0;
return keys[sectionID].names.size();
}
bool CIniFile::DeleteSection(string const& sectionName)
{
int sectionID = FindSection(sectionName);
if(sectionID == noID)
return false;
auto sectionPos = section.begin() + sectionID;
auto keyPos = keys.begin() + sectionID;
section.erase(sectionPos, sectionPos + 1);
keys.erase(keyPos, keyPos + 1);
return true;
}
int CIniFile::FindValue(int const §ionID, string const &valueName) const
{
if(keys.empty() || sectionID >= keys.size())
return noID;
for(int valueID = 0; valueID < keys[sectionID].names.size(); valueID++)
if(keys[sectionID].names[valueID] == valueName)
return valueID;
return noID;
}
string CIniFile::GetValueName(int const §ionID, int const &valueID) const
{
if(sectionID < keys.size() && valueID < keys[sectionID].names.size())
return keys[sectionID].names[valueID];
return "";
}
string CIniFile::GetValueName(string const §ionName, int const &valueID) const
{
int sectionID = FindSection(sectionName);
if(sectionID == noID)
return "ID non trovato!";
return GetValueName(sectionID, valueID);
}
int CIniFile::NumKeyCommentsInSection(int const §ionID) const
{
if(sectionID < keys.size())
return keys[sectionID].comment.size();
return 0;
}
int CIniFile::NumKeyCommentsInSection(string const §ionName) const
{
int sectionID = FindSection(sectionName);
if(sectionID == noID)
return 0;
return keys[sectionID].comment.size();
}
bool CIniFile::AddKeyCommentInSection(int const §ionID, string const &comment)
{
if(sectionID < keys.size()) {
keys[sectionID].comment.push_back(comment);
return true;
}
return false;
}
bool CIniFile::AddKeyCommentInSection(string const §ionName, string const &comment)
{
int sectionID = FindSection(sectionName);
if (sectionID == noID)
return false;
return AddKeyCommentInSection(sectionID, comment);
}
string CIniFile::GetKeyComment(int const §ionID, int const &commentID) const
{
if(sectionID < keys.size() && commentID < keys[sectionID].comment.size())
return keys[sectionID].comment[commentID];
return "";
}
string CIniFile::GetKeyComment(string const §ionName, int const &commentID) const
{
int sectionID = FindSection(sectionName);
if (sectionID == noID)
return "ID non trovato!";
return GetKeyComment(sectionID, commentID);
}
bool CIniFile::DeleteValueInSection(string const §ionName, string const &valueName)
{
int sectionID = FindSection(sectionName);
if(sectionID == noID)
return false;
int valueID = FindValue(sectionID, valueName);
if(valueID == noID)
return false;
auto namePos = keys[sectionID].names.begin() + valueID;
auto valuePos = keys[sectionID].value.begin() + valueID;
keys[sectionID].names.erase(namePos, namePos + 1);
keys[sectionID].value.erase(valuePos, valuePos + 1);
return true;
}
bool CIniFile::DeleteCommentInSection(int const §ionID, int const &commentID)
{
if(sectionID < keys.size() && commentID < keys[sectionID].comment.size()) {
auto commentPos = keys[sectionID].comment.begin() + commentID;
keys[sectionID].comment.erase(commentPos, commentPos + 1);
return true;
}
return false;
}
bool CIniFile::DeleteCommentInSection(string const §ionName, int const &commentID)
{
int sectionID = FindSection(sectionName);
if(sectionID == noID)
return false;
return DeleteCommentInSection(sectionID, commentID);
}
bool CIniFile::DeleteAllCommentsInSection(string const §ionName)
{
int sectionID = FindSection(sectionName);
if(sectionID == noID)
return false;
return DeleteAllCommentsInSection(sectionID);
}
bool CIniFile::DeleteAllCommentsInSection(int const §ionID)
{
if(sectionID < keys.size()) {
keys[sectionID].comment.clear();
return true;
}
return false;
}
void CIniFile::NewHeaderComment(string const &comment)
{
comments.push_back(comment);
}
string CIniFile::GetHeaderComment(int const &commentID) const
{
if(commentID < comments.size())
return comments[commentID];
return "";
}
void CIniFile::GetAllHeaderComments()
{
for(int commentID = 0; commentID < NumHeaderComments(); commentID++)
cout<<";"<<GetHeaderComment(commentID)<<endl;
}
bool CIniFile::DeleteHeaderComment(int commentID)
{
if (commentID < comments.size()) {
auto commentPos = comments.begin() + commentID;
comments.erase(commentPos, commentPos + 1);
return true;
}
return false;
}
void CIniFile::Type_Choice_SetValue(int type_choice, string const &putKeys, string const &putString, string insValue)
{
if(type_choice == 1){
insValue = getBoolValue(insValue);
if(insValue != "2")
SetValue(putKeys,putString,insValue);
}
else if (type_choice > 1)
cout<<"Hai inserito un codice di tipo della variabile sbagliato!"<<endl;
else
SetValue(putKeys,putString,insValue);
}
void CIniFile::Type_Choice_GetValue(int type_choice, string const &putKeys, string const &putString, string insValue)
{
try
{
if(type_choice == 1)
cout<<"Valore: "<<GetValue(putKeys,putString,insValue)<<endl;
else if(type_choice == 2)
cout<<"Valore: "<<GetValue(putKeys,putString,stoi(insValue))<<endl;
else if(type_choice == 3)
cout<<"Valore: "<<GetValue(putKeys,putString,stof(insValue))<<endl;
else if(type_choice == 4)
{
insValue = getBoolValue(insValue);
int insBool = stoi(insValue);
if(insBool != 2)
cout<<"Valore: "<<GetValue(putKeys,putString,boost::lexical_cast<bool>(insBool))<<endl;
}
else
cout<<"Hai inserito un codice di tipo della variabile sbagliato!"<<endl;
}
catch(exception& e) {
cout << "Conversione fallita! Proabile errore di scelta di tipo del valore." << endl;
cout <<"Eccezione lanciata: "<<e.what()<<endl;
}
}
bool CIniFile::SetValue(string const §ionName, string const &valueName, const string& value, bool const &create)
{
int sectionID = FindSection(sectionName);
if(sectionID == noID) {
if(create){
sectionID = AddSection(sectionName);
cout<<"Nuova sezione creata!"<<endl;
}
else
return false;
}
int valueID = FindValue(sectionID, valueName);
if(valueID == noID){
if(!create)
return false;
keys[sectionID].names.push_back(valueName);
keys[sectionID].value.push_back(value);
}
else{
keys[sectionID].value[valueID] = value;
cout<<"Valore modificato correttamente!"<<endl;
}
return true;
}
string CIniFile::getBoolValue(string ret)
{
//Valori accettati per ritornare TRUE '1', 'yes', 'true' e 'on',
//invece '0', 'no', 'false' e 'off' ritornano quindi FALSE.
if(ret == "1" || ret == "yes" || ret == "true" || ret == "on")
ret = "1";
else if(ret == "0" || ret == "no" || ret == "false" || ret == "off")
ret = "0";
else{
cout<<"Errore nell'inserimento del valore booleano."<<endl;
ret = "2";
}
return ret;
}
void CIniFile::toString()
{
GetAllHeaderComments();
for(int sectionID = 0; sectionID < NumSections(); sectionID++) {
cout<<"\nSezione = " << GetSection(sectionID) << endl;
for (int valueID = 0; valueID < NumKeyValuesInSection(sectionID); valueID++)
cout<<"Nome Valore = "<<GetValueName(sectionID,valueID)<<", Valore = "<<GetValue<string>(sectionID,valueID)<<endl;
}
}