Add exception for zip processing.
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
- New dependency: libfmt. - Translate error messages.
This commit is contained in:
parent
2709be1620
commit
4e8c6e7489
|
@ -25,6 +25,7 @@ find_package(Boost 1.65.0 REQUIRED COMPONENTS program_options locale)
|
||||||
find_package(Gettext REQUIRED)
|
find_package(Gettext REQUIRED)
|
||||||
find_package(Filesystem REQUIRED COMPONENTS Final Experimental)
|
find_package(Filesystem REQUIRED COMPONENTS Final Experimental)
|
||||||
find_package(LibArchive REQUIRED)
|
find_package(LibArchive REQUIRED)
|
||||||
|
find_package(fmt REQUIRED CONFIG)
|
||||||
|
|
||||||
add_subdirectory(src)
|
add_subdirectory(src)
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
:uri-boost: https://www.boost.org/
|
:uri-boost: https://www.boost.org/
|
||||||
:uri-gettext: https://www.gnu.org/software/gettext/
|
:uri-gettext: https://www.gnu.org/software/gettext/
|
||||||
:uri-libarchive: https://www.libarchive.org/
|
:uri-libarchive: https://www.libarchive.org/
|
||||||
|
:uri-fmt: https://github.com/fmtlib/fmt
|
||||||
|
|
||||||
*{project}* is a search tool for EPUB ebooks.
|
*{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-boost}[Boost] (tested: 1.75.0 / 1.65.0)
|
||||||
* link:{uri-gettext}[gettext] (tested: 0.21 / 0.19)
|
* link:{uri-gettext}[gettext] (tested: 0.21 / 0.19)
|
||||||
* link:{uri-libarchive}[libarchive] (tested: 3.5 / 3.2)
|
* link:{uri-libarchive}[libarchive] (tested: 3.5 / 3.2)
|
||||||
|
* link:{uri-fmt}[fmt] (tested: 7.0 / 4.0)
|
||||||
* Optional
|
* Optional
|
||||||
** Tests: link:{uri-catch}[Catch] (tested: 2.13 / 1.10)
|
** Tests: link:{uri-catch}[Catch] (tested: 2.13 / 1.10)
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ unset(headers_src)
|
||||||
|
|
||||||
target_link_libraries(${PROJECT_NAME}
|
target_link_libraries(${PROJECT_NAME}
|
||||||
Boost::program_options Boost::locale
|
Boost::program_options Boost::locale
|
||||||
std::filesystem LibArchive::LibArchive)
|
std::filesystem LibArchive::LibArchive fmt::fmt)
|
||||||
|
|
||||||
target_include_directories(${PROJECT_NAME}
|
target_include_directories(${PROJECT_NAME}
|
||||||
PRIVATE "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>")
|
PRIVATE "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>")
|
||||||
|
|
|
@ -71,10 +71,18 @@ int main(int argc, char *argv[])
|
||||||
for (const auto &file : vm["input-file"].as<std::vector<std::string>>())
|
for (const auto &file : vm["input-file"].as<std::vector<std::string>>())
|
||||||
{
|
{
|
||||||
cout << " " << file << ":\n";
|
cout << " " << file << ":\n";
|
||||||
|
try
|
||||||
|
{
|
||||||
for (const auto &entry : epubgrep::zip::list(file))
|
for (const auto &entry : epubgrep::zip::list(file))
|
||||||
{
|
{
|
||||||
cout << " " << entry << '\n';
|
cout << " " << entry << '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (const epubgrep::zip::exception &e)
|
||||||
|
{
|
||||||
|
cerr << translate("ERROR: ") << e.what() << '\n';
|
||||||
|
cerr << translate("Error while processing epub file.") << '\n';
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
14
src/zip.cpp
14
src/zip.cpp
|
@ -20,6 +20,9 @@
|
||||||
|
|
||||||
#include <archive.h>
|
#include <archive.h>
|
||||||
#include <archive_entry.h>
|
#include <archive_entry.h>
|
||||||
|
#include <boost/locale/message.hpp>
|
||||||
|
#include <fmt/format.h>
|
||||||
|
#include <fmt/ostream.h> // For compatibility with fmt 4.
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
@ -27,6 +30,9 @@
|
||||||
namespace epubgrep::zip
|
namespace epubgrep::zip
|
||||||
{
|
{
|
||||||
|
|
||||||
|
using boost::locale::translate;
|
||||||
|
using fmt::format;
|
||||||
|
|
||||||
std::vector<std::string> list(const fs::path &filepath)
|
std::vector<std::string> list(const fs::path &filepath)
|
||||||
{
|
{
|
||||||
struct archive *zipfile{};
|
struct archive *zipfile{};
|
||||||
|
@ -41,8 +47,8 @@ std::vector<std::string> list(const fs::path &filepath)
|
||||||
result = archive_read_open_filename(zipfile, filepath.c_str(), 10240);
|
result = archive_read_open_filename(zipfile, filepath.c_str(), 10240);
|
||||||
if (result != ARCHIVE_OK)
|
if (result != ARCHIVE_OK)
|
||||||
{
|
{
|
||||||
// NOLINTNEXTLINE
|
throw exception{format(translate("Could not open {0:s}.").str(),
|
||||||
throw "Zip file not okay. 🙁";
|
filepath.string())};
|
||||||
}
|
}
|
||||||
|
|
||||||
while (archive_read_next_header(zipfile, &entry) == ARCHIVE_OK)
|
while (archive_read_next_header(zipfile, &entry) == ARCHIVE_OK)
|
||||||
|
@ -54,8 +60,8 @@ std::vector<std::string> list(const fs::path &filepath)
|
||||||
result = archive_read_free(zipfile);
|
result = archive_read_free(zipfile);
|
||||||
if (result != ARCHIVE_OK)
|
if (result != ARCHIVE_OK)
|
||||||
{
|
{
|
||||||
// NOLINTNEXTLINE
|
throw exception{format(translate("ERROR: Could not close {0:s}.").str(),
|
||||||
throw "Error closing zipfile. 🙁";
|
filepath.string())};
|
||||||
}
|
}
|
||||||
|
|
||||||
return toc;
|
return toc;
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
#include "fs-compat.hpp"
|
#include "fs-compat.hpp"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
@ -27,6 +28,12 @@ namespace epubgrep::zip
|
||||||
|
|
||||||
std::vector<std::string> list(const fs::path &filepath);
|
std::vector<std::string> list(const fs::path &filepath);
|
||||||
|
|
||||||
|
class exception : public std::runtime_error
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using std::runtime_error::runtime_error;
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace epubgrep::zip
|
} // namespace epubgrep::zip
|
||||||
|
|
||||||
#endif // EPUBGREP_ZIP_HPP
|
#endif // EPUBGREP_ZIP_HPP
|
||||||
|
|
Loading…
Reference in New Issue
Block a user