From 468ca9487b684fd55c9bd0f804bd345c41894e08 Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Tue, 2 Mar 2021 20:36:32 +0100 Subject: [PATCH] Introduce IArchiveWriter --- openVulkanoCpp/IO/Archive/ArchiveWriter.cpp | 68 +--------------- openVulkanoCpp/IO/Archive/ArchiveWriter.hpp | 24 ++---- openVulkanoCpp/IO/Archive/IArchiveWriter.hpp | 86 ++++++++++++++++++++ 3 files changed, 97 insertions(+), 81 deletions(-) create mode 100644 openVulkanoCpp/IO/Archive/IArchiveWriter.hpp diff --git a/openVulkanoCpp/IO/Archive/ArchiveWriter.cpp b/openVulkanoCpp/IO/Archive/ArchiveWriter.cpp index 82f0344..81635ea 100644 --- a/openVulkanoCpp/IO/Archive/ArchiveWriter.cpp +++ b/openVulkanoCpp/IO/Archive/ArchiveWriter.cpp @@ -34,28 +34,9 @@ namespace openVulkanoCpp } ArchiveWriter::~ArchiveWriter() - { - archive_entry_free(m_archiveEntry); - } - - void ArchiveWriter::Close() { archive_write_close(m_archive); - } - - bool ArchiveWriter::AddFile(const char* filePath) - { - std::filesystem::path path(filePath); - std::string fileName = path.filename().string(); - if (std::filesystem::is_regular_file(path)) - { - return AddFile(filePath, fileName.c_str()); - } - else if (std::filesystem::is_directory(path)) - { - AddFiles(filePath, fileName.c_str()); - } - return false; + archive_entry_free(m_archiveEntry); } bool ArchiveWriter::AddFile(const char* fileName, const char* inArchiveName) @@ -75,12 +56,6 @@ namespace openVulkanoCpp return true; } - bool ArchiveWriter::AddFile(const char* inArchiveName, const void* buffer, size_t length) - { - FileDescription description = FileDescription::MakeDescriptionForFile(inArchiveName, length); - return AddFile(description, buffer); - } - bool ArchiveWriter::AddFile(const FileDescription& description, const void* buffer) { WriteHeader(description); @@ -89,18 +64,6 @@ namespace openVulkanoCpp return ok; } - bool ArchiveWriter::AddFile(const char* fileName, const std::vector>& buffers) - { - size_t size = 0; - for(const auto& buffer : buffers) - { - if (buffer.first == nullptr) continue; - size += buffer.second; - } - FileDescription description = FileDescription::MakeDescriptionForFile(fileName, size); - return AddFile(description, buffers); - } - bool ArchiveWriter::AddFile(const FileDescription& description, const std::vector>& buffers) { WriteHeader(description); @@ -128,31 +91,4 @@ namespace openVulkanoCpp ChkErr(archive_write_header(m_archive, m_archiveEntry)); m_bytesWritten += fileDescription.size; } - - bool ArchiveWriter::AddFiles(const char* dirName) - { - return AddFiles(dirName, ""); - } - - bool ArchiveWriter::AddFiles(const char* dirName, const char* inArchiveDirName) - { - return AddFiles(std::filesystem::path(dirName), std::string(inArchiveDirName)); - } - - bool ArchiveWriter::AddFiles(const std::filesystem::path& dirName, const std::string& inArchiveDirName) - { - std::string sDirName = dirName.string(); - AddFile(sDirName.c_str(), inArchiveDirName.c_str()); - for(const auto& entry : std::filesystem::directory_iterator(dirName)) - { - std::string fPath = inArchiveDirName + "/" + entry.path().filename().string(); - if (entry.is_directory()) AddFiles(entry, fPath); - else - { - std::string entryName = entry.path().string(); - AddFile(entryName.c_str(), fPath.c_str()); - } - } - return true; - } -} \ No newline at end of file +} diff --git a/openVulkanoCpp/IO/Archive/ArchiveWriter.hpp b/openVulkanoCpp/IO/Archive/ArchiveWriter.hpp index b0b2bc3..21ba9b9 100644 --- a/openVulkanoCpp/IO/Archive/ArchiveWriter.hpp +++ b/openVulkanoCpp/IO/Archive/ArchiveWriter.hpp @@ -8,14 +8,16 @@ #include "ArchiveBase.hpp" #include "ArchiveConfiguration.hpp" -#include "IO/FileDescription.hpp" -#include -#include +#include "IArchiveWriter.hpp" namespace openVulkanoCpp { - class ArchiveWriter final : public ArchiveBase + class MultiPartArchiveWriter; + + class ArchiveWriter final : public ArchiveBase, public IArchiveWriter { + friend MultiPartArchiveWriter; + size_t m_bytesWritten = 0; public: @@ -24,19 +26,11 @@ namespace openVulkanoCpp ~ArchiveWriter() override; - void Close(); - [[nodiscard]] size_t GetTotalWrittenBytes() const { return m_bytesWritten; } - bool AddFile(const char* fileName); - bool AddFile(const char* fileName, const char* inArchiveName); - bool AddFile(const char* inArchiveName, const void* buffer, size_t length); - bool AddFile(const FileDescription& description, const void* buffer); - bool AddFile(const char* fileName, const std::vector>& buffers); - bool AddFile(const FileDescription& description, const std::vector>& buffers); - bool AddFiles(const char* dirName); - bool AddFiles(const char* dirName, const char* inArchiveDirName); - bool AddFiles(const std::filesystem::path& dirName, const std::string& inArchiveDirName); + bool AddFile(const FileDescription& description, const void* buffer) override; + bool AddFile(const FileDescription& description, const std::vector>& buffers) override; + bool AddFile(const char* fileName, const char* inArchiveName) override; private: void WriteHeader(const FileDescription& fileDescription); diff --git a/openVulkanoCpp/IO/Archive/IArchiveWriter.hpp b/openVulkanoCpp/IO/Archive/IArchiveWriter.hpp new file mode 100644 index 0000000..30978d1 --- /dev/null +++ b/openVulkanoCpp/IO/Archive/IArchiveWriter.hpp @@ -0,0 +1,86 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include "IO/FileDescription.hpp" +#include + +namespace openVulkanoCpp +{ + class IArchiveWriter + { + protected: + IArchiveWriter() = default; + + public: + virtual ~IArchiveWriter() = default; + + virtual bool AddFile(const FileDescription& description, const void* buffer) = 0; + virtual bool AddFile(const FileDescription& description, const std::vector>& buffers) = 0; + virtual bool AddFile(const char* fileName, const char* inArchiveName) = 0; + + bool AddFile(const char* filePath) + { + std::filesystem::path path(filePath); + std::string fileName = path.filename().string(); + if (std::filesystem::is_regular_file(path)) + { + return AddFile(filePath, fileName.c_str()); + } + else if (std::filesystem::is_directory(path)) + { + AddFiles(filePath, fileName.c_str()); + } + return false; + } + + bool AddFile(const char* inArchiveName, const void* buffer, size_t length) + { + FileDescription description = FileDescription::MakeDescriptionForFile(inArchiveName, length); + return AddFile(description, buffer); + } + + bool AddFile(const char* fileName, const std::vector>& buffers) + { + size_t size = 0; + for(const auto& buffer : buffers) + { + if (buffer.first == nullptr) continue; + size += buffer.second; + } + FileDescription description = FileDescription::MakeDescriptionForFile(fileName, size); + return AddFile(description, buffers); + } + + bool AddFiles(const char* dirName) + { + return AddFiles(dirName, ""); + } + + bool AddFiles(const char* dirName, const char* inArchiveDirName) + { + return AddFiles(std::filesystem::path(dirName), std::string(inArchiveDirName)); + } + + bool AddFiles(const std::filesystem::path& dirName, const std::string& inArchiveDirName) + { + std::string sDirName = dirName.string(); + AddFile(sDirName.c_str(), inArchiveDirName.c_str()); + for(const auto& entry : std::filesystem::directory_iterator(dirName)) + { + std::string fPath = inArchiveDirName + "/" + entry.path().filename().string(); + if (entry.is_directory()) AddFiles(entry, fPath); + else + { + std::string entryName = entry.path().string(); + AddFile(entryName.c_str(), fPath.c_str()); + } + } + return true; + } + }; +} \ No newline at end of file