Move ArchiveConfiguration into it's own file
This commit is contained in:
89
openVulkanoCpp/IO/Archive/ArchiveConfiguration.cpp
Normal file
89
openVulkanoCpp/IO/Archive/ArchiveConfiguration.cpp
Normal 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)];
|
||||
}
|
||||
}
|
||||
29
openVulkanoCpp/IO/Archive/ArchiveConfiguration.hpp
Normal file
29
openVulkanoCpp/IO/Archive/ArchiveConfiguration.hpp
Normal 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;
|
||||
};
|
||||
}
|
||||
@@ -6,80 +6,12 @@
|
||||
|
||||
#include "ArchiveWriter.hpp"
|
||||
#include "LibArchiveHelper.hpp"
|
||||
#include "Base/Utils.hpp"
|
||||
#include <archive.h>
|
||||
#include <archive_entry.h>
|
||||
#include <iostream>
|
||||
|
||||
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(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)
|
||||
: ArchiveBase(archive_write_new(), archive_entry_new(), logger)
|
||||
{ //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)
|
||||
{
|
||||
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 &&
|
||||
archiveConfiguration.compressionLevel > 0)
|
||||
|
||||
@@ -7,25 +7,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "ArchiveBase.hpp"
|
||||
#include "ArchiveConfiguration.hpp"
|
||||
#include "IO/FileDescription.hpp"
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
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
|
||||
{
|
||||
size_t m_bytesWritten = 0;
|
||||
|
||||
Reference in New Issue
Block a user