-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_parser.cpp
More file actions
322 lines (272 loc) · 9.63 KB
/
Copy pathjson_parser.cpp
File metadata and controls
322 lines (272 loc) · 9.63 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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <vector>
using namespace std;
enum button { HELP, OPEN, CHECK, DATA, EXIT };
button in_put;
bool open_or_not=false;
bool maydata = false;
char json_line[1000];
int numsymb = -1; //counts all characters in the file
int att = 0; //counts all variables and its values
vector <char> bufor;
struct syntax {
string name;//name of the variable
string val_str;
int val_int;
float val_float;
bool val_bool;
//these conditions check if the value was written to the file
bool if_str = false, if_int = false, if_float = false, if_bool = false;
};
syntax variable[100];//saves data
void menu() {
char press;
cin >> press;
switch (press) {
case 'h':
in_put = HELP;
break;
case 'o':
in_put = OPEN;
break;
case 'c':
in_put = CHECK;
break;
case 'p':
in_put = DATA;
break;
case 'e':
in_put = EXIT;
break;
}
}
void openclose2() {
char p;
string name;
cin >> name;
ifstream yfile;
yfile.open(name, ios::in);
if (!yfile) {
open_or_not = false;
cerr << "Name of the file is wrong or it doesn't exist \n";
}
else {
open_or_not = true;
}
if (open_or_not == true) {
while (yfile >> p) {
numsymb += 1;
json_line[numsymb] = p;
}
cout << "The file was opened " << name << endl;
yfile.close();
}
}
void check() { //this function doesn't work for lists and vocabularies
int end = 0;
bool right = true;
if (json_line[0] == '{' && json_line[numsymb] == '}') {
for (int i = 1; i <= numsymb - 1; i++) {
if (json_line[i] == '"') {
for (int j = i; j++;) {
bufor.push_back(json_line[j]);
if (json_line[j + 1] == '"') {
end = j + 1;
break;
}
}
string temp_str(bufor.begin(), bufor.end());
bufor.clear();
variable[att].name = temp_str;
i = end + 1;//the value of i is set to the position of :
}
if (json_line[i] == ':') {
if (json_line[i + 1] == '"') {
for (int j = i + 1; j++;) {
bufor.push_back(json_line[j]);
if (json_line[j + 1] == '"') {
end = j + 1;
break;
}
}
string temp_str(bufor.begin(), bufor.end());
bufor.clear();
variable[att].val_str = temp_str;
variable[att].if_str = true;
i = end + 1;//the value of i is set to the position of ,
}
else {
bool what_is_it; //check the type
for (int j = i; j++;) { //
bufor.push_back(json_line[j]);
if (json_line[j + 1] == ',') {
end = j + 1;
break;
}
else if (json_line[j + 1] == '}') {
end = j + 1;
break;
}
}
string temp_str(bufor.begin(), bufor.end());
for (int k = 0; k <= bufor.size(); k++) {
if (temp_str[k] == '.') {
try {
variable[att].val_float = stof(temp_str);
variable[att].if_float = true;
what_is_it = false;
break;
}
catch (std::invalid_argument& e) {
cerr << "Invalid argument" << i << endl;
right = false;
break;
}
}
else {
what_is_it = true;
}
}
bufor.clear();
if (what_is_it == true) {
try {
variable[att].val_int = stoi(temp_str);
variable[att].if_int = true;
}
catch (std::invalid_argument& e) {
if (temp_str == "true") {
variable[att].val_bool = true;
variable[att].if_bool = true;
}
else if (temp_str == "false") {
variable[att].val_bool = false;
variable[att].if_bool = true;
}
else if (temp_str == "NULL") {
variable[att].val_str = "NULL";
}
else {
cout << "Invalid argument" << i << endl;
right = false;
break;
}
}
}
i = end;
}
}
else {
cout << "Invalid position: " << i <<" there is no \":\""<< endl;
right = false;
break;
}
if (json_line[i] == ',') {
att += 1;
}
}
}
else {
if (json_line[0] != '{') {
cout << "Invalid position:" << 0 << " there is no \"{\"" << endl;
right = false;
}
if (json_line[0] != '}') {
cout << "Invalid position:" << numsymb << " there is no \"}\"" << endl;
right = false;
}
}
if (right == true) {
cout << "The file is a correct JSON" << endl;
}
}
void data() {
cout << "variables "<<att<<"first variable "<< variable[0].name << endl;
string buf1,var_name,buf2;
cin >> buf1 >>var_name >> buf2;
if (buf1=="$['") {
for (int j = 0; j <= att; j++) {
if (variable[att].name == var_name ) {
int buf = 2 + variable[att].name.length() + 1;
if (buf2) {
if (variable[att].if_str == true) {
cout << "The Variable " << variable[att].name << "has value";
for (int k = 0; k <= variable[att].val_str.length(); k++) {
cout << variable[k].val_str;
if (k == 10) {
cout << "...." << endl;
break;
}
}
}
else if (variable[att].if_int == true) {
cout << variable[att].val_int << endl;
}
else if (variable[att].if_float == true) {
cout << variable[att].val_float << endl;
}
else if (variable[att].if_bool == true) {
cout << variable[att].val_bool << endl;
}
}
else {
cout << "Wrong path" << endl;
break;
}
}
}
}
else {
cout << "Wrong path" << endl;
break;
}
}
int main()
{
cout << "This program verifies correctness of JSON format" << endl;
cout << "h - HELP" << endl;
while (true) {
menu();
if (in_put == HELP) {
cout << "Created by: Serhii Pyskovatskyi 196662 IBM I rok" << endl;
cout << " o - opens file \nOperacja c - checks correctness of JSON format \nOperacja h – help";
}
if (in_put == OPEN) {
system("cls");
openclose2();
}
if (in_put == CHECK && open_or_not == true) {
system("cls");
check();
maydata = true;
}
if (in_put == DATA && maydata == true) {
system("cls");
data();
}
if (in_put == EXIT) {
system("cls");
bufor.clear();
for (int i = 0; i <= numsymb; i++) {
json_line[i] = ' ';
}
for (int i = 0; i <= att; i++) {
variable[i].name ="";
variable[i].val_int = 0;
variable[i].val_float = 0;
variable[i].val_str = "";
variable[i].val_bool = false;
variable[i].if_bool = false;
variable[i].if_int = false;
variable[i].if_float = false;
variable[i].if_str = false;
}
numsymb = -1;
att = 0;
open_or_not = false;
maydata = false;
}
}
}