bool GameSaver::SaveToFile(const std::string &filename) { bool success = false; try { if (!FileSystem::rawFileSystem.MakeDirectory(Pi::GetSaveDir())) { throw CouldNotOpenFileException(); } Serializer::Writer wr; m_game->Serialize(wr); const std::string data = wr.GetData(); FILE *f = fopen(filename.c_str(), "wb"); if (!f) throw CouldNotOpenFileException(); size_t nwritten = fwrite(data.data(), data.length(), 1, f); fclose(f); if (nwritten != 1) throw CouldNotWriteToFileException(); success = true; } catch (CouldNotOpenFileException) { Gui::Screen::ShowBadError(Lang::GAME_LOAD_CANNOT_OPEN); } catch (CouldNotWriteToFileException) { Gui::Screen::ShowBadError(Lang::GAME_SAVE_CANNOT_WRITE); } return success; }
/* * Method: ExportToLua * * Export of generated system for personal interest, customisation, etc * * Availability: * * alpha 33 * * Status: * * experimental */ static int l_starsystem_export_to_lua(lua_State *l) { PROFILE_SCOPED() LUA_DEBUG_START(l); StarSystem *s = LuaObject<StarSystem>::CheckFromLua(1); static const std::string EXPORTED_SYSTEMS_DIR_NAME("exported_systems"); if (!FileSystem::userFiles.MakeDirectory(EXPORTED_SYSTEMS_DIR_NAME)) { throw CouldNotOpenFileException(); } // construct the filename with folder and extension try { const std::string filename(EXPORTED_SYSTEMS_DIR_NAME + "/" + FileSystem::SanitiseFileName(s->GetName()) + ".lua"); const std::string finalPath = FileSystem::NormalisePath( FileSystem::JoinPathBelow(FileSystem::GetUserDir(), filename)); s->ExportToLua(finalPath.c_str()); } catch (std::invalid_argument &) { return luaL_error(l, "could not export system -- name forms an invalid path"); } LUA_DEBUG_END(l, 0); return 0; }
Game *Game::LoadGame(const std::string &filename) { printf("Game::LoadGame('%s')\n", filename.c_str()); FILE *f = FileSystem::userFiles.OpenReadStream(FileSystem::JoinPathBelow(Pi::SAVE_DIR_NAME, filename)); if (!f) throw CouldNotOpenFileException(); Serializer::Reader rd(f); fclose(f); return new Game(rd); }
void Game::SaveGame(const std::string &filename, Game *game) { assert(game); if (!FileSystem::userFiles.MakeDirectory(Pi::SAVE_DIR_NAME)) { throw CouldNotOpenFileException(); } Serializer::Writer wr; game->Serialize(wr); const std::string data = wr.GetData(); FILE *f = FileSystem::userFiles.OpenWriteStream(FileSystem::JoinPathBelow(Pi::SAVE_DIR_NAME, filename)); if (!f) throw CouldNotOpenFileException(); size_t nwritten = fwrite(data.data(), data.length(), 1, f); fclose(f); if (nwritten != 1) throw CouldNotWriteToFileException(); }
/** Execute file on a specific Lua state. * @param L Lua state to execute the file in. * @param filename filet to load and excute. */ void LuaContext::do_file(lua_State *L, const char *filename) { // Load initialization code int err = 0; std::string errmsg; if ( (err = luaL_loadfile(L, filename)) != 0) { errmsg = lua_tostring(L, -1); lua_pop(L, 1); switch (err) { case LUA_ERRSYNTAX: throw SyntaxErrorException("Lua syntax error in file %s: %s", filename, errmsg.c_str()); case LUA_ERRMEM: throw OutOfMemoryException("Could not load Lua file %s", filename); case LUA_ERRFILE: throw CouldNotOpenFileException(filename, errmsg.c_str()); } } int errfunc = __enable_tracebacks ? 1 : 0; if ( (err = lua_pcall(L, 0, LUA_MULTRET, errfunc)) != 0 ) { // There was an error while executing the initialization file errmsg = lua_tostring(L, -1); lua_pop(L, 1); switch (err) { case LUA_ERRRUN: throw LuaRuntimeException("do_file", errmsg.c_str()); case LUA_ERRMEM: throw OutOfMemoryException("Could not execute Lua file %s", filename); case LUA_ERRERR: throw LuaErrorException("do_file", errmsg.c_str()); default: throw LuaErrorException("do_file/unknown error", errmsg.c_str()); } } }
bool GameLoader::LoadFromFile(const std::string &filename) { try { FILE *f = fopen(GetFilename().c_str(), "rb"); if (!f) throw CouldNotOpenFileException(); Serializer::Reader rd(f); fclose(f); m_game = new Game(rd); } catch (SavedGameCorruptException) { Gui::Screen::ShowBadError(Lang::GAME_LOAD_CORRUPT); } catch (CouldNotOpenFileException) { Gui::Screen::ShowBadError(Lang::GAME_LOAD_CANNOT_OPEN); } return m_game != 0; }
/** Initialize the Model using a URDF file * @param filename The filename of the URDF file * @return true if the model was intialized successfully */ bool Model::initFile(const std::string& filename) { // get the entire file std::string xml_string; std::fstream xml_file(filename.c_str(), std::fstream::in); if (xml_file.is_open()) { while ( xml_file.good() ) { std::string line; std::getline( xml_file, line); xml_string += (line + "\n"); } xml_file.close(); return Model::initString(xml_string); } else { throw CouldNotOpenFileException(filename.c_str()); } }
/** Read file into memory. * @param filename file path * @return memory location of file content, free after done */ char * WebServer::read_file(const char *filename) { FILE *f = fopen(filename, "rb"); if (! f) { throw CouldNotOpenFileException(filename, errno); } long size = 0; if ((fseek(f, 0, SEEK_END) != 0) || ((size = ftell(f)) == 1)) { fclose(f); throw Exception("Cannot determine file size of %s", filename); } fseek(f, 0, SEEK_SET); if ( size == 0 ) { fclose(f); throw Exception("File %s has zero length", filename); } else if (size > 1024 * 1024) { // keys or certs should not be that long... fclose(f); throw Exception("File %s is unexpectedly large", filename); } char *rv = (char *)malloc(size); if (fread(rv, size, 1, f) != 1) { int terrno = errno; fclose(f); free(rv); throw FileReadException(filename, terrno); } fclose(f); return rv; }