ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/trunk/Search/Outputer.cpp
Revision: 183
Committed: 2003-07-05T23:31:12-07:00 (21 years, 11 months ago) by douglas
File size: 12717 byte(s)
Log Message:
Fixed some stuff.

File Contents

# User Rev Content
1 douglas 1 /* ============================================================================
2     * Douglas Thrift's Search Engine License
3     *
4 douglas 28 * Copyright (C) 2002-2003, Douglas Thrift. All Rights Reserved.
5 douglas 1 * 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     // Outputer.cpp
50    
51     #include "Outputer.h"
52    
53 douglas 28 Outputer::Outputer(const string& headerFile, const string& bodyFile, const
54 douglas 15 string& footerFile, const string& notfoundFile, const string& pagesFile)
55 douglas 1 {
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 douglas 183 cerr << "query = " << this->query << "\n"
90     << "results = " << results << "\n"
91 douglas 1 << "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 douglas 15 void Outputer::header(const string& query, unsigned page, vector<string>
118     common, bool and_, bool or_, const string& ignore)
119 douglas 1 {
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 douglas 43 tag(line, "<?version?>", programName + ' ' + programVersion + ' ' +
136     platform());
137 douglas 1 tag(line, "<?query?>", query);
138 douglas 15 tag(line, "<?range?>", range(page));
139     tag(line, "<?total?>", total());
140     tag(line, "<?time?>", duration());
141     tag(line, "<?pages?>", pages(query, page));
142 douglas 1 tag(line, "<?ignore?>", ignore);
143     tag(line, "<?common?>", common[0]);
144 douglas 15 tag(line, "<?manycommon?>", manycommon(common));
145 douglas 1
146 douglas 30 cout << line << (fin.good() ? "\n" : "");
147 douglas 1 }
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 douglas 30 cout << line << (fin.good() ? "\n" : "");
198 douglas 1 }
199    
200     fin.close();
201     }
202     }
203    
204 douglas 15 void Outputer::footer(const string& query, unsigned page, vector<string>
205     common, bool and_, bool or_, const string& ignore)
206 douglas 1 {
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 douglas 43 tag(line, "<?version?>", programName + ' ' + programVersion + ' ' +
223     platform());
224 douglas 1 tag(line, "<?query?>", query);
225 douglas 15 tag(line, "<?range?>", range(page));
226     tag(line, "<?total?>", total());
227     tag(line, "<?time?>", duration());
228     tag(line, "<?pages?>", pages(query, page));
229 douglas 1 tag(line, "<?ignore?>", ignore);
230     tag(line, "<?common?>", common[0]);
231 douglas 15 tag(line, "<?manycommon?>", manycommon(common));
232 douglas 1
233 douglas 30 cout << line << (fin.good() ? "\n" : "");
234 douglas 1 }
235    
236     fin.close();
237     }
238    
239 douglas 15 void Outputer::notfound(const string& query, unsigned keywords)
240 douglas 1 {
241     ifstream fin(notfoundFile.c_str());
242    
243     string line;
244     while (fin.good())
245     {
246     getline(fin, line);
247    
248     conditional(line, fin, "<?ifmany?>", keywords > 1);
249    
250     tag(line, "<?query?>", query);
251    
252 douglas 30 cout << line << (fin.good() ? "\n" : "");
253 douglas 1 }
254    
255     fin.close();
256     }
257    
258     string Outputer::pages(string query, unsigned page)
259     {
260     entities(query, "&lt;", '<');
261     entities(query, "&gt;", '>');
262     entities(query, "&quot;", '\"');
263     entities(query, "&amp;", '&');
264    
265     entities(query, '%', "%25");
266     entities(query, '\t', "%09");
267     entities(query, ' ', "%20");
268     entities(query, '\"', "%22");
269     entities(query, '#', "%23");
270     entities(query, '$', "%24");
271     entities(query, '&', "%26");
272     entities(query, '\'', "%27");
273     entities(query, '+', "%2B");
274     entities(query, ',', "%2C");
275     entities(query, '/', "%2F");
276     entities(query, ':', "%3A");
277     entities(query, ';', "%3B");
278     entities(query, '<', "%3C");
279     entities(query, '=', "%3D");
280     entities(query, '>', "%3E");
281     entities(query, '?', "%3F");
282     entities(query, '@', "%40");
283     entities(query, '[', "%5B");
284     entities(query, ']', "%5D");
285     entities(query, '\\', "%5C");
286     entities(query, '^', "%5E");
287     entities(query, '`', "%60");
288     entities(query, '{', "%7B");
289     entities(query, '|', "%7C");
290     entities(query, '}', "%7D");
291     entities(query, '~', "%7E");
292    
293     string lines;
294    
295     ifstream fin(pagesFile.c_str());
296    
297     string line;
298     while (fin.good())
299     {
300     getline(fin, line);
301     conditional(line, fin, "<?ifprevious?>", page >= 1);
302     conditional(line, fin, "<?ifpage?>", false);
303     conditional(line, fin, "<?ifnum?>", false);
304     conditional(line, fin, "<?ifnext?>", false);
305    
306     char* cprevious = new char[1024];
307    
308     sprintf(cprevious, "%u", page);
309    
310     string previous = cprevious;
311    
312     delete [] cprevious;
313    
314     tag(line, "<?query?>", query);
315     tag(line, "<?previous?>", previous);
316    
317 douglas 30 lines += line + (fin.good() ? "\n" : "");
318 douglas 1 }
319    
320     fin.close();
321     fin.clear();
322    
323     for (int index = 0; index < numPages; index++)
324     {
325     fin.open(pagesFile.c_str());
326    
327     while (fin.good())
328     {
329     getline(fin, line);
330     if (index == page)
331     {
332     conditional(line, fin, "<?ifprevious?>", false);
333     conditional(line, fin, "<?ifpage?>", true);
334     conditional(line, fin, "<?ifnum?>", false);
335     conditional(line, fin, "<?ifnext?>", false);
336    
337     char* cpage = new char[1024];
338    
339     sprintf(cpage, "%u", (index + 1));
340    
341     string spage = cpage;
342    
343     delete [] cpage;
344    
345     tag(line, "<?page?>", spage);
346     }
347     else
348     {
349     conditional(line, fin, "<?ifprevious?>", false);
350     conditional(line, fin, "<?ifpage?>", false);
351     conditional(line, fin, "<?ifnum?>", true);
352     conditional(line, fin, "<?ifnext?>", false);
353    
354     char* cnum = new char[1024];
355    
356     sprintf(cnum, "%u", (index + 1));
357    
358     string num = cnum;
359    
360     delete [] cnum;
361    
362     tag(line, "<?query?>", query);
363     tag(line, "<?num?>", num);
364     }
365    
366 douglas 30 lines += line + (fin.good() ? "\n" : "");
367 douglas 1 }
368    
369     fin.close();
370     fin.clear();
371     }
372    
373     fin.open(pagesFile.c_str());
374    
375     while (fin.good())
376     {
377     getline(fin, line);
378     conditional(line, fin, "<?ifprevious?>", false);
379     conditional(line, fin, "<?ifpage?>", false);
380     conditional(line, fin, "<?ifnum?>", false);
381     conditional(line, fin, "<?ifnext?>", page + 2 <= numPages);
382    
383     char* cnext = new char[1024];
384    
385     sprintf(cnext, "%u", (page + 2));
386    
387     string next = cnext;
388    
389     delete [] cnext;
390    
391     tag(line, "<?query?>", query);
392     tag(line, "<?next?>", next);
393    
394 douglas 30 lines += line + (fin.good() ? "\n" : "");
395 douglas 1 }
396    
397     fin.close();
398    
399     return lines;
400     }
401    
402     string Outputer::range(unsigned page)
403     {
404     unsigned bottom = page * 10 + 1;
405     unsigned top = numWebpages > page * 10 + 10 ? page * 10 + 10 : numWebpages;
406    
407     char* cbottom = new char[1024];
408     char* ctop = new char[1024];
409    
410     sprintf(cbottom, "%u", bottom);
411     sprintf(ctop, "%u", top);
412    
413     string range = string(cbottom) + " - " + ctop;
414    
415     delete [] cbottom;
416     delete [] ctop;
417    
418     return range;
419     }
420    
421     string Outputer::total()
422     {
423     char* ctotal = new char[1024];
424    
425     sprintf(ctotal, "%u", numWebpages);
426    
427     string total = ctotal;
428    
429     delete [] ctotal;
430    
431     return total;
432     }
433    
434     string Outputer::duration()
435     {
436     char* ctime = new char[1024];
437     sprintf(ctime, "%.2f", time);
438    
439     string duration = ctime;
440    
441     delete [] ctime;
442    
443     return duration;
444     }
445    
446     string Outputer::manycommon(vector<string> common)
447     {
448     string line;
449    
450     for (int index = 0; index < common.size(); index++)
451     {
452     line += common[index];
453    
454     if (index != common.size() - 1) line += ' ';
455     }
456    
457     return line;
458     }
459    
460 douglas 15 void Outputer::tag(string& line, char* tag, const string& replacement)
461 douglas 1 {
462     int begin = 0;
463     while (begin < line.length())
464     {
465     int spot = line.find(tag, begin);
466    
467     if (spot != string::npos)
468     {
469     line.replace(spot, strlen(tag), replacement);
470     }
471     else
472     {
473     break;
474     }
475    
476     begin = spot + replacement.length();
477     }
478     }
479    
480     void Outputer::conditional(string& line, ifstream& fin, char* tag, bool
481     condition)
482     {
483     unsigned begin = 0;
484     while (begin < line.length())
485     {
486     unsigned start = line.find(tag, begin);
487     unsigned finish = line.find("<?endif?>", start);
488    
489     if (start == string::npos) break;
490    
491     string next;
492     while (finish == string::npos)
493     {
494     getline(fin, next);
495     line += '\n' + next;
496     finish = line.find("<?endif?>", start);
497     }
498    
499     if (condition)
500     {
501     line.erase(start, strlen(tag));
502     line.erase(finish - strlen(tag), 9);
503    
504     begin = finish - strlen(tag) - 9;
505     }
506     else
507     {
508     line.erase(start, finish - start + 9);
509    
510     begin = start;
511     }
512     }
513     }