diff --git a/openVulkanoCpp/IO/Archive/ArchiveConfiguration.cpp b/openVulkanoCpp/IO/Archive/ArchiveConfiguration.cpp index 93c6ead..a965570 100644 --- a/openVulkanoCpp/IO/Archive/ArchiveConfiguration.cpp +++ b/openVulkanoCpp/IO/Archive/ArchiveConfiguration.cpp @@ -13,7 +13,6 @@ namespace OpenVulkano 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" }; @@ -23,7 +22,7 @@ namespace OpenVulkano std::string ArchiveConfiguration::GetFileExtension() const { - const std::string_view& tName = TYPE_NAMES[static_cast(type)]; + const std::string_view tName = type.GetExtension(); if (type == ArchiveType::TAR) { const std::string_view& cName = COMP_NAMES[static_cast(compression)]; @@ -39,20 +38,12 @@ namespace OpenVulkano { std::string_view fName = fileName; ArchiveConfiguration ac; - int tId = 0; - for(const std::string_view& tName : TYPE_NAMES) + if (auto type = ArchiveType::FromExtension(fName)) { - if (tId > 0) - { - if (Utils::EndsWith(fName, tName)) - { - ac.type = static_cast(tId); - return ac; - } - } - tId++; + ac.type = *type; + return ac; } - tId = 0; + int tId = 0; for(const std::string_view& tsName : TAR_SORTS) { if (Utils::EndsWith(fName, tsName)) diff --git a/openVulkanoCpp/IO/Archive/ArchiveConfiguration.hpp b/openVulkanoCpp/IO/Archive/ArchiveConfiguration.hpp index 467a5cd..38215d7 100644 --- a/openVulkanoCpp/IO/Archive/ArchiveConfiguration.hpp +++ b/openVulkanoCpp/IO/Archive/ArchiveConfiguration.hpp @@ -6,12 +6,11 @@ #pragma once +#include "ArchiveType.hpp" #include namespace OpenVulkano { - enum class ArchiveType { TAR = 0, CPIO, ISO, ZIP, XAR, SEVEN_ZIP, WARC, SHAR }; - enum class CompressionType { AUTO = 0, NONE, GZIP, BZIP2, XZ, LZ4, ZSTD }; struct ArchiveConfiguration final diff --git a/openVulkanoCpp/IO/Archive/ArchiveType.hpp b/openVulkanoCpp/IO/Archive/ArchiveType.hpp new file mode 100644 index 0000000..fc7a9e2 --- /dev/null +++ b/openVulkanoCpp/IO/Archive/ArchiveType.hpp @@ -0,0 +1,57 @@ +/* + * 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 "Base/Utils.hpp" +#include +#include + +namespace OpenVulkano +{ + class ArchiveType final + { + static inline constexpr std::string_view TYPE_NAMES[] = { ".tar", ".cpio", ".iso", ".zip", ".xar", ".7z", ".warc", ".shar" }; + + public: + enum Type { TAR = 0, CPIO, ISO, ZIP, XAR, SEVEN_ZIP, WARC, SHAR }; + + constexpr ArchiveType(Type type) : m_type(type) {} + + [[nodiscard]] constexpr bool AllowsUnknownFileSize() const { return m_type == ZIP; } + + [[nodiscard]] constexpr bool AllowsMixedCompressionLevels() const { return m_type == ZIP; } + + [[nodiscard]] constexpr std::string_view GetExtension() const { return TYPE_NAMES[m_type]; } + + [[nodiscard]] constexpr bool operator==(Type rhs) const { return m_type == rhs; } + [[nodiscard]] constexpr bool operator==(ArchiveType rhs) const { return m_type == rhs.m_type; } + [[nodiscard]] constexpr bool operator!=(Type rhs) const { return m_type != rhs; } + [[nodiscard]] constexpr bool operator!=(ArchiveType rhs) const { return m_type != rhs.m_type; } + + [[nodiscard]] constexpr explicit operator int() const { return m_type; } + + [[nodiscard]] static constexpr std::optional FromExtension(std::string_view fName) + { + int tId = 0; + for(const std::string_view& tName : TYPE_NAMES) + { + if (tId > 0) + { + if (Utils::EndsWith(fName, tName)) + { + return static_cast(tId); + } + } + tId++; + } + return std::nullopt; + } + + private: + Type m_type; + }; +} \ No newline at end of file