forked from yangzhixuan/TCParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.cpp
180 lines (154 loc) · 3.65 KB
/
parse.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
#include <iostream>
#include <cctype>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
typedef vector<string> vstr;
char *target_path;
string DATA_START_MARKER("<!-- System Testing -->");
string DATA_END_MARKER("<!-- End System Testing -->");
void error(string v)
{
cout<<v<<endl;
exit(-1);
}
vstr readLines()
{
string str = "";
vstr v;
while(getline(cin, str))
v.push_back(str);
return v;
}
vstr getDataTable(const vstr& lines)
{
vstr v;
for(int i=0, seen=0; i<lines.size(); i++){
if(lines[i] == DATA_START_MARKER){
seen = 1;
continue;
}else if(lines[i] == DATA_END_MARKER)
return v;
if(seen)
v.push_back(lines[i]);
}
error("Invalid format!");
}
vector<vstr> mergeRows(const vstr& dt)
{
vector<vstr> ret;
vstr row;
for(int i=0, inrow=0; i<dt.size(); i++){
if(dt[i].find("<TR valign=\"top\">") != string::npos
|| dt[i].find("<tr valign=\"top\">") != string::npos){
if(inrow)
error("Invalid format! Nested <tr> tags");
inrow = 1;
row.clear();
continue;
}else if(inrow && dt[i].find("</TR>") != string::npos
|| inrow && dt[i].find("</tr>") != string::npos){
inrow = 0;
ret.push_back(row);
continue;
}
if(inrow)
row.push_back(dt[i]);
}
return ret;
}
template<typename T>
vector<T> slice(const vector<T>& vec, int s, int e)
{
return vec;
if(s < 0)
s += vec.size();
if(e < 0)
e += vec.size();
vector<T> ret;
for(int i=s; i<=e; i++)
ret.push_back(vec[i]);
return ret;
}
string removeHTMLTags(const string& str)
{
string s;
int tags = 0;
for(int i=0; i<str.length(); i++){
if(str[i] == '<')
tags++;
else if(str[i] == '>')
tags--;
else if(tags == 0)
s += str[i];
}
return s;
}
void removeHTMLTagsVec(vstr& lines)
{
vstr ret;
for(int i=0; i<lines.size(); i++){
string inner = removeHTMLTags(lines[i]);
if(inner != "")
ret.push_back(inner);
}
lines = ret;
}
const char* filename(const char*fmt, int d)
{
char buf[1024];
sprintf(buf, fmt, d);
return (string(target_path)+"/"+buf).c_str();
}
bool whiteString(const string& s)
{
for(int i=0; i<s.length(); i++)
if(!isspace(s[i]))
return false;
return true;
}
string trim(const string& s)
{
string ret = "";
for(int i = 0, seen = 0; i<s.length(); i++){
if(!isspace(s[i]))
seen = 1;
if(seen)
ret += s[i];
}
return ret;
}
void writeToFile(const vstr& lines, ofstream& in, ofstream& out)
{
int i = 0;
while(whiteString(lines[i]))
i++;//Skip leading white strings
do
in<<trim(lines[i++])<<endl;
while(!whiteString(lines[i]));
while(whiteString(lines[i]))
i++;//Skip white strings between inputs and outputs
do
out<<trim(lines[i++]);
while(!whiteString(lines[i]));
out<<endl;
}
int main(int argc, char **argv)
{
if(argc < 3)
return 1;
freopen(argv[1], "r", stdin);
vector<vstr> dtrs = mergeRows(getDataTable(readLines()));
cout<<dtrs.size()<<" Groups of data found"<<endl;
for_each(dtrs.begin(), dtrs.end(), removeHTMLTagsVec);
target_path = argv[2];
for(int i=0; i<dtrs.size(); i++){
ofstream in(filename("%d.in", i)), out(filename("%d.out", i));
writeToFile(dtrs[i], in, out);
in.close();
out.close();
}
return 0;
}