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
std::string basepath = FileSystem::GetPathDirectory(filename) + "/";
std::string basepath(FileSystem::GetPathDirectory(filename));
basepath += "/";
m_filename = filename;
u32 disc_lba = 0;

View file

@ -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<std::string> GetRootDirectoryList()

View file

@ -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<std::string> GetRootDirectoryList();

View file

@ -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;
}

View file

@ -1,4 +1,5 @@
#include "gamelistmodel.h"
#include "common/file_system.h"
#include "common/string_util.h"
#include "core/system.h"
#include <QtGui/QIcon>
@ -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<int>(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<int>(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);

View file

@ -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;