Write to file interface

This commit is contained in:
Kevin Trogant 2022-12-16 16:06:58 +01:00
parent 15a0b77960
commit ab5694db43
2 changed files with 77 additions and 0 deletions

View File

@ -112,3 +112,54 @@ bool Win32AssetManager::writeFile(const char* path, const FileBuffer& file_buffe
ZoneScoped;
return writeFile(path, file_buffer.data, file_buffer.size);
}
Win32AssetManager::FileHandle Win32AssetManager::createFile(const char* path)
{
ZoneScoped;
char _cp[MAX_PATH];
strncpy(_cp, m_asset_root, MAX_PATH);
strncat(_cp, "\\", MAX_PATH);
strncat(_cp, path, MAX_PATH);
HANDLE h = CreateFileA(_cp, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
if (!h || h == INVALID_HANDLE_VALUE)
return nullptr;
return (h);
}
bool Win32AssetManager::writeToFile(FileHandle _file, const void* data, size_t size)
{
ZoneScoped;
HANDLE file = (HANDLE)_file;
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;
}
}
return true;
}
bool Win32AssetManager::writeToFile(FileHandle file, const FileBuffer& file_buffer)
{
ZoneScoped;
return writeToFile(file, file_buffer.data, file_buffer.size);
}
void Win32AssetManager::closeFile(FileHandle file)
{
ZoneScoped;
CloseHandle((HANDLE)file);
}

View File

@ -6,6 +6,8 @@
class Win32AssetManager : public AssetManager
{
public:
typedef void* FileHandle;
/// @brief Create the singleton instance
///
/// Sets @ref AssetManager::ptr to the singleton instance
@ -29,6 +31,30 @@ public:
/// @return @c true if writing the file was successfull, @c false otherwise
bool writeFile(const char* path, const FileBuffer& file_buffer);
/// @brief Creates a file and opens it for writing.
///
/// Overwrites an already existing file.
/// @param path Path realtive to the asset root directory
/// @return Handle to the open file, or @c nullptr if the file could not be created.
FileHandle createFile(const char* path);
/// @brief Writes data to an open file
/// @param file File handle
/// @param data Data buffer
/// @param size Number of bytes
/// @return @c true if writing to the file was successfull, @c false otherwise
bool writeToFile(FileHandle file, const void* data, size_t size);
/// @brief Write data to an open file.
/// @param file File handle
/// @param file_buffer Data buffer
/// @return @c true if writing to the file was successfull, @c false otherwise
bool writeToFile(FileHandle file, const FileBuffer& file_buffer);
/// @brief Closes an open file.
/// @param file The file handle
void closeFile(FileHandle file);
private:
const char* m_asset_root;
};