2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-11-02 11:46:35 +00:00
qpdf/libqpdf/QPDFNumberTreeObjectHelper.cc

185 lines
4.1 KiB
C++
Raw Normal View History

2018-12-18 18:08:55 +00:00
#include <qpdf/QPDFNumberTreeObjectHelper.hh>
#include <qpdf/NNTree.hh>
2018-12-18 18:08:55 +00:00
class NumberTreeDetails: public NNTreeDetails
2018-12-18 18:08:55 +00:00
{
public:
virtual std::string const& itemsKey() const override
2018-12-18 18:08:55 +00:00
{
static std::string k("/Nums");
return k;
2018-12-18 18:08:55 +00:00
}
virtual bool keyValid(QPDFObjectHandle oh) const override
2018-12-18 18:08:55 +00:00
{
return oh.isInteger();
2018-12-18 18:08:55 +00:00
}
virtual int compareKeys(
QPDFObjectHandle a, QPDFObjectHandle b) const override
2018-12-18 18:08:55 +00:00
{
if (! (keyValid(a) && keyValid(b)))
2018-12-18 18:08:55 +00:00
{
// We don't call this without calling keyValid first
throw std::logic_error("comparing invalid keys");
2018-12-18 18:08:55 +00:00
}
auto as = a.getIntValue();
auto bs = b.getIntValue();
return ((as < bs) ? -1 : (as > bs) ? 1 : 0);
2018-12-18 18:08:55 +00:00
}
};
static NumberTreeDetails number_tree_details;
QPDFNumberTreeObjectHelper::Members::~Members()
{
}
QPDFNumberTreeObjectHelper::Members::Members(
QPDFObjectHandle& oh, QPDF* q, bool auto_repair) :
impl(std::make_shared<NNTreeImpl>(number_tree_details, q, oh, auto_repair))
{
}
QPDFNumberTreeObjectHelper::QPDFNumberTreeObjectHelper(
QPDFObjectHandle oh, QPDF& q, bool auto_repair) :
QPDFObjectHelper(oh),
m(new Members(oh, &q, auto_repair))
{
2018-12-18 18:08:55 +00:00
}
QPDFNumberTreeObjectHelper::QPDFNumberTreeObjectHelper(QPDFObjectHandle oh) :
QPDFObjectHelper(oh),
m(new Members(oh, nullptr, false))
{
}
2018-12-18 18:08:55 +00:00
QPDFNumberTreeObjectHelper::iterator::iterator(
std::shared_ptr<NNTreeIterator> const& i) :
impl(i)
{
}
bool
QPDFNumberTreeObjectHelper::iterator::valid() const
{
return impl->valid();
}
QPDFNumberTreeObjectHelper::iterator&
QPDFNumberTreeObjectHelper::iterator::operator++()
{
++(*impl);
return *this;
}
QPDFNumberTreeObjectHelper::iterator&
QPDFNumberTreeObjectHelper::iterator::operator--()
{
--(*impl);
return *this;
}
QPDFNumberTreeObjectHelper::iterator::reference
QPDFNumberTreeObjectHelper::iterator::operator*()
{
auto p = **impl;
return std::make_pair(p.first.getIntValue(), p.second);
}
bool
QPDFNumberTreeObjectHelper::iterator::operator==(iterator const& other) const
{
return *(impl) == *(other.impl);
}
QPDFNumberTreeObjectHelper::iterator
QPDFNumberTreeObjectHelper::begin() const
{
return iterator(std::make_shared<NNTreeIterator>(this->m->impl->begin()));
}
QPDFNumberTreeObjectHelper::iterator
QPDFNumberTreeObjectHelper::end() const
{
return iterator(std::make_shared<NNTreeIterator>(this->m->impl->end()));
}
QPDFNumberTreeObjectHelper::iterator
QPDFNumberTreeObjectHelper::last() const
{
return iterator(std::make_shared<NNTreeIterator>(this->m->impl->last()));
}
QPDFNumberTreeObjectHelper::iterator
QPDFNumberTreeObjectHelper::find(numtree_number key,
bool return_prev_if_not_found)
{
auto i = this->m->impl->find(QPDFObjectHandle::newInteger(key),
return_prev_if_not_found);
return iterator(std::make_shared<NNTreeIterator>(i));
}
2018-12-18 18:08:55 +00:00
QPDFNumberTreeObjectHelper::numtree_number
QPDFNumberTreeObjectHelper::getMin()
{
auto i = begin();
if (i == end())
2018-12-18 18:08:55 +00:00
{
return 0;
}
return (*i).first;
2018-12-18 18:08:55 +00:00
}
QPDFNumberTreeObjectHelper::numtree_number
QPDFNumberTreeObjectHelper::getMax()
{
auto i = last();
if (i == end())
2018-12-18 18:08:55 +00:00
{
return 0;
}
return (*i).first;
2018-12-18 18:08:55 +00:00
}
bool
QPDFNumberTreeObjectHelper::hasIndex(numtree_number idx)
{
auto i = find(idx);
return (i != this->end());
2018-12-18 18:08:55 +00:00
}
bool
QPDFNumberTreeObjectHelper::findObject(
numtree_number idx, QPDFObjectHandle& oh)
{
auto i = find(idx);
if (i == end())
2018-12-18 18:08:55 +00:00
{
return false;
}
oh = (*i).second;
return true;
}
bool
QPDFNumberTreeObjectHelper::findObjectAtOrBelow(
numtree_number idx, QPDFObjectHandle& oh,
numtree_number& offset)
{
auto i = find(idx, true);
if (i == end())
2018-12-18 18:08:55 +00:00
{
return false;
}
oh = (*i).second;
offset = idx - (*i).first;
2018-12-18 18:08:55 +00:00
return true;
}
std::map<QPDFNumberTreeObjectHelper::numtree_number, QPDFObjectHandle>
QPDFNumberTreeObjectHelper::getAsMap() const
{
std::map<numtree_number, QPDFObjectHandle> result;
result.insert(begin(), end());
2018-12-18 18:08:55 +00:00
return result;
}