2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-12-23 11:28:56 +00:00
qpdf/libqpdf/QPDFNameTreeObjectHelper.cc

203 lines
4.8 KiB
C++
Raw Normal View History

2018-12-19 02:56:56 +00:00
#include <qpdf/QPDFNameTreeObjectHelper.hh>
#include <qpdf/NNTree.hh>
namespace
{
class NameTreeDetails: public NNTreeDetails
{
public:
2023-05-20 12:56:33 +00:00
std::string const&
itemsKey() const override
{
static std::string k("/Names");
return k;
}
2023-05-20 12:56:33 +00:00
bool
keyValid(QPDFObjectHandle oh) const override
{
return oh.isString();
}
2023-05-20 12:56:33 +00:00
int
compareKeys(QPDFObjectHandle a, QPDFObjectHandle b) const override
{
if (!(keyValid(a) && keyValid(b))) {
// We don't call this without calling keyValid first
throw std::logic_error("comparing invalid keys");
}
auto as = a.getUTF8Value();
auto bs = b.getUTF8Value();
return ((as < bs) ? -1 : (as > bs) ? 1 : 0);
}
};
} // namespace
static NameTreeDetails name_tree_details;
2018-12-19 02:56:56 +00:00
QPDFNameTreeObjectHelper::~QPDFNameTreeObjectHelper() // NOLINT (modernize-use-equals-default)
{
// Must be explicit and not inline -- see QPDF_DLL_CLASS in README-maintainer. For this specific
// class, see github issue #745.
}
2023-05-21 17:35:09 +00:00
QPDFNameTreeObjectHelper::Members::Members(QPDFObjectHandle& oh, QPDF& q, bool auto_repair) :
impl(std::make_shared<NNTreeImpl>(name_tree_details, q, oh, auto_repair))
{
}
2023-05-21 17:35:09 +00:00
QPDFNameTreeObjectHelper::QPDFNameTreeObjectHelper(QPDFObjectHandle oh, QPDF& q, bool auto_repair) :
QPDFObjectHelper(oh),
m(new Members(oh, q, auto_repair))
2018-12-19 02:56:56 +00:00
{
}
QPDFNameTreeObjectHelper
QPDFNameTreeObjectHelper::newEmpty(QPDF& qpdf, bool auto_repair)
{
return {qpdf.makeIndirectObject("<< /Names [] >>"_qpdf), qpdf, auto_repair};
}
2023-05-21 17:35:09 +00:00
QPDFNameTreeObjectHelper::iterator::iterator(std::shared_ptr<NNTreeIterator> const& i) :
impl(i)
{
}
bool
QPDFNameTreeObjectHelper::iterator::valid() const
{
return impl->valid();
}
QPDFNameTreeObjectHelper::iterator&
QPDFNameTreeObjectHelper::iterator::operator++()
{
++(*impl);
updateIValue();
return *this;
}
QPDFNameTreeObjectHelper::iterator&
QPDFNameTreeObjectHelper::iterator::operator--()
{
--(*impl);
updateIValue();
return *this;
}
void
QPDFNameTreeObjectHelper::iterator::updateIValue()
{
if (impl->valid()) {
auto p = *impl;
this->ivalue.first = p->first.getUTF8Value();
this->ivalue.second = p->second;
} else {
this->ivalue.first = "";
this->ivalue.second = QPDFObjectHandle();
}
}
QPDFNameTreeObjectHelper::iterator::reference
QPDFNameTreeObjectHelper::iterator::operator*()
{
updateIValue();
return this->ivalue;
}
QPDFNameTreeObjectHelper::iterator::pointer
QPDFNameTreeObjectHelper::iterator::operator->()
{
updateIValue();
return &this->ivalue;
}
bool
QPDFNameTreeObjectHelper::iterator::operator==(iterator const& other) const
{
return *(impl) == *(other.impl);
}
2021-01-24 09:16:48 +00:00
void
2023-05-21 17:35:09 +00:00
QPDFNameTreeObjectHelper::iterator::insertAfter(std::string const& key, QPDFObjectHandle value)
2021-01-24 09:16:48 +00:00
{
impl->insertAfter(QPDFObjectHandle::newUnicodeString(key), value);
updateIValue();
2021-01-24 09:16:48 +00:00
}
2021-01-24 16:48:46 +00:00
void
QPDFNameTreeObjectHelper::iterator::remove()
{
impl->remove();
updateIValue();
2021-01-24 16:48:46 +00:00
}
QPDFNameTreeObjectHelper::iterator
QPDFNameTreeObjectHelper::begin() const
{
return {std::make_shared<NNTreeIterator>(m->impl->begin())};
}
QPDFNameTreeObjectHelper::iterator
QPDFNameTreeObjectHelper::end() const
{
return {std::make_shared<NNTreeIterator>(m->impl->end())};
}
QPDFNameTreeObjectHelper::iterator
QPDFNameTreeObjectHelper::last() const
{
return {std::make_shared<NNTreeIterator>(m->impl->last())};
}
QPDFNameTreeObjectHelper::iterator
2023-05-21 17:35:09 +00:00
QPDFNameTreeObjectHelper::find(std::string const& key, bool return_prev_if_not_found)
{
2023-05-21 17:35:09 +00:00
auto i = m->impl->find(QPDFObjectHandle::newUnicodeString(key), return_prev_if_not_found);
return {std::make_shared<NNTreeIterator>(i)};
}
QPDFNameTreeObjectHelper::iterator
QPDFNameTreeObjectHelper::insert(std::string const& key, QPDFObjectHandle value)
{
auto i = m->impl->insert(QPDFObjectHandle::newUnicodeString(key), value);
return {std::make_shared<NNTreeIterator>(i)};
}
2021-01-24 16:48:46 +00:00
bool
2023-05-21 17:35:09 +00:00
QPDFNameTreeObjectHelper::remove(std::string const& key, QPDFObjectHandle* value)
2021-01-24 16:48:46 +00:00
{
return m->impl->remove(QPDFObjectHandle::newUnicodeString(key), value);
2021-01-24 16:48:46 +00:00
}
2018-12-19 02:56:56 +00:00
bool
QPDFNameTreeObjectHelper::hasName(std::string const& name)
{
auto i = find(name);
return (i != end());
2018-12-19 02:56:56 +00:00
}
bool
2023-05-21 17:35:09 +00:00
QPDFNameTreeObjectHelper::findObject(std::string const& name, QPDFObjectHandle& oh)
2018-12-19 02:56:56 +00:00
{
auto i = find(name);
if (i == end()) {
2018-12-19 02:56:56 +00:00
return false;
}
oh = i->second;
2018-12-19 02:56:56 +00:00
return true;
}
void
QPDFNameTreeObjectHelper::setSplitThreshold(int t)
{
m->impl->setSplitThreshold(t);
}
2018-12-19 02:56:56 +00:00
std::map<std::string, QPDFObjectHandle>
QPDFNameTreeObjectHelper::getAsMap() const
{
std::map<std::string, QPDFObjectHandle> result;
result.insert(begin(), end());
return result;
2018-12-19 02:56:56 +00:00
}