From 23aee34002facf39b56d209320817375db3b6189 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sat, 9 Feb 2008 12:19:25 +0000 Subject: load/save console input history --- src/client/console.cc | 58 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) (limited to 'src/client/console.cc') diff --git a/src/client/console.cc b/src/client/console.cc index 71f3d5a..01f90da 100644 --- a/src/client/console.cc +++ b/src/client/console.cc @@ -4,13 +4,15 @@ the terms and conditions of the GNU General Public License version 2 */ +#include "filesystem/filesystem.h" +#include "core/core.h" +#include "render/render.h" #include "client/console.h" #include "client/video.h" #include "client/keyboard.h" -#include "core/core.h" -#include "render/render.h" #include +#include #include namespace client { @@ -79,12 +81,16 @@ void init() history.push_back(""); history_pos = history.rbegin(); input_pos = 0; + + load_history(); } void shutdown() { con_print << "Shutting down console..." << std::endl; + save_history(); + // remove engine functions core::func::remove("con_toggle"); @@ -282,6 +288,54 @@ bool visible() return console_visible; } +void save_history() +{ + + if (history.size() <= 1) + return; + + std::string filename(filesystem::writedir); + filename.append("history.txt"); + std::ofstream ofs(filename.c_str()); + + if (!ofs.is_open()) { + con_warn << "Could not write " << filename << std::endl; + return; + } + std::deque::iterator it; + size_t l = 1; + for (it = history.begin(); it != history.end(); it++) { + if (l < history.size()) + ofs << (*it) << std::endl; + l++; + } + + ofs.close(); +} + +void load_history() +{ + std::string filename(filesystem::writedir); + filename.append("history.txt"); + std::ifstream ifs(filename.c_str(), std::ifstream::in); + + if (!ifs.is_open()) { + con_warn << "Could not read " << filename << std::endl; + return; + } + + history.clear(); + char line[MAXCMDSIZE]; + while (ifs.getline(line, MAXCMDSIZE-1)) { + history.push_back(line); + } + + ifs.close(); + + history.push_back(""); + history_pos = history.rbegin(); + input_pos = 0; +} //--- private ----------------------------------------------------- std::ostream & Console::messagestream() -- cgit v1.2.3