// Larry Bush April 26, 2002 // // email : bushl2@rpi.edu // Lawrence_Bush@dps.state.ny.us // // Student ID : 660 220 742 // // File Name : line.h // Final Proj. : Grep for Windows // Class : Operating Systems // Instructor : Susan Bonner // Language : C++ // //****************************************************************************************** //****************************************************************************************** //****************************************************************************************** //************* ************** //************* Line Object ************** //************* ************** //************* Implementation ************** //************* ************** //****************************************************************************************** //****************************************************************************************** //****************************************************************************************** // line.h Declares and implements the line class. // The line class is used to store and process a line of the file buffer. // Processing a line includes storing the line, providing access functions // to the line, provide functions for searching the line for a user specified // search word, and replacing the word with the user specified replacement word // (if provided). // Don't compile this file twice. // Don't define this unique (it is safe to say) variable twice. #if !defined(AFX_LINE_H__A892AC06_6934_41DF_A784_5C8E45F6EEED__INCLUDED_) #define AFX_LINE_H__A892AC06_6934_41DF_A784_5C8E45F6EEED__INCLUDED_ #include #include #include // Line class // Stores individual lines for insertion into the List // using namespace std; class Line { private: //char line[255]; char *line; // *line is a pointer to the contents // of one line of inputted text // vector *line; public: //******************************************************* //******************************************************* //******** ********* //******** Line Object Constructors ********* //******** ********* //******************************************************* //******************************************************* // ******** default constructor ******** Line() { line='\0'; } // null terminated line // ******** constructor ******** Line(char inputline[]) { if (inputline != NULL) { // copy string from rhs if it exists line = new char[strlen(inputline)+1]; strcpy (line, inputline); } } // ******** copy constructor ******** Line(const Line &rhs) { if (rhs.line != NULL) { // copy string from rhs if it exists line = new char[strlen(rhs.line)+1]; strcpy(line,rhs.line); } else line = NULL; } //******************************************************* //******************************************************* //******** ********* //******** Line Object Destructor ********* //******** ********* //******************************************************* //******************************************************* ~Line() { if (line != NULL) delete [] line; //debug: cout<<"line destructor"; } // destructor //******************************************************* //******************************************************* //******** ********* //******** resetLine ********* //******** ********* //******************************************************* //******************************************************* // Used in replace first function to delete old line and put in new line. void resetLine(const char * inputline) { // inserts input string into Line object if (line != NULL) delete [] line; // Deallocate old line space. line = new char[strlen(inputline)+1]; // Make new space for string. strcpy (line, inputline); // copy in string } //******************************************************* //******************************************************* //******** ********* //******** setLineFromFile ********* //******** ********* //******************************************************* //******************************************************* // sets the line from the file to the member variable. void setLineFromFile(char * inputline) { // input line from file ( no need to skip spaces if (line != NULL) delete [] line;//new02 line = new char[strlen(inputline)+1]; strcpy (line, inputline); } char* getL() {return line;} void printline() { std::cout << line; } //******************************************************* //******************************************************* //******** ********* //******** replace_matches_on_this_line ********* //******** ********* //******************************************************* //******************************************************* // This is an algorithm that searches the string for the // word and replaces it with the replacement word. bool replace_matches_on_this_line(char * word_to_find, char * replacement_word) { // Debug: cout<<"Searching next line.\n"; // Call replace first function starting at the beginning of the string. int start_position = replace_first(word_to_find, replacement_word, 0); while(start_position != -1){// If -1, this means it reached the end of the string. start_position = replace_first(word_to_find, replacement_word, start_position); }//replace first until there are none left. return false; } //******************************************************* //******************************************************* //******** ********* //******** replace_first ********* //******** ********* //******************************************************* //******************************************************* // This function replaces just the first match found starting at the // passed in start position. int replace_first(char * word_to_find, char * replacement_word, int passed_in_start_position) { // Variable declarations int word_to_find_length; // lenth of find word int line_length; // lenth of total line int offset; // length of intermediate offset // (tracks the distance between the start position and here) // like base and limit style string s_line; // search line s_line = line; // set search line string s_replacement_word; // string replacement word s_replacement_word = replacement_word; // set string replacement word string s_word_to_find; // string word to find s_word_to_find = word_to_find; // set string word to find // cout<<"Searching next line.\n"; line_length = s_line.length(); // set line length variable word_to_find_length = s_word_to_find.length(); // set word to find length variable // Iterate through the string starting at start position // Stop when you reach the end or you found the word. // The for loop iterates through the string, marking the first character of // where we are testing for a match. for ( int start_position = passed_in_start_position ; start_position < line_length ; start_position++ ) { //main loop, start at each char in line // The while loop tests each letter in the word. // The offset is relative to the start address. // While quits if the match fails. // It replaces if all letters match. offset = 0; // initial offset while( (s_word_to_find[offset] == s_line[start_position+offset]) && ( (start_position+offset)