| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2017-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 "intervals.h" |
| 22 | |
| 23 | #include <algorithm> |
| 24 | #include <sstream> |
| 25 | #include <iostream> |
| 26 | #include <stdexcept> |
| 27 | |
| 28 | #include "output.h" |
| 29 | |
| 30 | // The interval container data structure could be much more sophisticated (e.g |
| 31 | // by using interval trees or even sorted lists), but as the number of intervals |
| 32 | // in typical use cases is small, this shouldn't make much difference and could |
| 33 | // actually be harmful. |
| 34 | // |
| 35 | // tl;dr Measure before you optimize |
| 36 | |
| 37 | void IntervalContainer::addInterval(Interval i) { |
| 38 | intervals.push_back(i); |
| 39 | } |
| 40 | |
| 41 | bool IntervalContainer::contains(int element) const { |
| 42 | // We interpret the empty container as one interval containing |
| 43 | // everything. This makes sense in the pdfgrep case: If the user doesn't |
| 44 | // restrict the intervals she want's all pages to be searched. |
| 45 | if (intervals.empty()) { |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | auto predicate = [=](const Interval &a) {return a.contains(element);}; |
| 50 | |
| 51 | return std::find_if(intervals.begin(), intervals.end(), predicate) |
| 52 | != intervals.end(); |
| 53 | } |
| 54 | |
| 55 | static Interval parse_interval(const std::string &str) { |
| 56 | size_t minus = str.find('-'); |
| 57 | |
| 58 | int from, to; |
| 59 | try { |
| 60 | // only one int, not a range |
| 61 | if (minus == std::string::npos) { |
| 62 | from = to = std::stoi(str); |
| 63 | } else { |
| 64 | auto from_str = str.substr(0, minus); |
| 65 | auto to_str = str.substr(minus+1, str.length()-minus); |
| 66 | |
| 67 | from = std::stoi(from_str); |
| 68 | to = std::stoi(to_str); |
| 69 | } |
| 70 | } catch (std::invalid_argument e) { |
| 71 | err() << "Invalid page range \"" << str << "\". " |
| 72 | << "Expected a single page or a range PAGE1-PAGE2." << std::endl; |
| 73 | exit(EXIT_ERROR); |
| 74 | } |
| 75 | |
| 76 | if (from <= 0 || to <= 0) { |
| 77 | err() << "Invalid page range \"" << str << "\". " |
| 78 | << "Page numbers must be positive." << std::endl; |
| 79 | exit(EXIT_ERROR); |
| 80 | } |
| 81 | |
| 82 | if (to < from) { |
| 83 | err() << "warning: Page range is empty: " << str << std::endl; |
| 84 | } |
| 85 | |
| 86 | return {from, to}; |
| 87 | } |
| 88 | |
| 89 | IntervalContainer IntervalContainer::fromString(const std::string &str) { |
| 90 | IntervalContainer c; |
| 91 | std::stringstream tokens(str); |
| 92 | |
| 93 | std::string interval; |
| 94 | while (std::getline(tokens, interval, ',')) { |
| 95 | c.addInterval(parse_interval(interval)); |
| 96 | } |
| 97 | |
| 98 | return c; |
| 99 | } |