This commit is contained in:
Scott E. Graves 2024-07-16 08:04:49 -05:00
parent 801af6b799
commit 5312797b97
4 changed files with 28 additions and 29 deletions

View File

@ -33,7 +33,7 @@ namespace repertory {
logging_consumer::logging_consumer(const std::string &log_directory, logging_consumer::logging_consumer(const std::string &log_directory,
const event_level &level) const event_level &level)
: 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"})) { log_path_(utils::path::combine(log_directory, {"repertory.log"})) {
if (not utils::file::create_full_directory_path(log_directory_)) { if (not utils::file::create_full_directory_path(log_directory_)) {
throw startup_exception("failed to create log directory|sp|" + throw startup_exception("failed to create log directory|sp|" +

View File

@ -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 { auto get_file_size(std::string path, std::uint64_t &file_size) -> bool {
file_size = 0u; file_size = 0u;
path = utils::path::finalize(path); path = utils::path::absolute(path);
#if defined(_WIN32) #if defined(_WIN32)
struct _stat64 st {}; 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 { auto move_file(std::string from, std::string to) -> bool {
from = utils::path::finalize(from); from = utils::path::absolute(from);
to = utils::path::finalize(to); to = utils::path::absolute(to);
const auto directory = utils::path::remove_file_name(to); const auto directory = utils::path::remove_file_name(to);
if (not create_full_directory_path(directory)) { if (not create_full_directory_path(directory)) {

View File

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

View File

@ -68,9 +68,6 @@ auto generate_test_file_name(const std::string &directory,
auto get_test_dir() -> std::string { auto get_test_dir() -> std::string {
auto dir = utils::get_environment_variable("PROJECT_TEST_DIR"); auto dir = utils::get_environment_variable("PROJECT_TEST_DIR");
if (not dir.empty()) { return utils::path::combine(dir.empty() ? "." : dir, {"test_data"});
return utils::path::absolute(dir);
}
return utils::path::absolute(utils::path::combine(".", {"test_data"}));
} }
} // namespace repertory } // namespace repertory