From 222f802015cc50dfea60e3bfef6c9f0b24f33f63 Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 21 May 2021 01:56:37 +0200 Subject: [PATCH] Basic zip file support. Dumping the TOC works. --- .drone.yml | 8 +++--- CMakeLists.txt | 3 ++- README.adoc | 4 ++- src/CMakeLists.txt | 2 +- src/main.cpp | 12 ++++++--- src/zip.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++ src/zip.hpp | 32 +++++++++++++++++++++++ 7 files changed, 114 insertions(+), 11 deletions(-) create mode 100644 src/zip.cpp create mode 100644 src/zip.hpp diff --git a/.drone.yml b/.drone.yml index 17abbb4..c50f551 100644 --- a/.drone.yml +++ b/.drone.yml @@ -26,7 +26,7 @@ steps: - alias apt-get='rm -f /var/cache/apt/archives/lock && apt-get' - apt-get update -q - apt-get install -qq build-essential cmake clang - - apt-get install -qq catch libboost-all-dev gettext + - apt-get install -qq catch libboost-all-dev gettext libarchive-dev - rm -rf build && mkdir -p build && cd build - cmake -G "Unix Makefiles" -DWITH_TESTS=YES .. - make VERBOSE=1 @@ -42,10 +42,10 @@ steps: - name: debian-package-cache path: /var/cache/apt/archives -- name: Download CMake 3.12 installer +- name: Download CMake 3.17 installer image: plugins/download settings: - source: https://cmake.org/files/v3.12/cmake-3.12.0-Linux-x86_64.sh + source: https://cmake.org/files/v3.17/cmake-3.17.0-Linux-x86_64.sh destination: cmake_installer.sh - name: GCC 7 / clang 6 @@ -61,7 +61,7 @@ steps: - alias apt-get='rm -f /var/cache/apt/archives/lock && apt-get' - apt-get update -q - apt-get install -qq build-essential clang - - apt-get install -qq catch libboost-all-dev gettext + - apt-get install -qq catch libboost-all-dev gettext libarchive-dev - sh cmake_installer.sh --skip-license --exclude-subdir --prefix=/usr/local - rm -rf build && mkdir -p build && cd build - cmake -G "Unix Makefiles" -DWITH_TESTS=YES .. diff --git a/CMakeLists.txt b/CMakeLists.txt index a637ed6..acf43db 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.12...3.18) +cmake_minimum_required(VERSION 3.17...3.18) # Global build options. set(CMAKE_BUILD_TYPE "Release" CACHE STRING "The type of build.") @@ -24,6 +24,7 @@ include(cmake/debug_flags.cmake) find_package(Boost 1.65.0 REQUIRED COMPONENTS program_options locale) find_package(Gettext REQUIRED) find_package(Filesystem REQUIRED COMPONENTS Final Experimental) +find_package(LibArchive REQUIRED) add_subdirectory(src) diff --git a/README.adoc b/README.adoc index 3a51120..9460894 100644 --- a/README.adoc +++ b/README.adoc @@ -11,6 +11,7 @@ :uri-catch: https://github.com/catchorg/Catch2 :uri-boost: https://www.boost.org/ :uri-gettext: https://www.gnu.org/software/gettext/ +:uri-libarchive: https://www.libarchive.org/ *{project}* is a search tool for EPUB ebooks. @@ -36,9 +37,10 @@ image::https://repology.org/badge/vertical-allrepos/epubgrep.svg[] * Tested OS: Linux * C\++ compiler with C++17 support (tested: link:{uri-gcc}[GCC] 7/10, link:{uri-clang}[clang] 6/11) -* link:{uri-cmake}[CMake] (at least: 3.12) +* link:{uri-cmake}[CMake] (at least: 3.17) * link:{uri-boost}[Boost] (tested: 1.75.0 / 1.65.0) * link:{uri-gettext}[gettext] (tested: 0.21 / 0.19) +* link:{uri-libarchive}[libarchive] (tested: 3.5 / 3.2) * Optional ** Tests: link:{uri-catch}[Catch] (tested: 2.13 / 1.10) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7aed0d1..fee319b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -14,7 +14,7 @@ unset(headers_src) target_link_libraries(${PROJECT_NAME} Boost::program_options Boost::locale - std::filesystem) + std::filesystem LibArchive::LibArchive) target_include_directories(${PROJECT_NAME} PRIVATE "$") diff --git a/src/main.cpp b/src/main.cpp index 8b2134e..3fedfb4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,6 +15,7 @@ */ #include "options.hpp" +#include "zip.hpp" #include #include @@ -64,13 +65,16 @@ int main(int argc, char *argv[]) cout << "perl-regexp: " << vm.count("perl-regexp") << '\n'; cout << "ignore-case: " << vm.count("ignore-case") << '\n'; - if (vm.count("input") > 0) + if (vm.count("input-file") > 0) { cout << "\nINPUT FILES: "; - for (const auto &input : vm["input"].as>()) + for (const auto &file : vm["input-file"].as>()) { - cout << input << " "; + cout << " " << file << ":\n"; + for (const auto &entry : epubgrep::zip::list(file)) + { + cout << " " << entry << '\n'; + } } - cout << '\n'; } } diff --git a/src/zip.cpp b/src/zip.cpp new file mode 100644 index 0000000..42b3c2f --- /dev/null +++ b/src/zip.cpp @@ -0,0 +1,64 @@ +/* This file is part of epubgrep. + * Copyright © 2021 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received zipfile copy of the GNU Affero General Public + * License along with this program. If not, see . + */ + +#include "zip.hpp" + +#include "fs-compat.hpp" + +#include +#include + +#include +#include + +namespace epubgrep::zip +{ + +std::vector list(const fs::path &filepath) +{ + struct archive *zipfile{}; + struct archive_entry *entry{}; + int result{}; + std::vector toc; + + zipfile = archive_read_new(); + archive_read_support_filter_all(zipfile); + archive_read_support_format_all(zipfile); + + result = archive_read_open_filename(zipfile, filepath.c_str(), 10240); + if (result != ARCHIVE_OK) + { + // NOLINTNEXTLINE + throw "Zip file not okay. 🙁"; + } + + while (archive_read_next_header(zipfile, &entry) == ARCHIVE_OK) + { + toc.emplace_back(archive_entry_pathname(entry)); + archive_read_data_skip(zipfile); + } + + result = archive_read_free(zipfile); + if (result != ARCHIVE_OK) + { + // NOLINTNEXTLINE + throw "Error closing zipfile. 🙁"; + } + + return toc; +} + +} // namespace epubgrep::zip diff --git a/src/zip.hpp b/src/zip.hpp new file mode 100644 index 0000000..ad7dba1 --- /dev/null +++ b/src/zip.hpp @@ -0,0 +1,32 @@ +/* This file is part of epubgrep. + * Copyright © 2021 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef EPUBGREP_ZIP_HPP +#define EPUBGREP_ZIP_HPP + +#include "fs-compat.hpp" + +#include +#include + +namespace epubgrep::zip +{ + +std::vector list(const fs::path &filepath); + +} // namespace epubgrep::zip + +#endif // EPUBGREP_ZIP_HPP