// Buffer.h #include #include class Buffer { public: void display() const; void erase(); const std::string & get_file_name() const { return file_name; } void insert(const std::string & new_line); void move_to_next_line(); void move_to_previous_line(); bool open(const std::string & new_file_name); bool save(const std::string & new_file_name); void set_window_height(int h) { window_height = h; } private: std::list ls_lines; int ix_current_line = 0; int ix_top_line = 0; std::list::iterator itr_current_line = ls_lines.end(); std::list::iterator itr_top_line = ls_lines.end(); std::string file_name; int window_height; }; inline void Buffer::erase() { if (ix_current_line < ls_lines.size()) { itr_current_line = ls_lines.erase(itr_current_line); if (ix_top_line == ix_current_line) itr_top_line = itr_current_line; } } inline void Buffer::insert(const std::string & new_line) { itr_current_line = ls_lines.insert(itr_current_line, new_line); if (ix_top_line == ix_current_line) itr_top_line = itr_current_line; move_to_next_line(); } inline void Buffer::move_to_next_line() { if (ix_current_line < ls_lines.size()) { ++ix_current_line; ++itr_current_line; // check if window needs to be scrolled down if (ix_current_line >= ix_top_line + window_height) { ++ix_top_line; ++itr_top_line; } } } inline void Buffer::move_to_previous_line() { if (ix_current_line > 0) { --ix_current_line; --itr_current_line; // check if window needs to be scrolled up if (ix_current_line < ix_top_line) { --ix_top_line; --itr_top_line; } } }