epubgrep/src/main.cpp

142 lines
4.3 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 "search.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 <cstdint>
#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 namespace epubgrep;
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 = 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;
}
if (vm.count("input-file") == 0)
{
cout << "NO INPUT FILE\n";
// TODO: Read data from stdin.
}
else
{
search::options opts;
if (vm.count("basic-regexp") > 0)
{
opts.regex = search::regex_kind::basic;
}
if (vm.count("extended-regexp") > 0)
{
opts.regex = search::regex_kind::extended;
}
if (vm.count("perl-regexp") > 0)
{
opts.regex = search::regex_kind::perl;
}
if (vm.count("grep") > 0)
{
opts.grep_like = true;
}
if (vm.count("ignore-case") > 0)
{
opts.ignore_case = true;
}
if (vm.count("raw") > 0)
{
opts.raw = true;
}
opts.context = vm["context"].as<std::uint64_t>();
for (const auto &filepath :
vm["input-file"].as<std::vector<std::string>>())
{
for (const auto &regex :
vm["regexp"].as<std::vector<std::string>>())
{
try
{
for (const auto &match :
search::search(filepath, regex, opts))
{
cout << match.filepath;
if (!match.headline.empty())
{
cout << ", " << match.headline;
}
if (!match.page.empty())
{
cout << ", page " << match.page;
}
cout << ": " << match.context.first << match.text
<< match.context.second << '\n';
}
}
catch (const std::exception &e)
{ // Unknown errors.
cerr << '\n' << translate("ERROR: ") << e.what() << '\n';
cerr << translate("Error while searching.") << '\n';
// NOTE: Maybe we should continue with the next regex/file?
return EXIT_FAILURE;
}
}
}
}
}