1
0
Fork 0

Codechange: use std::string_view over const std::string& for finding files

pull/14004/head
Rubidium 2025-04-20 23:08:19 +02:00 committed by rubidium42
parent 354ad43edb
commit e3858e81dc
2 changed files with 9 additions and 9 deletions

View File

@ -118,7 +118,7 @@ static void FillValidSearchPaths(bool only_local_path)
* @param subdir the subdirectory to look in * @param subdir the subdirectory to look in
* @return true if and only if the file can be opened * @return true if and only if the file can be opened
*/ */
bool FioCheckFileExists(const std::string &filename, Subdirectory subdir) bool FioCheckFileExists(std::string_view filename, Subdirectory subdir)
{ {
auto f = FioFOpenFile(filename, "rb", subdir); auto f = FioFOpenFile(filename, "rb", subdir);
return f.has_value(); return f.has_value();
@ -141,7 +141,7 @@ bool FileExists(const std::string &filename)
* @param filename Filename to look for. * @param filename Filename to look for.
* @return String containing the path if the path was found, else an empty string. * @return String containing the path if the path was found, else an empty string.
*/ */
std::string FioFindFullPath(Subdirectory subdir, const std::string &filename) std::string FioFindFullPath(Subdirectory subdir, std::string_view filename)
{ {
assert(subdir < NUM_SUBDIRS); assert(subdir < NUM_SUBDIRS);
@ -180,7 +180,7 @@ std::string FioFindDirectory(Subdirectory subdir)
return _personal_dir; return _personal_dir;
} }
static std::optional<FileHandle> FioFOpenFileSp(const std::string &filename, const char *mode, Searchpath sp, Subdirectory subdir, size_t *filesize) static std::optional<FileHandle> FioFOpenFileSp(std::string_view filename, const char *mode, Searchpath sp, Subdirectory subdir, size_t *filesize)
{ {
#if defined(_WIN32) #if defined(_WIN32)
/* fopen is implemented as a define with ellipses for /* fopen is implemented as a define with ellipses for
@ -195,7 +195,7 @@ static std::optional<FileHandle> FioFOpenFileSp(const std::string &filename, con
if (subdir == NO_DIRECTORY) { if (subdir == NO_DIRECTORY) {
buf = filename; buf = filename;
} else { } else {
buf = _searchpaths[sp] + _subdirs[subdir] + filename; buf = fmt::format("{}{}{}", _searchpaths[sp], _subdirs[subdir], filename);
} }
auto f = FileHandle::Open(buf, mode); auto f = FileHandle::Open(buf, mode);
@ -239,7 +239,7 @@ static std::optional<FileHandle> FioFOpenFileTar(const TarFileListEntry &entry,
* @param subdir Subdirectory to open. * @param subdir Subdirectory to open.
* @return File handle of the opened file, or \c nullptr if the file is not available. * @return File handle of the opened file, or \c nullptr if the file is not available.
*/ */
std::optional<FileHandle> FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize) std::optional<FileHandle> FioFOpenFile(std::string_view filename, const char *mode, Subdirectory subdir, size_t *filesize)
{ {
std::optional<FileHandle> f = std::nullopt; std::optional<FileHandle> f = std::nullopt;
assert(subdir < NUM_SUBDIRS || subdir == NO_DIRECTORY); assert(subdir < NUM_SUBDIRS || subdir == NO_DIRECTORY);
@ -252,7 +252,7 @@ std::optional<FileHandle> FioFOpenFile(const std::string &filename, const char *
/* We can only use .tar in case of data-dir, and read-mode */ /* We can only use .tar in case of data-dir, and read-mode */
if (!f.has_value() && mode[0] == 'r' && subdir != NO_DIRECTORY) { if (!f.has_value() && mode[0] == 'r' && subdir != NO_DIRECTORY) {
/* Filenames in tars are always forced to be lowercase */ /* Filenames in tars are always forced to be lowercase */
std::string resolved_name = filename; std::string resolved_name{filename};
strtolower(resolved_name); strtolower(resolved_name);
/* Resolve ".." */ /* Resolve ".." */

View File

@ -13,9 +13,9 @@
#include "core/enum_type.hpp" #include "core/enum_type.hpp"
#include "fileio_type.h" #include "fileio_type.h"
std::optional<FileHandle> FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize = nullptr); std::optional<FileHandle> FioFOpenFile(std::string_view filename, const char *mode, Subdirectory subdir, size_t *filesize = nullptr);
bool FioCheckFileExists(const std::string &filename, Subdirectory subdir); bool FioCheckFileExists(std::string_view filename, Subdirectory subdir);
std::string FioFindFullPath(Subdirectory subdir, const std::string &filename); std::string FioFindFullPath(Subdirectory subdir, std::string_view filename);
std::string FioGetDirectory(Searchpath sp, Subdirectory subdir); std::string FioGetDirectory(Searchpath sp, Subdirectory subdir);
std::string FioFindDirectory(Subdirectory subdir); std::string FioFindDirectory(Subdirectory subdir);
void FioCreateDirectory(const std::string &name); void FioCreateDirectory(const std::string &name);