From fca719634a0b2ca49ecde662247ecbcaec8a4840 Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 20 Aug 2021 15:35:10 +0200 Subject: [PATCH] Move OPF file path detection into own function. --- src/book.cpp | 40 ++++++++++++++++++++-------------------- src/book.hpp | 3 +++ 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/book.cpp b/src/book.cpp index 0431600..c839a55 100644 --- a/src/book.cpp +++ b/src/book.cpp @@ -189,28 +189,28 @@ string page(const document &doc, const size_t pos) return string(last); } +fs::path get_opf_file_path(const fs::path &zipfile) +{ + pugi::xml_document xml; + const std::string container{ + zip::read_file(zipfile, "META-INF/container.xml")}; + const auto result{xml.load_buffer(&container[0], container.size())}; + if (result) + { + return fs::path{xml.child("container") + .child("rootfiles") + .first_child() + .attribute("full-path") + .value()}; + } + LOG(log::sev::error) << result.description() << '\n'; + + return fs::path{}; +}; + std::vector list_spine(const fs::path &filepath) { - const auto opf_file_path{ - [&filepath] - { - pugi::xml_document xml; - const std::string container{ - zip::read_file(filepath, "META-INF/container.xml")}; - const auto result{xml.load_buffer(&container[0], container.size())}; - if (result) - { - return fs::path{xml.child("container") - .child("rootfiles") - .first_child() - .attribute("full-path") - .value()}; - } - LOG(log::sev::error) << result.description() << '\n'; - - return fs::path{}; - }()}; - + auto opf_file_path{get_opf_file_path(filepath)}; std::vector spine_filepaths; if (!opf_file_path.empty()) { diff --git a/src/book.hpp b/src/book.hpp index bcd3ff7..4404299 100644 --- a/src/book.hpp +++ b/src/book.hpp @@ -62,6 +62,9 @@ struct book //! Return current page if possible. [[nodiscard]] std::string page(const document &doc, size_t pos); +//! Returns the file path of the OPF file in the EPUB. +[[nodiscard]] fs::path get_opf_file_path(const fs::path &zipfile); + //! Returns the files in the EPUB “spine” (all pages that are actually text). [[nodiscard]] std::vector list_spine(const fs::path &filepath);