ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/Search/trunk/Outputter.cpp
Revision: 372
Committed: 2008-08-23T04:00:12-07:00 (16 years, 9 months ago) by douglas
File size: 12200 byte(s)
Log Message:
Update copyright dates.

File Contents

# Content
1 /* ============================================================================
2 * Douglas Thrift's Search Engine License
3 *
4 * Copyright (C) 2002-2004, 2008, 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 Outputter
46 //
47 // Douglas Thrift
48 //
49 // $Id$
50
51 #include "Outputter.hpp"
52
53 void Outputter::output(Searcher& searcher, size_t page)
54 {
55 MultiSet pagesSet(searcher.getPages());
56
57 numWebpages = pagesSet.size();
58 numPages = (numWebpages + 9) / 10;
59
60 string query(searcher.getQueryString());
61 vector<string> common(searcher.getCommonUsed());
62
63 MultiSetIterator itor(pagesSet.begin());
64
65 for (size_t count(0); count < page * 10 && itor != pagesSet.end(); count++)
66 {
67 itor++;
68 }
69
70 for (short index(0); index < 10 && itor != pagesSet.end(); index++, itor++)
71 {
72 webpages.push_back(*itor);
73 }
74
75 this->query = searcher.getQuery().size() > 0;
76 results = webpages.size() > 0;
77 time = searcher.time();
78
79 if (debug)
80 {
81 cerr << "query = " << this->query << "\n"
82 << "results = " << results << "\n"
83 << "time = " << duration() << "\n";
84 }
85
86 entities(query, '&', "&amp;");
87 entities(query, '\"', "&quot;");
88 entities(query, '<', "&lt;");
89 entities(query, '>', "&gt;");
90
91 string ignore(searcher.getIgnore());
92
93 header(query, page, common, searcher.getAnd(), searcher.getOr(), ignore);
94
95 if (results) body(); else if (this->query)
96 {
97 notfound(query, searcher.getQuery().size());
98 }
99
100 footer(query, page, common, searcher.getAnd(), searcher.getOr(), ignore);
101 }
102
103 void Outputter::header(const string& query, size_t page, const vector<string>&
104 common, bool and_, bool or_, const string& ignore)
105 {
106 ifstream fin(headerFile.c_str());
107 string line;
108
109 while (fin.good())
110 {
111 getline(fin, line);
112 conditional(line, fin, "<?ifquery?>", this->query);
113 conditional(line, fin, "<?ifresults?>", results);
114 conditional(line, fin, "<?ifor?>", or_);
115 conditional(line, fin, "<?ifand?>", and_);
116 conditional(line, fin, "<?ifignore?>", ignore != "");
117 conditional(line, fin, "<?ifcommon?>", common.size() == 1);
118 conditional(line, fin, "<?ifmanycommon?>", common.size() > 1);
119 #ifndef _OpenSSL_
120 tag(line, "<?version?>", programName + ' ' + programVersion + ' ' +
121 platform());
122 #else
123 tag(line, "<?version?>", programName + ' ' + programVersion + ' ' +
124 platform() + ' ' + openssl());
125 #endif
126 tag(line, "<?query?>", query);
127 tag(line, "<?range?>", range(page));
128 tag(line, "<?total?>", total());
129 tag(line, "<?time?>", duration());
130 tag(line, "<?pages?>", pages(query, page));
131 tag(line, "<?ignore?>", ignore);
132 tag(line, "<?common?>", common[0]);
133 tag(line, "<?manycommon?>", manycommon(common));
134
135 cout << line << (fin.good() ? "\n" : "");
136 }
137
138 fin.close();
139 }
140
141 void Outputter::body()
142 {
143 for (size_t index = 0; index < webpages.size(); index++)
144 {
145 Ranker webpage(webpages[index]);
146 string title(webpage.getTitle());
147
148 if (title == "")
149 {
150 title = webpage.getURL();
151
152 entities(title, '&', "&amp;");
153 entities(title, '\"', "&quot;");
154 entities(title, '<', "&lt;");
155 entities(title, '>', "&gt;");
156 }
157
158 string address(webpage.getURL()), sample(webpage.getSample()),
159 description(webpage.getDescription());
160 ostringstream size;
161
162 size.precision(0);
163 size.setf(ios_base::fixed, ios_base::floatfield);
164
165 size << double(webpage.getSize()) / double(1024) << "k";
166
167 entities(address, '&', "&amp;");
168 entities(address, '\"', "&quot;");
169 entities(address, '<', "&lt;");
170 entities(address, '>', "&gt;");
171
172 ifstream fin(bodyFile.c_str());
173 string line;
174
175 while (fin.good())
176 {
177 getline(fin, line);
178 conditional(line, fin, "<?ifdescription?>", description != "");
179 tag(line, "<?address?>", address);
180 tag(line, "<?title?>", title);
181 tag(line, "<?sample?>", sample);
182 tag(line, "<?description?>", description);
183 tag(line, "<?size?>", size.str());
184
185 cout << line << (fin.good() ? "\n" : "");
186 }
187
188 fin.close();
189 }
190 }
191
192 void Outputter::footer(const string& query, size_t page, const vector<string>&
193 common, bool and_, bool or_, const string& ignore)
194 {
195 ifstream fin(footerFile.c_str());
196 string line;
197
198 while (fin.good())
199 {
200 getline(fin, line);
201 conditional(line, fin, "<?ifquery?>", this->query);
202 conditional(line, fin, "<?ifresults?>", results);
203 conditional(line, fin, "<?ifor?>", or_);
204 conditional(line, fin, "<?ifand?>", and_);
205 conditional(line, fin, "<?ifignore?>", ignore != "");
206 conditional(line, fin, "<?ifcommon?>", common.size() == 1);
207 conditional(line, fin, "<?ifmanycommon?>", common.size() > 1);
208 #ifndef _OpenSSL_
209 tag(line, "<?version?>", programName + ' ' + programVersion + ' ' +
210 platform());
211 #else
212 tag(line, "<?version?>", programName + ' ' + programVersion + ' ' +
213 platform() + ' ' + openssl());
214 #endif
215 tag(line, "<?query?>", query);
216 tag(line, "<?range?>", range(page));
217 tag(line, "<?total?>", total());
218 tag(line, "<?time?>", duration());
219 tag(line, "<?pages?>", pages(query, page));
220 tag(line, "<?ignore?>", ignore);
221 tag(line, "<?common?>", common[0]);
222 tag(line, "<?manycommon?>", manycommon(common));
223
224 cout << line << (fin.good() ? "\n" : "");
225 }
226
227 fin.close();
228 }
229
230 void Outputter::notfound(const string& query, size_t keywords)
231 {
232 ifstream fin(notfoundFile.c_str());
233 string line;
234
235 while (fin.good())
236 {
237 getline(fin, line);
238 conditional(line, fin, "<?ifmany?>", keywords > 1);
239 tag(line, "<?query?>", query);
240
241 cout << line << (fin.good() ? "\n" : "");
242 }
243
244 fin.close();
245 }
246
247 string Outputter::pages(string query, size_t page)
248 {
249 entities(query, "&lt;", '<');
250 entities(query, "&gt;", '>');
251 entities(query, "&quot;", '\"');
252 entities(query, "&amp;", '&');
253 entities(query, '%', "%25");
254 entities(query, '\t', "%09");
255 entities(query, ' ', "%20");
256 entities(query, '\"', "%22");
257 entities(query, '#', "%23");
258 entities(query, '$', "%24");
259 entities(query, '&', "%26");
260 entities(query, '\'', "%27");
261 entities(query, '+', "%2B");
262 entities(query, ',', "%2C");
263 entities(query, '/', "%2F");
264 entities(query, ':', "%3A");
265 entities(query, ';', "%3B");
266 entities(query, '<', "%3C");
267 entities(query, '=', "%3D");
268 entities(query, '>', "%3E");
269 entities(query, '?', "%3F");
270 entities(query, '@', "%40");
271 entities(query, '[', "%5B");
272 entities(query, ']', "%5D");
273 entities(query, '\\', "%5C");
274 entities(query, '^', "%5E");
275 entities(query, '`', "%60");
276 entities(query, '{', "%7B");
277 entities(query, '|', "%7C");
278 entities(query, '}', "%7D");
279 entities(query, '~', "%7E");
280
281 ifstream fin(pagesFile.c_str());
282 string lines, line;
283
284 while (fin.good())
285 {
286 getline(fin, line);
287 conditional(line, fin, "<?ifprevious?>", page >= 1);
288 conditional(line, fin, "<?ifpage?>", false);
289 conditional(line, fin, "<?ifnum?>", false);
290 conditional(line, fin, "<?ifnext?>", false);
291
292 ostringstream previous;
293
294 previous << page;
295
296 tag(line, "<?query?>", query);
297 tag(line, "<?previous?>", previous.str());
298
299 lines += line + (fin.good() ? "\n" : "");
300 }
301
302 fin.close();
303 fin.clear();
304
305 for (size_t index(0); index < numPages; index++)
306 {
307 fin.open(pagesFile.c_str());
308
309 while (fin.good())
310 {
311 getline(fin, line);
312
313 if (index == page)
314 {
315 conditional(line, fin, "<?ifprevious?>", false);
316 conditional(line, fin, "<?ifpage?>", true);
317 conditional(line, fin, "<?ifnum?>", false);
318 conditional(line, fin, "<?ifnext?>", false);
319
320 ostringstream current;
321
322 current << index + 1;
323
324 tag(line, "<?page?>", current.str());
325 }
326 else
327 {
328 conditional(line, fin, "<?ifprevious?>", false);
329 conditional(line, fin, "<?ifpage?>", false);
330 conditional(line, fin, "<?ifnum?>", true);
331 conditional(line, fin, "<?ifnext?>", false);
332
333 ostringstream num;
334
335 num << index + 1;
336
337 tag(line, "<?query?>", query);
338 tag(line, "<?num?>", num.str());
339 }
340
341 lines += line + (fin.good() ? "\n" : "");
342 }
343
344 fin.close();
345 fin.clear();
346 }
347
348 fin.open(pagesFile.c_str());
349
350 while (fin.good())
351 {
352 getline(fin, line);
353 conditional(line, fin, "<?ifprevious?>", false);
354 conditional(line, fin, "<?ifpage?>", false);
355 conditional(line, fin, "<?ifnum?>", false);
356 conditional(line, fin, "<?ifnext?>", page + 2 <= numPages);
357
358 ostringstream next;
359
360 next << page + 2;
361
362 tag(line, "<?query?>", query);
363 tag(line, "<?next?>", next.str());
364
365 lines += line + (fin.good() ? "\n" : "");
366 }
367
368 fin.close();
369
370 return lines;
371 }
372
373 string Outputter::range(size_t page)
374 {
375 size_t bottom(page * 10 + 1), top(numWebpages > page * 10 + 10 ? page *
376 10 + 10 : numWebpages);
377 ostringstream range;
378
379 range << bottom << " - " << top;
380
381 return range.str();
382 }
383
384 string Outputter::total()
385 {
386 ostringstream total;
387
388 total << numWebpages;
389
390 return total.str();
391 }
392
393 string Outputter::duration()
394 {
395 ostringstream duration;
396
397 duration.precision(2);
398 duration.setf(ios_base::fixed, ios_base::floatfield);
399
400 duration << time;
401
402 return duration.str();
403 }
404
405 string Outputter::manycommon(const vector<string>& common)
406 {
407 string line;
408
409 for (size_t index = 0; index < common.size(); index++)
410 {
411 line += common[index];
412
413 if (index != common.size() - 1) line += ' ';
414 }
415
416 return line;
417 }
418
419 void Outputter::tag(string& line, const char* tag, const string& replacement)
420 {
421 size_t begin(0);
422
423 while (begin < line.length())
424 {
425 int spot(line.find(tag, begin));
426
427 if (spot != string::npos)
428 {
429 line.replace(spot, strlen(tag), replacement);
430 }
431 else break;
432
433 begin = spot + replacement.length();
434 }
435 }
436
437 void Outputter::conditional(string& line, ifstream& fin, const char* tag, bool
438 condition)
439 {
440 size_t begin(0);
441
442 while (begin < line.length())
443 {
444 size_t start(line.find(tag, begin)), finish(line.find("<?endif?>",
445 start));
446
447 if (start == string::npos) break;
448
449 string next;
450
451 while (finish == string::npos)
452 {
453 getline(fin, next);
454
455 line += '\n' + next;
456
457 finish = line.find("<?endif?>", start);
458 }
459
460 if (condition)
461 {
462 line.erase(start, strlen(tag));
463 line.erase(finish - strlen(tag), 9);
464
465 begin = finish - strlen(tag) - 9;
466 }
467 else
468 {
469 line.erase(start, finish - start + 9);
470
471 begin = start;
472 }
473 }
474 }

Properties

Name Value
svn:eol-style native
svn:keywords Id