-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIRJSON.cpp
More file actions
448 lines (421 loc) · 14.2 KB
/
IRJSON.cpp
File metadata and controls
448 lines (421 loc) · 14.2 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
439
440
441
442
443
444
445
446
447
448
//---------------------------------------------------------------------------
#include "IRJSON.h"
#include <sstream>
#include <iostream>
//#define DEBUG
const IRJSONObject IRJSONObject_null;
const IRJSONData IRJSONData_null(&IRJSONObject_null);
const std::vector <IRJSONObject *> vectorIRJSONObject_null;
const std::vector <std::string> vectorString_null;
IRJSONObject::IRJSONObject(std::string json_text)
{
//Extraction(json_text);
}
IRJSONObject::~IRJSONObject()
{
// On parcours le tableau et on supprime tous les pointeurs
for(unsigned int i=0; i<donnees.size() ; i++)
{
delete donnees[i];
donnees[i] = NULL;
}
donnees.clear();
#ifdef DEBUG
std::cout << "*** DEBUG - Suppression des elements.\n";
#endif
}
unsigned int IRJSONObject::Extraction(std::string json_text, unsigned int position, int niveau)
{
// On récupère le niveau
this->niveau = niveau;
// Si le niveau est 0, on enlève du texte tous les caractères génants (\n, \r, \t)
#ifdef DEBUG
std::cout << "*** DEBUG - Texte avant : \n";
std::cout << json_text << '\n';
#endif
// Préparation du fichier : suppression des sauts de ligne et des tabulations
unsigned int j = 0;
while(j < json_text.length())
{
if(json_text[j] == '\n' || json_text[j] == '\r' || json_text[j] == '\t') {json_text.erase(j,1);}
else {j++;}
}
#ifdef DEBUG
std::cout << "*** DEBUG - Texte apres : \n";
#endif
// On vérifie que le texte commence par une accolade
unsigned int i = position;
while(json_text[i] == ' ' && i<json_text.length()) {i++;}
//unsigned int premierCaractere = i;
if(i == json_text.length()) {
#ifdef DEBUG
std::cout << "*** DEBUG - La chaine ne contient que des caractères espaces." << std::endl;
#endif
return false;
}
if(json_text[i] != '{') {
#ifdef DEBUG
std::cout << "*** DEBUG - Le caractère '{' n'a pas ete trouve." << std::endl;
#endif
return 0;
}
#ifdef DEBUG
std::cout << "**** DEBUG - Accolade ouvrante trouvee.\n";
#endif
// i contient la position de l'accolade ouvrante
// Extraction des données
do
{
IRJSONData * data = new IRJSONData(this);
unsigned int position = data->Extraction(json_text, i+1);
// Si la position retournée vaut 0 -> erreur
#ifdef DEBUG
std::cout << data->Description();
std::cout << "*** DEBUG - position : " << position << "\n";
#endif
if(position == 0) {return 0;}
donnees.push_back(data);
// On avance jusqu'au prochain caractère
while(json_text[position] == ' ') {position++;}
if(json_text[position] == '}') {return position+1;} // fin extraction si accolade fermante
if(json_text[position] != ',') {return 0;} // Erreur d'extraction si le caractère n'est pas une virgule
i = position; // i contient la position de la virgule
}while(i<json_text.length());
#ifdef DEBUG
std::cout << "*** DEBUG - On a atteint la fin de la chaine de caractères sans avoir trouvé de '}' ni de ','!!!\n";
#endif
return 0;
}
std::string IRJSONObject::Description(/*unsigned int niveau*/)
{
std::string description = "";
for(unsigned int i=0 ; i<donnees.size() ; i++)
{
description += donnees[i]->Description();
}
return description;
}
std::string IRJSONObject::NomAttribut(unsigned int indice)
{
if(indice >=0 && indice <donnees.size())
{
return donnees[indice]->Attribut();
}
return "";
}
int IRJSONObject::Indice(std::string attribut) const
{
for(unsigned int i=0 ; i<donnees.size() ; i++)
{
if(donnees[i]->Attribut() == attribut)
{
return i;
}
}
return -1;
}
std::string IRJSONObject::Valeur(std::string attribut) const
{
int indice = this->Indice(attribut);
if(indice == -1) return "";
return donnees[indice]->Valeur();
}
const IRJSONObject * IRJSONObject::Objet(std::string attribut) const
{
int indice = this->Indice(attribut);
if(indice == -1) return &IRJSONObject_null;
return donnees[indice]->Fils();
}
const std::vector <IRJSONObject *> & IRJSONObject::TabObjet(std::string attribut) const
{
int indice = this->Indice(attribut);
if(indice == -1) return vectorIRJSONObject_null;
return donnees[indice]->Tab();
}
const std::vector <std::string> & IRJSONObject::TabValeur(std::string attribut) const
{
int indice = this->Indice(attribut);
#ifdef DEBUG
std::cout << "INDICE : " << indice << std::endl;
#endif
if(indice == -1) return vectorString_null;
return donnees[indice]->TabValeur();
}
const IRJSONData * IRJSONObject::Data(unsigned int indice)
{
if(indice >=0 && indice <donnees.size())
{
return donnees[indice];
}
return &IRJSONData_null;
}
const IRJSONData * IRJSONObject::operator[](unsigned int indice)
{
if(indice >=0 && indice <donnees.size())
{
return donnees[indice];
}
return &IRJSONData_null;
}
IRJSONData::IRJSONData(const IRJSONObject * monPere) : pere(monPere)
{
attribut = "";
valeur = "";
type = TYPE_ERREUR;
fils = NULL;
}
IRJSONData::~IRJSONData()
{
if(fils != NULL)
{
delete fils;
fils = NULL;
}
for(unsigned int i=0 ; i<tab.size() ; i++)
{
delete tab[i];
tab[i] = NULL;
}
}
unsigned int IRJSONData::Extraction(std::string json_text, unsigned int positionDepart)
{
#ifdef DEBUG
std::cout << "*** DEBUG - positionDepart : " << positionDepart << std::endl;
std::cout << "*** DEBUG - json_text[positionDepart] : " << json_text[positionDepart] << std::endl;
#endif
// On détecte si c'est un objet vide
unsigned int j = positionDepart;
while(json_text[j] == ' ') {j++;}
if(json_text[j] == '}')
{
//std::cout << "TYPE : VIDE" << std::endl;
type = TYPE_VIDE;
while(json_text[j] == ' ') {j++;}
if(json_text[j] == ',') return j+1;
else return j;
}
// Extraction de l'attribut
unsigned int debutAttribut = json_text.find('"', positionDepart) + 1;
#ifdef DEBUG
std::cout << "*** DEBUG - debutAttribut : " << debutAttribut << std::endl;
#endif
unsigned int finAttribut = json_text.find('"', debutAttribut);
#ifdef DEBUG
std::cout << "*** DEBUG - finAttribut : " << finAttribut << std::endl;
#endif
if(!(debutAttribut == std::string::npos || finAttribut == std::string::npos))
{
attribut = json_text.substr(debutAttribut, finAttribut-debutAttribut);
}
else
{
attribut = "";
type = TYPE_ERREUR;
return 0;
}
// On cherche les :
unsigned int deuxpoints = json_text.find(':', finAttribut);
if(deuxpoints == std::string::npos)
{
type = TYPE_ERREUR;
return 0;
}
unsigned int i = deuxpoints+1;
while(json_text[i] == ' ') {i++;}
#ifdef DEBUG
std::cout << "***DEBUG - i : " << i << std::endl;
std::cout << "***DEBUG - json_texte[i] : " << json_text[i] << std::endl;
#endif
switch (json_text[i])
{
case '{' :
type = TYPE_JSONOBJECT;
break;
case '[' :
type = TYPE_TABLEAU;
break;
case '"' :
type = TYPE_CHAINE;
break;
default :
type = TYPE_ENTIER;
}
if(type == TYPE_CHAINE )
{
unsigned int debutValeur = i + 1;
unsigned int finValeur = json_text.find('"', debutValeur);
if(finValeur == std::string::npos)
{
type = TYPE_ERREUR;
return 0;
}
else
{
valeur = json_text.substr(debutValeur, finValeur-debutValeur);
return finValeur+1;
}
}
else if(type == TYPE_ENTIER) // ENTIER, REEL, BOOLEAN (true/false)
{
unsigned int debutValeur = i;
unsigned int finValeur = i;
while(json_text[finValeur] != ' ' && json_text[finValeur] != ',' && json_text[finValeur] != '}' && finValeur<json_text.length() ) {finValeur++;}
if(finValeur >= json_text.length())
{
type = TYPE_ERREUR;
return 0;
}
else
{
valeur = json_text.substr(debutValeur, finValeur-debutValeur);
return finValeur;
}
}
else if(type == TYPE_JSONOBJECT )
{
#ifdef DEBUG
std::cout << "***** NOUVEAU FILS - pere : " << pere->Niveau() << "\n";
#endif
fils = new IRJSONObject();
int new_position = fils->Extraction(json_text, i, pere->Niveau()+1);
#ifdef DEBUG
std::cout << "***** NIVEAU DU FILS : " << fils->Niveau() << "\n";
#endif
if(new_position == 0) {return 0;}
return new_position;
}
else if(type == TYPE_TABLEAU)
{
// On parcours jusqu'au prochain caractère. Si c'est une accolade, on est dans un tableau de JSONOBJECT
#ifdef DEBUG
std::cout << "*** TEST - TABLEAU - " << i << "\n";
#endif
unsigned int prochain_caractere = i + 1;
while(json_text[prochain_caractere] == ' '){prochain_caractere++;}
#ifdef DEBUG
std::cout << "*** TEST - Prochain caractere - " << json_text[prochain_caractere] << "\n";
#endif
if(json_text[prochain_caractere] == '{')
{
do
{
type = TYPE_TABLEAU_JSONOBJECT;
IRJSONObject * json_object = new IRJSONObject();
#ifdef DEBUG
std::cout << "*** TEST - TABLEAU JSONOBJECT - " << prochain_caractere << "\n";
#endif
unsigned int position = json_object->Extraction(json_text, prochain_caractere, pere->Niveau()+1);
// Si la position retournée vaut 0 -> erreur
if(position == 0) {return 0;}
tab.push_back(json_object);
prochain_caractere = position;
while(json_text[prochain_caractere] == ' '){prochain_caractere++;}
if(json_text[prochain_caractere] == ']') {return prochain_caractere+1;}
else if(json_text[prochain_caractere] != ',') {return 0;}
#ifdef DEBUG
std::cout << "*** TEST - Virgule trouvee : " << prochain_caractere << std::endl;
#endif
prochain_caractere += 1; // On passe la virgule
} while (prochain_caractere < json_text.length());
}
else {
type = TYPE_TABLEAU_VALEUR;
// On va jusqu'au prochain caractère
i++;
#ifdef DEBUG
std::cout << "TABLEAU DE VALEUR " << std::endl;
#endif
while(json_text[i] == ' ') {i++;}
while(json_text[i] != ']')
{
#ifdef DEBUG
std::cout << "Boucle : " << json_text[i] << " - " << i << std::endl;
#endif
if(json_text[i] == ',')
{
i++;
while(json_text[i] == ' ') {i++;}
}
if(json_text[i] == '"')
{
// C'est un texte
unsigned int debutValeur = i+1;
unsigned int finValeur = json_text.find('"', debutValeur);
if(finValeur == std::string::npos)
{
type = TYPE_ERREUR;
return 0;
}
else
{
valeur = json_text.substr(debutValeur, finValeur-debutValeur);
tabValeur.push_back(valeur);
i = finValeur+1;
while(json_text[i] == ' ') {i++;}
}
}
else
{
// C'est une valeur
unsigned int debutValeur = i;
unsigned int finValeur = i;
while(json_text[finValeur] != ' ' && json_text[finValeur] != ',' && json_text[finValeur] != '}' && json_text[finValeur] != ']' && finValeur<json_text.length() ) {finValeur++;}
if(finValeur >= json_text.length())
{
type = TYPE_ERREUR;
return 0;
}
else
{
valeur = json_text.substr(debutValeur, finValeur-debutValeur);
tabValeur.push_back(valeur);
i = finValeur;
while(json_text[i] == ' ') {i++;}
}
}
}
#ifdef DEBUB
std::cout << "LENGTH : " << tabValeur.size() << std::endl;
#endif
return i+1; // On renvoit la position juste après le caractère ']'
}
}
return 0;
}
std::string IRJSONData::Description(/*unsigned int niveau*/)
{
#ifdef DEBUG
std::cout << "*** DEBUG - niveau : " << pere->Niveau() << "\n";
std::cout << "*** DEBUG - attribut : " << attribut << "\n";
std::cout << "*** DEBUG - valeur : " << valeur << "\n";
#endif
std::stringstream description;
if(type == TYPE_CHAINE || type == TYPE_ENTIER)
{
for(unsigned int i=0 ; i<pere->Niveau() ; i++) description << "\t";
description << attribut << " : " << valeur << std::endl;
}
else if(type == TYPE_JSONOBJECT)
{
for(unsigned int i=0 ; i<pere->Niveau() ; i++) description << "\t";
description << attribut << " : " << std::endl;
description << fils->Description() << std::endl;
}
else if(type == TYPE_TABLEAU_JSONOBJECT)
{
for(unsigned int i=0 ; i<pere->Niveau() ; i++) description << "\t";
description << attribut << " : " << std::endl;
for(unsigned j=0 ; j<tab.size() ; j++)
{
for(unsigned int i=0 ; i<pere->Niveau()+1 ; i++) description << "\t";
description << "[" << j << "]" << std::endl;
description << tab[j]->Description() ;
}
}
else if(type == TYPE_VIDE)
{
for(unsigned int i=0 ; i<pere->Niveau() ; i++) description << "\t";
description << attribut << " : {}" << std::endl;
}
return description.str();
}