Browse Source

Make CreateFileStream return optional, rather than unique_ptr

pull/2862/head
Vladimir Olteanu 5 years ago committed by Anders Jenbo
parent
commit
b853559573
  1. 6
      Source/mpqapi.cpp
  2. 2
      Source/options.cpp
  3. 8
      Source/utils/file_util.cpp
  4. 3
      Source/utils/file_util.h

6
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<std::fstream> s_;
std::optional<std::fstream> s_;
};
constexpr std::size_t BlockEntrySize = INDEX_ENTRIES * sizeof(_BLOCKENTRY);

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

8
Source/utils/file_util.cpp

@ -164,17 +164,17 @@ void RemoveFile(const char *lpFileName)
#endif
}
std::unique_ptr<std::fstream> CreateFileStream(const char *path, std::ios::openmode mode)
std::optional<std::fstream> 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<std::fstream>(pathUtf16.get(), mode);
return { std::fstream(pathUtf16.get(), mode) };
#else
return std::make_unique<std::fstream>(path, mode);
return { std::fstream(path, mode) };
#endif
}

3
Source/utils/file_util.h

@ -6,6 +6,7 @@
#include <memory>
#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<std::fstream> CreateFileStream(const char *path, std::ios::openmode mode);
std::optional<std::fstream> CreateFileStream(const char *path, std::ios::openmode mode);
FILE *FOpen(const char *path, const char *mode);
#if defined(_WIN64) || defined(_WIN32)

Loading…
Cancel
Save