From 500e1f9892dd41419663e9f72cf47bab5b2aca0b Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Mon, 23 Sep 2024 00:33:33 -0500 Subject: Saving work. --- src/archive.cpp | 84 +++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 26 deletions(-) (limited to 'src/archive.cpp') diff --git a/src/archive.cpp b/src/archive.cpp index 0339d85..93b3773 100644 --- a/src/archive.cpp +++ b/src/archive.cpp @@ -1,21 +1,19 @@ #include "archive.hpp" -#include "main_window.hpp" - #include +#include -namespace getsuyomi { +namespace bookmouse { -ZipArchive::ZipArchive(const QString& filename) +ZipArchive::ZipArchive(const std::string& filename) { - auto filenameUtf8 = filename.toUtf8(); - qDebug("Open file %s", filenameUtf8.data()); + // qDebug("Open file %s", filename.data()); int err; - m_archive = zip_open(filenameUtf8.data(), 0, &err); + m_archive = zip_open(filename.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)); + // 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"); } @@ -25,8 +23,12 @@ ZipArchive::ZipArchive(const QString& filename) void ZipArchive::populate() { - auto numEntries = zip_get_num_entries(m_archive, ZIP_FL_UNCHANGED); - qDebug("%zu pages", numEntries); + auto numEntriesResult = zip_get_num_entries(m_archive, ZIP_FL_UNCHANGED); + if (numEntriesResult < 0) { + throw std::runtime_error("Invalid number of entries"); + } + auto numEntries = static_cast(numEntriesResult); + // qDebug("%zu pages", numEntries); struct NameIndex { std::string name; @@ -42,19 +44,23 @@ void ZipArchive::populate() struct zip_stat stats; auto status = zip_stat_index(m_archive, idx, 0, &stats); + if (status != 0) { + continue; + } + static_assert(ZIP_STAT_NAME != 0); static_assert(ZIP_STAT_SIZE != 0); static_assert(ZIP_STAT_INDEX != 0); // auto* nameCString = zip_get_name(m_archive, idx, ZIP_FL_ENC_RAW); auto* nameCString = stats.name; if (nameCString == nullptr) { - zip_error_t* error = zip_get_error(m_archive); - qWarning("cannot get name %s", zip_error_strerror(error)); + // zip_error_t* error = zip_get_error(m_archive); + // qWarning("cannot get name %s", zip_error_strerror(error)); continue; } std::string name{nameCString}; if (name.empty() || name.back() == '/') { - qDebug("Directory %s", name.empty() ? "N/A" : nameCString); + // qDebug("Directory %s", name.empty() ? "N/A" : nameCString); continue; } @@ -85,47 +91,73 @@ ZipArchive::~ZipArchive() } } +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::operator=(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; + return *this; +} + ArchiveResult ZipArchive::getPage(size_t page) { - qDebug("Getting page %zu", page); + // qDebug("Getting page %zu", page); if (page > m_sortedIndices.size()) { return ArchiveResult::error(ArchiveError::BadIndex); } if (m_pages[page] != std::nullopt) { - qDebug("Page found %zu", page); + // qDebug("Page found %zu", page); return ArchiveResult::okay(std::cref(*m_pages[page])); } auto* file = zip_fopen_index(m_archive, m_sortedIndices[page], 0); if (file == nullptr) { - zip_error_t* error = zip_get_error(m_archive); - qWarning("cannot get file name %s: %s", m_filenames[page].data(), zip_error_strerror(error)); + // zip_error_t* error = zip_get_error(m_archive); + // qWarning("cannot get file name %s: %s", m_filenames[page].data(), zip_error_strerror(error)); return ArchiveResult::error(ArchiveError::ZipError); } - QByteArray data; + std::vector data; data.resize(m_fileSizes[page]); - auto index = m_sortedIndices[page]; + // auto index = m_sortedIndices[page]; - qDebug("Reading in page data"); - auto bytesRead = zip_fread(file, data.data(), data.size()); + // qDebug("Reading in page data"); + auto bytesReadResult = zip_fread(file, data.data(), data.size()); + if (bytesReadResult < 0) { + return ArchiveResult::error(ArchiveError::ZipError); + } zip_fclose(file); file = nullptr; + auto bytesRead = static_cast(bytesReadResult); if (bytesRead != data.size()) { return ArchiveResult::error(ArchiveError::ZipError); } - m_pages[page] = QImage(); - qDebug("Loading QImage from page data"); + m_pages[page] = data; + // qDebug("Loading QImage from page data"); + /* auto loaded = m_pages[page]->loadFromData(data); if (!loaded) { - qWarning("Failed to load QImage"); + // qWarning("Failed to load QImage"); m_pages[page] = std::nullopt; return ArchiveResult::error(ArchiveError::BadData); } - + */ return ArchiveResult::okay(std::cref(*m_pages[page])); } @@ -133,4 +165,4 @@ size_t ZipArchive::numPages() const { return m_pages.size(); } -} // namespace getsuyomi +} // namespace bookmouse -- cgit v1.2.3