Core/File: Add ReadWhole

This commit is contained in:
Lynix
2022-05-08 15:50:29 +02:00
parent 3c8b4c2587
commit 5f389ef0a4
3 changed files with 25 additions and 17 deletions

View File

@@ -267,6 +267,26 @@ namespace Nz
File& File::operator=(File&& file) noexcept = default;
std::optional<std::vector<UInt8>> File::ReadWhole(const std::filesystem::path& path)
{
File file(path);
if (!file.Open(OpenMode::ReadOnly | OpenMode::Unbuffered)) //< unbuffered since we will read all the file at once
{
NazaraError("Failed to open \"" + path.generic_u8string() + '"');
return std::nullopt;
}
std::size_t size = static_cast<std::size_t>(file.GetSize());
std::vector<UInt8> content(size);
if (size > 0 && file.Read(&content[0], size) != size)
{
NazaraError("Failed to read file");
return std::nullopt;
}
return content;
}
/*!
* \brief Flushes the stream
*