122 lines
3.8 KiB
C++
122 lines
3.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 "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
|
|
{
|
|
for (const auto &filepath :
|
|
vm["input-file"].as<std::vector<std::string>>())
|
|
{
|
|
for (const auto ®ex :
|
|
vm["regexp"].as<std::vector<std::string>>())
|
|
{
|
|
search::options opts;
|
|
if (vm.count("basic-regexp") > 0)
|
|
{
|
|
opts.regex = search::regex_type::basic;
|
|
}
|
|
if (vm.count("extended-regexp") > 0)
|
|
{
|
|
opts.regex = search::regex_type::extended;
|
|
}
|
|
if (vm.count("perl-regexp") > 0)
|
|
{
|
|
opts.regex = search::regex_type::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.nostrip = true;
|
|
}
|
|
opts.context = vm["context"].as<std::uint64_t>();
|
|
|
|
for (const auto &match : search::search(filepath, regex, opts))
|
|
{
|
|
cout << match.filepath << ", " << match.headline << ", "
|
|
<< match.page << ": " << match.context.first
|
|
<< match.text << match.context.second << '\n';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|