FileSystem: Add Error to file writing

This commit is contained in:
Stenzek 2024-07-06 17:46:05 +10:00
parent 29b3fa2b95
commit eba0794b4f
No known key found for this signature in database
2 changed files with 12 additions and 6 deletions

View file

@ -1181,26 +1181,32 @@ std::optional<std::string> FileSystem::ReadFileToString(std::FILE* fp)
return res;
}
bool FileSystem::WriteBinaryFile(const char* filename, const void* data, size_t data_length)
bool FileSystem::WriteBinaryFile(const char* filename, const void* data, size_t data_length, Error* error)
{
ManagedCFilePtr fp = OpenManagedCFile(filename, "wb");
ManagedCFilePtr fp = OpenManagedCFile(filename, "wb", error);
if (!fp)
return false;
if (data_length > 0 && std::fwrite(data, 1u, data_length, fp.get()) != data_length)
{
Error::SetErrno(error, "fwrite() failed: ", errno);
return false;
}
return true;
}
bool FileSystem::WriteStringToFile(const char* filename, std::string_view sv)
bool FileSystem::WriteStringToFile(const char* filename, std::string_view sv, Error* error)
{
ManagedCFilePtr fp = OpenManagedCFile(filename, "wb");
ManagedCFilePtr fp = OpenManagedCFile(filename, "wb", error);
if (!fp)
return false;
if (sv.length() > 0 && std::fwrite(sv.data(), 1u, sv.length(), fp.get()) != sv.length())
{
Error::SetErrno(error, "fwrite() failed: ", errno);
return false;
}
return true;
}

View file

@ -145,8 +145,8 @@ std::optional<std::vector<u8>> ReadBinaryFile(const char* filename, Error* error
std::optional<std::vector<u8>> ReadBinaryFile(std::FILE* fp);
std::optional<std::string> ReadFileToString(const char* filename, Error* error = nullptr);
std::optional<std::string> ReadFileToString(std::FILE* fp);
bool WriteBinaryFile(const char* filename, const void* data, size_t data_length);
bool WriteStringToFile(const char* filename, std::string_view sv);
bool WriteBinaryFile(const char* filename, const void* data, size_t data_length, Error* error = nullptr);
bool WriteStringToFile(const char* filename, std::string_view sv, Error* error = nullptr);
/// creates a directory in the local filesystem
/// if the directory already exists, the return value will be true.