From c46fa74abc1cd0a50799d979049631abc8b28d00 Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=B4me=20Chilliet?= Date: Fri, 2 Dec 2016 18:41:32 +0700 Subject: [PATCH] Dirs are now created if missing under GNU/Linux --- Source/Account.cpp | 2 ++ Source/GameTick.cpp | 5 +++-- Source/Settings.cpp | 2 ++ Source/Utils/Folders.cpp | 28 +++++++++++++++++++++++----- Source/Utils/Folders.h | 1 + 5 files changed, 31 insertions(+), 7 deletions(-) diff --git a/Source/Account.cpp b/Source/Account.cpp index f650330..b93eff1 100644 --- a/Source/Account.cpp +++ b/Source/Account.cpp @@ -115,6 +115,7 @@ Account* Account::loadFile(string filename) FILE *tfile; int numaccounts; int accountactive; + errno = 0; tfile = fopen(filename.c_str(), "rb" ); @@ -194,6 +195,7 @@ Account* Account::loadFile(string filename) void Account::saveFile(string filename, Account* accountactive) { FILE *tfile; + errno = 0; tfile = fopen(filename.c_str(), "wb" ); if (tfile) { diff --git a/Source/GameTick.cpp b/Source/GameTick.cpp index 8646111..e7f6c50 100644 --- a/Source/GameTick.cpp +++ b/Source/GameTick.cpp @@ -746,11 +746,11 @@ void LoadCampaign() vector ListCampaigns() { + errno = 0; DIR *campaigns = opendir(Folders::getResourcePath("Campaigns").c_str()); struct dirent *campaign = NULL; if (!campaigns) { - perror("Problem while loading campaigns"); - cerr << "campaign folder was : " << Folders::getResourcePath("Campaigns") << endl; + perror(("Problem while loading campaigns from " + Folders::getResourcePath("Campaigns")).c_str()); exit(EXIT_FAILURE); } vector campaignNames; @@ -818,6 +818,7 @@ void Game::Loadlevel(const std::string& name) int mapvers; FILE *tfile; + errno = 0; tfile = fopen( Folders::getResourcePath("Maps/"+name).c_str(), "rb" ); if (tfile) { pause_sound(stream_firesound); diff --git a/Source/Settings.cpp b/Source/Settings.cpp index 2ebfbff..42d5d35 100644 --- a/Source/Settings.cpp +++ b/Source/Settings.cpp @@ -80,6 +80,7 @@ void SaveSettings() newscreenheight = screenheight; if (newscreenheight < 0) newscreenheight = screenheight; + errno = 0; ofstream opstream(Folders::getConfigFilePath()); if (opstream.fail()) { perror(("Couldn't save config file " + Folders::getConfigFilePath()).c_str()); @@ -172,6 +173,7 @@ void SaveSettings() bool LoadSettings() { + errno = 0; ifstream ipstream(Folders::getConfigFilePath(), std::ios::in); if ( ipstream.fail() ) { perror(("Couldn't read config file " + Folders::getConfigFilePath()).c_str()); diff --git a/Source/Utils/Folders.cpp b/Source/Utils/Folders.cpp index 7e08959..0a36e5c 100644 --- a/Source/Utils/Folders.cpp +++ b/Source/Utils/Folders.cpp @@ -20,13 +20,15 @@ along with Lugaru. If not, see . #include "Folders.h" #include +#include const std::string Folders::dataDir = DATADIR; std::string Folders::getScreenshotDir() { - /* TODO - create folder if missing */ - return getUserDataPath() + "/Screenshots"; + std::string screenshotDir = getUserDataPath() + "/Screenshots"; + makeDirectory(screenshotDir); + return screenshotDir; } std::string Folders::getResourcePath(std::string filepath) @@ -36,12 +38,16 @@ std::string Folders::getResourcePath(std::string filepath) std::string Folders::getUserDataPath() { - return getGenericDirectory("XDG_DATA_HOME", ".local/share"); + std::string userDataPath = getGenericDirectory("XDG_DATA_HOME", ".local/share"); + makeDirectory(userDataPath); + return userDataPath; } std::string Folders::getConfigFilePath() { - return getGenericDirectory("XDG_CONFIG_HOME", ".config") + "/config.txt"; + std::string configFolder = getGenericDirectory("XDG_CONFIG_HOME", ".config"); + makeDirectory(configFolder); + return configFolder + "/config.txt"; } /* Generic code for XDG ENVVAR test and fallback */ @@ -55,8 +61,20 @@ std::string Folders::getGenericDirectory(const char* ENVVAR, const std::string f if((path != NULL) && (strlen(path) != 0)) { ret = std::string(path) + '/' + fallback + "/lugaru"; } else { - ret = ""; + ret = "."; } } return ret; } + +bool Folders::makeDirectory(std::string path) { + errno = 0; + int status = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + if (status == 0) { + return true; + } else if(errno == EEXIST) { + return true; + } else { + return false; + } +} diff --git a/Source/Utils/Folders.h b/Source/Utils/Folders.h index 1dd46fb..0343b6c 100644 --- a/Source/Utils/Folders.h +++ b/Source/Utils/Folders.h @@ -46,6 +46,7 @@ public: private: static std::string getGenericDirectory(const char* ENVVAR, const std::string fallback); + static bool makeDirectory(std::string path); }; #endif /* _FOLDERS_H_ */ -- 2.39.5