1 |
|
/* ============================================================================ |
2 |
|
* Douglas Thrift's Search Engine License |
3 |
|
* |
4 |
< |
* Copyright (C) 2002, Douglas Thrift. All Rights Reserved. |
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 |
|
* |
46 |
|
// |
47 |
|
// Douglas Thrift |
48 |
|
// |
49 |
< |
// Outputer.cpp |
49 |
> |
// $Id: Outputer.cpp,v 1.11 2003/07/20 01:46:12 douglas Exp $ |
50 |
|
|
51 |
|
#include "Outputer.h" |
52 |
|
|
53 |
< |
Outputer::Outputer(string& headerFile, string& bodyFile, string& footerFile, |
54 |
< |
string& notfoundFile, string& pagesFile) |
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; |
86 |
|
|
87 |
|
if (debug) |
88 |
|
{ |
89 |
< |
cerr << "query = " << (this->query ? "true" : "false") << "\n" |
90 |
< |
<< "results = " << (results ? "true" : "false") << "\n" |
89 |
> |
cerr << "query = " << this->query << "\n" |
90 |
> |
<< "results = " << results << "\n" |
91 |
|
<< "time = " << duration() << "\n"; |
92 |
|
} |
93 |
|
|
114 |
|
ignore); |
115 |
|
} |
116 |
|
|
117 |
< |
void Outputer::header(string& query, unsigned page, vector<string> common, |
118 |
< |
bool and_, bool or_, string& ignore) |
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 |
|
|
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 |
< |
|
144 |
< |
string fred = range(page); |
145 |
< |
|
146 |
< |
tag(line, "<?range?>", fred); |
140 |
< |
|
141 |
< |
string martha = total(); |
142 |
< |
|
143 |
< |
tag(line, "<?total?>", martha); |
144 |
< |
|
145 |
< |
string george = duration(); |
146 |
< |
|
147 |
< |
tag(line, "<?time?>", george); |
148 |
< |
|
149 |
< |
string derf = pages(query, page); |
150 |
< |
|
151 |
< |
tag(line, "<?pages?>", derf); |
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 |
< |
string elaine = manycommon(common); |
156 |
< |
|
157 |
< |
tag(line, "<?manycommon?>", elaine); |
158 |
< |
|
159 |
< |
cout << line << "\n"; |
151 |
> |
cout << line << (fin.good() ? "\n" : ""); |
152 |
|
} |
153 |
|
|
154 |
|
fin.close(); |
171 |
|
string address = webpage.getURL(); |
172 |
|
string sample = webpage.getSample(); |
173 |
|
string description = webpage.getDescription(); |
174 |
+ |
ostringstream size; |
175 |
|
|
176 |
< |
char* csize = new char[1024]; |
177 |
< |
sprintf(csize, "%.0fk", (double(webpage.getSize()) / double(1024))); |
178 |
< |
|
186 |
< |
string size = csize; |
187 |
< |
|
188 |
< |
delete [] csize; |
176 |
> |
size.precision(0); |
177 |
> |
size.setf(ios_base::fixed, ios_base::floatfield); |
178 |
> |
size << double(webpage.getSize()) / double(1024) << "k"; |
179 |
|
|
180 |
|
entities(address, '&', "&"); |
181 |
|
entities(address, '\"', """); |
195 |
|
tag(line, "<?title?>", title); |
196 |
|
tag(line, "<?sample?>", sample); |
197 |
|
tag(line, "<?description?>", description); |
198 |
< |
tag(line, "<?size?>", size); |
198 |
> |
tag(line, "<?size?>", size.str()); |
199 |
|
|
200 |
< |
cout << line << "\n"; |
200 |
> |
cout << line << (fin.good() ? "\n" : ""); |
201 |
|
} |
202 |
|
|
203 |
|
fin.close(); |
204 |
|
} |
205 |
|
} |
206 |
|
|
207 |
< |
void Outputer::footer(string& query, unsigned page, vector<string> common, |
208 |
< |
bool and_, bool or_, string& ignore) |
207 |
> |
void Outputer::footer(const string& query, unsigned page, vector<string> |
208 |
> |
common, bool and_, bool or_, const string& ignore) |
209 |
|
{ |
210 |
|
ifstream fin(footerFile.c_str()); |
211 |
|
|
222 |
|
conditional(line, fin, "<?ifcommon?>", common.size() == 1); |
223 |
|
conditional(line, fin, "<?ifmanycommon?>", common.size() > 1); |
224 |
|
|
225 |
+ |
#ifndef _OpenSSL_ |
226 |
+ |
tag(line, "<?version?>", programName + ' ' + programVersion + ' ' + |
227 |
+ |
platform()); |
228 |
+ |
#else |
229 |
+ |
tag(line, "<?version?>", programName + ' ' + programVersion + ' ' + |
230 |
+ |
platform() + ' ' + openssl()); |
231 |
+ |
#endif |
232 |
|
tag(line, "<?query?>", query); |
233 |
< |
|
234 |
< |
string fred = range(page); |
235 |
< |
|
236 |
< |
tag(line, "<?range?>", fred); |
240 |
< |
|
241 |
< |
string martha = total(); |
242 |
< |
|
243 |
< |
tag(line, "<?total?>", martha); |
244 |
< |
|
245 |
< |
string george = duration(); |
246 |
< |
|
247 |
< |
tag(line, "<?time?>", george); |
248 |
< |
|
249 |
< |
string derf = pages(query, page); |
250 |
< |
|
251 |
< |
tag(line, "<?pages?>", derf); |
233 |
> |
tag(line, "<?range?>", range(page)); |
234 |
> |
tag(line, "<?total?>", total()); |
235 |
> |
tag(line, "<?time?>", duration()); |
236 |
> |
tag(line, "<?pages?>", pages(query, page)); |
237 |
|
tag(line, "<?ignore?>", ignore); |
238 |
|
tag(line, "<?common?>", common[0]); |
239 |
+ |
tag(line, "<?manycommon?>", manycommon(common)); |
240 |
|
|
241 |
< |
string elaine = manycommon(common); |
256 |
< |
|
257 |
< |
tag(line, "<?manycommon?>", elaine); |
258 |
< |
|
259 |
< |
cout << line << "\n"; |
241 |
> |
cout << line << (fin.good() ? "\n" : ""); |
242 |
|
} |
243 |
|
|
244 |
|
fin.close(); |
245 |
|
} |
246 |
|
|
247 |
< |
void Outputer::notfound(string& query, unsigned keywords) |
247 |
> |
void Outputer::notfound(const string& query, unsigned keywords) |
248 |
|
{ |
249 |
|
ifstream fin(notfoundFile.c_str()); |
250 |
|
|
257 |
|
|
258 |
|
tag(line, "<?query?>", query); |
259 |
|
|
260 |
< |
cout << line << "\n"; |
260 |
> |
cout << line << (fin.good() ? "\n" : ""); |
261 |
|
} |
262 |
|
|
263 |
|
fin.close(); |
311 |
|
conditional(line, fin, "<?ifnum?>", false); |
312 |
|
conditional(line, fin, "<?ifnext?>", false); |
313 |
|
|
314 |
< |
char* cprevious = new char[1024]; |
333 |
< |
|
334 |
< |
sprintf(cprevious, "%u", page); |
314 |
> |
ostringstream previous; |
315 |
|
|
316 |
< |
string previous = cprevious; |
337 |
< |
|
338 |
< |
delete [] cprevious; |
316 |
> |
previous << page; |
317 |
|
|
318 |
|
tag(line, "<?query?>", query); |
319 |
< |
tag(line, "<?previous?>", previous); |
319 |
> |
tag(line, "<?previous?>", previous.str()); |
320 |
|
|
321 |
< |
lines += line + "\n"; |
321 |
> |
lines += line + (fin.good() ? "\n" : ""); |
322 |
|
} |
323 |
|
|
324 |
|
fin.close(); |
338 |
|
conditional(line, fin, "<?ifnum?>", false); |
339 |
|
conditional(line, fin, "<?ifnext?>", false); |
340 |
|
|
341 |
< |
char* cpage = new char[1024]; |
364 |
< |
|
365 |
< |
sprintf(cpage, "%u", (index + 1)); |
341 |
> |
ostringstream current; |
342 |
|
|
343 |
< |
string spage = cpage; |
343 |
> |
current << index + 1; |
344 |
|
|
345 |
< |
delete [] cpage; |
370 |
< |
|
371 |
< |
tag(line, "<?page?>", spage); |
345 |
> |
tag(line, "<?page?>", current.str()); |
346 |
|
} |
347 |
|
else |
348 |
|
{ |
351 |
|
conditional(line, fin, "<?ifnum?>", true); |
352 |
|
conditional(line, fin, "<?ifnext?>", false); |
353 |
|
|
354 |
< |
char* cnum = new char[1024]; |
381 |
< |
|
382 |
< |
sprintf(cnum, "%u", (index + 1)); |
354 |
> |
ostringstream num; |
355 |
|
|
356 |
< |
string num = cnum; |
385 |
< |
|
386 |
< |
delete [] cnum; |
356 |
> |
num << index + 1; |
357 |
|
|
358 |
|
tag(line, "<?query?>", query); |
359 |
< |
tag(line, "<?num?>", num); |
359 |
> |
tag(line, "<?num?>", num.str()); |
360 |
|
} |
361 |
|
|
362 |
< |
lines += line + "\n"; |
362 |
> |
lines += line + (fin.good() ? "\n" : ""); |
363 |
|
} |
364 |
|
|
365 |
|
fin.close(); |
376 |
|
conditional(line, fin, "<?ifnum?>", false); |
377 |
|
conditional(line, fin, "<?ifnext?>", page + 2 <= numPages); |
378 |
|
|
379 |
< |
char* cnext = new char[1024]; |
410 |
< |
|
411 |
< |
sprintf(cnext, "%u", (page + 2)); |
412 |
< |
|
413 |
< |
string next = cnext; |
379 |
> |
ostringstream next; |
380 |
|
|
381 |
< |
delete [] cnext; |
381 |
> |
next << page + 2; |
382 |
|
|
383 |
|
tag(line, "<?query?>", query); |
384 |
< |
tag(line, "<?next?>", next); |
384 |
> |
tag(line, "<?next?>", next.str()); |
385 |
|
|
386 |
< |
lines += line + "\n"; |
386 |
> |
lines += line + (fin.good() ? "\n" : ""); |
387 |
|
} |
388 |
|
|
389 |
|
fin.close(); |
395 |
|
{ |
396 |
|
unsigned bottom = page * 10 + 1; |
397 |
|
unsigned top = numWebpages > page * 10 + 10 ? page * 10 + 10 : numWebpages; |
398 |
+ |
ostringstream range; |
399 |
|
|
400 |
< |
char* cbottom = new char[1024]; |
434 |
< |
char* ctop = new char[1024]; |
435 |
< |
|
436 |
< |
sprintf(cbottom, "%u", bottom); |
437 |
< |
sprintf(ctop, "%u", top); |
438 |
< |
|
439 |
< |
string range = string(cbottom) + " - " + ctop; |
400 |
> |
range << bottom << " - " << top; |
401 |
|
|
402 |
< |
delete [] cbottom; |
442 |
< |
delete [] ctop; |
443 |
< |
|
444 |
< |
return range; |
402 |
> |
return range.str(); |
403 |
|
} |
404 |
|
|
405 |
|
string Outputer::total() |
406 |
|
{ |
407 |
< |
char* ctotal = new char[1024]; |
450 |
< |
|
451 |
< |
sprintf(ctotal, "%u", numWebpages); |
407 |
> |
ostringstream total; |
408 |
|
|
409 |
< |
string total = ctotal; |
409 |
> |
total << numWebpages; |
410 |
|
|
411 |
< |
delete [] ctotal; |
456 |
< |
|
457 |
< |
return total; |
411 |
> |
return total.str(); |
412 |
|
} |
413 |
|
|
414 |
|
string Outputer::duration() |
415 |
|
{ |
416 |
< |
char* ctime = new char[1024]; |
463 |
< |
sprintf(ctime, "%.2f", time); |
464 |
< |
|
465 |
< |
string duration = ctime; |
416 |
> |
ostringstream duration; |
417 |
|
|
418 |
< |
delete [] ctime; |
418 |
> |
duration.precision(2); |
419 |
> |
duration << time; |
420 |
|
|
421 |
< |
return duration; |
421 |
> |
return duration.str(); |
422 |
|
} |
423 |
|
|
424 |
|
string Outputer::manycommon(vector<string> common) |
435 |
|
return line; |
436 |
|
} |
437 |
|
|
438 |
< |
void Outputer::tag(string& line, char* tag, string& replacement) |
438 |
> |
void Outputer::tag(string& line, char* tag, const string& replacement) |
439 |
|
{ |
440 |
|
int begin = 0; |
441 |
|
while (begin < line.length()) |