-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
106 lines (94 loc) · 1.42 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
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void json_tree(string);
int main()
{
string file_path;
cout << "Enter your file path : ";
cin >> file_path;
json_tree(file_path);
return 0;
}
void json_tree(string file_path)
{
ifstream fin;
ofstream fout;
int tabs, tokens; //here tokens are { } , :
tokens = -1;
char ch;
fin.open(file_path.c_str());
if (!fin)
cout << "Failed to open file";
else
{
fout.open("result.json");
while (fin.get(ch))
{
if (ch == '{')
{
tokens++;
//open braces tabs
tabs = tokens;
if (tokens > 0)
fout << "\n";
while (tabs)
{
fout << "\t";
tabs--;
}
fout << ch << "\n";
//json key:value tabs
tabs = tokens + 1;
while (tabs)
{
fout << "\t";
tabs--;
}
}
else if (ch == ':')
{
fout << " : ";
}
else if (ch == ',')
{
fout << ",\n";
tabs = tokens + 1;
while (tabs)
{
fout << "\t";
tabs--;
}
}
else if (ch == '}')
{
tabs = tokens;
fout << "\n";
while (tabs)
{
fout << "\t";
tabs--;
}
fout << ch << "\n";
tokens--;
tabs = tokens + 1;
while (tabs)
{
fout << "\t";
tabs--;
}
}
else
{
if (ch == '\n' || ch == '\t')
continue;
else
fout << ch;
}
}
fout.close();
}
cout << "Formatting Done \nCheck 'result.json' file\n";
fin.close();
}