summaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2021-01-16 18:11:17 +0100
committerJay Berkenbilt <ejb@ql.org>2021-01-24 09:22:59 +0100
commit5f0708418a0a9cbacc033f52efc11c759120fd09 (patch)
tree272e5d97b37b6c6d65653ad04d8dc768ef18041d /include
parent4a1cce0a470e6deed6dbeb6093e4e5c16f53439d (diff)
downloadqpdf-5f0708418a0a9cbacc033f52efc11c759120fd09.tar.zst
Add iterators to name/number tree helpers
Diffstat (limited to 'include')
-rw-r--r--include/qpdf/QPDFNameTreeObjectHelper.hh63
-rw-r--r--include/qpdf/QPDFNumberTreeObjectHelper.hh61
2 files changed, 124 insertions, 0 deletions
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 <qpdf/QPDFObjGen.hh>
#include <map>
#include <memory>
+#include <iterator>
#include <qpdf/DLL.h>
@@ -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<std::string, QPDFObjectHandle>,
+ void,
+ std::pair<std::string, QPDFObjectHandle>*,
+ std::pair<std::string, QPDFObjectHandle>>
+ {
+ 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<NNTreeIterator> const&);
+ std::shared_ptr<NNTreeIterator> 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<numtree_number, QPDFObjectHandle>,
+ void,
+ std::pair<numtree_number, QPDFObjectHandle>*,
+ std::pair<numtree_number, QPDFObjectHandle>>
+ {
+ 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<NNTreeIterator> const&);
+ std::shared_ptr<NNTreeIterator> 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