From 0e648c7c62944cd81aa57f10ac01cdabe2f2b7e0 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Sun, 29 Sep 2024 09:28:28 -0500 Subject: Remember various settings. --- src/archive.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 71 insertions(+), 14 deletions(-) (limited to 'src/archive.cpp') diff --git a/src/archive.cpp b/src/archive.cpp index 0339d85..1ce79b4 100644 --- a/src/archive.cpp +++ b/src/archive.cpp @@ -6,26 +6,81 @@ namespace getsuyomi { +using fud::FudStatus; + ZipArchive::ZipArchive(const QString& filename) { auto filenameUtf8 = filename.toUtf8(); qDebug("Open file %s", filenameUtf8.data()); int err; m_archive = zip_open(filenameUtf8.data(), 0, &err); + if (m_archive == nullptr) { zip_error_t error; zip_error_init_with_code(&error, err); qCritical("%s: cannot open zip archive '%s': %s", AppName, filenameUtf8.data(), zip_error_strerror(&error)); zip_error_fini(&error); - throw std::runtime_error("Bad zip file"); + return; } - populate(); + auto status = populate(); + if (status != FudStatus::Success) { + cleanup(); + return; + } +} + +ZipArchive::ZipArchive(ZipArchive&& rhs) + :m_archive{rhs.m_archive}, + m_sortedIndices{std::move(rhs.m_sortedIndices)}, + m_filenames{std::move(rhs.m_filenames)}, + m_fileSizes{std::move(rhs.m_fileSizes)}, + m_pages{std::move(rhs.m_pages)} +{ + rhs.m_archive = nullptr; +} + +ZipArchive::~ZipArchive() +{ + cleanup(); } -void ZipArchive::populate() +ZipArchive& ZipArchive::operator=(ZipArchive&& rhs) { - auto numEntries = zip_get_num_entries(m_archive, ZIP_FL_UNCHANGED); + m_archive = rhs.m_archive; + m_sortedIndices = std::move(rhs.m_sortedIndices); + m_filenames = std::move(rhs.m_filenames); + m_fileSizes = std::move(rhs.m_fileSizes); + m_pages = std::move(rhs.m_pages); + + rhs.m_archive = nullptr; + + return *this; +} + +bool ZipArchive::valid() const { + return m_archive != nullptr; +} + +void ZipArchive::cleanup() { + if (m_archive != nullptr) { + zip_discard(m_archive); + m_archive = nullptr; + } +} + +fud::FudStatus ZipArchive::populate() +{ + if (!valid()) { + qCritical("Invalid zipArchive"); + return FudStatus::ObjectInvalid; + } + + auto numEntriesResult = zip_get_num_entries(m_archive, ZIP_FL_UNCHANGED); + if (numEntriesResult < 0) { + return FudStatus::Failure; + } + size_t numEntries = static_cast(numEntriesResult); qDebug("%zu pages", numEntries); struct NameIndex { @@ -42,6 +97,9 @@ void ZipArchive::populate() struct zip_stat stats; auto status = zip_stat_index(m_archive, idx, 0, &stats); + if (status < 0) { + return FudStatus::Failure; + } static_assert(ZIP_STAT_NAME != 0); static_assert(ZIP_STAT_SIZE != 0); static_assert(ZIP_STAT_INDEX != 0); @@ -62,6 +120,11 @@ void ZipArchive::populate() m_pages.emplace_back(std::nullopt); } + + if (m_pages.empty()) { + return FudStatus::Empty; + } + std::sort(nameIndices.begin(), nameIndices.end(), [](const auto& lhs, const auto& rhs) { return std::lexicographical_compare(lhs.name.begin(), lhs.name.end(), rhs.name.begin(), rhs.name.end()); }); @@ -75,14 +138,8 @@ void ZipArchive::populate() m_filenames.push_back(nameIndex.name); m_fileSizes.push_back(nameIndex.filesize); } -} -ZipArchive::~ZipArchive() -{ - if (m_archive != nullptr) { - zip_discard(m_archive); - m_archive = nullptr; - } + return FudStatus::Success; } ArchiveResult ZipArchive::getPage(size_t page) @@ -105,11 +162,11 @@ ArchiveResult ZipArchive::getPage(size_t page) } QByteArray data; - data.resize(m_fileSizes[page]); - auto index = m_sortedIndices[page]; + data.resize(static_cast(m_fileSizes[page])); + // auto index = m_sortedIndices[page]; qDebug("Reading in page data"); - auto bytesRead = zip_fread(file, data.data(), data.size()); + auto bytesRead = zip_fread(file, data.data(), static_cast(data.size())); zip_fclose(file); file = nullptr; -- cgit v1.2.3