// Oldham, Jeffrey D. // 2000Mar13 // CS1321 // Modified by: // Massingill, Berna L. // 2001 March // Brain-Dead String Editor Class Implementation // uses our dll class #include "dll.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 = 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 = 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::link * p = b.line.begin(); p != b.line.end(); p = b.line.succ(p)) 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::link * p = b.line.begin(); p != b.pos; p = b.line.succ(p)) out << ' '; return out << '^'; } private: dll line; // contents of line dll::link * pos; // position of "cursor" -- where // characters will be inserted/deleted };