From 5f0708418a0a9cbacc033f52efc11c759120fd09 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Sat, 16 Jan 2021 12:11:17 -0500 Subject: Add iterators to name/number tree helpers --- include/qpdf/QPDFNameTreeObjectHelper.hh | 63 ++++++++++++++++++++++++++++++ include/qpdf/QPDFNumberTreeObjectHelper.hh | 61 +++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) (limited to 'include') diff --git a/include/qpdf/QPDFNameTreeObjectHelper.hh b/include/qpdf/QPDFNameTreeObjectHelper.hh index 255ff24e..f94d34cf 100644 --- a/include/qpdf/QPDFNameTreeObjectHelper.hh +++ b/include/qpdf/QPDFNameTreeObjectHelper.hh @@ -26,6 +26,7 @@ #include #include #include +#include #include @@ -35,6 +36,8 @@ // normalized for lookup purposes. class NNTreeImpl; +class NNTreeIterator; +class NNTreeDetails; class QPDFNameTreeObjectHelper: public QPDFObjectHelper { @@ -54,6 +57,66 @@ class QPDFNameTreeObjectHelper: public QPDFObjectHelper QPDF_DLL bool findObject(std::string const& utf8, QPDFObjectHandle& oh); + class iterator: public std::iterator< + std::bidirectional_iterator_tag, + std::pair, + void, + std::pair*, + std::pair> + { + friend class QPDFNameTreeObjectHelper; + public: + QPDF_DLL + bool valid() const; + QPDF_DLL + iterator& operator++(); + QPDF_DLL + iterator operator++(int) + { + iterator t = *this; + ++(*this); + return t; + } + QPDF_DLL + iterator& operator--(); + QPDF_DLL + iterator operator--(int) + { + iterator t = *this; + --(*this); + return t; + } + QPDF_DLL + reference operator*(); + QPDF_DLL + bool operator==(iterator const& other) const; + QPDF_DLL + bool operator!=(iterator const& other) const + { + return ! operator==(other); + } + + private: + iterator(std::shared_ptr const&); + std::shared_ptr impl; + }; + + // The iterator looks like map iterator, so i.first is a string + // and i.second is a QPDFObjectHandle. + QPDF_DLL + iterator begin() const; + QPDF_DLL + iterator end() const; + // Return a bidirectional iterator that points to the last item. + QPDF_DLL + iterator last() const; + + // Find the entry with the given key. If return_prev_if_not_found + // is true and the item is not found, return the next lower item. + QPDF_DLL + iterator find(std::string const& key, + bool return_prev_if_not_found = false); + // Return the contents of the name tree as a map. Note that name // trees may be very large, so this may use a lot of RAM. It is // more efficient to use QPDFNameTreeObjectHelper's iterator. diff --git a/include/qpdf/QPDFNumberTreeObjectHelper.hh b/include/qpdf/QPDFNumberTreeObjectHelper.hh index 7fb9195c..393e8dba 100644 --- a/include/qpdf/QPDFNumberTreeObjectHelper.hh +++ b/include/qpdf/QPDFNumberTreeObjectHelper.hh @@ -33,6 +33,8 @@ // PDF spec (ISO 32000) for a description of number trees. class NNTreeImpl; +class NNTreeIterator; +class NNTreeDetails; class QPDFNumberTreeObjectHelper: public QPDFObjectHelper { @@ -73,6 +75,65 @@ class QPDFNumberTreeObjectHelper: public QPDFObjectHelper bool findObjectAtOrBelow(numtree_number idx, QPDFObjectHandle& oh, numtree_number& offset); + class iterator: public std::iterator< + std::bidirectional_iterator_tag, + std::pair, + void, + std::pair*, + std::pair> + { + friend class QPDFNumberTreeObjectHelper; + public: + QPDF_DLL + bool valid() const; + QPDF_DLL + iterator& operator++(); + QPDF_DLL + iterator operator++(int) + { + iterator t = *this; + ++(*this); + return t; + } + QPDF_DLL + iterator& operator--(); + QPDF_DLL + iterator operator--(int) + { + iterator t = *this; + --(*this); + return t; + } + QPDF_DLL + reference operator*(); + QPDF_DLL + bool operator==(iterator const& other) const; + QPDF_DLL + bool operator!=(iterator const& other) const + { + return ! operator==(other); + } + + private: + iterator(std::shared_ptr const&); + std::shared_ptr impl; + }; + + // The iterator looks like map iterator, so i.first is a string + // and i.second is a QPDFObjectHandle. + QPDF_DLL + iterator begin() const; + QPDF_DLL + iterator end() const; + // Return a bidirectional iterator that points to the last item. + QPDF_DLL + iterator last() const; + + // Find the entry with the given key. If return_prev_if_not_found + // is true and the item is not found, return the next lower item. + QPDF_DLL + iterator find(numtree_number key, bool return_prev_if_not_found = false); + // Return the contents of the number tree as a map. Note that // number trees may be very large, so this may use a lot of RAM. // It is more efficient to use QPDFNumberTreeObjectHelper's -- cgit v1.2.3-54-g00ecf