diff --git a/openVulkanoCpp/IO/Archive/ArchiveConfiguration.cpp b/openVulkanoCpp/IO/Archive/ArchiveConfiguration.cpp new file mode 100644 index 0000000..463d0eb --- /dev/null +++ b/openVulkanoCpp/IO/Archive/ArchiveConfiguration.cpp @@ -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 + +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(type)]; + if (type == ArchiveType::TAR) + { + const std::string_view& cName = COMP_NAMES[static_cast(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(tId); + return ac; + } + } + tId++; + } + tId = 0; + for(const std::string_view& tsName : TAR_SORTS) + { + if (Utils::EndsWith(fName, tsName)) + { + ac.compression = static_cast(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(tId); + return ac; + } + tId++; + } + return ac; + } + + int ArchiveConfiguration::GetLibArchiveArchiveType() const + { + return TYPE_MAP[static_cast(type)]; + } + + int ArchiveConfiguration::GetLibArchiveCompressionType() const + { + return COMP_MAP[static_cast(compression)]; + } +} \ No newline at end of file diff --git a/openVulkanoCpp/IO/Archive/ArchiveConfiguration.hpp b/openVulkanoCpp/IO/Archive/ArchiveConfiguration.hpp new file mode 100644 index 0000000..c9c0a37 --- /dev/null +++ b/openVulkanoCpp/IO/Archive/ArchiveConfiguration.hpp @@ -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 + +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; + }; +} diff --git a/openVulkanoCpp/IO/Archive/ArchiveWriter.cpp b/openVulkanoCpp/IO/Archive/ArchiveWriter.cpp index 863cb75..4de1e76 100644 --- a/openVulkanoCpp/IO/Archive/ArchiveWriter.cpp +++ b/openVulkanoCpp/IO/Archive/ArchiveWriter.cpp @@ -6,80 +6,12 @@ #include "ArchiveWriter.hpp" #include "LibArchiveHelper.hpp" -#include "Base/Utils.hpp" #include #include #include 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(type)]; - if (type == ArchiveType::TAR) - { - const std::string_view& cName = COMP_NAMES[static_cast(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(tId); - return ac; - } - } - tId++; - } - tId = 0; - for(const std::string_view& tsName : TAR_SORTS) - { - if (Utils::EndsWith(fName, tsName)) - { - ac.compression = static_cast(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(tId); - return ac; - } - tId++; - } - return ac; - } - ArchiveWriter::ArchiveWriter(const char* fileName, const std::shared_ptr& logger) : ArchiveWriter(fileName, ArchiveConfiguration::FromFileName(fileName), logger) {} @@ -87,10 +19,10 @@ namespace openVulkanoCpp ArchiveWriter::ArchiveWriter(const char* fileName, ArchiveConfiguration archiveConfiguration, const std::shared_ptr& logger) : ArchiveBase(archive_write_new(), archive_entry_new(), logger) { //TODO error handling - ChkErr(archive_write_set_format(m_archive, TYPE_MAP[static_cast(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(archiveConfiguration.compression)])); + ChkErr(archive_write_add_filter(m_archive, archiveConfiguration.GetLibArchiveCompressionType())); } if (archiveConfiguration.compression != ArchiveConfiguration::CompressionType::NONE && archiveConfiguration.compressionLevel > 0) diff --git a/openVulkanoCpp/IO/Archive/ArchiveWriter.hpp b/openVulkanoCpp/IO/Archive/ArchiveWriter.hpp index de87189..b0b2bc3 100644 --- a/openVulkanoCpp/IO/Archive/ArchiveWriter.hpp +++ b/openVulkanoCpp/IO/Archive/ArchiveWriter.hpp @@ -7,25 +7,13 @@ #pragma once #include "ArchiveBase.hpp" +#include "ArchiveConfiguration.hpp" #include "IO/FileDescription.hpp" #include #include 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;