epubgrep/src/main.cpp

90 lines
2.8 KiB
C++

/* This file is part of epubgrep.
* Copyright © 2021 tastytea <tastytea@tastytea.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "options.hpp"
#include "zip.hpp"
#include <boost/locale/generator.hpp>
#include <boost/locale/message.hpp>
#include <boost/program_options/errors.hpp>
#include <boost/program_options/variables_map.hpp>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <locale>
#include <string>
#include <vector>
int main(int argc, char *argv[])
{
namespace po = boost::program_options;
using boost::locale::translate;
using std::cerr;
using std::cout;
boost::locale::generator locale_generator;
locale_generator.add_messages_path("translations");
locale_generator.add_messages_path("/usr/share/locale");
locale_generator.add_messages_domain("epubgrep");
std::locale::global(locale_generator(""));
cout.imbue(std::locale());
cerr.imbue(std::locale());
po::variables_map vm;
try
{
vm = epubgrep::options::parse_options(argc, argv);
}
catch (std::exception &e)
{ // Exceptions we can't recover from or ones we don't know.
cerr << translate("ERROR: ") << e.what() << '\n';
cerr << translate("Error while parsing options.") << '\n';
return EXIT_FAILURE;
}
if (vm.count("help") + vm.count("version") > 0)
{
return EXIT_SUCCESS;
}
cout << "extended-regexp: " << vm.count("extended-regexp") << '\n';
cout << "perl-regexp: " << vm.count("perl-regexp") << '\n';
cout << "ignore-case: " << vm.count("ignore-case") << '\n';
if (vm.count("input-file") > 0)
{
cout << "\nINPUT FILES: ";
for (const auto &file : vm["input-file"].as<std::vector<std::string>>())
{
cout << " " << file << ":\n";
try
{
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';
}
}
}
}