epubgrep/src/search.hpp

73 lines
1.9 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/>.
*/
#ifndef EPUBGREP_SEARCH_HPP
#define EPUBGREP_SEARCH_HPP
#include "fs-compat.hpp"
#include <boost/regex.hpp>
#include <cstdint>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
namespace epubgrep::search
{
using match_context = std::pair<std::string, std::string>;
struct match
{
std::string text; //!< Matched string.
match_context context; //!< The context around the match.
std::string filepath; //!< The file path of the matched line.
std::string headline; //!< The last headline, if available.
std::string page; //!< The page number, if available.
};
enum class regex_kind
{
basic,
extended,
perl
};
struct options
{
regex_kind regex{regex_kind::basic};
bool grep_like{false};
bool ignore_case{false};
bool raw{false};
std::uint64_t context{0};
};
[[nodiscard]] std::vector<match> search(const fs::path &filepath,
std::string_view regex,
const options &opts);
[[nodiscard]] match_context
context(const boost::match_results<std::string::const_iterator> &match,
std::uint64_t words);
void cleanup_text(std::string &text);
} // namespace epubgrep::search
#endif // EPUBGREP_SEARCH_HPP