ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/trunk/Search/Outputer.cpp
Revision: 207
Committed: 2003-07-17T20:52:39-07:00 (21 years, 11 months ago) by douglas
File size: 12485 byte(s)
Log Message:
Replaced some sprintf stuff with ostringstream stuff, booyah!

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.10 2003/07/18 03:52:39 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 #ifndef _OpenSSL_
136 tag(line, "<?version?>", programName + ' ' + programVersion + ' ' +
137 platform());
138 #else
139 tag(line, "<?version?>", programName + ' ' + programVersion + ' ' +
140 platform() + ' ' + openssl());
141 #endif
142 tag(line, "<?query?>", query);
143 tag(line, "<?range?>", range(page));
144 tag(line, "<?total?>", total());
145 tag(line, "<?time?>", duration());
146 tag(line, "<?pages?>", pages(query, page));
147 tag(line, "<?ignore?>", ignore);
148 tag(line, "<?common?>", common[0]);
149 tag(line, "<?manycommon?>", manycommon(common));
150
151 cout << line << (fin.good() ? "\n" : "");
152 }
153
154 fin.close();
155 }
156
157 void Outputer::body()
158 {
159 for (int index = 0; index < webpages.size(); index++)
160 {
161 Ranker webpage = webpages[index];
162 string title = webpage.getTitle();
163 if (title == "")
164 {
165 title = webpage.getURL();
166 entities(title, '&', "&amp;");
167 entities(title, '\"', "&quot;");
168 entities(title, '<', "&lt;");
169 entities(title, '>', "&gt;");
170 }
171 string address = webpage.getURL();
172 string sample = webpage.getSample();
173 string description = webpage.getDescription();
174 ostringstream size;
175
176 size.precision(1);
177 size << double(webpage.getSize()) / double(1024) << "k";
178
179 entities(address, '&', "&amp;");
180 entities(address, '\"', "&quot;");
181 entities(address, '<', "&lt;");
182 entities(address, '>', "&gt;");
183
184 ifstream fin(bodyFile.c_str());
185
186 string line;
187 while (fin.good())
188 {
189 getline(fin, line);
190
191 conditional(line, fin, "<?ifdescription?>", description != "");
192
193 tag(line, "<?address?>", address);
194 tag(line, "<?title?>", title);
195 tag(line, "<?sample?>", sample);
196 tag(line, "<?description?>", description);
197 tag(line, "<?size?>", size.str());
198
199 cout << line << (fin.good() ? "\n" : "");
200 }
201
202 fin.close();
203 }
204 }
205
206 void Outputer::footer(const string& query, unsigned page, vector<string>
207 common, bool and_, bool or_, const string& ignore)
208 {
209 ifstream fin(footerFile.c_str());
210
211 string line;
212 while (fin.good())
213 {
214 getline(fin, line);
215
216 conditional(line, fin, "<?ifquery?>", this->query);
217 conditional(line, fin, "<?ifresults?>", results);
218 conditional(line, fin, "<?ifor?>", or_);
219 conditional(line, fin, "<?ifand?>", and_);
220 conditional(line, fin, "<?ifignore?>", ignore != "");
221 conditional(line, fin, "<?ifcommon?>", common.size() == 1);
222 conditional(line, fin, "<?ifmanycommon?>", common.size() > 1);
223
224 #ifndef _OpenSSL_
225 tag(line, "<?version?>", programName + ' ' + programVersion + ' ' +
226 platform());
227 #else
228 tag(line, "<?version?>", programName + ' ' + programVersion + ' ' +
229 platform() + ' ' + openssl());
230 #endif
231 tag(line, "<?query?>", query);
232 tag(line, "<?range?>", range(page));
233 tag(line, "<?total?>", total());
234 tag(line, "<?time?>", duration());
235 tag(line, "<?pages?>", pages(query, page));
236 tag(line, "<?ignore?>", ignore);
237 tag(line, "<?common?>", common[0]);
238 tag(line, "<?manycommon?>", manycommon(common));
239
240 cout << line << (fin.good() ? "\n" : "");
241 }
242
243 fin.close();
244 }
245
246 void Outputer::notfound(const string& query, unsigned keywords)
247 {
248 ifstream fin(notfoundFile.c_str());
249
250 string line;
251 while (fin.good())
252 {
253 getline(fin, line);
254
255 conditional(line, fin, "<?ifmany?>", keywords > 1);
256
257 tag(line, "<?query?>", query);
258
259 cout << line << (fin.good() ? "\n" : "");
260 }
261
262 fin.close();
263 }
264
265 string Outputer::pages(string query, unsigned page)
266 {
267 entities(query, "&lt;", '<');
268 entities(query, "&gt;", '>');
269 entities(query, "&quot;", '\"');
270 entities(query, "&amp;", '&');
271
272 entities(query, '%', "%25");
273 entities(query, '\t', "%09");
274 entities(query, ' ', "%20");
275 entities(query, '\"', "%22");
276 entities(query, '#', "%23");
277 entities(query, '$', "%24");
278 entities(query, '&', "%26");
279 entities(query, '\'', "%27");
280 entities(query, '+', "%2B");
281 entities(query, ',', "%2C");
282 entities(query, '/', "%2F");
283 entities(query, ':', "%3A");
284 entities(query, ';', "%3B");
285 entities(query, '<', "%3C");
286 entities(query, '=', "%3D");
287 entities(query, '>', "%3E");
288 entities(query, '?', "%3F");
289 entities(query, '@', "%40");
290 entities(query, '[', "%5B");
291 entities(query, ']', "%5D");
292 entities(query, '\\', "%5C");
293 entities(query, '^', "%5E");
294 entities(query, '`', "%60");
295 entities(query, '{', "%7B");
296 entities(query, '|', "%7C");
297 entities(query, '}', "%7D");
298 entities(query, '~', "%7E");
299
300 string lines;
301
302 ifstream fin(pagesFile.c_str());
303
304 string line;
305 while (fin.good())
306 {
307 getline(fin, line);
308 conditional(line, fin, "<?ifprevious?>", page >= 1);
309 conditional(line, fin, "<?ifpage?>", false);
310 conditional(line, fin, "<?ifnum?>", false);
311 conditional(line, fin, "<?ifnext?>", false);
312
313 ostringstream previous;
314
315 previous << page;
316
317 tag(line, "<?query?>", query);
318 tag(line, "<?previous?>", previous.str());
319
320 lines += line + (fin.good() ? "\n" : "");
321 }
322
323 fin.close();
324 fin.clear();
325
326 for (int index = 0; index < numPages; index++)
327 {
328 fin.open(pagesFile.c_str());
329
330 while (fin.good())
331 {
332 getline(fin, line);
333 if (index == page)
334 {
335 conditional(line, fin, "<?ifprevious?>", false);
336 conditional(line, fin, "<?ifpage?>", true);
337 conditional(line, fin, "<?ifnum?>", false);
338 conditional(line, fin, "<?ifnext?>", false);
339
340 ostringstream current;
341
342 current << index + 1;
343
344 tag(line, "<?page?>", current.str());
345 }
346 else
347 {
348 conditional(line, fin, "<?ifprevious?>", false);
349 conditional(line, fin, "<?ifpage?>", false);
350 conditional(line, fin, "<?ifnum?>", true);
351 conditional(line, fin, "<?ifnext?>", false);
352
353 ostringstream num;
354
355 num << index + 1;
356
357 tag(line, "<?query?>", query);
358 tag(line, "<?num?>", num.str());
359 }
360
361 lines += line + (fin.good() ? "\n" : "");
362 }
363
364 fin.close();
365 fin.clear();
366 }
367
368 fin.open(pagesFile.c_str());
369
370 while (fin.good())
371 {
372 getline(fin, line);
373 conditional(line, fin, "<?ifprevious?>", false);
374 conditional(line, fin, "<?ifpage?>", false);
375 conditional(line, fin, "<?ifnum?>", false);
376 conditional(line, fin, "<?ifnext?>", page + 2 <= numPages);
377
378 ostringstream next;
379
380 next << page + 2;
381
382 tag(line, "<?query?>", query);
383 tag(line, "<?next?>", next.str());
384
385 lines += line + (fin.good() ? "\n" : "");
386 }
387
388 fin.close();
389
390 return lines;
391 }
392
393 string Outputer::range(unsigned page)
394 {
395 unsigned bottom = page * 10 + 1;
396 unsigned top = numWebpages > page * 10 + 10 ? page * 10 + 10 : numWebpages;
397 ostringstream range;
398
399 range << bottom << " - " << top;
400
401 return range.str();
402 }
403
404 string Outputer::total()
405 {
406 ostringstream total;
407
408 total << numWebpages;
409
410 return total.str();
411 }
412
413 string Outputer::duration()
414 {
415 ostringstream duration;
416
417 duration.precision(2);
418 duration << time;
419
420 return duration.str();
421 }
422
423 string Outputer::manycommon(vector<string> common)
424 {
425 string line;
426
427 for (int index = 0; index < common.size(); index++)
428 {
429 line += common[index];
430
431 if (index != common.size() - 1) line += ' ';
432 }
433
434 return line;
435 }
436
437 void Outputer::tag(string& line, char* tag, const string& replacement)
438 {
439 int begin = 0;
440 while (begin < line.length())
441 {
442 int spot = line.find(tag, begin);
443
444 if (spot != string::npos)
445 {
446 line.replace(spot, strlen(tag), replacement);
447 }
448 else
449 {
450 break;
451 }
452
453 begin = spot + replacement.length();
454 }
455 }
456
457 void Outputer::conditional(string& line, ifstream& fin, char* tag, bool
458 condition)
459 {
460 unsigned begin = 0;
461 while (begin < line.length())
462 {
463 unsigned start = line.find(tag, begin);
464 unsigned finish = line.find("<?endif?>", start);
465
466 if (start == string::npos) break;
467
468 string next;
469 while (finish == string::npos)
470 {
471 getline(fin, next);
472 line += '\n' + next;
473 finish = line.find("<?endif?>", start);
474 }
475
476 if (condition)
477 {
478 line.erase(start, strlen(tag));
479 line.erase(finish - strlen(tag), 9);
480
481 begin = finish - strlen(tag) - 9;
482 }
483 else
484 {
485 line.erase(start, finish - start + 9);
486
487 begin = start;
488 }
489 }
490 }