Make the LuaConsole not crash if the cached history file does not exist

This commit is contained in:
Alexander Bock
2017-03-06 14:20:03 -05:00
parent 403bb3c144
commit c93af1fcfc
+18 -18
View File
@@ -72,26 +72,28 @@ void LuaConsole::initialize() {
ghoul::filesystem::CacheManager::Persistent::Yes
);
std::ifstream file;
file.exceptions(~std::ofstream::goodbit);
file.open(filename, std::ios::binary | std::ios::in);
if (FileSys.fileExists(filename)) {
std::ifstream file;
file.exceptions(std::ofstream::badbit);
file.open(filename, std::ios::binary | std::ios::in);
// Read the number of commands from the history
int64_t nCommands;
file.read(reinterpret_cast<char*>(&nCommands), sizeof(int64_t));
// Read the number of commands from the history
int64_t nCommands;
file.read(reinterpret_cast<char*>(&nCommands), sizeof(int64_t));
for (int64_t i = 0; i < nCommands; ++i) {
int64_t length;
file.read(reinterpret_cast<char*>(&length), sizeof(int64_t));
for (int64_t i = 0; i < nCommands; ++i) {
int64_t length;
file.read(reinterpret_cast<char*>(&length), sizeof(int64_t));
std::vector<char> tmp(length + 1);
file.read(tmp.data(), length);
tmp[length] = '\0';
_commandsHistory.emplace_back(std::string(tmp.begin(), tmp.end()));
std::vector<char> tmp(length + 1);
file.read(tmp.data(), length);
tmp[length] = '\0';
_commandsHistory.emplace_back(std::string(tmp.begin(), tmp.end()));
}
file.close();
}
file.close();
_commands = _commandsHistory;
_commands.push_back("");
_activeCommand = _commands.size() - 1;
@@ -113,9 +115,7 @@ void LuaConsole::deinitialize() {
ghoul::filesystem::CacheManager::Persistent::Yes
);
std::ofstream file;
file.exceptions(~std::ofstream::goodbit);
file.open(filename, std::ios::binary | std::ios::in);
std::ofstream file(filename);
int64_t nCommands = _commandsHistory.size();
file.write(reinterpret_cast<const char*>(&nCommands), sizeof(int64_t));