WriteFile function

- Basic version takes buffer + size
- For convenience: version that takes a FileBuffer
This commit is contained in:
Kevin Trogant 2022-12-08 11:51:03 +01:00
parent 7fae583084
commit 31cd55cf1f
2 changed files with 53 additions and 1 deletions

View File

@ -66,4 +66,43 @@ void Win32AssetManager::releaseFileBuffer(FileBuffer& fb)
VirtualFree(data, 0, MEM_RELEASE); VirtualFree(data, 0, MEM_RELEASE);
fb.data = nullptr; fb.data = nullptr;
fb.size = 0; fb.size = 0;
} }
bool Win32AssetManager::writeFile(const char* path, const void* data, size_t size)
{
char _cp[MAX_PATH];
strncpy(_cp, m_asset_root, MAX_PATH);
strncat(_cp, "\\", MAX_PATH);
strncat(_cp, path, MAX_PATH);
HANDLE file =
CreateFileA(_cp, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, INVALID_HANDLE_VALUE);
if (!file) {
return false;
}
const char* at = reinterpret_cast<const char*>(data);
while (size >= UINT32_MAX) {
DWORD bytes_written = 0;
if (!WriteFile(file, at, UINT32_MAX, &bytes_written, nullptr)) {
CloseHandle(file);
return false;
}
size -= bytes_written;
at += bytes_written;
}
if (size > 0) {
DWORD bytes_written = 0;
if (!WriteFile(file, at, size, &bytes_written, nullptr)) {
CloseHandle(file);
return false;
}
}
CloseHandle(file);
return true;
}
bool Win32AssetManager::writeFile(const char* path, const FileBuffer& file_buffer)
{
return writeFile(path, file_buffer.data, file_buffer.size);
}

View File

@ -16,6 +16,19 @@ public:
void releaseFileBuffer(FileBuffer& fb) override; void releaseFileBuffer(FileBuffer& fb) override;
/// @brief Write data to a file.
/// @param path Path relative to the asset root directory
/// @param data Data buffer
/// @param size Number of bytes
/// @return @c true if writing the file was successfull, @c false otherwise
bool writeFile(const char* path, const void* data, size_t size);
/// @brief Write data to a file.
/// @param path Path relative to the asset root directory
/// @param file_buffer Data buffer
/// @return @c true if writing the file was successfull, @c false otherwise
bool writeFile(const char* path, const FileBuffer& file_buffer);
private: private:
const char* m_asset_root; const char* m_asset_root;
}; };