-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCIniFile.h
More file actions
230 lines (181 loc) · 7.84 KB
/
Copy pathCIniFile.h
File metadata and controls
230 lines (181 loc) · 7.84 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
//
// Created by samuel on 05/07/19.
//
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#include <typeinfo>
#include <boost/lexical_cast.hpp>
#include "Key.h"
#ifndef FILEINIPROJECT_CINIFILE_H
#define FILEINIPROJECT_CINIFILE_H
using namespace std;
class CIniFile
{
public:
enum error{noID = -1};
explicit CIniFile(const string& initialPath = "./Files", string fn = "ini_file.ini") : fileName(std::move(fn)){
defaultPath = initialPath + "/" + fileName;
}
virtual ~CIniFile() = default;
/// Funzioni di lettura e scrittura
bool ReadFile();
bool WriteFile();
/// Funzioni che riguardano le path e il nome del file
void setPath(string newPath)
{
defaultPath = std::move(newPath);
}
string getPath() const
{
return defaultPath;
}
const string &getFileName() const {
return fileName;
}
void setFileName(const string &fn) {
CIniFile::fileName = fn;
}
//Funzione che viene utilizzata per cambiare il nome del file insieme alla path
void ChangeFileName(const string& putString, const string& putKeys);
//Funzione che viene utilizzata per cambiare la path del file
string ChangePath(const string& putPath);
//Funzione che viene utilizzata per rinominare il file
void RenameFileName(const string& putString, const string& putKeys);
///Funzioni che riguardano le sezioni
//Funzione che cerca l'ID della sezione attraverso il suo nome e ritorna un chiave di errore se non esiste
int FindSection(string const §ionName) const;
//Ritorna il numero corrente delle sezioni all'interno dell'intero file ini
int NumSections() const
{
return section.size();
}
//Aggiunge una nuova sezione
int AddSection(string const §ionName);
//Ritorna una stringa contente il nome della sezione cercata
string GetSection(int const §ionID) const;
//Stampa il nome della sezione con i suoi relativi parametri e valori
void GetValuesInSection(int const §ionID);
//Ritorna il numero dei parametri presenti in una sezione
int NumKeyValuesInSection(int const §ionID);
int NumKeyValuesInSection(string const §ionName);
// Elimina un intera sezione e i suoi relativi parametri
bool DeleteSection(const string& sectionName);
///Funzioni che riguardano le chiavi all'interno delle sezioni
//Funzione che cerca l'ID del valore all'interno della sezione e ritorna un chiave di errore se non esiste
int FindValue(int const §ionID, string const &valueName) const;
// Ritorna il nome del parametro cercato per una specifica sezione
string GetValueName(int const §ionID, int const &valueID) const;
string GetValueName(string const §ionName, int const &valueID) const;
// Ritorna il numero dei commenti per una determinata sezione
int NumKeyCommentsInSection(int const §ionID) const;
int NumKeyCommentsInSection(string const §ionName) const;
// Aggiunge un commento interno alla sezione
bool AddKeyCommentInSection(int const §ionID, string const &comment);
bool AddKeyCommentInSection(string const §ionName, string const &comment);
// Ritorna un commento interno alla sezione
string GetKeyComment(int const §ionID, int const &commentID) const;
string GetKeyComment(string const §ionName, int const &commentID) const;
// Elimina un parametro e il corrispondente valore presente in una determinata sezione
bool DeleteValueInSection(string const §ionName, const string &valueName);
// Elimina un commento presente nella sezione
bool DeleteCommentInSection(int const §ionID, int const &commentID);
bool DeleteCommentInSection(string const §ionName, int const &commentID);
// Elimina tutti i commenti presenti nella sezione
bool DeleteAllCommentsInSection(int const §ionID);
bool DeleteAllCommentsInSection(string const §ionName);
///Funzioni che riguardano i commenti d'intestazione
// Ritorna il numero di commenti presenti nell'intestazione
int NumHeaderComments()
{
return comments.size();
}
// Aggiunge una riga di commento nell'intestazione
void NewHeaderComment(string const &comment);
// Ritorna un commento specifico presente nell'intestazione
string GetHeaderComment(int const &commentID) const;
// Ritorna tutti i commenti d'intestazione
void GetAllHeaderComments();
// Elimina un singolo commento d'intestazione
bool DeleteHeaderComment(int commentID);
// Elimina tutti i commenti d'intestazione
void DeleteAllHeaderComments()
{
comments.clear();
}
///Funzioni che servono per aggiungere un valore o visualizzarlo all'interno della sezione
//Funzione che serve per determinare il tipo di dato da inserire nel file INI
void Type_Choice_SetValue(int type_choice, string const &putKeys, string const &putString, string insValue);
void Type_Choice_GetValue(int type_choice, string const &putKeys, string const &putString, string insValue);
//Setta i valori delle chiavi all'interno del file
bool SetValue(string const §ionName, string const &valueName, const string& value, bool const &create = true);
//Fa ritornare i valori delle chiavi presenti all'interno del file
template<typename T>
T GetValue(int const §ionID, int const &valueID, T const defValue = T()) {
if (sectionID < keys.size() && valueID < keys[sectionID].names.size()) {
string ret = keys[sectionID].value[valueID];
if(typeid(T) == typeid(bool)) {
ret = getBoolValue(ret);
int val = stoi(ret);
if(val == 2)
goto boolean_label;
else
return boost::lexical_cast<T>(val);
}
else if(typeid(T) == typeid(int))
return boost::lexical_cast<T>(stoi(ret));
else if(typeid(T) == typeid(float))
return boost::lexical_cast<T>(stof(ret));
else if(typeid(T) == typeid(string))
return boost::lexical_cast<T>(ret);
else{
boolean_label:
cout<<"Ritorna il valore di default: ";
return boost::lexical_cast<T>(defValue);
}
}
}
template<typename T>
T GetValue(string const §ionName, string const &valueName, T const defValue = T()) {
int sectionID = FindSection(sectionName);
if(sectionID == noID)
return defValue;
int valueID = FindValue(sectionID, valueName);
if(valueID == noID)
return defValue;
string ret = keys[sectionID].value[valueID];
if(typeid(T) == typeid(bool)) {
ret = getBoolValue(ret);
int val = stoi(ret);
if (val != 2)
return boost::lexical_cast<T>(val);
else
goto boolean_label;
}
else if(typeid(T) == typeid(int))
return boost::lexical_cast<T>(stoi(ret));
else if(typeid(T) == typeid(float))
return boost::lexical_cast<T>(stof(ret));
else if(typeid(T) == typeid(string))
return boost::lexical_cast<T>(ret);
else{
boolean_label:
cout<<"Ritorna il valore di default: ";
return boost::lexical_cast<T>(defValue);
}
}
//Controllo del tipo Booleano nel ritorno del valore nella macro-funzione getValue.
//Questo controllo è fondamentale perchè all'interno file INI ci sono diverse modalità di dichiarazione
//di un valore booleano.
static string getBoolValue(string ret);
///Funzione che stampa l'intero file INI
void toString();
private:
string defaultPath;
string fileName;
vector<string> section;
vector<string> comments;
vector<Key> keys;
};
#endif //FILEINIPROJECT_CINIFILE_H