FileSystem: Migrate component extractors to std::string_view

This commit is contained in:
Connor McLaughlin 2021-02-16 00:48:14 +10:00
parent 8c7aec2edf
commit cacf12c209
6 changed files with 38 additions and 54 deletions

View file

@ -66,7 +66,8 @@ bool CDImageCueSheet::OpenAndParse(const char* filename)
} }
// get the directory of the filename // get the directory of the filename
std::string basepath = FileSystem::GetPathDirectory(filename) + "/"; std::string basepath(FileSystem::GetPathDirectory(filename));
basepath += "/";
m_filename = filename; m_filename = filename;
u32 disc_lba = 0; u32 disc_lba = 0;

View file

@ -249,7 +249,7 @@ bool IsAbsolutePath(const std::string_view& path)
#endif #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('.'); std::string_view::size_type pos = path.rfind('.');
if (pos == std::string::npos) if (pos == std::string::npos)
@ -260,58 +260,40 @@ std::string ReplaceExtension(std::string_view path, std::string_view new_extensi
return ret; return ret;
} }
std::string GetPathDirectory(const char* path) std::string_view GetPathDirectory(const std::string_view& path)
{ {
#ifdef WIN32 #ifdef _WIN32
const char* forwardslash_ptr = std::strrchr(path, '/'); std::string::size_type pos = path.find_last_of("/\\");
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 {};
#else #else
const char* slash_ptr = std::strrchr(path, '/'); std::string::size_type pos = path.find_last_of("/");
if (!slash_ptr)
return {};
#endif #endif
if (pos == std::string_view::npos)
if (slash_ptr == path)
return {}; return {};
std::string str; return path.substr(0, pos);
str.append(path, slash_ptr - path);
return str;
} }
std::string_view GetFileNameFromPath(const char* path) std::string_view GetFileNameFromPath(const std::string_view& path)
{ {
const char* end = path + std::strlen(path); #ifdef _WIN32
const char* start = std::max(std::strrchr(path, '/'), std::strrchr(path, '\\')); std::string::size_type pos = path.find_last_of("/\\");
if (!start) #else
return std::string_view(path, end - path); std::string::size_type pos = path.find_last_of("/");
else #endif
return std::string_view(start + 1, end - start); 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); std::string_view filename(GetFileNameFromPath(path));
const char* extension = std::strrchr(path, '.'); std::string::size_type pos = filename.rfind('.');
if (extension && extension > path) if (pos == std::string_view::npos)
end = extension - 1; return filename;
const char* start = std::max(std::strrchr(path, '/'), std::strrchr(path, '\\')); return filename.substr(0, pos);
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);
} }
std::vector<std::string> GetRootDirectoryList() std::vector<std::string> GetRootDirectoryList()

View file

@ -143,16 +143,16 @@ void SanitizeFileName(String& Destination, bool StripSlashes = true);
bool IsAbsolutePath(const std::string_view& path); bool IsAbsolutePath(const std::string_view& path);
/// Replaces the extension of a filename with another. /// 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. /// 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. /// 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. /// 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). /// Returns a list of "root directories" (i.e. root/home directories on Linux, drive letters on Windows).
std::vector<std::string> GetRootDirectoryList(); std::vector<std::string> GetRootDirectoryList();

View file

@ -875,8 +875,8 @@ void HostInterface::CheckForSettingsChanges(const Settings& old_settings)
void HostInterface::SetUserDirectoryToProgramDirectory() void HostInterface::SetUserDirectoryToProgramDirectory()
{ {
const std::string program_path = FileSystem::GetProgramPath(); const std::string program_path(FileSystem::GetProgramPath());
const std::string program_directory = FileSystem::GetPathDirectory(program_path.c_str()); const std::string program_directory(FileSystem::GetPathDirectory(program_path.c_str()));
m_user_directory = program_directory; m_user_directory = program_directory;
} }

View file

@ -1,4 +1,5 @@
#include "gamelistmodel.h" #include "gamelistmodel.h"
#include "common/file_system.h"
#include "common/string_util.h" #include "common/string_util.h"
#include "core/system.h" #include "core/system.h"
#include <QtGui/QIcon> #include <QtGui/QIcon>
@ -159,7 +160,7 @@ QVariant GameListModel::data(const QModelIndex& index, int role) const
case Column_FileTitle: 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<int>(file_title.length())); return QString::fromUtf8(file_title.data(), static_cast<int>(file_title.length()));
} }
@ -195,7 +196,7 @@ QVariant GameListModel::data(const QModelIndex& index, int role) const
case Column_FileTitle: 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<int>(file_title.length())); return QString::fromUtf8(file_title.data(), static_cast<int>(file_title.length()));
} }
@ -359,8 +360,8 @@ bool GameListModel::lessThan(const QModelIndex& left_index, const QModelIndex& r
case Column_FileTitle: case Column_FileTitle:
{ {
const std::string_view file_title_left(System::GetTitleForPath(left.path.c_str())); const std::string_view file_title_left(FileSystem::GetFileTitleFromPath(left.path));
const std::string_view file_title_right(System::GetTitleForPath(right.path.c_str())); const std::string_view file_title_right(FileSystem::GetFileTitleFromPath(right.path));
if (file_title_left == file_title_right) if (file_title_left == file_title_right)
return titlesLessThan(left_row, right_row, ascending); return titlesLessThan(left_row, right_row, ascending);

View file

@ -587,7 +587,7 @@ static void DoChangeDiscFromFile()
}; };
OpenFileSelector(ICON_FA_COMPACT_DISC " Select Disc Image", false, std::move(callback), GetDiscImageFilters(), 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() static void DoChangeDisc()
@ -950,7 +950,7 @@ static bool SettingInfoButton(const SettingInfo& si, const char* section)
CloseFileSelector(); CloseFileSelector();
}; };
OpenFileSelector(si.visible_name, false, std::move(callback), ImGuiFullscreen::FileSelectorFilters(), 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; return false;