| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2015-2023 by Hans-Peter Deifel * |
| 3 | * [email protected] * |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 2 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program; if not, write to the * |
| 17 | * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * |
| 18 | * Boston, MA 02110-1301 USA. * |
| 19 | ***************************************************************************/ |
| 20 | |
| 21 | #include "search.h" |
| 22 | #include "output.h" |
| 23 | |
| 24 | #include <algorithm> |
| 25 | #include <iostream> |
| 26 | #include <cmath> |
| 27 | #include <cstring> |
| 28 | |
| 29 | #ifdef HAVE_UNAC |
| 30 | #include <unac.h> |
| 31 | #endif |
| 32 | #include <cpp/poppler-page.h> |
| 33 | |
| 34 | |
| 35 | using namespace std; |
| 36 | |
| 37 | struct SearchState { |
| 38 | bool document_empty = true; |
| 39 | // Total match count in the current PDF |
| 40 | int total_count = 0; |
| 41 | }; |
| 42 | |
| 43 | // Returns the number of matches found |
| 44 | static int search_page(const Options& opts, |
| 45 | const string& page_text, |
| 46 | size_t pagenum, |
| 47 | const string& page_label, |
| 48 | const string& filename, |
| 49 | const Regengine& re, |
| 50 | SearchState& state); |
| 51 | |
| 52 | static string maybe_unac(const Options &opts, string str); |
| 53 | |
| 54 | static void handle_match(const Options& opts, |
| 55 | const string& filename, |
| 56 | size_t page, |
| 57 | const string& page_label, |
| 58 | vector<match>& line, |
| 59 | vector<match>& last_line, |
| 60 | const match& mt, |
| 61 | bool previous_matches); |
| 62 | |
| 63 | static void flush_line_matches(const Options& opts, |
| 64 | const string& filename, |
| 65 | size_t page, |
| 66 | const string& page_label, |
| 67 | vector<match>& line, |
| 68 | vector<match>& last_line, |
| 69 | bool previous_matches); |
| 70 | |
| 71 | static string ustring_to_string(const poppler::ustring& str) { |
| 72 | poppler::byte_array arr = str.to_utf8(); |
| 73 | if (arr.empty()) { |
| 74 | return string(); |
| 75 | } else { |
| 76 | return string(arr.data(), arr.size()); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | int search_document(const Options &opts, unique_ptr<poppler::document> doc, |
| 81 | unique_ptr<Cache> cache, const string &filename, |
| 82 | const Regengine &re) { |
| 83 | |
| 84 | SearchState state; |
| 85 | |
| 86 | // doc->pages() returns an int, although it should be a size_t |
| 87 | auto doc_pages = static_cast<size_t>(doc->pages()); |
| 88 | |
| 89 | for (size_t pagenum = 1; pagenum <= doc_pages; pagenum++) { |
| 90 | if (!opts.page_range.contains(pagenum)) { |
| 91 | continue; |
| 92 | } |
| 93 | |
| 94 | CachePage cachepage; |
| 95 | |
| 96 | if (!opts.use_cache || !cache->get_page(pagenum, cachepage)) { |
| 97 | unique_ptr<poppler::page> page(doc->create_page(pagenum-1)); |
| 98 | |
| 99 | if (!page) { |
| 100 | if (!opts.quiet) { |
| 101 | err() << "Could not search in page " << pagenum |
| 102 | << " of " << filename << endl; |
| 103 | } |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | cachepage.text = ustring_to_string(page->text(page->page_rect(poppler::media_box))); |
| 108 | string& pagetext = cachepage.text; |
| 109 | |
| 110 | // newer versions of poppler generate spurious |
| 111 | // whitespace at the end of pages. Since in a pdf |
| 112 | // trailing whitespace text is visually identical to no |
| 113 | // text, we can just remove it. |
| 114 | auto whitespace_start = |
| 115 | std::find_if(pagetext.rbegin(), |
| 116 | pagetext.rend(), [](unsigned char ch) { |
| 117 | return !std::isspace(ch); |
| 118 | }); |
| 119 | pagetext.erase(whitespace_start.base(), pagetext.end()); |
| 120 | |
| 121 | |
| 122 | if (!pagetext.empty()) { |
| 123 | // there is text on this page, document can't be empty |
| 124 | state.document_empty = false; |
| 125 | } |
| 126 | |
| 127 | // TODO Don't read label if we don't need it |
| 128 | cachepage.label = ustring_to_string(page->label()); |
| 129 | |
| 130 | // Update the rendering cache |
| 131 | if (opts.use_cache) { |
| 132 | cache->set_page(pagenum, cachepage); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | string& text = cachepage.text; |
| 137 | string& label = cachepage.label; |
| 138 | |
| 139 | if (!text.empty()) { |
| 140 | // there is text on this page, document can't be empty |
| 141 | state.document_empty = false; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | int page_count = search_page(opts, text, pagenum, label, filename, re, state); |
| 146 | |
| 147 | if (page_count > 0 && opts.quiet) { |
| 148 | break; |
| 149 | } |
| 150 | |
| 151 | if (opts.only_filenames == OnlyFilenames::WITH_MATCHES |
| 152 | && page_count > 0) { |
| 153 | if (!opts.quiet) { |
| 154 | print_only_filename(opts.outconf, filename); |
| 155 | } |
| 156 | break; |
| 157 | } |
| 158 | if (page_count > 0 && opts.pagecount && |
| 159 | opts.only_filenames == OnlyFilenames::NOPE && !opts.quiet) { |
| 160 | line_prefix(context { filename, pagenum, label, opts.outconf }, false) |
| 161 | << page_count << endl; |
| 162 | } |
| 163 | |
| 164 | if (opts.max_count > 0 && state.total_count >= opts.max_count) { |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | if (opts.only_filenames == OnlyFilenames::WITHOUT_MATCH |
| 170 | && state.total_count == 0 |
| 171 | && !opts.quiet) { |
| 172 | print_only_filename(opts.outconf, filename); |
| 173 | } |
| 174 | |
| 175 | if (opts.count && opts.only_filenames == OnlyFilenames::NOPE && !opts.quiet) { |
| 176 | line_prefix(context {filename, 0, "", opts.outconf}, false) << state.total_count << endl; |
| 177 | } |
| 178 | |
| 179 | if (opts.warn_empty && state.document_empty) { |
| 180 | err() << "File does not contain text: " << filename << endl; |
| 181 | } |
| 182 | |
| 183 | // Save the cache for a later use |
| 184 | if (opts.use_cache) { |
| 185 | cache->dump(); |
| 186 | } |
| 187 | |
| 188 | return state.total_count; |
| 189 | } |
| 190 | |
| 191 | static int search_page(const Options& opts, |
| 192 | const string& page_text, |
| 193 | size_t pagenum, |
| 194 | const string& page_label, |
| 195 | const string& filename, |
| 196 | const Regengine& re, |
| 197 | SearchState& state) { |
| 198 | // Count of matches just on this page |
| 199 | int page_count = 0; |
| 200 | |
| 201 | // We need that in flush_line_matches to know if we need to print a |
| 202 | // context separator. |
| 203 | bool previous_matches = state.total_count > 0; |
| 204 | |
| 205 | string text = maybe_unac(opts, page_text); |
| 206 | |
| 207 | size_t index = 0; |
| 208 | struct match mt = { text, 0, 0 }; |
| 209 | |
| 210 | // matches found in current line |
| 211 | vector<match> current; |
| 212 | |
| 213 | // last line that contained matches. We only have to store at most one |
| 214 | // match, but the ability to store 0 matches is crucial for the initial |
| 215 | // state. |
| 216 | vector<match> last_line; |
| 217 | |
| 218 | while (re.exec(text, index, mt)) { |
| 219 | state.total_count++; |
| 220 | page_count++; |
| 221 | |
| 222 | if (opts.quiet || opts.only_filenames == OnlyFilenames::WITH_MATCHES) { |
| 223 | return page_count; |
| 224 | } |
| 225 | |
| 226 | handle_match(opts, filename, pagenum, page_label, current, last_line, mt, previous_matches); |
| 227 | |
| 228 | if (opts.max_count > 0 && state.total_count >= opts.max_count) { |
| 229 | flush_line_matches(opts, filename, pagenum, page_label, current, last_line, previous_matches); |
| 230 | if (!last_line.empty() && !opts.count && !opts.pagecount) { |
| 231 | // Print final context after last match |
| 232 | struct context cntxt = {filename, pagenum, page_label, opts.outconf}; |
| 233 | print_context_after(cntxt, *last_line.rbegin()); |
| 234 | } |
| 235 | return page_count; |
| 236 | } |
| 237 | |
| 238 | index = mt.end; |
| 239 | |
| 240 | // prevent loop if match is empty |
| 241 | if (mt.start == mt.end) { |
| 242 | index++; |
| 243 | } |
| 244 | |
| 245 | if(index >= text.size()) { |
| 246 | break; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | flush_line_matches(opts, filename, pagenum, page_label, current, last_line, previous_matches); |
| 251 | |
| 252 | if (!last_line.empty() && !opts.count && !opts.pagecount) { |
| 253 | // Print final context after last match |
| 254 | struct context cntxt = {filename, pagenum, page_label, opts.outconf}; |
| 255 | print_context_after(cntxt, *last_line.rbegin()); |
| 256 | } |
| 257 | |
| 258 | return page_count; |
| 259 | } |
| 260 | |
| 261 | static void flush_line_matches(const Options& opts, |
| 262 | const string& filename, |
| 263 | size_t page, |
| 264 | const string& page_label, |
| 265 | vector<match>& line, |
| 266 | vector<match>& last_line, |
| 267 | bool previous_matches) { |
| 268 | |
| 269 | struct context cntxt = {filename, page, page_label, opts.outconf}; |
| 270 | |
| 271 | // We don't want any output: |
| 272 | if (line.empty() || opts.count || opts.pagecount |
| 273 | || opts.only_filenames != OnlyFilenames::NOPE) { |
| 274 | goto out; |
| 275 | } |
| 276 | |
| 277 | // context printing |
| 278 | if (last_line.empty()) { |
| 279 | // There was no previous match |
| 280 | |
| 281 | if (previous_matches) { |
| 282 | print_context_separator(opts.outconf); |
| 283 | } |
| 284 | // (see document_empty) |
| 285 | print_context_before(cntxt, *line.begin()); |
| 286 | } else { |
| 287 | print_context_between(cntxt, *last_line.rbegin(), *line.begin()); |
| 288 | } |
| 289 | |
| 290 | if (opts.outconf.only_matching) { |
| 291 | for (auto const &mt : line) { |
| 292 | print_only_match(cntxt, mt); |
| 293 | } |
| 294 | } else { |
| 295 | print_matches(cntxt, line); |
| 296 | } |
| 297 | |
| 298 | out: |
| 299 | line.swap(last_line); |
| 300 | line.clear(); |
| 301 | } |
| 302 | |
| 303 | static void handle_match(const Options& opts, |
| 304 | const string& filename, |
| 305 | size_t page, |
| 306 | const string& page_label, |
| 307 | vector<match>& line, |
| 308 | vector<match>& last_line, |
| 309 | const match& mt, |
| 310 | bool previous_matches) { |
| 311 | if (line.empty()) { |
| 312 | line.push_back(mt); |
| 313 | return; |
| 314 | } |
| 315 | size_t end_last = line.back().end; |
| 316 | // TODO: Don't search whole string, just up to mt.start |
| 317 | auto next_newline = mt.string.find('\n', end_last); |
| 318 | if (next_newline == string::npos || next_newline > mt.start) { |
| 319 | line.push_back(mt); |
| 320 | } else { |
| 321 | flush_line_matches(opts, filename, page, page_label, line, last_line, previous_matches); |
| 322 | line.push_back(mt); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | static string maybe_unac(const Options &opts, string str) { |
| 327 | #ifdef HAVE_UNAC |
| 328 | return simple_unac(opts, str); |
| 329 | #else |
| 330 | (void) opts; |
| 331 | return str; |
| 332 | #endif |
| 333 | } |