git repos / saland

commit 2f45d954

sago007 · 2017-05-13 20:39
2f45d9546f664f89b795e4f6db7d15a28c5cd3e8 patch · browse files
parent 1ffbd4bae4bc0b0aabff1b58e5d43ef74686d830

Make sure that the function that creates a file will also create the path. Due to the union file system it can be hard to check if it exist.

Changed files

M src/sago/SagoMisc.cpp before
M src/saland/Game.cpp before
diff --git a/src/sago/SagoMisc.cpp b/src/sago/SagoMisc.cpp index da58bd8..d91f6e8 100644 --- a/src/sago/SagoMisc.cpp +++ b/src/sago/SagoMisc.cpp
@@ -71,7 +71,18 @@ std::string GetFileContent(const char* filename) {
return ret;
}
+void CreatePathToFile(const std::string& path) {
+ size_t end_of_path = path.find_last_of("/");
+ if (end_of_path == std::string::npos) {
+ //No path
+ return;
+ }
+ std::string path2dir = path.substr(0, end_of_path);
+ PHYSFS_mkdir(path2dir.c_str());
+}
+
void WriteFileContent(const char* filename, const std::string& content) {
+ CreatePathToFile(filename);
PHYSFS_file* myfile = PHYSFS_openWrite(filename);
if (!myfile) {
cerr << "Failed to open file for writing, " << PHYSFS_getLastError() << "\n";
diff --git a/src/saland/Game.cpp b/src/saland/Game.cpp index 45ec732..d1623f3 100644 --- a/src/saland/Game.cpp +++ b/src/saland/Game.cpp
@@ -61,6 +61,8 @@ Game::Game() {
}
Game::~Game() {
+ std::string data2save = sago::tiled::tilemap2string(data->tm);
+ sago::WriteFileContent("maps/sample1.tmx", data2save);
}
bool Game::IsActive() {