From b68bfbc7cfc38bb4f46106f4d21728a67db864be Mon Sep 17 00:00:00 2001 From: Richard Date: Sun, 25 Feb 2024 15:52:16 -0700 Subject: [PATCH] Implement find_path with std::filesystem --- common/find_path.cpp | 76 ++++++++++++---------------------------- tests/test_find_path.cpp | 24 +++++++++++-- 2 files changed, 44 insertions(+), 56 deletions(-) diff --git a/common/find_path.cpp b/common/find_path.cpp index d7825df4d..5d40e12ae 100644 --- a/common/find_path.cpp +++ b/common/find_path.cpp @@ -4,10 +4,7 @@ #include "prototyp.h" #include "cmdfiles.h" -#include "fractint.h" #include "id_data.h" -#include "make_path.h" -#include "prompts2.h" #include "search_path.h" #ifdef WIN32 @@ -16,80 +13,51 @@ #include #endif -#include #include namespace fs = std::filesystem; -/* - *---------------------------------------------------------------------- - * - * find_path - * - * Find where a file is. - * We return filename if it is an absolute path. - * Otherwise, we first try FRACTDIR/filename, SRCDIR/filename, - * and then ./filename. - * - * Results: - * Returns full pathname in fullpathname. - * - * Side effects: - * None. - * - *---------------------------------------------------------------------- - */ +// find_path +// +// Find where a file is. +// std::string find_path(const char *filename, const std::function &get_env) // return full pathnames { - char fullpathname[FILE_MAX_PATH]; - fullpathname[0] = 0; // indicate none found - - char fname[FILE_MAX_FNAME]; - char ext[FILE_MAX_EXT]; - char temp_path[FILE_MAX_PATH]; + const fs::path file_path{fs::path{filename}.make_preferred()}; // check current directory if curdir= parameter set - split_fname_ext(filename, fname, ext); - make_path(temp_path, "", "", fname, ext); - if (g_check_cur_dir && fs::exists(temp_path)) // file exists + if (g_check_cur_dir && exists(file_path.filename())) // file exists { - return (fs::current_path() / filename).make_preferred().string(); + return (fs::current_path() / file_path.filename()).make_preferred().string(); } // check for absolute path - //if (fs::path{filename}.is_absolute() && ) - //{ - //} - std::strcpy(temp_path, filename); // avoid side effect changes to filename - if (temp_path[0] == SLASHC || (temp_path[0] && temp_path[1] == ':')) + if (file_path.is_absolute() && access(file_path.string().c_str(), 0) == 0) // file exists { - if (access(temp_path, 0) == 0) // file exists - { - std::strcpy(fullpathname, temp_path); - return fullpathname; - } - - split_fname_ext(temp_path, fname, ext); - make_path(temp_path, "", "", fname, ext); + return file_path.string(); } + const auto check_dir = [&](const std::string &dir) + { + fs::path check_path{fs::path{dir} / file_path.filename()}; + return exists(check_path) ? check_path.make_preferred().string() : std::string{}; + }; + // check FRACTDIR - make_path(temp_path, "", g_fractal_search_dir1.c_str(), fname, ext); - if (access(temp_path, 0) == 0) + const std::string dir1_path{check_dir(g_fractal_search_dir1)}; + if (!dir1_path.empty()) { - std::strcpy(fullpathname, temp_path); - return fullpathname; + return dir1_path; } // check SRCDIR - make_path(temp_path, "", g_fractal_search_dir2.c_str(), fname, ext); - if (access(temp_path, 0) == 0) + const std::string dir2_path{check_dir(g_fractal_search_dir2)}; + if (!dir2_path.empty()) { - std::strcpy(fullpathname, temp_path); - return fullpathname; + return dir2_path; } // check PATH - return search_path(temp_path, "PATH", get_env); + return search_path(file_path.filename().string().c_str(), "PATH", get_env); } diff --git a/tests/test_find_path.cpp b/tests/test_find_path.cpp index 899d30361..06e3f46b2 100644 --- a/tests/test_find_path.cpp +++ b/tests/test_find_path.cpp @@ -61,7 +61,7 @@ void TestFindPath::SetUp() } // namespace -TEST_F(TestFindPath, foundInCurrentDirectory) +TEST_F(TestFindPath, fileFoundInCurrentDirectory) { current_path_saver cd(ID_TEST_DATA_DIR); g_check_cur_dir = true; @@ -71,6 +71,17 @@ TEST_F(TestFindPath, foundInCurrentDirectory) ASSERT_EQ((fs::path{ID_TEST_DATA_DIR} / ID_TEST_IFS_FILE).make_preferred(), fs::path{result}); } +TEST_F(TestFindPath, filePathFoundInCurrentDirectory) +{ + current_path_saver cd(ID_TEST_DATA_DIR); + g_check_cur_dir = true; + const fs::path relativeFile{fs::path{ID_TEST_DATA_SUBDIR}.filename() / ID_TEST_IFS_FILE}; + + const std::string result = find_path(relativeFile.string().c_str(), m_empty_env); + + ASSERT_EQ((fs::path{ID_TEST_DATA_DIR} / ID_TEST_IFS_FILE).make_preferred(), fs::path{result}); +} + TEST_F(TestFindPath, notFoundInCurrentDirectory) { current_path_saver cd(ID_TEST_DATA_DIR); @@ -133,8 +144,17 @@ TEST_F(TestFindPath, foundInPathSubDir) TEST_F(TestFindPath, notFoundInPath) { - const std::string result = find_path(m_non_existent, m_env_path); ASSERT_TRUE(result.empty()); } + +TEST_F(TestFindPath, absolutePath) +{ + const fs::path absoluteLocation{(fs::path{ID_TEST_DATA_DIR} / ID_TEST_IFS_FILE).make_preferred()}; + ASSERT_TRUE(exists(absoluteLocation)); + + const std::string result = find_path(absoluteLocation.string().c_str(), m_empty_env); + + ASSERT_EQ(absoluteLocation.string(), result); +}