ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/trunk/Search/Search.cpp
Revision: 146
Committed: 2003-06-01T01:26:29-07:00 (22 years ago) by douglas
File size: 14084 byte(s)
Log Message:
Upped version to 1.2a.

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 Main
46     //
47     // Douglas Thrift
48     //
49     // Search.cpp
50    
51     #include "Search.h"
52     #include "Indexer.h"
53     #include "Searcher.h"
54     #include "Outputer.h"
55    
56     #ifndef _WIN32
57     #include <sys/utsname.h>
58     #else
59     #include <windows.h>
60     #endif // _WIN32
61    
62     string program;
63     string programName = "Douglas Thrift's Search Engine";
64 douglas 146 string programVersion = "1.2a";
65 douglas 1 bool debug = false;
66    
67     int main(int argc, char* argv[])
68     {
69     program = argv[0];
70    
71     bool indexMode = false;
72     string indexURL;
73     set<string> indexDomains;
74     set<string> indexRestrictions;
75    
76     unsigned page = 1;
77     string query;
78    
79     vector<string> indices;
80    
81     string header = "header.html";
82     string body = "body.html";
83     string footer = "footer.html";
84     string notfound = "notfound.html";
85     string pages = "pages.html";
86    
87 douglas 37 string email;
88    
89 douglas 1 for (int index = 1; index < argc; index++)
90     {
91     string arg(argv[index]);
92    
93     if (arg == "-help")
94     {
95     usage();
96     return 0;
97     }
98     else if (arg == "-version")
99     {
100     version();
101     return 0;
102     }
103     else if (arg == "-license")
104     {
105     license();
106     return 0;
107     }
108     else if (arg == "-P")
109     {
110     if (++index < argc)
111     {
112     page = strtoul(argv[index],0,0);
113     }
114     else
115     {
116     cerr << program << ": Bad arguments\n";
117 douglas 35 usage();
118 douglas 1 return 1;
119     }
120     }
121     else if (arg == "-i")
122     {
123     indexMode = true;
124    
125     if (++index < argc)
126     {
127     indexURL = argv[index];
128     }
129     else
130     {
131     cerr << program << ": Bad arguments\n";
132 douglas 35 usage();
133 douglas 1 return 1;
134     }
135     }
136     else if (arg == "-d")
137     {
138     if (++index < argc)
139     {
140     indexDomains.insert(argv[index]);
141     }
142     else
143     {
144     cerr << program << ": Bad arguments\n";
145 douglas 35 usage();
146 douglas 1 return 1;
147     }
148     }
149     else if (arg == "-r")
150     {
151     if (++index < argc)
152     {
153     indexRestrictions.insert(argv[index]);
154     }
155     else
156     {
157     cerr << program << ": Bad arguments\n";
158 douglas 35 usage();
159 douglas 1 return 1;
160     }
161     }
162     else if (arg == "-h")
163     {
164     if (++index < argc)
165     {
166     header = argv[index];
167     }
168     else
169     {
170     cerr << program << ": Bad arguments\n";
171 douglas 35 usage();
172 douglas 1 return 1;
173     }
174     }
175     else if (arg == "-b")
176     {
177     if (++index < argc)
178     {
179     body = argv[index];
180     }
181     else
182     {
183     cerr << program << ": Bad arguments\n";
184 douglas 35 usage();
185 douglas 1 return 1;
186     }
187     }
188     else if (arg == "-f")
189     {
190     if (++index < argc)
191     {
192     footer = argv[index];
193     }
194     else
195     {
196     cerr << program << ": Bad arguments\n";
197 douglas 35 usage();
198 douglas 1 return 1;
199     }
200     }
201     else if (arg == "-n")
202     {
203     if (++index < argc)
204     {
205     notfound = argv[index];
206     }
207     else
208     {
209     cerr << program << ": Bad arguments\n";
210 douglas 35 usage();
211 douglas 1 return 1;
212     }
213     }
214     else if (arg == "-p")
215     {
216     if (++index < argc)
217     {
218     pages = argv[index];
219     }
220     else
221     {
222     cerr << program << ": Bad arguments\n";
223 douglas 35 usage();
224 douglas 1 return 1;
225     }
226     }
227     else if (arg == "-D")
228     {
229     debug = true;
230     }
231     else
232     {
233     indices.push_back(arg);
234     }
235     }
236    
237     if (indices.size() < 1)
238     {
239     usage();
240     return 0;
241     }
242    
243     if (indexMode)
244     {
245     if (indices.size() > 1)
246     {
247     cerr << program << ": Too many indices, can only build one index"
248     << " at a time\n";
249 douglas 37 usage();
250 douglas 1 return 1;
251     }
252    
253     if (indexDomains.size() < 1)
254     {
255     cerr << program << ": Must specify at least one domain\n";
256 douglas 37 usage();
257 douglas 1 return 1;
258     }
259    
260     Indexer indexer(indices[0], indexDomains, indexRestrictions);
261    
262     indexer.index(indexURL);
263     }
264     else
265     {
266     string line;
267     getline(cin, line);
268     query = line;
269    
270     Searcher searcher(query);
271    
272     searcher.search(indices);
273    
274     Outputer outputer(header, body, footer, notfound,
275     pages);
276    
277     outputer.output(searcher, page < 1 ? page : --page);
278     }
279    
280     return 0;
281     }
282    
283 douglas 12 string agent(bool version)
284     {
285     string agent = programName + (version ? ('/' + programVersion) : "");
286    
287     return agent;
288     }
289    
290 douglas 1 string platform()
291     {
292     string platform;
293     string os;
294     string version;
295     string architecture;
296     string marketing;
297    
298     #ifdef _WIN32
299     OSVERSIONINFO* computer = new OSVERSIONINFO;
300     computer->dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
301     GetVersionEx(computer);
302    
303     os = computer->dwPlatformId == VER_PLATFORM_WIN32_NT ? "Windows NT" :
304     "Windows";
305     unsigned major = computer->dwMajorVersion;
306     unsigned minor = computer->dwMinorVersion;
307    
308     delete computer;
309    
310     SYSTEM_INFO* system = new SYSTEM_INFO;
311     GetSystemInfo(system);
312    
313     switch (system->wProcessorArchitecture)
314     {
315     case PROCESSOR_ARCHITECTURE_INTEL:
316     architecture = "ix86";
317     break;
318     case PROCESSOR_ARCHITECTURE_MIPS:
319     architecture = "mips";
320     break;
321     case PROCESSOR_ARCHITECTURE_ALPHA:
322     architecture = "alpha";
323     break;
324     case PROCESSOR_ARCHITECTURE_PPC:
325     architecture = "ppc";
326     break;
327     case PROCESSOR_ARCHITECTURE_IA64:
328     architecture = "ia64";
329     break;
330     case PROCESSOR_ARCHITECTURE_IA32_ON_WIN64:
331     architecture = "ix86_on_win64";
332     break;
333     case PROCESSOR_ARCHITECTURE_AMD64:
334     architecture = "amd64";
335     break;
336     default:
337     architecture = "unknown";
338     break;
339     }
340    
341     char* cversion = new char[1024];
342     sprintf(cversion, "%u.%u", major, minor);
343     version = cversion;
344    
345     delete [] cversion;
346    
347     if (major == 4 && minor <= 3 && os != "Windows NT")
348     {
349     marketing = " [Windows 95]";
350     }
351     else if (major == 4 && minor == 10 && os != "Windows NT")
352     {
353     marketing = " [Windows 98]";
354     }
355     else if (major == 5 && minor == 0 && os == "Windows NT")
356     {
357     marketing = " [Windows 2000]";
358     }
359     else if (major == 4 && minor == 90 && os != "Windows NT")
360     {
361     marketing = " [Windows ME]";
362     }
363     else if (major == 5 && minor == 1 && os == "Windows NT")
364     {
365     marketing = " [Windows XP]";
366     }
367     else if (major == 5 && minor == 2 && os == "Windows NT")
368     {
369     marketing = " [Windows .NET Server]";
370     }
371     #else // _WIN32
372     struct utsname* computer = new struct utsname;
373     uname(computer);
374    
375     os = computer->sysname;
376     version = computer->release;
377     architecture = computer->machine;
378    
379     delete computer;
380     #endif // _WIN32
381    
382     platform = "(" + os + " " + version + marketing + " " + architecture + ")";
383    
384     return platform;
385     }
386    
387     void usage()
388     {
389 douglas 47 #ifdef _WIN32
390     OSVERSIONINFO* computer = new OSVERSIONINFO;
391     computer->dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
392     GetVersionEx(computer);
393    
394     string program = ::program;
395     if (computer->dwPlatformId != VER_PLATFORM_WIN32_NT)
396     {
397     program = "Search";
398     }
399    
400     delete computer;
401     #endif // _WIN32
402    
403 douglas 1 string tab(8 + program.length(), ' ');
404    
405     cout << "Usage: " << program << " [index ...] [-P page] [-h header] [-b bo"
406     << "dy]\n"
407     << tab << "[-f footer] [-n notfound] [-p pages]\n"
408     << tab << "[-i begin] [-d domain ...] [-r restriction ...]\n"
409     << tab << "[-D] [-version] [-help]\n"
410     << "Options:\n"
411     << " index Index file to use (can only use one file for i"
412     << "ndexing)\n"
413     << " -P page Page of search to display (defaults to 1)\n"
414     << " -h header Header template to use (defaults to header.htm"
415     << "l)\n"
416     << " -b body Body template to use (defaults to body.html)\n"
417     << " -f footer Footer template to use (defaults to footer.htm"
418     << "l)\n"
419     << " -n notfound Not found template to use (defaults to notfoun"
420     << "d.html)\n"
421     << " -p pages Pages template to use (defaults to pages.html)"
422     << "\n"
423     << " -i begin URL to begin indexing (causes indexing rather "
424     << "than search)\n"
425     << " -d domain Domain to include in indexing\n"
426     << " -r restriction URL to restrict from indexing\n"
427     << " -D Display debug information\n"
428     << " -version Display version information and exit\n"
429     << " -license Display license information and exit\n"
430     << " -help Display this message and exit\n";
431     }
432    
433     void version()
434     {
435     cout << programName << " " << programVersion << " "<< platform() << "\n\n"
436 douglas 28 << " Copyright (C) 2002-2003, Douglas Thrift. All Rights Reserved.\n"
437     << "\n"
438 douglas 1 << " This product includes software developed by Douglas Thrift\n"
439     << " (http://computers.douglasthrift.net/searchengine/).\n";
440     }
441    
442     void license()
443     {
444     cout << "License:\n"
445     << " Douglas Thrift's Search Engine License\n\n"
446 douglas 28 << " Copyright (C) 2002-2003, Douglas Thrift. All Rights Reserved.\n"
447     << "\n"
448 douglas 1 << " Redistribution and use in source and binary forms, with or with"
449     << "out\n"
450     << " modification, are permitted provided that the following conditi"
451     << "ons are met:\n\n"
452     << " 1. Redistributions of source code must retain the above copyrig"
453     << "ht notice,\n"
454     << " this list of conditions and the following disclaimer.\n\n"
455     << " 2. Redistributions in binary form must reproduce the above copy"
456     << "right notice,\n"
457     << " this list of conditions and the following disclaimer in the "
458     << "documentation\n"
459     << " and/or other materials provided with the distribution.\n\n"
460     << " 3. The end-user documentation included with the redistribution,"
461     << " if any, must\n"
462     << " include the following acknowledgment:\n\n"
463     << " \"This product includes software developed by Douglas Thr"
464     << "ift\n"
465     << " (http://computers.douglasthrift.net/searchengine/).\"\n\n"
466     << " Alternately, this acknowledgment may appear in the software "
467     << "itself, if\n"
468     << " and wherever such third-party acknowledgments normally appea"
469     << "r.\n\n"
470     << " 4. The names \"Douglas Thrift\" and \"Douglas Thrift\'s Search "
471     << "Engine\" must not\n"
472     << " be used to endorse or promote products derived from this sof"
473     << "tware without\n"
474     << " specific prior written permission. For written permission, p"
475     << "lease visit\n"
476     << " http://www.douglasthrift.net/contact.cgi for contact inform"
477     << "ation.\n\n"
478     << " 5. Products derived from this software may not be called \"Doug"
479     << "las Thrift\'s\n"
480     << " Search Engine\", nor may \"Douglas Thrift\'s Search Engine\""
481     << " appear in their\n"
482     << " name, without prior written permission.\n\n"
483     << " THIS SOFTWARE IS PROVIDED \"AS IS\" AND ANY EXPRESS OR IMPLIED "
484     << "WARRANTIES,\n"
485     << " INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHA"
486     << "NTABILITY AND\n"
487     << " FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SH"
488     << "ALL THE\n"
489     << " COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIR"
490     << "ECT,\n"
491     << " INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU"
492     << "DING, BUT NOT\n"
493     << " LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS O"
494     << "F USE, DATA,\n"
495     << " OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY"
496     << " THEORY OF\n"
497     << " LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCL"
498     << "UDING\n"
499     << " NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF T"
500     << "HIS SOFTWARE,\n"
501     << " EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n";
502     }
503    
504     void entities(string& line, char character, char* entity)
505     {
506     int begin = 0;
507    
508     while (begin < line.length())
509     {
510     int spot = line.find(character, begin);
511    
512     int end = spot + 1;
513    
514     if (spot != string::npos)
515     {
516     line.replace(spot, 1, entity);
517     }
518     else
519     {
520     break;
521     }
522    
523     begin = end;
524     }
525     }
526    
527     void entities(string& line, char* entity, char character)
528     {
529     int begin = 0;
530    
531     while (begin < line.length())
532     {
533     int spot = line.find(entity, begin);
534    
535     int end = spot + 1;
536    
537     if (spot != string::npos)
538     {
539     line.replace(spot, strlen(entity), 1, character);
540     }
541     else
542     {
543     break;
544     }
545    
546     begin = end;
547     }
548     }
549    
550     void normalize(string& abbynormal)
551     {
552     for (unsigned index = 0; index < abbynormal.length(); index++)
553     {
554     if (isspace(abbynormal[index]))
555     {
556     unsigned next = index + 1;
557     while (isspace(abbynormal[next]))
558     {
559     next++;
560     }
561     abbynormal.replace(index, next - index, 1, abbynormal[index]);
562     }
563     }
564    
565     if (isspace(abbynormal[0])) abbynormal.erase(0, 1);
566     if (isspace(abbynormal[abbynormal.length() - 1]))
567     abbynormal.erase(abbynormal.length() - 1, 1);
568     }