Skip to content

Commit

Permalink
open_save_file opens files relative to g_save_dir
Browse files Browse the repository at this point in the history
savedir=<path> can be used to set the savedir; default is user's
documents directory.

Fixes #94
  • Loading branch information
LegalizeAdulthood committed Sep 3, 2024
1 parent 919890f commit 922ebff
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
13 changes: 12 additions & 1 deletion libid/save_file.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
#include "save_file.h"

#include "special_dirs.h"

#include <filesystem>

namespace fs = std::filesystem;

std::FILE *open_save_file(const std::string &name, const std::string &mode)
{
return std::fopen(name.c_str(), mode.c_str());
fs::path path{name};
if (path.is_relative())
{
path = fs::path{g_save_dir} / path;
}
return std::fopen(path.string().c_str(), mode.c_str());
}
2 changes: 1 addition & 1 deletion libid/special_dirs.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#include "special_dirs.h"

std::string g_save_dir;
std::string g_save_dir{get_documents_dir()};
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ add_executable(test-id
test_make_path.cpp
test_math.cpp
test_merge_path_names.cpp
test_open_save_file.cpp
test_path_match.cpp
test_search_path.cpp
test_split_path.cpp
Expand Down
2 changes: 1 addition & 1 deletion tests/images/image_compare_test.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ else()
set(COMMAND_ECHO "NONE")
endif()

list(APPEND PARAMETERS "savename=${TEST_SAVE_IMAGE}")
list(APPEND PARAMETERS "savename=${TEST_SAVE_IMAGE}" "savedir=.")

if(DEBUG)
dump_var(ID)
Expand Down
66 changes: 66 additions & 0 deletions tests/test_open_save_file.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <save_file.h>

#include "test_data.h"

#include <special_dirs.h>
#include <value_saver.h>

#include <gtest/gtest.h>

#include <filesystem>
#include <string_view>

using namespace testing;
namespace fs = std::filesystem;

TEST(TestSaveDir, initialValue)
{
EXPECT_EQ(get_documents_dir(), g_save_dir);
}

TEST(TestOpenSaveFile, filename)
{
constexpr std::string_view name{"tmp.txt"};
if (fs::exists(name))
{
fs::remove(name);
}
const auto path{fs::path{ID_TEST_DATA_DIR} / name};
if (exists(path))
{
fs::remove(path);
}
ValueSaver save_dir{g_save_dir, ID_TEST_DATA_DIR};

std::FILE *file{open_save_file(name.data(), "w")};

ASSERT_NE(nullptr, file);
std::fclose(file);
ASSERT_FALSE(fs::exists(name));
ASSERT_TRUE(fs::exists(path));
fs::remove(path);
}

TEST(TestOpenSaveFile, relativeDir)
{
constexpr std::string_view name{"tmp.txt"};
if (fs::exists(name))
{
fs::remove(name);
}
const auto relative{fs::path{ID_TEST_DATA_SUBDIR_NAME} / name};
const auto path{fs::path{ID_TEST_DATA_DIR} / relative};
if (exists(path))
{
fs::remove(path);
}
ValueSaver save_dir{g_save_dir, ID_TEST_DATA_DIR};

std::FILE *file{open_save_file(relative.string(), "w")};

ASSERT_NE(nullptr, file);
std::fclose(file);
ASSERT_FALSE(fs::exists(name));
ASSERT_TRUE(fs::exists(path));
fs::remove(path);
}

0 comments on commit 922ebff

Please sign in to comment.