ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/trunk/Search/Outputer.cpp
Revision: 205
Committed: 2003-07-17T00:03:08-07:00 (21 years, 11 months ago) by douglas
File size: 12897 byte(s)
Log Message:
Moved openssl() to Search.h and fixed <?version?> tag to use it.

File Contents

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