From 3b11f220621a0a476fff75b7d34e6bc25db9a4e2 Mon Sep 17 00:00:00 2001 From: Eladash Date: Fri, 25 Sep 2020 09:42:41 +0300 Subject: [PATCH] MSVC bug workaround --- Utilities/File.cpp | 18 +++--------------- Utilities/File.h | 5 ++++- rpcs3/Crypto/utils.cpp | 3 ++- rpcs3/Emu/VFS.cpp | 10 ++-------- 4 files changed, 11 insertions(+), 25 deletions(-) diff --git a/Utilities/File.cpp b/Utilities/File.cpp index da745125b4..f2612c9c2c 100644 --- a/Utilities/File.cpp +++ b/Utilities/File.cpp @@ -325,12 +325,6 @@ std::string fs::get_parent_dir(const std::string& path) // Search upper bound (set to the last character, npos for empty string) auto last = path.size() - 1; -#ifdef _WIN32 - const auto& delim = "/\\"; -#else - const auto& delim = "/"; -#endif - while (true) { const auto pos = path.find_last_of(delim, last, sizeof(delim) - 1); @@ -355,13 +349,13 @@ bool fs::stat(const std::string& path, stat_t& info) std::string_view epath = path; // '/' and '\\' Not allowed by FindFirstFileExW at the end of path but we should allow it - if (auto not_del = epath.find_last_not_of("/\\"); not_del != umax && not_del != epath.size() - 1) + if (auto not_del = epath.find_last_not_of(delim); not_del != umax && not_del != epath.size() - 1) { epath.remove_suffix(epath.size() - 1 - not_del); } // Handle drives specially - if (epath.find_first_of("/\\") == umax && epath.ends_with(':')) + if (epath.find_first_of(delim) == umax && epath.ends_with(':')) { WIN32_FILE_ATTRIBUTE_DATA attrs; @@ -411,7 +405,7 @@ bool fs::stat(const std::string& path, stat_t& info) ~close_t() { FindClose(handle); } }; - for (close_t find_manage{handle}; attrs.cFileName != wpath_view.substr(wpath_view.find_last_of(L"/\\") + 1);) + for (close_t find_manage{handle}; attrs.cFileName != wpath_view.substr(wpath_view.find_last_of(wdelim) + 1);) { if (!FindNextFileW(handle, &attrs)) { @@ -1620,12 +1614,6 @@ std::string fs::escape_path(std::string_view path) { std::string real; real.resize(path.size()); -#ifdef _WIN32 - constexpr auto& delim = "/\\"; -#else - constexpr auto& delim = "/"; -#endif - auto get_char = [&](std::size_t& from, std::size_t& to, std::size_t count) { std::memcpy(&real[to], &path[from], count); diff --git a/Utilities/File.h b/Utilities/File.h index 11576b6c5d..f297db1212 100644 --- a/Utilities/File.h +++ b/Utilities/File.h @@ -8,12 +8,15 @@ #include #include - namespace fs { #ifdef _WIN32 + static constexpr auto delim = "/\\"; + static constexpr auto wdelim = L"/\\"; using native_handle = void*; #else + static constexpr auto delim = "/"; + static constexpr auto wdelim = L"/"; using native_handle = int; #endif diff --git a/rpcs3/Crypto/utils.cpp b/rpcs3/Crypto/utils.cpp index 4694eb73c6..297259ec72 100644 --- a/rpcs3/Crypto/utils.cpp +++ b/rpcs3/Crypto/utils.cpp @@ -8,6 +8,7 @@ #include #include "Utilities/StrUtil.h" #include "Utilities/span.h" +#include "Utilities/File.h" #include #include @@ -124,7 +125,7 @@ char* extract_file_name(const char* file_path, char real_file_name[MAX_PATH]) { std::string_view v(file_path); - if (auto pos = v.find_last_of("/\\"); pos != umax) + if (auto pos = v.find_last_of(fs::delim); pos != umax) { v.remove_prefix(pos + 1); } diff --git a/rpcs3/Emu/VFS.cpp b/rpcs3/Emu/VFS.cpp index 2f279f0391..cb8544a37a 100644 --- a/rpcs3/Emu/VFS.cpp +++ b/rpcs3/Emu/VFS.cpp @@ -712,14 +712,8 @@ std::string vfs::host::hash_path(const std::string& path, const std::string& dev bool vfs::host::rename(const std::string& from, const std::string& to, const lv2_fs_mount_point* mp, bool overwrite) { -#ifdef _WIN32 - constexpr auto& delim = "/\\"; -#else - constexpr auto& delim = "/"; -#endif - // Lock mount point, close file descriptors, retry - const auto from0 = std::string_view(from).substr(0, from.find_last_not_of(delim) + 1); + const auto from0 = std::string_view(from).substr(0, from.find_last_not_of(fs::delim) + 1); const auto escaped_from = fs::escape_path(from); // Lock app_home as well because it could be in the same drive as current mount point (TODO) @@ -727,7 +721,7 @@ bool vfs::host::rename(const std::string& from, const std::string& to, const lv2 auto check_path = [&](std::string_view path) { - return path.starts_with(from) && (path.size() == from.size() || path[from.size()] == delim[0] || path[from.size()] == delim[1]); + return path.starts_with(from) && (path.size() == from.size() || path[from.size()] == fs::delim[0] || path[from.size()] == fs::delim[1]); }; idm::select([&](u32 id, lv2_file& file)