-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
80 lines (71 loc) · 1.55 KB
/
main.cpp
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
#include "JsonParser.hpp"
#include "util.hpp"
int main(int argc, char *argv[])
{
JsonParser jsonParser;
if (argc < 2 || argc > 3)
{
std::cerr << "Usage: ./test FILE [STRING]\n" << std::endl;
std::cerr << "This program is used to test microJSON.\n" << std::endl;
std::cerr << "Arguments:" << std::endl;
std::cerr <<
" FILE : The input file."
<< std::endl;
std::cerr <<
" STRING : (Optional) The Key to search a value in an object.\n"
<< std::endl;
std::cerr << "Notes:" << std::endl;
std::cerr <<
"- This program is designed for testing microJSON."
<< std::endl;
std::cerr
<< "- You must provide one to two arguments, ";
std::cerr
<< "where FILE is required, and STRING is optional."
<< std::endl;
return 1;
}
else
{
std::cout << MAGENTA
<< "\n* Parsed JSON result\n"
<< RESET << std::endl;
jsonParser.parseJson(argv[1]);
printJson(jsonParser.getJson());
}
if (argc == 3)
{
std::cout << MAGENTA
<< "\n* Print all values found by Key\n"
<< RESET << std::endl;
JsonData json = jsonParser.getJson();
std::vector<JsonData> arr = jsonParser.findDataByKey(json, argv[2]);
std::size_t size = arr.size();
if (size == 0)
{
std::cout << "No matching value found." << std::endl;
}
else
{
std::cout << "[ ";
for (std::size_t i = 0; i < size; ++i)
{
std::cout << arr[i]._str;
if (i < size - 1)
{
std::cout << ", ";
}
else
{
// skip comma character
}
}
std::cout << " ]" << std::endl;
}
}
else
{
// skipped
}
return 0;
}