Core/File: Add WriteWhole static function

This commit is contained in:
SirLynix 2022-05-12 18:11:41 +02:00
parent a8e69882fb
commit 7c2b8e0576
2 changed files with 21 additions and 2 deletions

View File

@ -57,6 +57,7 @@ namespace Nz
static inline ByteArray ComputeHash(HashType hash, const std::filesystem::path& filePath);
static inline ByteArray ComputeHash(AbstractHash& hash, const std::filesystem::path& filePath);
static std::optional<std::vector<UInt8>> ReadWhole(const std::filesystem::path& path);
static bool WriteWhole(const std::filesystem::path& path, const void* data, std::size_t size);
private:
void FlushStream() override;

View File

@ -272,7 +272,7 @@ namespace Nz
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() + '"');
NazaraError("failed to open \"" + path.generic_u8string() + '"');
return std::nullopt;
}
@ -280,13 +280,31 @@ namespace Nz
std::vector<UInt8> content(size);
if (size > 0 && file.Read(&content[0], size) != size)
{
NazaraError("Failed to read file");
NazaraError("failed to read file");
return std::nullopt;
}
return content;
}
bool File::WriteWhole(const std::filesystem::path& path, const void* data, std::size_t size)
{
File file(path);
if (!file.Open(OpenMode::WriteOnly | OpenMode::Unbuffered)) //< unbuffered since we will write all the file at once
{
NazaraError("failed to open \"" + path.generic_u8string() + '"');
return false;
}
if (file.Write(data, size) != size)
{
NazaraError("failed to write file");
return false;
}
return true;
}
/*!
* \brief Flushes the stream
*