-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfiguration.cpp
More file actions
187 lines (157 loc) · 5.21 KB
/
configuration.cpp
File metadata and controls
187 lines (157 loc) · 5.21 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
#ifndef CONFIGURATION_CPP
#define CONFIGURATION_CPP
#include <QDir>
#include <QFileInfo>
#include <QString>
#include <QJsonDocument>
#include <QJsonValue>
#include <QJsonArray>
#include <QJsonObject>
#include <QTextStream>
#include <QtDebug>
class Configuration
{
private:
QString filename;
QJsonObject m_parsed;
public:
~Configuration()
{
}
Configuration()
{
//Initialisation de la config
QString path("Configuration/");
QDir dir;
filename = path + "config.json";
//création du dossier Configuration lorsque le dossier n'existe pas.
if (!dir.exists(path))
{
dir.mkpath(path);
}
QByteArray values = readAll();
QTextStream ts(stdout);
QJsonParseError parseError;
QJsonDocument doc;
{
doc = QJsonDocument::fromJson(values, &parseError);
}
if (parseError.error != QJsonParseError::NoError) {
qWarning() << "Erreur lors du parsement : " << parseError.offset << ":" << parseError.errorString();
} else {
qDebug() << "La configuration " + filename + " à bien été parser";
m_parsed = doc.object();
}
}
//Création du fichier preciser l'extension .json exemple new Configuration("bdd.json") avec le nom spécific dans le dossier Configuration
Configuration(QString fn)
{
//Initialisation de la config
QString path("Configuration/");
QDir dir;
filename = path + fn;
//création du dossier Configuration lorsque le dossier n'existe pas.
if (!dir.exists(path))
{
dir.mkpath(path);
}
QByteArray values = readAll();
QTextStream ts(stdout);
QJsonParseError parseError;
QJsonDocument doc;
{
doc = QJsonDocument::fromJson(values, &parseError);
}
if (parseError.error != QJsonParseError::NoError) {
qWarning() << "Erreur lors du parsement : " << parseError.offset << ":" << parseError.errorString();
} else {
qDebug() << "La configuration " + filename + " à bien été parser";
ts << doc.toJson(QJsonDocument::Compact);
m_parsed = doc.object();
}
}
QByteArray readAll()
{
QFile file;
QByteArray value;
bool fileExists = QFileInfo::exists(filename) && QFileInfo(filename).isFile();
//Savoir si le fichier config.json ce trouve bien dans le repertoire Configuration/
//Sinon écrire la nouvelle configuration dans le fichier config.json
if(fileExists)
{
file.setFileName(filename);
file.open(QIODevice::ReadWrite);
} else {
file.setFileName(filename);
file.open(QIODevice::NewOnly | QIODevice::ReadWrite);
//file->write("{}");//{'base_de_donnees': {'ip': '127.0.0.1','port': 3306}
}
/* qDebug() << "test";
value = file.readAll();
m_parsed = value;//QJsonDocument::fromJson(value.toUtf8()).object();
qDebug() << m_parsed;*/
value = file.readAll();
return value;
}
//récupération de tous le contenus du format json en object dans la config
QJsonObject getAll()
{
return m_parsed;
}
void setAll(QJsonObject obj)
{
m_parsed = obj;
//sauvegarde dans le fichier les objets convertie en json
QByteArray new_conf = QJsonDocument(m_parsed).toJson();
QFile file(filename);
if(!file.open(QIODevice::WriteOnly))
{
qDebug() << "Erreur lors de l'ecriture du fichier configuration !";
return;
}
file.write(new_conf);
file.close();
}
//récupere la valeur du nom de l'objet
QJsonValue get(QString key)
{
return m_parsed.value(key);
}
//Permet d'enregistrer un nouveau objet grace a son nom spécific ainsi que sa valeur en nombre entier
void set(QString key, int value)
{
QJsonObject *obj;
obj = &m_parsed;
obj->insert(key, value);
m_parsed = *obj;
//sauvegarde dans le fichier les objets convertie en json
QByteArray new_conf = QJsonDocument(m_parsed).toJson();
QFile file(filename);
if(!file.open(QIODevice::WriteOnly))
{
qDebug() << "Erreur lors de l'ecriture du fichier configuration !";
return;
}
file.write(new_conf);
file.close();
}
//Permet d'enregistrer un nouveau objet grace a son nom spécific ainsi que sa valeur qui est une chaine de caractere contrairement à la fonction prècedente
void set(QString key, QString value)
{
QJsonObject *obj;
obj = &m_parsed;
obj->insert(key, value);
m_parsed = *obj;
//sauvegarde dans le fichier les objets convertie en json
QByteArray new_conf = QJsonDocument(m_parsed).toJson();
QFile file(filename);
if(!file.open(QIODevice::WriteOnly))
{
qDebug() << "Erreur lors de l'ecriture du fichier configuration !";
return;
}
file.write(new_conf);
file.close();
}
};
#endif