diff options
Diffstat (limited to 'src/search.cc')
-rw-r--r-- | src/search.cc | 61 |
1 files changed, 32 insertions, 29 deletions
diff --git a/src/search.cc b/src/search.cc index ffea9f6..89d416c 100644 --- a/src/search.cc +++ b/src/search.cc @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2017 by Hans-Peter Deifel * + * Copyright (C) 2015-2018 by Hans-Peter Deifel * * hpd@hpdeifel.de * * * * This program is free software; you can redistribute it and/or modify * @@ -44,10 +44,6 @@ static int search_page(const Options &opts, const string &text, size_t pagenum, const string &filename, const Regengine &re, SearchState &state); -#ifdef HAVE_UNAC -/* convenience layer over libunac */ -static string simple_unac(const Options &opts, string str); -#endif static string maybe_unac(const Options &opts, string std); static void handle_match(const Options &opts, const string &filename, size_t page, vector<match> &line, vector<match> &last_line, const match &mt, @@ -58,8 +54,11 @@ static void flush_line_matches(const Options &opts, const string &filename, size static string page_text(poppler::page &page) { poppler::byte_array arr = page.text(page.page_rect(poppler::media_box)).to_utf8(); - char *c_str = &arr[0]; - return string(c_str, arr.size()); + if (arr.empty()) { + return string(); + } else { + return string(arr.data(), arr.size()); + } } int search_document(const Options &opts, unique_ptr<poppler::document> doc, @@ -72,6 +71,9 @@ int search_document(const Options &opts, unique_ptr<poppler::document> doc, size_t doc_pages = static_cast<size_t>(doc->pages()); for (size_t pagenum = 1; pagenum <= doc_pages; pagenum++) { + if (opts.page_range.contains(pagenum) == false) + continue; + string text; if (!opts.use_cache || !cache->get_page(pagenum, text)) { unique_ptr<poppler::page> page(doc->create_page(pagenum-1)); @@ -98,7 +100,19 @@ int search_document(const Options &opts, unique_ptr<poppler::document> doc, int page_count = search_page(opts, text, pagenum, filename, re, state); - if (page_count > 0 && opts.pagecount && !opts.quiet) { + if (page_count > 0 && opts.quiet) { + break; + } + + if (opts.only_filenames == OnlyFilenames::WITH_MATCHES + && page_count > 0) { + if (!opts.quiet) { + print_only_filename(opts.outconf, filename); + } + break; + } + if (page_count > 0 && opts.pagecount && + opts.only_filenames == OnlyFilenames::NOPE && !opts.quiet) { line_prefix(opts.outconf, filename, false, pagenum) << page_count << endl; } @@ -107,7 +121,13 @@ int search_document(const Options &opts, unique_ptr<poppler::document> doc, } } - if (opts.count && !opts.quiet) { + if (opts.only_filenames == OnlyFilenames::WITHOUT_MATCH + && state.total_count == 0 + && !opts.quiet) { + print_only_filename(opts.outconf, filename); + } + + if (opts.count && opts.only_filenames == OnlyFilenames::NOPE && !opts.quiet) { line_prefix(opts.outconf, filename, false) << state.total_count << endl; } @@ -150,7 +170,7 @@ static int search_page(const Options &opts, const string &page_text, state.total_count++; page_count++; - if (opts.quiet) { + if (opts.quiet || opts.only_filenames == OnlyFilenames::WITH_MATCHES) { return page_count; } @@ -195,7 +215,8 @@ static void flush_line_matches(const Options &opts, const string &filename, size struct context cntxt = {filename, page, opts.outconf}; // We don't want any output: - if (line.empty() || opts.count || opts.pagecount) + if (line.empty() || opts.count || opts.pagecount + || opts.only_filenames != OnlyFilenames::NOPE) goto out; // context printing @@ -242,24 +263,6 @@ static void handle_match(const Options &opts, const string &filename, size_t pag } } -#ifdef HAVE_UNAC -static string simple_unac(const Options &opts, string str) -{ - if (!opts.use_unac) - return str; - - char *res = NULL; - size_t reslen = 0; - - if (unac_string("UTF-8", str.c_str(), str.size(), &res, &reslen)) { - perror("pdfgrep: Failed to remove accents: "); - return str; - } - - return res; -} -#endif - static string maybe_unac(const Options &opts, string str) { #ifdef HAVE_UNAC return simple_unac(opts, str); |