refactor
This commit is contained in:
parent
801af6b799
commit
5312797b97
@ -33,7 +33,7 @@ namespace repertory {
|
||||
logging_consumer::logging_consumer(const std::string &log_directory,
|
||||
const event_level &level)
|
||||
: event_level_(level),
|
||||
log_directory_(utils::path::finalize(log_directory)),
|
||||
log_directory_(utils::path::absolute(log_directory)),
|
||||
log_path_(utils::path::combine(log_directory, {"repertory.log"})) {
|
||||
if (not utils::file::create_full_directory_path(log_directory_)) {
|
||||
throw startup_exception("failed to create log directory|sp|" +
|
||||
|
@ -485,7 +485,7 @@ auto get_modified_time(const std::string &path,
|
||||
|
||||
auto get_file_size(std::string path, std::uint64_t &file_size) -> bool {
|
||||
file_size = 0u;
|
||||
path = utils::path::finalize(path);
|
||||
path = utils::path::absolute(path);
|
||||
|
||||
#if defined(_WIN32)
|
||||
struct _stat64 st {};
|
||||
@ -549,8 +549,8 @@ auto is_modified_date_older_than(const std::string &path,
|
||||
}
|
||||
|
||||
auto move_file(std::string from, std::string to) -> bool {
|
||||
from = utils::path::finalize(from);
|
||||
to = utils::path::finalize(to);
|
||||
from = utils::path::absolute(from);
|
||||
to = utils::path::absolute(to);
|
||||
|
||||
const auto directory = utils::path::remove_file_name(to);
|
||||
if (not create_full_directory_path(directory)) {
|
||||
|
@ -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
|
||||
|
@ -68,9 +68,6 @@ auto generate_test_file_name(const std::string &directory,
|
||||
|
||||
auto get_test_dir() -> std::string {
|
||||
auto dir = utils::get_environment_variable("PROJECT_TEST_DIR");
|
||||
if (not dir.empty()) {
|
||||
return utils::path::absolute(dir);
|
||||
}
|
||||
return utils::path::absolute(utils::path::combine(".", {"test_data"}));
|
||||
return utils::path::combine(dir.empty() ? "." : dir, {"test_data"});
|
||||
}
|
||||
} // namespace repertory
|
||||
|
Loading…
x
Reference in New Issue
Block a user