// Oldham, Jeffrey D. // 2000Mar13 // CS1321 // Modified by: // Massingill, Berna L. // 2001 March // Brain-Dead String Editor Class Implementation // uses our dll class // modified to use dll class with iterators -- see "****" comments. #include "dll-2.h" #include #include class buffer { public: // Constructor. // Initially the line is empty, and the "cursor" points past // end-of-line. buffer(void) { pos = line.end(); } // Move cursor backward. // Post: cursor has been moved left one position (or if already at // start of line, is unchanged). void backCursor(void) { if (pos != line.begin()) --pos; // **** was: pos = line.pred(pos) return; } // Move cursor forward. // Post: cursor has been moved right one position (or if already at // end of line, is unchanged). void forwardCursor(void) { if (pos != line.end()) ++pos; // **** was: pos = line.succ(pos) } // Delete character. // Post: character at current cursor position has been deleted, // cursor points to next character. void deleteChar(void) { if (pos != line.end()) pos = line.erase(pos); } // Insert characters. // Post: characters in "s" have inserted before the current cursor // position. void insertChars(const string & s) { if (s.empty()) return; else { line.insert(pos, s[0]); insertChars(s.substr(1)); return; } } // Output operator. friend ostream& operator<<(ostream& out, const buffer & b) { for (dll::iterator p = b.line.begin(); // **** was: dll::link * p p != b.line.end(); ++p) // **** was: p = b.line.succ(p) out << *p; // **** was: out << p->itm return out; } // Print a line showing the cursor's position. // Post: prints to output stream "out" a line of spaces and a '^' // showing the cursor's current position. // returns updated "out". friend ostream& printCursor(ostream& out, const buffer & b) { for (dll::iterator p = b.line.begin(); // **** was: dll::link * p p != b.pos; ++p) // **** was: p = b.line.succ(p) out << ' '; return out << '^'; } private: dll line; // contents of line dll::iterator pos; // position of "cursor" -- where // characters will be inserted/deleted // **** was: dll::link * pos };