|
|
|
@ -28,18 +28,20 @@
|
|
|
|
|
|
|
|
|
|
namespace repertory::utils::path {
|
|
|
|
|
auto absolute(std::string path) -> std::string {
|
|
|
|
|
path = finalize(path);
|
|
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
|
if (not path.empty() && ::PathIsRelative(&path[0u])) {
|
|
|
|
|
if (not path.empty() && ::PathIsRelative(path.c_str())) {
|
|
|
|
|
std::string temp;
|
|
|
|
|
temp.resize(MAX_PATH + 1);
|
|
|
|
|
path = _fullpath(&temp[0u], &path[0u], MAX_PATH);
|
|
|
|
|
path = _fullpath(temp.data(), path.c_str(), MAX_PATH);
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
if (not path.empty() && (path[0u] != '/')) {
|
|
|
|
|
if (not path.empty() && (path[0U] != '/')) {
|
|
|
|
|
auto found = false;
|
|
|
|
|
auto tmp = path;
|
|
|
|
|
do {
|
|
|
|
|
auto *res = realpath(&tmp[0u], nullptr);
|
|
|
|
|
auto *res = realpath(&tmp[0U], nullptr);
|
|
|
|
|
if (res) {
|
|
|
|
|
path = combine(res, {path.substr(tmp.size())});
|
|
|
|
|
free(res);
|
|
|
|
@ -47,18 +49,18 @@ auto absolute(std::string path) -> std::string {
|
|
|
|
|
} else if (tmp == ".") {
|
|
|
|
|
found = true;
|
|
|
|
|
} else {
|
|
|
|
|
tmp = dirname(&tmp[0u]);
|
|
|
|
|
tmp = dirname(&tmp[0U]);
|
|
|
|
|
}
|
|
|
|
|
} while (not found);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return finalize(path);
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto combine(std::string path,
|
|
|
|
|
const std::vector<std::string> &paths) -> std::string {
|
|
|
|
|
return finalize(
|
|
|
|
|
return absolute(
|
|
|
|
|
std::accumulate(paths.begin(), paths.end(), path,
|
|
|
|
|
[](std::string next_path, const auto &path_part) {
|
|
|
|
|
if (next_path.empty()) {
|
|
|
|
@ -78,7 +80,7 @@ auto create_api_path(std::string path) -> std::string {
|
|
|
|
|
path = path.substr(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (path[0u] != '/') {
|
|
|
|
|
if (path[0U] != '/') {
|
|
|
|
|
path = "/" + path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -92,14 +94,14 @@ auto create_api_path(std::string path) -> std::string {
|
|
|
|
|
auto finalize(std::string path) -> std::string {
|
|
|
|
|
format_path(path, not_directory_seperator, directory_seperator);
|
|
|
|
|
format_path(path, directory_seperator, not_directory_seperator);
|
|
|
|
|
if ((path.size() > 1u) &&
|
|
|
|
|
(path[path.size() - 1u] == directory_seperator[0u])) {
|
|
|
|
|
path = path.substr(0u, path.size() - 1u);
|
|
|
|
|
if ((path.size() > 1U) &&
|
|
|
|
|
(path[path.size() - 1U] == directory_seperator[0U])) {
|
|
|
|
|
path = path.substr(0U, path.size() - 1U);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
|
if ((path.size() >= 2u) && (path[1u] == ':')) {
|
|
|
|
|
path[0u] = utils::string::to_lower(std::string(1u, path[0u]))[0u];
|
|
|
|
|
if ((path.size() >= 2u) && (path[1U] == ':')) {
|
|
|
|
|
path[0U] = utils::string::to_lower(std::string(1U, path[0U]))[0U];
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
@ -108,7 +110,7 @@ auto finalize(std::string path) -> std::string {
|
|
|
|
|
|
|
|
|
|
auto format_path(std::string &path, const std::string &sep,
|
|
|
|
|
const std::string ¬_sep) -> std::string & {
|
|
|
|
|
std::replace(path.begin(), path.end(), not_sep[0u], sep[0u]);
|
|
|
|
|
std::replace(path.begin(), path.end(), not_sep[0U], sep[0U]);
|
|
|
|
|
|
|
|
|
|
while (utils::string::contains(path, sep + sep)) {
|
|
|
|
|
utils::string::replace(path, sep + sep, sep);
|
|
|
|
@ -131,7 +133,7 @@ auto get_parent_api_path(const std::string &path) -> std::string {
|
|
|
|
|
|
|
|
|
|
#if !defined(_WIN32)
|
|
|
|
|
auto get_parent_directory(std::string path) -> std::string {
|
|
|
|
|
auto ret = std::string(dirname(&path[0u]));
|
|
|
|
|
auto ret = std::string(dirname(&path[0U]));
|
|
|
|
|
if (ret == ".") {
|
|
|
|
|
ret = "/";
|
|
|
|
|
}
|
|
|
|
@ -159,10 +161,10 @@ auto is_trash_directory(std::string path) -> bool {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto remove_file_name(std::string path) -> std::string {
|
|
|
|
|
path = finalize(path);
|
|
|
|
|
path = absolute(path);
|
|
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
|
::PathRemoveFileSpec(&path[0u]);
|
|
|
|
|
::PathRemoveFileSpec(&path[0U]);
|
|
|
|
|
path = path.c_str();
|
|
|
|
|
#else
|
|
|
|
|
if (path != "/") {
|
|
|
|
@ -171,7 +173,7 @@ auto remove_file_name(std::string path) -> std::string {
|
|
|
|
|
i--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
path = (i > 0) ? finalize(path.substr(0, i)) : "/";
|
|
|
|
|
path = (i > 0) ? absolute(path.substr(0, i)) : "/";
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
@ -188,15 +190,15 @@ auto resolve(std::string path) -> std::string {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return finalize(utils::string::replace(path, "~", home));
|
|
|
|
|
return absolute(utils::string::replace(path, "~", home));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
auto strip_to_file_name(std::string path) -> std::string {
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
|
return ::PathFindFileName(&path[0u]);
|
|
|
|
|
return ::PathFindFileName(path.c_str());
|
|
|
|
|
#else
|
|
|
|
|
return utils::string::contains(path, "/") ? basename(&path[0u]) : path;
|
|
|
|
|
return utils::string::contains(path, "/") ? basename(&path[0U]) : path;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
} // namespace repertory::utils::path
|
|
|
|
|