-
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.
Implement search_path, find_path with std::filesystem
- Loading branch information
1 parent
33b023b
commit 817a255
Showing
20 changed files
with
407 additions
and
159 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
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,94 @@ | ||
#include "find_path.h" | ||
|
||
#include "port.h" | ||
#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 | ||
#include <corecrt_io.h> | ||
#else | ||
#include <unistd.h> | ||
#endif | ||
|
||
#include <filesystem> | ||
|
||
namespace fs = std::filesystem; | ||
|
||
/* | ||
*---------------------------------------------------------------------- | ||
* | ||
* findpath -- | ||
* | ||
* 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. | ||
* | ||
*---------------------------------------------------------------------- | ||
*/ | ||
std::string find_path(const char *filename, | ||
const std::function<const char *(const char *)> &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]; | ||
|
||
// 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 | ||
{ | ||
return (fs::current_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 (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); | ||
} | ||
|
||
// check FRACTDIR | ||
make_path(temp_path, "", g_fractal_search_dir1.c_str(), fname, ext); | ||
if (access(temp_path, 0) == 0) | ||
{ | ||
std::strcpy(fullpathname, temp_path); | ||
return fullpathname; | ||
} | ||
|
||
// check SRCDIR | ||
make_path(temp_path, "", g_fractal_search_dir2.c_str(), fname, ext); | ||
if (access(temp_path, 0) == 0) | ||
{ | ||
std::strcpy(fullpathname, temp_path); | ||
return fullpathname; | ||
} | ||
|
||
// check PATH | ||
return search_path(temp_path, "PATH", get_env); | ||
} |
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
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,36 @@ | ||
#include "search_path.h" | ||
|
||
#include <boost/algorithm/string/classification.hpp> | ||
#include <boost/algorithm/string/split.hpp> | ||
|
||
#include <filesystem> | ||
#include <vector> | ||
|
||
namespace fs = std::filesystem; | ||
|
||
std::string search_path(const char *filename, const char *path_var, std::function<const char *(const char *)> get_env) | ||
{ | ||
if (filename == nullptr || path_var == nullptr) | ||
{ | ||
return {}; | ||
} | ||
|
||
const char *path = get_env(path_var); | ||
if (path == nullptr) | ||
{ | ||
return {}; | ||
} | ||
|
||
std::vector<std::string> parts; | ||
split(parts, path, boost::algorithm::is_any_of(PATH_SEPARATOR)); | ||
for (const fs::path dir : parts) | ||
{ | ||
fs::path candidate{dir / filename}; | ||
if (exists(candidate)) | ||
{ | ||
return candidate.make_preferred().string(); | ||
} | ||
} | ||
|
||
return {}; | ||
} |
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,14 @@ | ||
#pragma once | ||
|
||
#include <cstdlib> | ||
#include <cstring> | ||
#include <functional> | ||
#include <string> | ||
|
||
std::string find_path(const char *filename, const std::function<const char *(const char *)> &get_env); | ||
|
||
inline void findpath(char const *filename, char *fullpathname) | ||
{ | ||
const std::string result = find_path(filename, [](const char *var) -> const char * { return std::getenv(var); }); | ||
std::strcpy(fullpathname, result.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
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,18 @@ | ||
#pragma once | ||
|
||
#include <cstdlib> | ||
#include <functional> | ||
#include <string> | ||
|
||
#ifdef WIN32 | ||
constexpr const char *const PATH_SEPARATOR{";"}; | ||
#else | ||
constexpr const char *const PATH_SEPARATOR{":"}; | ||
#endif | ||
|
||
std::string search_path(const char *filename, const char *path_var, std::function<const char *(const char *)> get_env); | ||
|
||
inline std::string search_path(const char * filename, const char *path_var) | ||
{ | ||
return search_path(filename, path_var, [](const char *var) -> const char * { return std::getenv(var); }); | ||
} |
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 |
---|---|---|
@@ -1,9 +1,12 @@ | ||
#pragma once | ||
|
||
#define ID_TEST_DATA_DIR "@ID_TEST_DATA_DIR@" | ||
#define ID_TEST_DATA_DIR "@ID_TEST_DATA_DIR@" | ||
#define ID_TEST_DATA_SUBDIR "@ID_TEST_DATA_SUBDIR@" | ||
|
||
// IFS test data | ||
#define ID_TEST_IFS_FILE "test.ifs" | ||
#define ID_TEST_IFS_FILE "@ID_TEST_IFS_FILE@" | ||
#define ID_TEST_FIRST_IFS_NAME "@ID_TEST_FIRST_IFS_NAME@" | ||
#define ID_TEST_FIRST_IFS_PARAM1 "@ID_TEST_FIRST_IFS_PARAM1@" | ||
#define ID_TEST_SECOND_IFS_NAME "@ID_TEST_SECOND_IFS_NAME@" | ||
|
||
#define ID_TEST_IFS_FILE2 "@ID_TEST_IFS_FILE2@" |
Oops, something went wrong.