diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 8944255f4b..12ff721e44 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -1288,7 +1288,7 @@ static bool ConScript(std::span argv) if (!CloseConsoleLogIfActive()) { if (argv.size() < 2) return false; - _iconsole_output_file = FileHandle::Open(std::string(argv[1]), "ab"); + _iconsole_output_file = FileHandle::Open(argv[1], "ab"); if (!_iconsole_output_file.has_value()) { IConsolePrint(CC_ERROR, "Could not open console log file '{}'.", argv[1]); } else { diff --git a/src/fileio.cpp b/src/fileio.cpp index 14106c61c0..4485b73473 100644 --- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -1166,13 +1166,13 @@ uint FileScanner::Scan(const std::string_view extension, const std::string &dire * @param mode Mode to open file. * @return FileHandle, or std::nullopt on failure. */ -std::optional FileHandle::Open(const std::string &filename, const std::string &mode) +std::optional FileHandle::Open(const std::string &filename, std::string_view mode) { #if defined(_WIN32) /* Windows also requires mode to be wchar_t. */ auto f = _wfopen(OTTD2FS(filename).c_str(), OTTD2FS(mode).c_str()); #else - auto f = fopen(filename.c_str(), mode.c_str()); + auto f = fopen(filename.c_str(), std::string{mode}.c_str()); #endif /* _WIN32 */ if (f == nullptr) return std::nullopt; diff --git a/src/fileio_type.h b/src/fileio_type.h index 54e47aeff4..d4a10a744c 100644 --- a/src/fileio_type.h +++ b/src/fileio_type.h @@ -130,7 +130,9 @@ DECLARE_INCREMENT_DECREMENT_OPERATORS(Searchpath) class FileHandle { public: - static std::optional Open(const std::string &filename, const std::string &mode); + static std::optional Open(const std::string &filename, std::string_view mode); + static std::optional Open(std::string_view filename, std::string_view mode) { return FileHandle::Open(std::string{filename}, mode); } + static std::optional Open(const char *filename, std::string_view mode) { return FileHandle::Open(std::string{filename}, mode); } inline void Close() { this->f.reset(); } diff --git a/src/settingsgen/settingsgen.cpp b/src/settingsgen/settingsgen.cpp index 5e20d2080b..6dbbf8b0ac 100644 --- a/src/settingsgen/settingsgen.cpp +++ b/src/settingsgen/settingsgen.cpp @@ -502,9 +502,9 @@ int CDECL main(int argc, char *argv[]) * @param mode Mode to open file. * @return FileHandle, or std::nullopt on failure. */ -std::optional FileHandle::Open(const std::string &filename, const std::string &mode) +std::optional FileHandle::Open(const std::string &filename, std::string_view mode) { - auto f = fopen(filename.c_str(), mode.c_str()); + auto f = fopen(filename.c_str(), std::string{mode}.c_str()); if (f == nullptr) return std::nullopt; return FileHandle(f); }