Move ArchiveConfiguration into it's own file

This commit is contained in:
2021-02-24 02:30:11 +01:00
parent 71855a1d9f
commit a302681f4d
4 changed files with 121 additions and 83 deletions

View File

@@ -0,0 +1,89 @@
/*
* 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/.
*/
#include "ArchiveConfiguration.hpp"
#include "Base/Utils.hpp"
#include <archive.h>
namespace openVulkanoCpp
{
namespace
{
constexpr int TYPE_MAP[] = { ARCHIVE_FORMAT_TAR, ARCHIVE_FORMAT_CPIO, ARCHIVE_FORMAT_ISO9660, ARCHIVE_FORMAT_ZIP, ARCHIVE_FORMAT_XAR, ARCHIVE_FORMAT_7ZIP, ARCHIVE_FORMAT_WARC, ARCHIVE_FORMAT_SHAR };
constexpr std::string_view TYPE_NAMES[] = { ".tar", ".cpio", ".iso", ".zip", ".xar", ".7z", ".warc", ".shar" };
constexpr int COMP_MAP[] = { ARCHIVE_FILTER_COMPRESS, ARCHIVE_FILTER_NONE, ARCHIVE_FILTER_GZIP, ARCHIVE_FILTER_BZIP2, ARCHIVE_FILTER_XZ, ARCHIVE_FILTER_LZ4, ARCHIVE_FILTER_ZSTD };
constexpr std::string_view COMP_NAMES[] = { ".Z", "", ".gz", ".bz2", ".xz", ".lz", ".zst" };
constexpr std::string_view TAR_SORTS[] = { ".tZ", ".tar", ".tgz", ".tbz2", ".txz", ".tlz", ".tzst" };
}
std::string ArchiveConfiguration::GetFileExtension() const
{
const std::string_view& tName = TYPE_NAMES[static_cast<int>(type)];
if (type == ArchiveType::TAR)
{
const std::string_view& cName = COMP_NAMES[static_cast<int>(compression)];
std::string extension;
extension.reserve(tName.length() + cName.length());
extension.append(tName).append(cName);
return extension;
}
return std::string(tName);
}
ArchiveConfiguration ArchiveConfiguration::FromFileName(const char* fileName)
{
std::string_view fName = fileName;
ArchiveConfiguration ac;
int tId = 0;
for(const std::string_view& tName : TYPE_NAMES)
{
if (tId > 0)
{
if (Utils::EndsWith(fName, tName))
{
ac.type = static_cast<ArchiveType>(tId);
return ac;
}
}
tId++;
}
tId = 0;
for(const std::string_view& tsName : TAR_SORTS)
{
if (Utils::EndsWith(fName, tsName))
{
ac.compression = static_cast<CompressionType>(tId);
return ac;
}
tId++;
}
tId = 0;
for(const std::string_view& cName : COMP_NAMES)
{
std::string tName(".tar");
tName.append(cName);
if (Utils::EndsWith(fName, tName))
{
ac.compression = static_cast<CompressionType>(tId);
return ac;
}
tId++;
}
return ac;
}
int ArchiveConfiguration::GetLibArchiveArchiveType() const
{
return TYPE_MAP[static_cast<int>(type)];
}
int ArchiveConfiguration::GetLibArchiveCompressionType() const
{
return COMP_MAP[static_cast<int>(compression)];
}
}

View File

