| 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 | #ifndef INTERVALS_H |
| 22 | #define INTERVALS_H |
| 23 | |
| 24 | #include <vector> |
| 25 | #include <string> |
| 26 | |
| 27 | /* This file implements a interval container that supports insertion and |
| 28 | * inclusion tests for integer intervals. |
| 29 | * |
| 30 | * Used for the --page-range feature |
| 31 | */ |
| 32 | |
| 33 | struct Interval { |
| 34 | /* from and to are inclusive. */ |
| 35 | Interval(int from, int to) { |
| 36 | this->from = from; |
| 37 | this->to = to; |
| 38 | } |
| 39 | |
| 40 | int from; |
| 41 | int to; |
| 42 | |
| 43 | inline bool contains(int element) const { |
| 44 | return element >= from && element <= to; |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | class IntervalContainer { |
| 49 | public: |
| 50 | IntervalContainer() {} |
| 51 | |
| 52 | /** Parses a string into intervals. |
| 53 | * |
| 54 | * This accepts the format used for the --page-range option: A comma |
| 55 | * separated list of intervals, which can be either a single integer or |
| 56 | * two integers separated by a minus character. Whitespace is not |
| 57 | * allowed. |
| 58 | * |
| 59 | * More precisely, the following grammar is implemented: |
| 60 | * |
| 61 | * INTERVALS ::= 𝝴 | INTERVAL ',' INTERVALS |
| 62 | * INTERVAL ::= int | int '-' int |
| 63 | * |
| 64 | */ |
| 65 | static IntervalContainer fromString(const std::string &str); |
| 66 | |
| 67 | void addInterval(Interval i); |
| 68 | |
| 69 | /** Returns true if either the element is contained in any interval or |
| 70 | * this container is empty. |
| 71 | * |
| 72 | * The latter case is there to simplify --page-range. If no intervals |
| 73 | * are specified, we want to search every page. |
| 74 | */ |
| 75 | bool contains(int element) const; |
| 76 | |
| 77 | private: |
| 78 | std::vector<Interval> intervals; |
| 79 | }; |
| 80 | |
| 81 | #endif /* INTERVALS_H */ |
| 82 | |
| 83 | /* Local Variables: */ |
| 84 | /* mode: c++ */ |
| 85 | /* End: */ |