mirror of https://github.com/OpenTTD/OpenTTD
Codechange: add std::string_view variant of FileHandle::Open
parent
9116f96e2e
commit
8b3511ede8
|
@ -1288,7 +1288,7 @@ static bool ConScript(std::span<std::string_view> argv)
|
||||||
if (!CloseConsoleLogIfActive()) {
|
if (!CloseConsoleLogIfActive()) {
|
||||||
if (argv.size() < 2) return false;
|
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()) {
|
if (!_iconsole_output_file.has_value()) {
|
||||||
IConsolePrint(CC_ERROR, "Could not open console log file '{}'.", argv[1]);
|
IConsolePrint(CC_ERROR, "Could not open console log file '{}'.", argv[1]);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1166,13 +1166,13 @@ uint FileScanner::Scan(const std::string_view extension, const std::string &dire
|
||||||
* @param mode Mode to open file.
|
* @param mode Mode to open file.
|
||||||
* @return FileHandle, or std::nullopt on failure.
|
* @return FileHandle, or std::nullopt on failure.
|
||||||
*/
|
*/
|
||||||
std::optional<FileHandle> FileHandle::Open(const std::string &filename, const std::string &mode)
|
std::optional<FileHandle> FileHandle::Open(const std::string &filename, std::string_view mode)
|
||||||
{
|
{
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
/* Windows also requires mode to be wchar_t. */
|
/* Windows also requires mode to be wchar_t. */
|
||||||
auto f = _wfopen(OTTD2FS(filename).c_str(), OTTD2FS(mode).c_str());
|
auto f = _wfopen(OTTD2FS(filename).c_str(), OTTD2FS(mode).c_str());
|
||||||
#else
|
#else
|
||||||
auto f = fopen(filename.c_str(), mode.c_str());
|
auto f = fopen(filename.c_str(), std::string{mode}.c_str());
|
||||||
#endif /* _WIN32 */
|
#endif /* _WIN32 */
|
||||||
|
|
||||||
if (f == nullptr) return std::nullopt;
|
if (f == nullptr) return std::nullopt;
|
||||||
|
|
|
@ -130,7 +130,9 @@ DECLARE_INCREMENT_DECREMENT_OPERATORS(Searchpath)
|
||||||
|
|
||||||
class FileHandle {
|
class FileHandle {
|
||||||
public:
|
public:
|
||||||
static std::optional<FileHandle> Open(const std::string &filename, const std::string &mode);
|
static std::optional<FileHandle> Open(const std::string &filename, std::string_view mode);
|
||||||
|
static std::optional<FileHandle> Open(std::string_view filename, std::string_view mode) { return FileHandle::Open(std::string{filename}, mode); }
|
||||||
|
static std::optional<FileHandle> Open(const char *filename, std::string_view mode) { return FileHandle::Open(std::string{filename}, mode); }
|
||||||
|
|
||||||
inline void Close() { this->f.reset(); }
|
inline void Close() { this->f.reset(); }
|
||||||
|
|
||||||
|
|
|
@ -502,9 +502,9 @@ int CDECL main(int argc, char *argv[])
|
||||||
* @param mode Mode to open file.
|
* @param mode Mode to open file.
|
||||||
* @return FileHandle, or std::nullopt on failure.
|
* @return FileHandle, or std::nullopt on failure.
|
||||||
*/
|
*/
|
||||||
std::optional<FileHandle> FileHandle::Open(const std::string &filename, const std::string &mode)
|
std::optional<FileHandle> 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;
|
if (f == nullptr) return std::nullopt;
|
||||||
return FileHandle(f);
|
return FileHandle(f);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue