This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse2json.cpp
227 lines (212 loc) · 6.76 KB
/
parse2json.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
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
#include <fstream>
#include <string>
#include <iostream>
#include <algorithm>
bool startswith(std::string &source, std::string compare) {
return source.substr(0, compare.size()) == compare;
}
bool endswith(std::string const & value, std::string const & ending)
{
if (ending.size() > value.size()) return false;
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
}
inline std::string& ltrim(std::string& s, const char* t = " \t\n\r\f\v")
{
s.erase(0, s.find_first_not_of(t));
return s;
}
inline std::string& rtrim(std::string& s, const char* t = " \t\n\r\f\v")
{
s.erase(s.find_last_not_of(t) + 1);
return s;
}
inline std::string& trim(std::string& s, const char* t = " \t\n\r\f\v")
{
return ltrim(rtrim(s, t), t);
}
void replace(std::string& str, const std::string& from, const std::string& to) {
size_t start_pos = 0;
do {
start_pos = str.find(from, start_pos);
if(start_pos == std::string::npos)
return;
str.replace(start_pos, from.length(), to);
start_pos += to.size();
} while (start_pos != std::string::npos);
}
enum LAST { CLASS, ARRAY, PROPERTY };
int main(int argc, char** argv)
{
if (argv[1] == "--version") {
std::cout << "0.2" << std::endl;
return 0;
}
std::ifstream ifile(argv[1]);
std::ofstream ofile;
if (argc == 3) {
ofile.open(argv[2], std::ios::out);
std::cout << "Parsing " << argv[1] << " into " << argv[2] << std::endl;
}
std::ostream & output = (argc == 3) ? ofile : std::cout;
std::string str;
LAST last = CLASS;
output << "{";
bool emptyClass = true;
bool classEnd = false;
while (std::getline(ifile, str))
{
trim(str);
replace(str, "\\", "\\\\");
replace(str, "\t","");
replace(str, "\n","");
if (!startswith(str, "//") && !startswith(str, "#")) {
if (startswith(str, "class ")) {
if (last == PROPERTY || last == ARRAY) {
output << ",";
}
if (emptyClass && classEnd) {
output << ",";
}
if (endswith(str, "{};")) {
//Empty Class
if (endswith( str, " {};")) {
output << "\"" << str.substr(6, str.size() - 10) << "\":{}";
} else {
output << "\"" << str.substr(6, str.size() - 9) << "\":{}";
}
classEnd = true;
} else {
if (endswith(str, "{")) {
if (endswith(str, " {")) {
output << "\"" << str.substr(6, str.size() - 8) << "\":{";
} else {
output << "\"" << str.substr(6, str.size() - 7) << "\":{";
}
} else {
output << "\"" << str.substr(6, str.size() - 6) << "\":";
}
classEnd = false;
}
emptyClass = true;
last = CLASS;
} else {
std::size_t found = str.find("=");
if (found != std::string::npos) {
emptyClass = false;
std::size_t array = str.find("[]");
if (last != CLASS || (last == CLASS && classEnd)) {
output << ",";
}
if (array != std::string::npos && array < found) {
//Array
output << "\"" << str.substr(0, array) << "\":[";
std::size_t end_array = str.find("};");
std::string value;
if (end_array != std::string::npos) {
if (str.find("= {") == std::string::npos) {
value = str.substr(found + 2, str.size() - found - 4);
} else {
value = str.substr(found + 3, str.size() - found - 5);
}
replace(value, "{", "[");
replace(value, "}", "]");
std::string newvalue = "";
bool stringOpen = false;
for (int i = 0; i < value.size(); i++) {
if (stringOpen) {
if (value[i] == '"') {
if (value[i + 1] == '"') {
newvalue += "\\\"\\\"";
i++;
continue;
} else {
stringOpen = false;
}
}
} else {
if (value[i] == '"') {
stringOpen = true;
}
}
newvalue += value[i];
}
value = newvalue;
} else {
bool done = false;
std::string newvalue = "";
while (!done) {
std::string next;
std::getline(ifile, next);
done = next.find("};") != std::string::npos;
if (!done) {
std::string value = trim(next);
if (value == "{") {
continue;
}
bool stringOpen = false;
for (int i = 0; i < value.size(); i++) {
if (stringOpen) {
if (value[i] == '"') {
if (value[i + 1] == '"') {
newvalue += "\\\"\\\"";
i++;
continue;
} else {
stringOpen = false;
}
}
} else {
if (value[i] == '"') {
stringOpen = true;
}
}
newvalue += value[i];
}
}
}
value = newvalue;
}
output << value << "]";
last = ARRAY;
} else {
//Property
std::string value;
std::string property;
std::size_t eqpos = str.find(" = ");
if (eqpos + 1 == found) {
value = str.substr(found + 2, str.size() - found - 3);
property = str.substr(0, found - 1);
} else {
value = str.substr(found + 1, str.size() - found - 2);
property = str.substr(0, found);
}
while (value.find("\" \\\\n \"") != std::string::npos) {
replace(value, "\" \\\\n \"", "\\n");
}
if (value != "\"\"") {
if (startswith(value, "\"\"\"")) {
value = value.substr(1, value.size() - 1);
replace(value, "\"\"", "\\\"\\\"");
value = "\"" + value;
} else {
replace(value, "\"\"", "\\\"\\\"");
}
}
output << "\"" << property << "\":" << value;
last = PROPERTY;
}
} else {
if (str == "};") {
output << "}";
classEnd = true;
} else {
output << str;
}
}
}
}
}
output << "}" << std::endl;
if (argc == 3)
ofile.close();
}