/* This file is part of epubgrep. * Copyright © 2021 tastytea * * 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 . */ #include "options.hpp" #include "search.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include 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>()) { for (const auto ®ex : vm["regexp"].as>()) { search::options opts; // TODO: regex type. if (vm.count("ignore-case") > 0) { opts.ignore_case = true; } if (vm.count("raw") > 0) { opts.nostrip = true; } opts.context = vm["context"].as(); 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'; } } } } }