diff options
Diffstat (limited to '')
-rw-r--r-- | src/common/file_util.cpp | 8 | ||||
-rw-r--r-- | src/common/file_util.h | 7 | ||||
-rw-r--r-- | src/core/file_sys/vfs_real.cpp | 44 | ||||
-rw-r--r-- | src/yuzu/game_list_p.h | 2 |
4 files changed, 37 insertions, 24 deletions
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 190cac6d9..3ce590062 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -884,12 +884,12 @@ std::string_view RemoveTrailingSlash(std::string_view path) { return path; } -std::string SanitizePath(std::string_view path_, bool with_platform_slashes) { +std::string SanitizePath(std::string_view path_, DirectorySeparator directory_separator) { std::string path(path_); - char type1 = '\\'; - char type2 = '/'; + char type1 = directory_separator == DirectorySeparator::BackwardSlash ? '/' : '\\'; + char type2 = directory_separator == DirectorySeparator::BackwardSlash ? '\\' : '/'; - if (with_platform_slashes) { + if (directory_separator == DirectorySeparator::PlatformDefault) { #ifdef _WIN32 type1 = '/'; type2 = '\\'; diff --git a/src/common/file_util.h b/src/common/file_util.h index ca63d7466..2711872ae 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -182,9 +182,12 @@ std::vector<T> SliceVector(const std::vector<T>& vector, size_t first, size_t la return std::vector<T>(vector.begin() + first, vector.begin() + first + last); } +enum class DirectorySeparator { ForwardSlash, BackwardSlash, PlatformDefault }; + // Removes trailing slash, makes all '\\' into '/', and removes duplicate '/'. Makes '/' into '\\' -// if windows and with_platform_slashes is true. -std::string SanitizePath(std::string_view path, bool with_platform_slashes = false); +// depending if directory_separator is BackwardSlash or PlatformDefault and running on windows +std::string SanitizePath(std::string_view path, + DirectorySeparator directory_separator = DirectorySeparator::ForwardSlash); // simple wrapper for cstdlib file functions to // hopefully will make error checking easier diff --git a/src/core/file_sys/vfs_real.cpp b/src/core/file_sys/vfs_real.cpp index 21ea35aaf..1b5919737 100644 --- a/src/core/file_sys/vfs_real.cpp +++ b/src/core/file_sys/vfs_real.cpp @@ -53,7 +53,7 @@ bool RealVfsFilesystem::IsWritable() const { } VfsEntryType RealVfsFilesystem::GetEntryType(std::string_view path_) const { - const auto path = FileUtil::SanitizePath(path_, true); + const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault); if (!FileUtil::Exists(path)) return VfsEntryType::None; if (FileUtil::IsDirectory(path)) @@ -63,7 +63,7 @@ VfsEntryType RealVfsFilesystem::GetEntryType(std::string_view path_) const { } VirtualFile RealVfsFilesystem::OpenFile(std::string_view path_, Mode perms) { - const auto path = FileUtil::SanitizePath(path_, true); + const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault); if (cache.find(path) != cache.end()) { auto weak = cache[path]; if (!weak.expired()) { @@ -82,15 +82,17 @@ VirtualFile RealVfsFilesystem::OpenFile(std::string_view path_, Mode perms) { } VirtualFile RealVfsFilesystem::CreateFile(std::string_view path_, Mode perms) { - const auto path = FileUtil::SanitizePath(path_, true); + const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault); if (!FileUtil::Exists(path) && !FileUtil::CreateEmptyFile(path)) return nullptr; return OpenFile(path, perms); } VirtualFile RealVfsFilesystem::CopyFile(std::string_view old_path_, std::string_view new_path_) { - const auto old_path = FileUtil::SanitizePath(old_path_, true); - const auto new_path = FileUtil::SanitizePath(new_path_, true); + const auto old_path = + FileUtil::SanitizePath(old_path_, FileUtil::DirectorySeparator::PlatformDefault); + const auto new_path = + FileUtil::SanitizePath(new_path_, FileUtil::DirectorySeparator::PlatformDefault); if (!FileUtil::Exists(old_path) || FileUtil::Exists(new_path) || FileUtil::IsDirectory(old_path) || !FileUtil::Copy(old_path, new_path)) @@ -99,8 +101,10 @@ VirtualFile RealVfsFilesystem::CopyFile(std::string_view old_path_, std::string_ } VirtualFile RealVfsFilesystem::MoveFile(std::string_view old_path_, std::string_view new_path_) { - const auto old_path = FileUtil::SanitizePath(old_path_, true); - const auto new_path = FileUtil::SanitizePath(new_path_, true); + const auto old_path = + FileUtil::SanitizePath(old_path_, FileUtil::DirectorySeparator::PlatformDefault); + const auto new_path = + FileUtil::SanitizePath(new_path_, FileUtil::DirectorySeparator::PlatformDefault); if (!FileUtil::Exists(old_path) || FileUtil::Exists(new_path) || FileUtil::IsDirectory(old_path) || !FileUtil::Rename(old_path, new_path)) @@ -119,7 +123,7 @@ VirtualFile RealVfsFilesystem::MoveFile(std::string_view old_path_, std::string_ } bool RealVfsFilesystem::DeleteFile(std::string_view path_) { - const auto path = FileUtil::SanitizePath(path_, true); + const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault); if (cache.find(path) != cache.end()) { if (!cache[path].expired()) cache[path].lock()->Close(); @@ -129,13 +133,13 @@ bool RealVfsFilesystem::DeleteFile(std::string_view path_) { } VirtualDir RealVfsFilesystem::OpenDirectory(std::string_view path_, Mode perms) { - const auto path = FileUtil::SanitizePath(path_, true); + const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault); // Cannot use make_shared as RealVfsDirectory constructor is private return std::shared_ptr<RealVfsDirectory>(new RealVfsDirectory(*this, path, perms)); } VirtualDir RealVfsFilesystem::CreateDirectory(std::string_view path_, Mode perms) { - const auto path = FileUtil::SanitizePath(path_, true); + const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault); if (!FileUtil::Exists(path) && !FileUtil::CreateDir(path)) return nullptr; // Cannot use make_shared as RealVfsDirectory constructor is private @@ -144,8 +148,10 @@ VirtualDir RealVfsFilesystem::CreateDirectory(std::string_view path_, Mode perms VirtualDir RealVfsFilesystem::CopyDirectory(std::string_view old_path_, std::string_view new_path_) { - const auto old_path = FileUtil::SanitizePath(old_path_, true); - const auto new_path = FileUtil::SanitizePath(new_path_, true); + const auto old_path = + FileUtil::SanitizePath(old_path_, FileUtil::DirectorySeparator::PlatformDefault); + const auto new_path = + FileUtil::SanitizePath(new_path_, FileUtil::DirectorySeparator::PlatformDefault); if (!FileUtil::Exists(old_path) || FileUtil::Exists(new_path) || !FileUtil::IsDirectory(old_path)) return nullptr; @@ -155,8 +161,10 @@ VirtualDir RealVfsFilesystem::CopyDirectory(std::string_view old_path_, VirtualDir RealVfsFilesystem::MoveDirectory(std::string_view old_path_, std::string_view new_path_) { - const auto old_path = FileUtil::SanitizePath(old_path_, true); - const auto new_path = FileUtil::SanitizePath(new_path_, true); + const auto old_path = + FileUtil::SanitizePath(old_path_, FileUtil::DirectorySeparator::PlatformDefault); + const auto new_path = + FileUtil::SanitizePath(new_path_, FileUtil::DirectorySeparator::PlatformDefault); if (!FileUtil::Exists(old_path) || FileUtil::Exists(new_path) || FileUtil::IsDirectory(old_path) || !FileUtil::Rename(old_path, new_path)) return nullptr; @@ -164,9 +172,11 @@ VirtualDir RealVfsFilesystem::MoveDirectory(std::string_view old_path_, for (auto& kv : cache) { // Path in cache starts with old_path if (kv.first.rfind(old_path, 0) == 0) { - const auto file_old_path = FileUtil::SanitizePath(kv.first, true); + const auto file_old_path = + FileUtil::SanitizePath(kv.first, FileUtil::DirectorySeparator::PlatformDefault); const auto file_new_path = - FileUtil::SanitizePath(new_path + DIR_SEP + kv.first.substr(old_path.size()), true); + FileUtil::SanitizePath(new_path + DIR_SEP + kv.first.substr(old_path.size()), + FileUtil::DirectorySeparator::PlatformDefault); auto cached = cache[file_old_path]; if (!cached.expired()) { auto file = cached.lock(); @@ -181,7 +191,7 @@ VirtualDir RealVfsFilesystem::MoveDirectory(std::string_view old_path_, } bool RealVfsFilesystem::DeleteDirectory(std::string_view path_) { - const auto path = FileUtil::SanitizePath(path_, true); + const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault); for (auto& kv : cache) { // Path in cache starts with old_path if (kv.first.rfind(path, 0) == 0) { diff --git a/src/yuzu/game_list_p.h b/src/yuzu/game_list_p.h index 49a3f6181..114a0fc7f 100644 --- a/src/yuzu/game_list_p.h +++ b/src/yuzu/game_list_p.h @@ -140,7 +140,7 @@ class GameListWorker : public QObject, public QRunnable { public: GameListWorker(FileSys::VirtualFilesystem vfs, QString dir_path, bool deep_scan) - : dir_path(std::move(dir_path)), deep_scan(deep_scan) {} + : vfs(std::move(vfs)), dir_path(std::move(dir_path)), deep_scan(deep_scan) {} public slots: /// Starts the processing of directory tree information. |