From f61427017ddc46f7fe51ba5f1edd2440beffc97f Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sun, 24 Jan 2021 14:13:08 +1000 Subject: [PATCH] GameList: Remove exe/psf extensions from titles in game list --- src/common/file_system.cpp | 26 ++++++++++++++++++++++++++ src/common/file_system.h | 6 ++++++ src/frontend-common/game_list.cpp | 16 ++-------------- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/common/file_system.cpp b/src/common/file_system.cpp index 6a24732b8..84079a303 100644 --- a/src/common/file_system.cpp +++ b/src/common/file_system.cpp @@ -287,6 +287,32 @@ std::string GetPathDirectory(const char* path) return str; } +std::string_view GetFileNameFromPath(const char* 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); +} + +std::string_view GetFileTitleFromPath(const char* path) +{ + const char* end = path + std::strlen(path); + const char* extension = std::strrchr(path, '.'); + if (extension && extension > path) + end = extension - 1; + + 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); +} + void BuildPathRelativeToFile(char* Destination, u32 cbDestination, const char* CurrentFileName, const char* NewFileName, bool OSPath /* = true */, bool Canonicalize /* = true */) { diff --git a/src/common/file_system.h b/src/common/file_system.h index 52cbad856..c3fd0f089 100644 --- a/src/common/file_system.h +++ b/src/common/file_system.h @@ -148,6 +148,12 @@ std::string ReplaceExtension(std::string_view path, std::string_view new_extensi /// Returns the directory component of a filename. std::string GetPathDirectory(const char* path); +/// Returns the filename component of a filename. +std::string_view GetFileNameFromPath(const char* path); + +/// Returns the file title (less the extension and path) from a filename. +std::string_view GetFileTitleFromPath(const char* path); + // search for files bool FindFiles(const char* Path, const char* Pattern, u32 Flags, FindResultsArray* pResults); diff --git a/src/frontend-common/game_list.cpp b/src/frontend-common/game_list.cpp index 4a203439b..13d86f471 100644 --- a/src/frontend-common/game_list.cpp +++ b/src/frontend-common/game_list.cpp @@ -52,16 +52,6 @@ const char* GameList::GetGameListCompatibilityRatingString(GameListCompatibility ""; } -static std::string_view GetFileNameFromPath(const char* path) -{ - const char* filename_end = path + std::strlen(path); - const char* filename_start = std::max(std::strrchr(path, '/'), std::strrchr(path, '\\')); - if (!filename_start) - return std::string_view(path, filename_end - path); - else - return std::string_view(filename_start + 1, filename_end - filename_start); -} - bool GameList::GetExeListEntry(const char* path, GameListEntry* entry) { FILESYSTEM_STAT_DATA ffd; @@ -96,9 +86,7 @@ bool GameList::GetExeListEntry(const char* path, GameListEntry* entry) return false; entry->code.clear(); - entry->title = GetFileNameFromPath(path); - - // no way to detect region... + entry->title = FileSystem::GetFileTitleFromPath(path); entry->path = path; entry->region = BIOS::GetPSExeDiscRegion(header); entry->total_size = ZeroExtend64(file_size); @@ -143,7 +131,7 @@ bool GameList::GetPsfListEntry(const char* path, GameListEntry* entry) if (title.has_value()) entry->title += title.value(); else - entry->title += GetFileNameFromPath(path); + entry->title += FileSystem::GetFileTitleFromPath(path); return true; }