From ab5694db4399dd7b175eb0e2c401ad2e8757905a Mon Sep 17 00:00:00 2001 From: Kevin Trogant Date: Fri, 16 Dec 2022 16:06:58 +0100 Subject: [PATCH] Write to file interface --- app/src/main/cpp/Win32AssetManager.cpp | 51 ++++++++++++++++++++++++++ app/src/main/cpp/Win32AssetManager.h | 26 +++++++++++++ 2 files changed, 77 insertions(+) diff --git a/app/src/main/cpp/Win32AssetManager.cpp b/app/src/main/cpp/Win32AssetManager.cpp index 190b08c..b45d3ea 100644 --- a/app/src/main/cpp/Win32AssetManager.cpp +++ b/app/src/main/cpp/Win32AssetManager.cpp @@ -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(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); +} \ No newline at end of file diff --git a/app/src/main/cpp/Win32AssetManager.h b/app/src/main/cpp/Win32AssetManager.h index c406d41..a17e277 100644 --- a/app/src/main/cpp/Win32AssetManager.h +++ b/app/src/main/cpp/Win32AssetManager.h @@ -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; };