/* Filename: search.cpp Author: Br. David Carlson Date: January 3, 2007 This program allows the user to repeatedly search through the text file weblist for a target string. For each search, the program prompts the user for this target string. The weblist file contains complete pathnames for various html files, one per line, like this example: /www/carlson/cs125/final.html /www/carlson/cs125/hw/homework2.html The weblist file is assumed to be located in the same directory as this search program. The weblist file can be created with the separate makelist script. For each search, the output of the program consists of the URL corresponding to each line of weblist that contains the target string. For example, if you look up cs125 and the above 2 lines of data are in your webfile, they should give output similar to this: http://cis.stvincent.edu/carlson/cs125/final.html http://cis.stvincent.edu/carlson/cs125/hw/homework2.html The user is instructed to enter the empty string to stop the program. With any other string, a new search is started. To compile this program in Linux, use: g++ search.cpp stringhelp.cpp -o search -s To run the program, put search and weblist in the same directory. Change to that directory and enter the following command: ./search */ #include "stringhelp.h" // Next line should give the name of the text file to be searched: #define DATAFILE "weblist" // The next line should give the start of all URLs for the html files in the above file: #define URLSTART "http://cis.stvincent.edu" // The next line should give the number of chars to remove at the start of each pathname when making a URL: #define REMOVECHARS 4 // Function prototype: void SearchFile(StringType Target); void PrintURL(StringType Line, int Length); int main(void) { StringType Target; int TargetLength; cout << "Enter the target string that you wish to search for in the list of html files (press Enter to quit):" << endl; TargetLength = MyGetLine(cin, Target, StrMax); while (TargetLength > 0) { SearchFile(Target); cout << endl << "Enter the target string that you wish to search for in the list of html files (press Enter to quit):" << endl; TargetLength = MyGetLine(cin, Target, StrMax); } return 0; } /* Given: Target String to search for. Task: To do a sequential search of the file specified by DATAFILE for all instances of the string Target. Each matching line of the file is printed for the user. Return: Nothing. */ void SearchFile(StringType Target) { fstream InFile; StringType Line; int LineLength; // Open the file afresh each time so that we start searching at the front of the file. InFile.open(DATAFILE, ios::in); if (InFile.fail()) { cout << "Error: Cannot open file named " << DATAFILE << " for input" << endl; exit(1); } cout << endl << "Matches:" << endl << endl; LineLength = MyGetLine(InFile, Line, StrMax); while (LineLength > 0) { if (SubstringPresent(Line, Target)) PrintURL(Line, LineLength); LineLength = MyGetLine(InFile, Line, StrMax); } InFile.close(); } /* Given: Line A string containing a complete pathname for a particular html file. Length The number of chars in the string in Line. Task: To print out the URL corresponding to this pathname. This is done by printing URLSTART followed by all of Line except the first REMOVECHARS characters. Return: Nothing. */ void PrintURL(StringType Line, int Length) { int k; cout << URLSTART; for (k = REMOVECHARS; k < Length; k++) cout.put(Line[k]); cout << endl; }