diff --git a/Source/mpqapi.cpp b/Source/mpqapi.cpp index 64bbe9ddc..d8b37e027 100644 --- a/Source/mpqapi.cpp +++ b/Source/mpqapi.cpp @@ -96,12 +96,12 @@ public: void Close() { - s_ = nullptr; + s_ = std::nullopt; } [[nodiscard]] bool IsOpen() const { - return s_ != nullptr; + return s_ != std::nullopt; } bool Seekp(std::streampos pos) @@ -151,7 +151,7 @@ private: return !s_->fail(); } - std::unique_ptr s_; + std::optional s_; }; constexpr std::size_t BlockEntrySize = INDEX_ENTRIES * sizeof(_BLOCKENTRY); diff --git a/Source/options.cpp b/Source/options.cpp index 3aac0e13b..45376b307 100644 --- a/Source/options.cpp +++ b/Source/options.cpp @@ -69,7 +69,7 @@ CSimpleIni &GetIni() auto path = GetIniPath(); auto stream = CreateFileStream(path.c_str(), std::fstream::in | std::fstream::binary); ini.SetSpaces(false); - if (stream != nullptr) + if (stream) ini.LoadData(*stream); isIniLoaded = true; } diff --git a/Source/utils/file_util.cpp b/Source/utils/file_util.cpp index e1195db3d..0fad6cde1 100644 --- a/Source/utils/file_util.cpp +++ b/Source/utils/file_util.cpp @@ -164,17 +164,17 @@ void RemoveFile(const char *lpFileName) #endif } -std::unique_ptr CreateFileStream(const char *path, std::ios::openmode mode) +std::optional CreateFileStream(const char *path, std::ios::openmode mode) { #if defined(_WIN64) || defined(_WIN32) const auto pathUtf16 = ToWideChar(path); if (pathUtf16 == nullptr) { LogError("UTF-8 -> UTF-16 conversion error code {}", ::GetLastError()); - return nullptr; + return {}; } - return std::make_unique(pathUtf16.get(), mode); + return { std::fstream(pathUtf16.get(), mode) }; #else - return std::make_unique(path, mode); + return { std::fstream(path, mode) }; #endif } diff --git a/Source/utils/file_util.h b/Source/utils/file_util.h index 4ba5d2674..177adb1fb 100644 --- a/Source/utils/file_util.h +++ b/Source/utils/file_util.h @@ -6,6 +6,7 @@ #include #include "utils/stdcompat/string_view.hpp" +#include "utils/stdcompat/optional.hpp" namespace devilution { @@ -14,7 +15,7 @@ bool FileExistsAndIsWriteable(const char *path); bool GetFileSize(const char *path, std::uintmax_t *size); bool ResizeFile(const char *path, std::uintmax_t size); void RemoveFile(const char *lpFileName); -std::unique_ptr CreateFileStream(const char *path, std::ios::openmode mode); +std::optional CreateFileStream(const char *path, std::ios::openmode mode); FILE *FOpen(const char *path, const char *mode); #if defined(_WIN64) || defined(_WIN32)