epubgrep/src/main.cpp

119 lines
3.6 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 <clocale>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <locale>
#include <string>
#include <typeinfo>
#include <vector>
int main(int argc, char *argv[])
{
namespace po = boost::program_options;
using boost::locale::translate;
using std::cerr;
using std::cout;
// locale_generator("").name.c_str() returns "*" instead of "". That's why
// the global C locale isn't changed. So we have to set it additionally.
std::setlocale(LC_ALL, "");
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 << '\n' << 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;
}
// Print all options.
for (const auto &opt : vm)
{
if (opt.second.value().type() == typeid(std::vector<std::string>))
{
for (const auto &str : opt.second.as<std::vector<std::string>>())
{
cout << opt.first << ": " << str << '\n';
}
}
else
{
auto second{opt.second.as<std::string>()};
if (second.empty())
{
cout << opt.first << ": true\n";
}
else
{
cout << opt.first << ": " << second << '\n';
}
}
}
if (vm.count("input-file") > 0)
{
cout << "\nINPUT FILES:\n";
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;
cout << "\n CONTENT:\n";
cout << "START" << epubgrep::zip::read_file(file, entry)
<< "END\n";
}
}
catch (const epubgrep::zip::exception &e)
{
cerr << '\n' << translate("ERROR: ") << e.what() << '\n';
cerr << translate("Error while processing EPUB file.") << '\n';
return EXIT_FAILURE;
}
}
}
}