diff --git a/CMakeLists.txt b/CMakeLists.txt index acf43db..3f65df2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,7 @@ 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) +find_package(fmt REQUIRED CONFIG) add_subdirectory(src) diff --git a/README.adoc b/README.adoc index 9460894..c6039b0 100644 --- a/README.adoc +++ b/README.adoc @@ -12,6 +12,7 @@ :uri-boost: https://www.boost.org/ :uri-gettext: https://www.gnu.org/software/gettext/ :uri-libarchive: https://www.libarchive.org/ +:uri-fmt: https://github.com/fmtlib/fmt *{project}* is a search tool for EPUB ebooks. @@ -41,6 +42,7 @@ image::https://repology.org/badge/vertical-allrepos/epubgrep.svg[] * 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) +* link:{uri-fmt}[fmt] (tested: 7.0 / 4.0) * Optional ** Tests: link:{uri-catch}[Catch] (tested: 2.13 / 1.10) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fee319b..9e9cc23 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 LibArchive::LibArchive) + std::filesystem LibArchive::LibArchive fmt::fmt) target_include_directories(${PROJECT_NAME} PRIVATE "$") diff --git a/src/main.cpp b/src/main.cpp index 3fedfb4..1f40716 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -71,9 +71,17 @@ int main(int argc, char *argv[]) for (const auto &file : vm["input-file"].as>()) { cout << " " << file << ":\n"; - for (const auto &entry : epubgrep::zip::list(file)) + try { - cout << " " << entry << '\n'; + for (const auto &entry : epubgrep::zip::list(file)) + { + cout << " " << entry << '\n'; + } + } + catch (const epubgrep::zip::exception &e) + { + cerr << translate("ERROR: ") << e.what() << '\n'; + cerr << translate("Error while processing epub file.") << '\n'; } } } diff --git a/src/zip.cpp b/src/zip.cpp index 42b3c2f..0c13049 100644 --- a/src/zip.cpp +++ b/src/zip.cpp @@ -20,6 +20,9 @@ #include #include +#include +#include +#include // For compatibility with fmt 4. #include #include @@ -27,6 +30,9 @@ namespace epubgrep::zip { +using boost::locale::translate; +using fmt::format; + std::vector list(const fs::path &filepath) { struct archive *zipfile{}; @@ -41,8 +47,8 @@ std::vector list(const fs::path &filepath) result = archive_read_open_filename(zipfile, filepath.c_str(), 10240); if (result != ARCHIVE_OK) { - // NOLINTNEXTLINE - throw "Zip file not okay. 🙁"; + throw exception{format(translate("Could not open {0:s}.").str(), + filepath.string())}; } while (archive_read_next_header(zipfile, &entry) == ARCHIVE_OK) @@ -54,8 +60,8 @@ std::vector list(const fs::path &filepath) result = archive_read_free(zipfile); if (result != ARCHIVE_OK) { - // NOLINTNEXTLINE - throw "Error closing zipfile. 🙁"; + throw exception{format(translate("ERROR: Could not close {0:s}.").str(), + filepath.string())}; } return toc; diff --git a/src/zip.hpp b/src/zip.hpp index ad7dba1..b12acd4 100644 --- a/src/zip.hpp +++ b/src/zip.hpp @@ -19,6 +19,7 @@ #include "fs-compat.hpp" +#include #include #include @@ -27,6 +28,12 @@ namespace epubgrep::zip std::vector list(const fs::path &filepath); +class exception : public std::runtime_error +{ +public: + using std::runtime_error::runtime_error; +}; + } // namespace epubgrep::zip #endif // EPUBGREP_ZIP_HPP