2018-12-18 18:08:55 +00:00
|
|
|
#include <qpdf/QPDFNumberTreeObjectHelper.hh>
|
2021-01-16 13:31:56 +00:00
|
|
|
#include <qpdf/NNTree.hh>
|
2018-12-18 18:08:55 +00:00
|
|
|
|
2021-01-16 13:31:56 +00:00
|
|
|
class NumberTreeDetails: public NNTreeDetails
|
2018-12-18 18:08:55 +00:00
|
|
|
{
|
2021-01-16 13:31:56 +00:00
|
|
|
public:
|
|
|
|
virtual std::string const& itemsKey() const override
|
2018-12-18 18:08:55 +00:00
|
|
|
{
|
2021-01-16 13:31:56 +00:00
|
|
|
static std::string k("/Nums");
|
|
|
|
return k;
|
2018-12-18 18:08:55 +00:00
|
|
|
}
|
2021-01-16 13:31:56 +00:00
|
|
|
virtual bool keyValid(QPDFObjectHandle oh) const override
|
2018-12-18 18:08:55 +00:00
|
|
|
{
|
2021-01-16 13:31:56 +00:00
|
|
|
return oh.isInteger();
|
2018-12-18 18:08:55 +00:00
|
|
|
}
|
2021-01-16 13:31:56 +00:00
|
|
|
virtual int compareKeys(
|
|
|
|
QPDFObjectHandle a, QPDFObjectHandle b) const override
|
2018-12-18 18:08:55 +00:00
|
|
|
{
|
2021-01-16 13:31:56 +00:00
|
|
|
if (! (keyValid(a) && keyValid(b)))
|
2018-12-18 18:08:55 +00:00
|
|
|
{
|
2021-01-16 13:31:56 +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
|
|
|
}
|
2021-01-16 13:31:56 +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
|
|
|
}
|
2021-01-16 13:31:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static NumberTreeDetails number_tree_details;
|
|
|
|
|
|
|
|
QPDFNumberTreeObjectHelper::Members::~Members()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFNumberTreeObjectHelper::Members::Members(QPDFObjectHandle& oh) :
|
|
|
|
impl(std::make_shared<NNTreeImpl>(number_tree_details, nullptr, oh, false))
|
|
|
|
{
|
2018-12-18 18:08:55 +00:00
|
|
|
}
|
|
|
|
|
2021-01-16 13:31:56 +00:00
|
|
|
QPDFNumberTreeObjectHelper::QPDFNumberTreeObjectHelper(QPDFObjectHandle oh) :
|
|
|
|
QPDFObjectHelper(oh),
|
|
|
|
m(new Members(oh))
|
|
|
|
{
|
|
|
|
}
|
2018-12-18 18:08:55 +00:00
|
|
|
|
|
|
|
QPDFNumberTreeObjectHelper::numtree_number
|
|
|
|
QPDFNumberTreeObjectHelper::getMin()
|
|
|
|
{
|
2021-01-16 13:31:56 +00:00
|
|
|
auto i = this->m->impl->begin();
|
|
|
|
if (i == this->m->impl->end())
|
2018-12-18 18:08:55 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2021-01-16 13:31:56 +00:00
|
|
|
return (*i).first.getIntValue();
|
2018-12-18 18:08:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDFNumberTreeObjectHelper::numtree_number
|
|
|
|
QPDFNumberTreeObjectHelper::getMax()
|
|
|
|
{
|
2021-01-16 13:31:56 +00:00
|
|
|
auto i = this->m->impl->last();
|
|
|
|
if (i == this->m->impl->end())
|
2018-12-18 18:08:55 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2021-01-16 13:31:56 +00:00
|
|
|
return (*i).first.getIntValue();
|
2018-12-18 18:08:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDFNumberTreeObjectHelper::hasIndex(numtree_number idx)
|
|
|
|
{
|
2021-01-16 13:31:56 +00:00
|
|
|
auto i = this->m->impl->find(QPDFObjectHandle::newInteger(idx));
|
|
|
|
return (i != this->m->impl->end());
|
2018-12-18 18:08:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDFNumberTreeObjectHelper::findObject(
|
|
|
|
numtree_number idx, QPDFObjectHandle& oh)
|
|
|
|
{
|
2021-01-16 13:31:56 +00:00
|
|
|
auto i = this->m->impl->find(QPDFObjectHandle::newInteger(idx));
|
|
|
|
if (i == this->m->impl->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)
|
|
|
|
{
|
2021-01-16 13:31:56 +00:00
|
|
|
auto i = this->m->impl->find(QPDFObjectHandle::newInteger(idx), true);
|
|
|
|
if (i == this->m->impl->end())
|
2018-12-18 18:08:55 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
oh = (*i).second;
|
2021-01-16 13:31:56 +00:00
|
|
|
offset = idx - (*i).first.getIntValue();
|
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;
|
2021-01-16 13:31:56 +00:00
|
|
|
for (auto i: *(this->m->impl))
|
2018-12-18 18:08:55 +00:00
|
|
|
{
|
2021-01-16 13:31:56 +00:00
|
|
|
result.insert(
|
|
|
|
std::make_pair(i.first.getIntValue(),
|
|
|
|
i.second));
|
2018-12-18 18:08:55 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|