Skip to content

Commit

Permalink
use std::filesystem::create_directories in lif::createDirIfNotExisting
Browse files Browse the repository at this point in the history
This automatically handles the creation of the whole directory tree
instead of failing if the parent directories don't exist, and it works
on all platforms the same way.
The downside is that we now need C++17 to build the game, but at this
point it's probably safe to assume it's supported on all the platforms
we care about.
  • Loading branch information
silverweed committed Sep 6, 2024
1 parent e645894 commit 75af9b5
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ endif()

add_executable(${PROJECT_NAME} ${LIFISH_SRC})
set_target_properties(${PROJECT_NAME} PROPERTIES
CXX_STANDARD 14
CXX_STANDARD 17
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
LINKER_LANGUAGE CXX
Expand Down
11 changes: 3 additions & 8 deletions src/core/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,7 @@ sf::View lif::keepRatio(const sf::Vector2f& size, const sf::Vector2u& designedsi
return view;
}

bool lif::createDirIfNotExisting(const std::string& path) {
#if defined(_WIN32) || defined(__MINGW32__)
bool ok = !!CreateDirectory(path.c_str(), NULL);
return ok || GetLastError() == ERROR_ALREADY_EXISTS;
#else
bool ok = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0;
return ok || errno == EEXIST;
#endif
bool lif::createDirIfNotExisting(const std::filesystem::path& path) {
bool ok = std::filesystem::create_directories(path);
return ok;
}
3 changes: 2 additions & 1 deletion src/core/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <array>
#include <cassert>
#include <ostream>
#include <filesystem>

// Enable automatic sf::Time / json conversion:
// https://github.com/nlohmann/json#basic-usage
Expand Down Expand Up @@ -226,6 +227,6 @@ void testMusic();
sf::View keepRatio(const sf::Vector2f& size, const sf::Vector2u& designedsize);

/** Note: this will NOT create a directory recursively */
bool createDirIfNotExisting(const std::string& path);
bool createDirIfNotExisting(const std::filesystem::path& path);

} // end namespace lif
6 changes: 4 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,10 @@ int main(int argc, char **argv) {

if (ui.mustSaveGame()) {
const auto saveName = ui.getSaveName() + ".lifish";
lif::SaveManager::saveGame(saveName, game->getLM());
std::cerr << "Saved game in " << saveName << "." << std::endl;
if (lif::SaveManager::saveGame(saveName, game->getLM()))
std::cerr << "Saved game in " << saveName << "." << std::endl;
else
std::cerr << "Failed to save game in " << saveName << "." << std::endl;
}

///// LOGIC LOOP /////
Expand Down

0 comments on commit 75af9b5

Please sign in to comment.