From cacf12c209b737e2a46ec88f299df9005ba195e0 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Tue, 16 Feb 2021 00:48:14 +1000 Subject: [PATCH] FileSystem: Migrate component extractors to std::string_view --- src/common/cd_image_cue.cpp | 3 +- src/common/file_system.cpp | 64 ++++++++++----------------- src/common/file_system.h | 8 ++-- src/core/host_interface.cpp | 4 +- src/duckstation-qt/gamelistmodel.cpp | 9 ++-- src/frontend-common/fullscreen_ui.cpp | 4 +- 6 files changed, 38 insertions(+), 54 deletions(-) diff --git a/src/common/cd_image_cue.cpp b/src/common/cd_image_cue.cpp index 7a22c8723..ba27c6ada 100644 --- a/src/common/cd_image_cue.cpp +++ b/src/common/cd_image_cue.cpp @@ -66,7 +66,8 @@ bool CDImageCueSheet::OpenAndParse(const char* filename) } // get the directory of the filename - std::string basepath = FileSystem::GetPathDirectory(filename) + "/"; + std::string basepath(FileSystem::GetPathDirectory(filename)); + basepath += "/"; m_filename = filename; u32 disc_lba = 0; diff --git a/src/common/file_system.cpp b/src/common/file_system.cpp index 289c7c5c6..7a176770e 100644 --- a/src/common/file_system.cpp +++ b/src/common/file_system.cpp @@ -249,7 +249,7 @@ bool IsAbsolutePath(const std::string_view& path) #endif } -std::string ReplaceExtension(std::string_view path, std::string_view new_extension) +std::string ReplaceExtension(const std::string_view& path, const std::string_view& new_extension) { std::string_view::size_type pos = path.rfind('.'); if (pos == std::string::npos) @@ -260,58 +260,40 @@ std::string ReplaceExtension(std::string_view path, std::string_view new_extensi return ret; } -std::string GetPathDirectory(const char* path) +std::string_view GetPathDirectory(const std::string_view& path) { -#ifdef WIN32 - const char* forwardslash_ptr = std::strrchr(path, '/'); - const char* backslash_ptr = std::strrchr(path, '\\'); - const char* slash_ptr; - if (forwardslash_ptr && backslash_ptr) - slash_ptr = std::max(forwardslash_ptr, backslash_ptr); - else if (backslash_ptr) - slash_ptr = backslash_ptr; - else if (forwardslash_ptr) - slash_ptr = forwardslash_ptr; - else - return {}; +#ifdef _WIN32 + std::string::size_type pos = path.find_last_of("/\\"); #else - const char* slash_ptr = std::strrchr(path, '/'); - if (!slash_ptr) - return {}; + std::string::size_type pos = path.find_last_of("/"); #endif - - if (slash_ptr == path) + if (pos == std::string_view::npos) return {}; - std::string str; - str.append(path, slash_ptr - path); - return str; + return path.substr(0, pos); } -std::string_view GetFileNameFromPath(const char* path) +std::string_view GetFileNameFromPath(const std::string_view& path) { - const char* end = path + std::strlen(path); - const char* start = std::max(std::strrchr(path, '/'), std::strrchr(path, '\\')); - if (!start) - return std::string_view(path, end - path); - else - return std::string_view(start + 1, end - start); +#ifdef _WIN32 + std::string::size_type pos = path.find_last_of("/\\"); +#else + std::string::size_type pos = path.find_last_of("/"); +#endif + if (pos == std::string_view::npos) + return path; + + return path.substr(pos + 1); } -std::string_view GetFileTitleFromPath(const char* path) +std::string_view GetFileTitleFromPath(const std::string_view& path) { - const char* end = path + std::strlen(path); - const char* extension = std::strrchr(path, '.'); - if (extension && extension > path) - end = extension - 1; + std::string_view filename(GetFileNameFromPath(path)); + std::string::size_type pos = filename.rfind('.'); + if (pos == std::string_view::npos) + return filename; - const char* start = std::max(std::strrchr(path, '/'), std::strrchr(path, '\\')); - if (!start) - return std::string_view(path, end - path); - else if (start < end) - return std::string_view(start + 1, end - start); - else - return std::string_view(path); + return filename.substr(0, pos); } std::vector GetRootDirectoryList() diff --git a/src/common/file_system.h b/src/common/file_system.h index 29bc6c77e..7b543e549 100644 --- a/src/common/file_system.h +++ b/src/common/file_system.h @@ -143,16 +143,16 @@ void SanitizeFileName(String& Destination, bool StripSlashes = true); bool IsAbsolutePath(const std::string_view& path); /// Replaces the extension of a filename with another. -std::string ReplaceExtension(std::string_view path, std::string_view new_extension); +std::string ReplaceExtension(const std::string_view& path, const std::string_view& new_extension); /// Returns the directory component of a filename. -std::string GetPathDirectory(const char* path); +std::string_view GetPathDirectory(const std::string_view& path); /// Returns the filename component of a filename. -std::string_view GetFileNameFromPath(const char* path); +std::string_view GetFileNameFromPath(const std::string_view& path); /// Returns the file title (less the extension and path) from a filename. -std::string_view GetFileTitleFromPath(const char* path); +std::string_view GetFileTitleFromPath(const std::string_view& path); /// Returns a list of "root directories" (i.e. root/home directories on Linux, drive letters on Windows). std::vector GetRootDirectoryList(); diff --git a/src/core/host_interface.cpp b/src/core/host_interface.cpp index 56a203d86..787c165e7 100644 --- a/src/core/host_interface.cpp +++ b/src/core/host_interface.cpp @@ -875,8 +875,8 @@ void HostInterface::CheckForSettingsChanges(const Settings& old_settings) void HostInterface::SetUserDirectoryToProgramDirectory() { - const std::string program_path = FileSystem::GetProgramPath(); - const std::string program_directory = FileSystem::GetPathDirectory(program_path.c_str()); + const std::string program_path(FileSystem::GetProgramPath()); + const std::string program_directory(FileSystem::GetPathDirectory(program_path.c_str())); m_user_directory = program_directory; } diff --git a/src/duckstation-qt/gamelistmodel.cpp b/src/duckstation-qt/gamelistmodel.cpp index 3518ed734..f2f481c33 100644 --- a/src/duckstation-qt/gamelistmodel.cpp +++ b/src/duckstation-qt/gamelistmodel.cpp @@ -1,4 +1,5 @@ #include "gamelistmodel.h" +#include "common/file_system.h" #include "common/string_util.h" #include "core/system.h" #include @@ -159,7 +160,7 @@ QVariant GameListModel::data(const QModelIndex& index, int role) const case Column_FileTitle: { - const std::string_view file_title(System::GetTitleForPath(ge.path.c_str())); + const std::string_view file_title(FileSystem::GetFileTitleFromPath(ge.path)); return QString::fromUtf8(file_title.data(), static_cast(file_title.length())); } @@ -195,7 +196,7 @@ QVariant GameListModel::data(const QModelIndex& index, int role) const case Column_FileTitle: { - const std::string_view file_title(System::GetTitleForPath(ge.path.c_str())); + const std::string_view file_title(FileSystem::GetFileTitleFromPath(ge.path)); return QString::fromUtf8(file_title.data(), static_cast(file_title.length())); } @@ -359,8 +360,8 @@ bool GameListModel::lessThan(const QModelIndex& left_index, const QModelIndex& r case Column_FileTitle: { - const std::string_view file_title_left(System::GetTitleForPath(left.path.c_str())); - const std::string_view file_title_right(System::GetTitleForPath(right.path.c_str())); + const std::string_view file_title_left(FileSystem::GetFileTitleFromPath(left.path)); + const std::string_view file_title_right(FileSystem::GetFileTitleFromPath(right.path)); if (file_title_left == file_title_right) return titlesLessThan(left_row, right_row, ascending); diff --git a/src/frontend-common/fullscreen_ui.cpp b/src/frontend-common/fullscreen_ui.cpp index 50a15906b..7ad3e2d98 100644 --- a/src/frontend-common/fullscreen_ui.cpp +++ b/src/frontend-common/fullscreen_ui.cpp @@ -587,7 +587,7 @@ static void DoChangeDiscFromFile() }; OpenFileSelector(ICON_FA_COMPACT_DISC " Select Disc Image", false, std::move(callback), GetDiscImageFilters(), - FileSystem::GetPathDirectory(System::GetMediaFileName().c_str())); + std::string(FileSystem::GetPathDirectory(System::GetMediaFileName().c_str()))); } static void DoChangeDisc() @@ -950,7 +950,7 @@ static bool SettingInfoButton(const SettingInfo& si, const char* section) CloseFileSelector(); }; OpenFileSelector(si.visible_name, false, std::move(callback), ImGuiFullscreen::FileSelectorFilters(), - FileSystem::GetPathDirectory(value.c_str()).c_str()); + std::string(FileSystem::GetPathDirectory(value.c_str()))); } return false;