-
Notifications
You must be signed in to change notification settings - Fork 2
/
Page.cpp
202 lines (181 loc) · 3.78 KB
/
Page.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
#include "Page.h"
Page::Page(string& html)
{
cout << "bulding page... string size: " << html.length() << endl;
if(html.empty())
{
cout << "ERROR: unable to build Page" << endl;
return;
}
try{
makeText(html);
}catch(int err)
{
switch(err)
{
case TITLE_ERROR:
cout << "ERROR: cant find page title" << endl;
throw TITLE_ERROR;
break;
case CANT_FIND_TABLE_ERROR:
cout << "ERROR: cant locate actual data in page" << endl;
throw CANT_FIND_TABLE_ERROR;
break;
default:
cout << "ERROR: unknown" << endl;
throw UNKNOWN_ERROR;
break;
}
}
}
Page::~Page()
{
cout << "killing Page..." <<endl;
}
void Page::makeText(string& html)
{
int index = 0;
index = findTitle(html, index);
if(index == -1)
throw TITLE_ERROR;
index = runToActualText(html, index);
if(index == -1)
throw CANT_FIND_TABLE_ERROR;
manageTableContent(html, index);
}
int Page::findTitle(string& from, int index)
{
cout << "**** in findTitle ****" << endl; //** DEBUG **
string temp;
while(index < (int)from.length())
{
if(from[index] == '<')
{
//title>
index++;
string titleTag = from.substr(index, 5); //legth of title
if(titleTag == "title") //check if the tag is title
{
while(from[index] != '>')
index++;
index++;
while(from[index] != '<')
{
temp += from[index];
index++;
}
this->title = temp; //sets the title
return index;
}
}
index++;
}
return -1;
}
int Page::runToActualText(string& from, int index)
{
cout << "**** in runToActualText ****" << endl; //**DEBUG**
cout << "**** index: "<< index <<" ****" << endl; //**DEBUG**
while(index < (int)from.length())
{
if(from[index] == '<')
{
index++;
if(from[index] == '!')
{
//!--FileName
string bodyTag = from.substr(index, 11); //!--FileName
if(bodyTag == "!--FileName") //check if the tag is body tag
{
while(from[index] != '>')
index++;
return index;
}
}
}
index++;
}
return -1;
}
void Page::manageTableContent(string& html, int index)
{
cout << "**** in manageTableContent ****" << endl;//** DEBUG **
cout << "**** index: "<< index <<" ****" << endl; //** DEBUG **
/**
* Actual Body Text In String
*/
string temp;
for (int i = index; i < (int)html.length(); i++)
{
if(html[i] == '<')
{
//<tr> / <td> / <th>
string tableTag = html.substr(i, 4); //legth of "tr/td"
if(tableTag == "<tr>")
{
temp += "\n"; //new row -> new line
i = stitchText(html, temp, i+4);
if(i == -1) //EOF
goto finishBuild;
}
else if(tableTag == "<td>" || tableTag == "<th>")
{
temp += "\t"; // new cell -> tab between data
i = stitchText(html, temp, i+4);
if(i == -1) //EOF
goto finishBuild;
}
else if(tableTag == "<td ") // a Year title (in grades table)
{
temp += "\t";
while(html[i] != '>')
i++;
i = stitchText(html, temp, i);
}
}
}
finishBuild:
this->text = temp;
}
int Page::stitchText(string& from, string& to, int index)
{
if(from[index] == '<')
{
string bTag = from.substr(index, 3);
if(bTag != "<b>")
return index-1; //go back one step - for the main function to inc i
index += 3;
}
while(from[index] != '<' && index < (int)from.length())
{
if(from[index] == '&')
{
//
string nbspChr = from.substr(index, 6);
if(nbspChr == " ")
index += 6;
}
if(endOfString(index, from.length()))
return -1; //EOF
else if(from[index] == '<')
return index -1; //go back one step - for the main function to inc i
if(from[index] != '\n')
to += from[index];
index++;
}
return index-1;
}
bool Page::endOfString(int index, int length)
{
if(index < length)
return false;
return true;
}
string Page::getString()
{
return this->text;
}
string Page::getTitle()
{
return this->title;
}