From 974bce19ed9a7ec688cee37f199be46f575d2f3d Mon Sep 17 00:00:00 2001 From: Silent Date: Sun, 6 Oct 2019 12:32:08 +0200 Subject: [PATCH] Use path_append instead of operator + to concat paths This allows to cleanly prevent double // slashes when appending paths While this should not be a problem, Windows seems to have problems with such paths when paths are very long - and preventing this is trivial enough. --- Utilities/File.cpp | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/Utilities/File.cpp b/Utilities/File.cpp index 5b49fc5ebf..ba7859034e 100644 --- a/Utilities/File.cpp +++ b/Utilities/File.cpp @@ -177,6 +177,27 @@ static fs::error to_error(int e) #endif +static std::string path_append(std::string_view path, std::string_view more) +{ + std::string result; + + if (const size_t src_slash_pos = path.find_last_not_of('/'); src_slash_pos != path.npos) + { + path.remove_suffix(path.length() - src_slash_pos - 1); + result = path; + } + + result.push_back('/'); + + if (const size_t dst_slash_pos = more.find_first_not_of('/'); dst_slash_pos != more.npos) + { + more.remove_prefix(dst_slash_pos); + result.append(more); + } + + return result; +} + namespace fs { thread_local error g_tls_error = error::ok; @@ -1586,7 +1607,7 @@ bool fs::remove_all(const std::string& path, bool remove_root) if (entry.is_directory == false) { - if (!remove_file(path + '/' + entry.name)) + if (!remove_file(path_append(path, entry.name))) { return false; } @@ -1594,7 +1615,7 @@ bool fs::remove_all(const std::string& path, bool remove_root) if (entry.is_directory == true) { - if (!remove_all(path + '/' + entry.name)) + if (!remove_all(path_append(path, entry.name))) { return false; } @@ -1632,7 +1653,7 @@ u64 fs::get_dir_size(const std::string& path, u64 rounding_alignment) if (entry.is_directory == true) { - result += get_dir_size(path + '/' + entry.name, rounding_alignment); + result += get_dir_size(path_append(path, entry.name), rounding_alignment); } }