-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
open_save_file opens files relative to g_save_dir
savedir=<path> can be used to set the savedir; default is user's documents directory. Fixes #94
- Loading branch information
1 parent
919890f
commit 922ebff
Showing
5 changed files
with
81 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |