90 likes | 183 Views
CSE870 Homework #5 Design Pattern Case Studies with C++. by Rajat Joy Rahul Kalaskar Amal Latif Prakarn Unachak. pg. 108. /** * Options this class contains the cmd line options parse_args method used * to assign value to the function ptr compare */
E N D
CSE870 Homework #5Design Pattern Case Studies with C++ by Rajat Joy Rahul Kalaskar Amal Latif Prakarn Unachak
pg. 108 /** * Options this class contains the cmd line options parse_args method used * to assign value to the function ptr compare */ Options:: parse_args (int argc, char *argv[]) { // . . . if (this->enabled (Options:: NORMAL)) // check if the cmd line option is // NORMAL this->compare = &strcmp; // assign addr of strcmp to compare else if (this->enabled (Options:: FOLD)) // check if the cmd line option is // FOLD this->compare = &strcasecmp; // assign addr of strcmp to compare else if (this->enabled (Options:: NUMERIC)) // check if the cmd line option is // NUMERIC this->compare = &numcmp; // assign addr of strcmp to compare // . . .
pg. 109 /** * This method implements the numeric comparison * s1 and s2 are strings */int numcmp (const char *s1, const char * s2){ double d1 = strtod (s1, 0), d2 = strtod (s2, 0); // strtod => sting to double if (d1 < d2) return -1; // if d1 is less than d2 return // -1 ; else if (d1 > d2) return 1; // if d1 is greater than d2 // return 1 else // if (d1 == d2) return 0; // return 0 for all other case}
pg. 114 /** * This method is used to read and initialize Access table is input stream at Access table */ template <class T>void operator>> (istream &is, Access_Table< T> &at){ Input input; // class used to read the text char *buffer = input.read (is); // Read entire stdin into buffer. size_t num_lines = input.replaced (); // gives the no. of line of text. at.make_table (num_lines, buffer); // Factory Method initializes // Access_ Table<>.}
pg. 115 • /** * Factory Method initializes Access_ Table. * num_lines no of lines in the text * buffer contains the text */int Sort_AT_Adapter:: make_table (size_t num_lines, char *buffer){ // Array assignment op. this->access_array_.resize (num_lines); // resize access array according // to the new no. of line (resize // needs to be defined in // Sort_AT_Adaptor) this->buffer_ = buffer; // Obtain ownership. size_t count = 0; //initialize the counter.
pg. 116 // Iterate through the buffer and determine where the beginning of lines and fields // must go. // Line_Ptrs-Iter gives on line at a time. The line is then assigned in the // access_array for (Line_Ptrs_Iter iter (buffer, num_lines); iter.is_done () == 0; iter.next ()) { Line_Ptrs line_ptr = iter.current_element (); // get the current line // from the iter. this->access_array_[ count++] = line_ptr; // assign this line to // the current // position in the // access array.} }
pg. 118 /** * Constructor for Line_Ptr_Iter class buffer buffer containing * the lines * num_lines no. of line in the buffer */ Line_Ptrs_Iter:: Line_Ptrs_Iter(char *buffer, size_t num_lines);
pg 119 /** * This method gets the current element in the iter returns the ptr to Line_Ptrs */ Line_Ptrs Line_Ptrs_Iter:: current_element (void) { Line_Ptrs lp; //lp is a data structure of type Line_Ptrs // Determine beginningof next line and next field ... lp.bol_ = // . . . // assigns a ptr lp.bof_ = // . . . // assigns a ptr return lp; }
pg. 120 /** * Out the text depending upon the options. * os is the output stream at is the access table. */ template <class T> void operator<< (ostream &os,const Access_Table< T> &at) {if (Options:: instance()->enabled (Options::REVERSE)) // check for options. for (size_t i = at.size(); i > 0; -- i) // if the option is REVERSE, os << at[ i - 1]; // display the access table from // last to first Array entryelse for (size_t i = 0; i < at.size(); ++ i) // if not, display the access table os << at[i]; // from first Array entry to the // last }