Introduce IArchiveWriter
This commit is contained in:
@@ -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<std::pair<const void*, size_t>>& 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<std::pair<const void*, size_t>>& 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;
|
||||
}
|
||||
}
|
||||
@@ -8,14 +8,16 @@
|
||||
|
||||
#include "ArchiveBase.hpp"
|
||||
#include "ArchiveConfiguration.hpp"
|
||||
#include "IO/FileDescription.hpp"
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
#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<std::pair<const void*, size_t>>& buffers);
|
||||
bool AddFile(const FileDescription& description, const std::vector<std::pair<const void*, size_t>>& 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<std::pair<const void*, size_t>>& buffers) override;
|
||||
bool AddFile(const char* fileName, const char* inArchiveName) override;
|
||||
|
||||
private:
|
||||
void WriteHeader(const FileDescription& fileDescription);
|
||||
|
||||
86
openVulkanoCpp/IO/Archive/IArchiveWriter.hpp
Normal file
86
openVulkanoCpp/IO/Archive/IArchiveWriter.hpp
Normal file
@@ -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 <vector>
|
||||
|
||||
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<std::pair<const void*, size_t>>& 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<std::pair<const void*, size_t>>& 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;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user