diff options
| author | Stijn Buys <ingar@osirion.org> | 2008-02-09 12:19:25 +0000 | 
|---|---|---|
| committer | Stijn Buys <ingar@osirion.org> | 2008-02-09 12:19:25 +0000 | 
| commit | 23aee34002facf39b56d209320817375db3b6189 (patch) | |
| tree | b94a4b8164ef28a56fc9e1cb71e50dc2f0abb0ca /src/client/console.cc | |
| parent | 2b6208917e92d93f94ad6620c5135d1bcd237ea8 (diff) | |
load/save console input history
Diffstat (limited to 'src/client/console.cc')
| -rw-r--r-- | src/client/console.cc | 58 | 
1 files changed, 56 insertions, 2 deletions
| 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 <iostream> +#include <fstream>  #include <cmath>  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<std::string>::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() | 