@@ -0,0 +1,29 @@
/*
* 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 <string>
namespace openVulkanoCpp
{
struct ArchiveConfiguration
{
enum class ArchiveType { TAR = 0, CPIO, ISO, ZIP, XAR, SEVEN_ZIP, WARC, SHAR };
enum class CompressionType { AUTO, NONE, GZIP, BZIP2, XZ, LZ4, ZSTD };
ArchiveType type = ArchiveType::TAR;
CompressionType compression = CompressionType::AUTO;
int compressionLevel = -1;
[[nodiscard]] std::string GetFileExtension() const;
static ArchiveConfiguration FromFileName(const char* fileName);
[[nodiscard]] int GetLibArchiveArchiveType() const;
[[nodiscard]] int GetLibArchiveCompressionType() const;
};
}

View File

@@ -6,80 +6,12 @@
#include "ArchiveWriter.hpp" #include "ArchiveWriter.hpp"
#include "LibArchiveHelper.hpp" #include "LibArchiveHelper.hpp"
#include "Base/Utils.hpp"
#include <archive.h> #include <archive.h>
#include <archive_entry.h> #include <archive_entry.h>
#include <iostream> #include <iostream>
namespace openVulkanoCpp namespace openVulkanoCpp
{ {
namespace
{
constexpr int TYPE_MAP[] = { ARCHIVE_FORMAT_TAR, ARCHIVE_FORMAT_CPIO, ARCHIVE_FORMAT_ISO9660, ARCHIVE_FORMAT_ZIP, ARCHIVE_FORMAT_XAR, ARCHIVE_FORMAT_7ZIP, ARCHIVE_FORMAT_WARC, ARCHIVE_FORMAT_SHAR };
constexpr std::string_view TYPE_NAMES[] = { ".tar", ".cpio", ".iso", ".zip", ".xar", ".7z", ".warc", ".shar" };
constexpr int COMP_MAP[] = { ARCHIVE_FILTER_COMPRESS, ARCHIVE_FILTER_NONE, ARCHIVE_FILTER_GZIP, ARCHIVE_FILTER_BZIP2, ARCHIVE_FILTER_XZ, ARCHIVE_FILTER_LZ4, ARCHIVE_FILTER_ZSTD };
constexpr std::string_view COMP_NAMES[] = { ".Z", "", ".gz", ".bz2", ".xz", ".lz", ".zst" };
constexpr std::string_view TAR_SORTS[] = { ".tZ", ".tar", ".tgz", ".tbz2", ".txz", ".tlz", ".tzst" };
}
std::string ArchiveConfiguration::GetFileExtension() const
{
const std::string_view& tName = TYPE_NAMES[static_cast<int>(type)];
if (type == ArchiveType::TAR)
{
const std::string_view& cName = COMP_NAMES[static_cast<int>(compression)];
std::string extension;
extension.reserve(tName.length() + cName.length());
extension.append(tName).append(cName);
return extension;
}
return std::string(tName);
}
ArchiveConfiguration ArchiveConfiguration::FromFileName(const char* fileName)
{
std::string_view fName = fileName;
ArchiveConfiguration ac;
int tId = 0;
for(const std::string_view& tName : TYPE_NAMES)
{
if (tId > 0)
{
if (Utils::EndsWith(fName, tName))
{
ac.type = static_cast<ArchiveType>(tId);
return ac;
}
}
tId++;
}
tId = 0;
for(const std::string_view& tsName : TAR_SORTS)
{
if (Utils::EndsWith(fName, tsName))
{
ac.compression = static_cast<CompressionType>(tId);
return ac;
}
tId++;
}
tId = 0;
for(const std::string_view& cName : COMP_NAMES)
{
std::string tName(".tar");
tName.append(cName);
if (Utils::EndsWith(fName, tName))
{
ac.compression = static_cast<CompressionType>(tId);
return ac;
}
tId++;
}
return ac;
}
ArchiveWriter::ArchiveWriter(const char* fileName, const std::shared_ptr<spdlog::logger>& logger) ArchiveWriter::ArchiveWriter(const char* fileName, const std::shared_ptr<spdlog::logger>& logger)
: ArchiveWriter(fileName, ArchiveConfiguration::FromFileName(fileName), logger) : ArchiveWriter(fileName, ArchiveConfiguration::FromFileName(fileName), logger)
{} {}
@@ -87,10 +19,10 @@ namespace openVulkanoCpp
ArchiveWriter::ArchiveWriter(const char* fileName, ArchiveConfiguration archiveConfiguration, const std::shared_ptr<spdlog::logger>& logger) ArchiveWriter::ArchiveWriter(const char* fileName, ArchiveConfiguration archiveConfiguration, const std::shared_ptr<spdlog::logger>& logger)
: ArchiveBase(archive_write_new(), archive_entry_new(), logger) : ArchiveBase(archive_write_new(), archive_entry_new(), logger)
{ //TODO error handling { //TODO error handling
ChkErr(archive_write_set_format(m_archive, TYPE_MAP[static_cast<int>(archiveConfiguration.type)])); ChkErr(archive_write_set_format(m_archive, archiveConfiguration.GetLibArchiveArchiveType()));
if (archiveConfiguration.type == ArchiveConfiguration::ArchiveType::TAR) if (archiveConfiguration.type == ArchiveConfiguration::ArchiveType::TAR)
{ {
ChkErr(archive_write_add_filter(m_archive, COMP_MAP[static_cast<int>(archiveConfiguration.compression)])); ChkErr(archive_write_add_filter(m_archive, archiveConfiguration.GetLibArchiveCompressionType()));
} }
if (archiveConfiguration.compression != ArchiveConfiguration::CompressionType::NONE && if (archiveConfiguration.compression != ArchiveConfiguration::CompressionType::NONE &&
archiveConfiguration.compressionLevel > 0) archiveConfiguration.compressionLevel > 0)

View File

@@ -7,25 +7,13 @@
#pragma once #pragma once
#include "ArchiveBase.hpp" #include "ArchiveBase.hpp"
#include "ArchiveConfiguration.hpp"
#include "IO/FileDescription.hpp" #include "IO/FileDescription.hpp"
#include <string_view> #include <string_view>
#include <vector> #include <vector>
namespace openVulkanoCpp namespace openVulkanoCpp
{ {
struct ArchiveConfiguration
{
enum class ArchiveType { TAR = 0, CPIO, ISO, ZIP, XAR, SEVEN_ZIP, WARC, SHAR };
enum class CompressionType { AUTO, NONE, GZIP, BZIP2, XZ, LZ4, ZSTD };
ArchiveType type = ArchiveType::TAR;
CompressionType compression = CompressionType::AUTO;
int compressionLevel = -1;
[[nodiscard]] std::string GetFileExtension() const;
static ArchiveConfiguration FromFileName(const char* fileName);
};
class ArchiveWriter final : public ArchiveBase class ArchiveWriter final : public ArchiveBase
{ {
size_t m_bytesWritten = 0; size_t m_bytesWritten = 0;