1 |
/* ============================================================================ |
2 |
* Douglas Thrift's Search Engine License |
3 |
* |
4 |
* Copyright (C) 2002-2003, Douglas Thrift. All Rights Reserved. |
5 |
* Redistribution and use in source and binary forms, with or without |
6 |
* modification, are permitted provided that the following conditions are met: |
7 |
* |
8 |
* 1. Redistributions of source code must retain the above copyright notice, |
9 |
* this list of conditions and the following disclaimer. |
10 |
* |
11 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
12 |
* this list of conditions and the following disclaimer in the documentation |
13 |
* and/or other materials provided with the distribution. |
14 |
* |
15 |
* 3. The end-user documentation included with the redistribution, if any, must |
16 |
* include the following acknowledgment: |
17 |
* |
18 |
* "This product includes software developed by Douglas Thrift |
19 |
* (http://computers.douglasthrift.net/searchengine/)." |
20 |
* |
21 |
* Alternately, this acknowledgment may appear in the software itself, if |
22 |
* and wherever such third-party acknowledgments normally appear. |
23 |
* |
24 |
* 4. The names "Douglas Thrift" and "Douglas Thrift's Search Engine" must not |
25 |
* be used to endorse or promote products derived from this software without |
26 |
* specific prior written permission. For written permission, please visit |
27 |
* http://www.douglasthrift.net/contact.cgi for contact information. |
28 |
* |
29 |
* 5. Products derived from this software may not be called "Douglas Thrift's |
30 |
* Search Engine", nor may "Douglas Thrift's Search Engine" appear in their |
31 |
* name, without prior written permission. |
32 |
* |
33 |
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
34 |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
35 |
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
36 |
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
37 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
38 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
39 |
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
40 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
41 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
42 |
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
43 |
* ============================================================================ |
44 |
*/ |
45 |
// Douglas Thrift's Search Engine Page |
46 |
// |
47 |
// Douglas Thrift |
48 |
// |
49 |
// $Id: Page.cpp,v 1.4 2003/07/15 07:22:06 douglas Exp $ |
50 |
|
51 |
#include "Page.h" |
52 |
|
53 |
Page::Page(string& url) : URL(url) |
54 |
{ |
55 |
size = 0; |
56 |
} |
57 |
|
58 |
void Page::setSize(unsigned size) |
59 |
{ |
60 |
this->size = size; |
61 |
} |
62 |
|
63 |
void Page::setTitle(string& title) |
64 |
{ |
65 |
this->title = title; |
66 |
} |
67 |
|
68 |
void Page::setDescription(string& description) |
69 |
{ |
70 |
this->description = description; |
71 |
} |
72 |
|
73 |
void Page::setText(string& text) |
74 |
{ |
75 |
this->text = text; |
76 |
} |
77 |
|
78 |
void Page::setHeadings(vector<string>& headings) |
79 |
{ |
80 |
this->headings = headings; |
81 |
} |
82 |
|
83 |
istream& operator>>(istream& is, Page& data) |
84 |
{ |
85 |
string line; |
86 |
getline(is, line); |
87 |
if (line == " <page>") |
88 |
{ |
89 |
do |
90 |
{ |
91 |
getline(is, line); |
92 |
|
93 |
if (line.find(" <address>") == 0) |
94 |
{ |
95 |
unsigned start = line.find("<address>") + 9; |
96 |
unsigned finish = line.find("</address>", start); |
97 |
|
98 |
string address = line.substr(start, finish - start); |
99 |
|
100 |
entities(address, "<", '<'); |
101 |
entities(address, ">", '>'); |
102 |
entities(address, "&", '&'); |
103 |
|
104 |
data.setAddress(address); |
105 |
} |
106 |
else if (line.find(" <port>") == 0) |
107 |
{ |
108 |
unsigned start = line.find("<port>") + 6; |
109 |
unsigned finish = line.find("</port>", start); |
110 |
|
111 |
istringstream buffer(line.substr(start, finish - start)); |
112 |
unsigned port; |
113 |
|
114 |
buffer >> port; |
115 |
|
116 |
data.setPort(port); |
117 |
} |
118 |
#ifdef _OpenSSL_ |
119 |
else if (line.find(" <tls>") == 0) |
120 |
{ |
121 |
unsigned start = line.find("<tls>") + 5; |
122 |
unsigned finish = line.find("</tls>", start); |
123 |
|
124 |
istringstream buffer(line.substr(start, finish - start)); |
125 |
bool tls; |
126 |
|
127 |
buffer.setf(ios_base::boolalpha); |
128 |
buffer >> tls; |
129 |
|
130 |
data.setTls(tls); |
131 |
} |
132 |
#endif |
133 |
else if (line.find(" <path>") == 0) |
134 |
{ |
135 |
unsigned start = line.find("<path>") + 6; |
136 |
unsigned finish = line.find("</path>", start); |
137 |
|
138 |
string path = line.substr(start, finish - start); |
139 |
|
140 |
entities(path, "<", '<'); |
141 |
entities(path, ">", '>'); |
142 |
entities(path, "&", '&'); |
143 |
|
144 |
data.setPath(path); |
145 |
} |
146 |
else if (line.find(" <size>") == 0) |
147 |
{ |
148 |
unsigned start = line.find("<size>") + 6; |
149 |
unsigned finish = line.find("</size>", start); |
150 |
|
151 |
istringstream buffer(line.substr(start, finish - start)); |
152 |
unsigned size; |
153 |
|
154 |
buffer >> size; |
155 |
|
156 |
data.setSize(size); |
157 |
} |
158 |
else if (line.find(" <title>") == 0) |
159 |
{ |
160 |
unsigned start = line.find("<title>") + 7; |
161 |
unsigned finish = line.find("</title>", start); |
162 |
|
163 |
string title = line.substr(start, finish - start); |
164 |
|
165 |
while (finish == string::npos) |
166 |
{ |
167 |
getline(is, line); |
168 |
finish = line.find("</title>"); |
169 |
title += '\n' + line.substr(0, finish - 0); |
170 |
} |
171 |
|
172 |
entities(title, "<", '<'); |
173 |
entities(title, ">", '>'); |
174 |
entities(title, "&", '&'); |
175 |
|
176 |
data.setTitle(title); |
177 |
} |
178 |
else if (line.find(" <description>") == 0) |
179 |
{ |
180 |
unsigned start = line.find("<description>") + 13; |
181 |
unsigned finish = line.find("</description>", start); |
182 |
|
183 |
string description = line.substr(start, finish - start); |
184 |
|
185 |
entities(description, "<", '<'); |
186 |
entities(description, ">", '>'); |
187 |
entities(description, "&", '&'); |
188 |
|
189 |
data.setDescription(description); |
190 |
} |
191 |
else if (line.find(" <text>") == 0) |
192 |
{ |
193 |
unsigned start = line.find("<text>") + 6; |
194 |
unsigned finish = line.find("</text>", start); |
195 |
|
196 |
string text = line.substr(start, finish - start); |
197 |
|
198 |
while (finish == string::npos) |
199 |
{ |
200 |
getline(is, line); |
201 |
finish = line.find("</text>"); |
202 |
text += '\n' + line.substr(0, finish - 0); |
203 |
} |
204 |
|
205 |
entities(text, "<", '<'); |
206 |
entities(text, ">", '>'); |
207 |
entities(text, "&", '&'); |
208 |
|
209 |
data.setText(text); |
210 |
} |
211 |
else if (line.find(" <heading>") == 0) |
212 |
{ |
213 |
unsigned start = line.find("<heading>") + 9; |
214 |
unsigned finish = line.find("</heading>", start); |
215 |
|
216 |
string heading = line.substr(start, finish - start); |
217 |
|
218 |
while (finish == string::npos) |
219 |
{ |
220 |
getline(is, line); |
221 |
finish = line.find("</heading>"); |
222 |
heading += line.substr(0, finish - 0); |
223 |
} |
224 |
|
225 |
entities(heading, "<", '<'); |
226 |
entities(heading, ">", '>'); |
227 |
entities(heading, "&", '&'); |
228 |
|
229 |
data.headings.push_back(heading); |
230 |
} |
231 |
} |
232 |
while (line != " </page>"); |
233 |
} |
234 |
|
235 |
return is; |
236 |
} |
237 |
|
238 |
ostream& operator<<(ostream& os, Page& data) |
239 |
{ |
240 |
string address = data.getAddress(); |
241 |
|
242 |
entities(address, '&', "&"); |
243 |
entities(address, '<', "<"); |
244 |
entities(address, '>', ">"); |
245 |
|
246 |
os << " <page>\n" << " <address>" << address << "</address>\n"; |
247 |
|
248 |
#ifndef _OpenSSL_ |
249 |
if (data.getPort() != 80) |
250 |
#else |
251 |
if (data.getPort() != 80 && !data.getTls()) |
252 |
#endif |
253 |
{ |
254 |
os << " <port>" << data.getPort() << "</port>\n"; |
255 |
} |
256 |
|
257 |
#ifdef _OpenSSL_ |
258 |
os.setf(ios::boolalpha); |
259 |
os << " <tls>" << data.getTls() << "</tls>\n"; |
260 |
#endif |
261 |
|
262 |
string path = data.getPath(); |
263 |
|
264 |
entities(path, '&', "&"); |
265 |
entities(path, '<', "<"); |
266 |
entities(path, '>', ">"); |
267 |
|
268 |
os << " <path>" << path << "</path>\n"; |
269 |
|
270 |
os << " <size>" << data.getSize() << "</size>\n"; |
271 |
|
272 |
if(data.getTitle() != "") |
273 |
{ |
274 |
string title = data.getTitle(); |
275 |
|
276 |
entities(title, '&', "&"); |
277 |
entities(title, '<', "<"); |
278 |
entities(title, '>', ">"); |
279 |
|
280 |
os << " <title>" << title << "</title>\n"; |
281 |
} |
282 |
|
283 |
if(data.getDescription() != "") |
284 |
{ |
285 |
string description = data.getDescription(); |
286 |
|
287 |
entities(description, '&', "&"); |
288 |
entities(description, '<', "<"); |
289 |
entities(description, '>', ">"); |
290 |
|
291 |
os << " <description>" << description << "</description>\n"; |
292 |
} |
293 |
|
294 |
string text = data.getText(); |
295 |
|
296 |
entities(text, '&', "&"); |
297 |
entities(text, '<', "<"); |
298 |
entities(text, '>', ">"); |
299 |
|
300 |
os << " <text>" << text << "</text>\n"; |
301 |
|
302 |
for (int index = 0; index < data.getHeadings().size(); index++) |
303 |
{ |
304 |
string heading = data.getHeadings()[index]; |
305 |
|
306 |
entities(heading, '&', "&"); |
307 |
entities(heading, '<', "<"); |
308 |
entities(heading, '>', ">"); |
309 |
|
310 |
os << " <heading>" << heading << "</heading>\n"; |
311 |
} |
312 |
|
313 |
os << " </page>"; |
314 |
|
315 |
return os; |
316 |
} |