2018-12-19 02:56:56 +00:00
|
|
|
#include <qpdf/QPDFNameTreeObjectHelper.hh>
|
2022-02-04 21:31:31 +00:00
|
|
|
|
2021-01-16 13:31:56 +00:00
|
|
|
#include <qpdf/NNTree.hh>
|
|
|
|
|
2022-04-16 17:21:57 +00:00
|
|
|
namespace
|
2021-01-16 13:31:56 +00:00
|
|
|
{
|
2022-04-16 17:21:57 +00:00
|
|
|
class NameTreeDetails: public NNTreeDetails
|
2021-01-16 13:31:56 +00:00
|
|
|
{
|
2022-04-16 17:21:57 +00:00
|
|
|
public:
|
2023-05-20 12:56:33 +00:00
|
|
|
std::string const&
|
2022-04-16 17:21:57 +00:00
|
|
|
itemsKey() const override
|
|
|
|
{
|
|
|
|
static std::string k("/Names");
|
|
|
|
return k;
|
|
|
|
}
|
2023-05-20 12:56:33 +00:00
|
|
|
bool
|
2022-04-16 17:21:57 +00:00
|
|
|
keyValid(QPDFObjectHandle oh) const override
|
|
|
|
{
|
|
|
|
return oh.isString();
|
|
|
|
}
|
2023-05-20 12:56:33 +00:00
|
|
|
int
|
2022-04-16 17:21:57 +00:00
|
|
|
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);
|
2021-01-16 13:31:56 +00:00
|
|
|
}
|
2022-04-16 17:21:57 +00:00
|
|
|
};
|
|
|
|
} // namespace
|
2021-01-16 13:31:56 +00:00
|
|
|
|
|
|
|
static NameTreeDetails name_tree_details;
|
2018-12-19 02:56:56 +00:00
|
|
|
|
2023-06-01 13:47:36 +00:00
|
|
|
QPDFNameTreeObjectHelper::~QPDFNameTreeObjectHelper() // NOLINT (modernize-use-equals-default)
|
2022-08-07 13:09:04 +00:00
|
|
|
{
|
|
|
|
// Must be explicit and not inline -- see QPDF_DLL_CLASS in README-maintainer. For this specific
|
|
|
|
// class, see github issue #745.
|
|
|
|
}
|
|
|
|
|
2022-04-16 17:12:49 +00:00
|
|
|
QPDFNameTreeObjectHelper::Members::Members(QPDFObjectHandle& oh, QPDF& q, bool auto_repair) :
|
2021-01-16 23:35:30 +00:00
|
|
|
impl(std::make_shared<NNTreeImpl>(name_tree_details, q, oh, auto_repair))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFNameTreeObjectHelper::QPDFNameTreeObjectHelper(QPDFObjectHandle oh, QPDF& q, bool auto_repair) :
|
|
|
|
QPDFObjectHelper(oh),
|
2022-04-16 17:12:49 +00:00
|
|
|
m(new Members(oh, q, auto_repair))
|
2018-12-19 02:56:56 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-01-24 08:55:18 +00:00
|
|
|
QPDFNameTreeObjectHelper
|
|
|
|
QPDFNameTreeObjectHelper::newEmpty(QPDF& qpdf, bool auto_repair)
|
|
|
|
{
|
2023-05-27 20:04:32 +00:00
|
|
|
return {qpdf.makeIndirectObject("<< /Names [] >>"_qpdf), qpdf, auto_repair};
|
2021-01-24 08:55:18 +00:00
|
|
|
}
|
|
|
|
|
2021-01-16 17:11:17 +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);
|
2021-01-25 13:05:43 +00:00
|
|
|
updateIValue();
|
2021-01-16 17:11:17 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFNameTreeObjectHelper::iterator&
|
|
|
|
QPDFNameTreeObjectHelper::iterator::operator--()
|
|
|
|
{
|
|
|
|
--(*impl);
|
2021-01-25 13:05:43 +00:00
|
|
|
updateIValue();
|
2021-01-16 17:11:17 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-01-25 13:05:43 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 17:11:17 +00:00
|
|
|
QPDFNameTreeObjectHelper::iterator::reference
|
|
|
|
QPDFNameTreeObjectHelper::iterator::operator*()
|
|
|
|
{
|
2021-01-25 13:05:43 +00:00
|
|
|
updateIValue();
|
|
|
|
return this->ivalue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFNameTreeObjectHelper::iterator::pointer
|
|
|
|
QPDFNameTreeObjectHelper::iterator::operator->()
|
|
|
|
{
|
|
|
|
updateIValue();
|
|
|
|
return &this->ivalue;
|
2021-01-16 17:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDFNameTreeObjectHelper::iterator::operator==(iterator const& other) const
|
|
|
|
{
|
|
|
|
return *(impl) == *(other.impl);
|
|
|
|
}
|
|
|
|
|
2021-01-24 09:16:48 +00:00
|
|
|
void
|
|
|
|
QPDFNameTreeObjectHelper::iterator::insertAfter(std::string const& key, QPDFObjectHandle value)
|
|
|
|
{
|
|
|
|
impl->insertAfter(QPDFObjectHandle::newUnicodeString(key), value);
|
2021-01-25 13:05:43 +00:00
|
|
|
updateIValue();
|
2021-01-24 09:16:48 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 16:48:46 +00:00
|
|
|
void
|
|
|
|
QPDFNameTreeObjectHelper::iterator::remove()
|
|
|
|
{
|
|
|
|
impl->remove();
|
2021-01-25 13:05:43 +00:00
|
|
|
updateIValue();
|
2021-01-24 16:48:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-16 17:11:17 +00:00
|
|
|
QPDFNameTreeObjectHelper::iterator
|
|
|
|
QPDFNameTreeObjectHelper::begin() const
|
|
|
|
{
|
2023-05-27 20:04:32 +00:00
|
|
|
return {std::make_shared<NNTreeIterator>(m->impl->begin())};
|
2021-01-16 17:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDFNameTreeObjectHelper::iterator
|
|
|
|
QPDFNameTreeObjectHelper::end() const
|
|
|
|
{
|
2023-05-27 20:04:32 +00:00
|
|
|
return {std::make_shared<NNTreeIterator>(m->impl->end())};
|
2021-01-16 17:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDFNameTreeObjectHelper::iterator
|
|
|
|
QPDFNameTreeObjectHelper::last() const
|
|
|
|
{
|
2023-05-27 20:04:32 +00:00
|
|
|
return {std::make_shared<NNTreeIterator>(m->impl->last())};
|
2021-01-16 17:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDFNameTreeObjectHelper::iterator
|
|
|
|
QPDFNameTreeObjectHelper::find(std::string const& key, bool return_prev_if_not_found)
|
|
|
|
{
|
|
|
|
auto i = m->impl->find(QPDFObjectHandle::newUnicodeString(key), return_prev_if_not_found);
|
2023-05-27 20:04:32 +00:00
|
|
|
return {std::make_shared<NNTreeIterator>(i)};
|
2021-01-16 17:11:17 +00:00
|
|
|
}
|
|
|
|
|
2021-01-23 23:33:55 +00:00
|
|
|
QPDFNameTreeObjectHelper::iterator
|
|
|
|
QPDFNameTreeObjectHelper::insert(std::string const& key, QPDFObjectHandle value)
|
|
|
|
{
|
|
|
|
auto i = m->impl->insert(QPDFObjectHandle::newUnicodeString(key), value);
|
2023-05-27 20:04:32 +00:00
|
|
|
return {std::make_shared<NNTreeIterator>(i)};
|
2021-01-23 23:33:55 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 16:48:46 +00:00
|
|
|
bool
|
|
|
|
QPDFNameTreeObjectHelper::remove(std::string const& key, QPDFObjectHandle* value)
|
|
|
|
{
|
|
|
|
return m->impl->remove(QPDFObjectHandle::newUnicodeString(key), value);
|
|
|
|
}
|
|
|
|
|
2018-12-19 02:56:56 +00:00
|
|
|
bool
|
|
|
|
QPDFNameTreeObjectHelper::hasName(std::string const& name)
|
|
|
|
{
|
2021-01-16 17:11:17 +00:00
|
|
|
auto i = find(name);
|
|
|
|
return (i != end());
|
2018-12-19 02:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDFNameTreeObjectHelper::findObject(std::string const& name, QPDFObjectHandle& oh)
|
|
|
|
{
|
2021-01-16 17:11:17 +00:00
|
|
|
auto i = find(name);
|
|
|
|
if (i == end()) {
|
2018-12-19 02:56:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-01-25 13:05:43 +00:00
|
|
|
oh = i->second;
|
2018-12-19 02:56:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-23 23:33:55 +00:00
|
|
|
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
|
|
|
|
{
|
2021-01-16 13:31:56 +00:00
|
|
|
std::map<std::string, QPDFObjectHandle> result;
|
2021-01-16 17:11:17 +00:00
|
|
|
result.insert(begin(), end());
|
2021-01-16 13:31:56 +00:00
|
|
|
return result;
|
2018-12-19 02:56:56 +00:00
|
|
|
}
|