This commit is contained in:
2024-08-02 20:59:41 -05:00
parent bba7f10703
commit 6718eb374d
6 changed files with 91 additions and 38 deletions

View File

@ -141,9 +141,9 @@ get_not_directory_seperator<wchar_t>() -> std::basic_string_view<wchar_t> {
return not_directory_seperator_w;
}
[[nodiscard]] inline auto absolute(std::string_view path) -> std::string;
[[nodiscard]] auto absolute(std::string_view path) -> std::string;
[[nodiscard]] inline auto absolute(std::wstring_view path) -> std::wstring;
[[nodiscard]] auto absolute(std::wstring_view path) -> std::wstring;
[[nodiscard]] inline auto
combine(std::string path,
@ -226,14 +226,6 @@ get_parent_api_path(std::wstring_view path) -> std::wstring;
[[nodiscard]] auto unmake_file_uri(std::wstring_view uri) -> std::wstring;
inline auto absolute(std::string_view path) -> std::string {
return std::filesystem::absolute(finalize(path)).lexically_normal().string();
}
inline auto absolute(std::wstring_view path) -> std::wstring {
return std::filesystem::absolute(finalize(path)).lexically_normal().wstring();
}
template <typename string_t>
[[nodiscard]] inline auto combine_t(
string_t path,

View File

@ -36,6 +36,40 @@ static const std::wstring directory_seperator_str_w{
} // namespace
namespace repertory::utils::path {
inline auto absolute(std::string_view path) -> std::string {
std::string abs_path{path};
#ifdef _WIN32
if (not abs_path.empty() && ::PathIsRelative(abs_path.c_str())) {
std::string temp;
temp.resize(MAX_PATH + 1);
abs_path = _fullpath(temp.c_str(), abs_path.data(), MAX_PATH);
}
#else
if (not abs_path.empty() && (abs_path.at(0U) != '/')) {
auto found{false};
std::string tmp{abs_path};
do {
auto *res = realpath(tmp.c_str(), nullptr);
if (res) {
abs_path = combine(res, {abs_path.substr(tmp.size())});
free(res);
found = true;
} else if (tmp == ".") {
found = true;
} else {
tmp = dirname(tmp.c_str());
}
} while (not found);
}
#endif
return finalize(abs_path);
}
inline auto absolute(std::wstring_view path) -> std::wstring {
return utils::string::from_utf8(absolute(utils::string::to_utf8(path)));
}
auto find_program_in_path(const std::string &name_without_extension)
-> std::string {
static std::mutex mtx{};

View File

@ -120,43 +120,43 @@ TEST(utils_path, combine) {
}
TEST(utils_path, format_path) {
std::string path{"\\"};
utils::path::format_path(path, utils::path::directory_seperator,
utils::path::not_directory_seperator);
std::string path{"./"};
utils::path::format_path(path, utils::path::slash, utils::path::backslash);
EXPECT_STREQ(".", path.c_str());
path = "~/.test";
utils::path::format_path(path, utils::path::slash, utils::path::backslash);
EXPECT_STREQ("~/.test", path.c_str());
path = "\\";
utils::path::format_path(path, utils::path::slash, utils::path::backslash);
EXPECT_STREQ("/", path.c_str());
path = "\\\\";
utils::path::format_path(path, utils::path::directory_seperator,
utils::path::not_directory_seperator);
utils::path::format_path(path, utils::path::slash, utils::path::backslash);
EXPECT_STREQ("/", path.c_str());
path = "\\\\\\";
utils::path::format_path(path, utils::path::directory_seperator,
utils::path::not_directory_seperator);
utils::path::format_path(path, utils::path::slash, utils::path::backslash);
EXPECT_STREQ("/", path.c_str());
path = "\\\\\\\\";
utils::path::format_path(path, utils::path::directory_seperator,
utils::path::not_directory_seperator);
utils::path::format_path(path, utils::path::slash, utils::path::backslash);
EXPECT_STREQ("/", path.c_str());
path = "/";
utils::path::format_path(path, utils::path::directory_seperator,
utils::path::not_directory_seperator);
utils::path::format_path(path, utils::path::slash, utils::path::backslash);
EXPECT_STREQ("/", path.c_str());
path = "//";
utils::path::format_path(path, utils::path::directory_seperator,
utils::path::not_directory_seperator);
utils::path::format_path(path, utils::path::slash, utils::path::backslash);
EXPECT_STREQ("/", path.c_str());
path = "///";
utils::path::format_path(path, utils::path::directory_seperator,
utils::path::not_directory_seperator);
utils::path::format_path(path, utils::path::slash, utils::path::backslash);
EXPECT_STREQ("/", path.c_str());
path = "////";
utils::path::format_path(path, utils::path::directory_seperator,
utils::path::not_directory_seperator);
utils::path::format_path(path, utils::path::slash, utils::path::backslash);
EXPECT_STREQ("/", path.c_str());
}
@ -245,4 +245,20 @@ TEST(utils_path, finalize) {
EXPECT_STREQ("/cow/moose/dog/chicken", s.c_str());
#endif
}
#if !defined(_WIN32)
TEST(utils_path, resolve) {
std::cout << utils::path::resolve("~") << std::endl;
std::cout << utils::path::combine("~",
{
".local",
})
<< std::endl;
std::cout << utils::path::resolve(utils::path::combine("~",
{
".local",
}))
<< std::endl;
}
#endif // !defined(_WIN32)
} // namespace repertory