From a03c6863ae4e008275cea9074b9177df7a83b665 Mon Sep 17 00:00:00 2001 From: m-holger Date: Thu, 13 Feb 2025 18:37:49 +0000 Subject: [PATCH] Move QPDFObjectHandle dictionary methods implementation to QPDF_Dictionary.cc --- libqpdf/QPDFObjectHandle.cc | 119 ++--------------------------------------------------------------------------------------------------------------------- libqpdf/QPDF_Dictionary.cc | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 117 deletions(-) diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index 961c3c2..f96301f 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -974,7 +974,7 @@ QPDFObjectHandle::eraseItemAndGetOld(int at) return result; } -// Dictionary accessors +// Dictionary accessors are in QPDF_Dictionary.cc QPDFObjectHandle::QPDFDictItems QPDFObjectHandle::ditems() @@ -982,59 +982,6 @@ QPDFObjectHandle::ditems() return {*this}; } -bool -QPDFObjectHandle::hasKey(std::string const& key) const -{ - auto dict = as_dictionary(strict); - if (dict) { - return dict.hasKey(key); - } else { - typeWarning("dictionary", "returning false for a key containment request"); - QTC::TC("qpdf", "QPDFObjectHandle dictionary false for hasKey"); - return false; - } -} - -QPDFObjectHandle -QPDFObjectHandle::getKey(std::string const& key) const -{ - if (auto dict = as_dictionary(strict)) { - return dict.getKey(key); - } - typeWarning("dictionary", "returning null for attempted key retrieval"); - QTC::TC("qpdf", "QPDFObjectHandle dictionary null for getKey"); - static auto constexpr msg = " -> null returned from getting key $VD from non-Dictionary"sv; - return QPDF_Null::create(obj, msg, ""); -} - -QPDFObjectHandle -QPDFObjectHandle::getKeyIfDict(std::string const& key) const -{ - return isNull() ? newNull() : getKey(key); -} - -std::set -QPDFObjectHandle::getKeys() const -{ - if (auto dict = as_dictionary(strict)) { - return dict.getKeys(); - } - typeWarning("dictionary", "treating as empty"); - QTC::TC("qpdf", "QPDFObjectHandle dictionary empty set for getKeys"); - return {}; -} - -std::map -QPDFObjectHandle::getDictAsMap() const -{ - if (auto dict = as_dictionary(strict)) { - return dict.getAsMap(); - } - typeWarning("dictionary", "treating as empty"); - QTC::TC("qpdf", "QPDFObjectHandle dictionary empty map for asMap"); - return {}; -} - // Array and Name accessors bool @@ -1208,56 +1155,7 @@ QPDFObjectHandle::getUniqueResourceName( " QPDFObjectHandle::getUniqueResourceName"); } -// Dictionary mutators - -void -QPDFObjectHandle::replaceKey(std::string const& key, QPDFObjectHandle const& value) -{ - if (auto dict = as_dictionary(strict)) { - checkOwnership(value); - dict.replaceKey(key, value); - return; - } - typeWarning("dictionary", "ignoring key replacement request"); - QTC::TC("qpdf", "QPDFObjectHandle dictionary ignoring replaceKey"); -} - -QPDFObjectHandle -QPDFObjectHandle::replaceKeyAndGetNew(std::string const& key, QPDFObjectHandle const& value) -{ - replaceKey(key, value); - return value; -} - -QPDFObjectHandle -QPDFObjectHandle::replaceKeyAndGetOld(std::string const& key, QPDFObjectHandle const& value) -{ - QPDFObjectHandle old = removeKeyAndGetOld(key); - replaceKey(key, value); - return old; -} - -void -QPDFObjectHandle::removeKey(std::string const& key) -{ - if (auto dict = as_dictionary(strict)) { - dict.removeKey(key); - return; - } - typeWarning("dictionary", "ignoring key removal request"); - QTC::TC("qpdf", "QPDFObjectHandle dictionary ignoring removeKey"); -} - -QPDFObjectHandle -QPDFObjectHandle::removeKeyAndGetOld(std::string const& key) -{ - auto result = QPDFObjectHandle::newNull(); - if (auto dict = as_dictionary(strict)) { - result = dict.getKey(key); - } - removeKey(key); - return result; -} +// Dictionary mutators are in QPDF_Dictionary.cc // Stream accessors @@ -2346,19 +2244,6 @@ QPDFObjectHandle::isImage(bool exclude_imagemask) const } void -QPDFObjectHandle::checkOwnership(QPDFObjectHandle const& item) const -{ - auto qpdf = getOwningQPDF(); - auto item_qpdf = item.getOwningQPDF(); - if ((qpdf != nullptr) && (item_qpdf != nullptr) && (qpdf != item_qpdf)) { - QTC::TC("qpdf", "QPDFObjectHandle check ownership"); - throw std::logic_error( - "Attempting to add an object from a different QPDF. Use " - "QPDF::copyForeignObject to add objects from another file."); - } -} - -void QPDFObjectHandle::assertPageObject() const { if (!isPageObject()) { diff --git a/libqpdf/QPDF_Dictionary.cc b/libqpdf/QPDF_Dictionary.cc index 978e572..9b5422a 100644 --- a/libqpdf/QPDF_Dictionary.cc +++ b/libqpdf/QPDF_Dictionary.cc @@ -169,3 +169,118 @@ BaseDictionary::replaceKey(std::string const& key, QPDFObjectHandle value) d->items[key] = value; } } + +void +QPDFObjectHandle::checkOwnership(QPDFObjectHandle const& item) const +{ + auto qpdf = getOwningQPDF(); + auto item_qpdf = item.getOwningQPDF(); + if (qpdf && item_qpdf && qpdf != item_qpdf) { + QTC::TC("qpdf", "QPDFObjectHandle check ownership"); + throw std::logic_error( + "Attempting to add an object from a different QPDF. Use " + "QPDF::copyForeignObject to add objects from another file."); + } +} + +bool +QPDFObjectHandle::hasKey(std::string const& key) const +{ + auto dict = as_dictionary(strict); + if (dict) { + return dict.hasKey(key); + } else { + typeWarning("dictionary", "returning false for a key containment request"); + QTC::TC("qpdf", "QPDFObjectHandle dictionary false for hasKey"); + return false; + } +} + +QPDFObjectHandle +QPDFObjectHandle::getKey(std::string const& key) const +{ + if (auto dict = as_dictionary(strict)) { + return dict.getKey(key); + } + typeWarning("dictionary", "returning null for attempted key retrieval"); + QTC::TC("qpdf", "QPDFObjectHandle dictionary null for getKey"); + static auto constexpr msg = " -> null returned from getting key $VD from non-Dictionary"sv; + return QPDF_Null::create(obj, msg, ""); +} + +QPDFObjectHandle +QPDFObjectHandle::getKeyIfDict(std::string const& key) const +{ + return isNull() ? newNull() : getKey(key); +} + +std::set +QPDFObjectHandle::getKeys() const +{ + if (auto dict = as_dictionary(strict)) { + return dict.getKeys(); + } + typeWarning("dictionary", "treating as empty"); + QTC::TC("qpdf", "QPDFObjectHandle dictionary empty set for getKeys"); + return {}; +} + +std::map +QPDFObjectHandle::getDictAsMap() const +{ + if (auto dict = as_dictionary(strict)) { + return dict.getAsMap(); + } + typeWarning("dictionary", "treating as empty"); + QTC::TC("qpdf", "QPDFObjectHandle dictionary empty map for asMap"); + return {}; +} + +void +QPDFObjectHandle::replaceKey(std::string const& key, QPDFObjectHandle const& value) +{ + if (auto dict = as_dictionary(strict)) { + checkOwnership(value); + dict.replaceKey(key, value); + return; + } + typeWarning("dictionary", "ignoring key replacement request"); + QTC::TC("qpdf", "QPDFObjectHandle dictionary ignoring replaceKey"); +} + +QPDFObjectHandle +QPDFObjectHandle::replaceKeyAndGetNew(std::string const& key, QPDFObjectHandle const& value) +{ + replaceKey(key, value); + return value; +} + +QPDFObjectHandle +QPDFObjectHandle::replaceKeyAndGetOld(std::string const& key, QPDFObjectHandle const& value) +{ + QPDFObjectHandle old = removeKeyAndGetOld(key); + replaceKey(key, value); + return old; +} + +void +QPDFObjectHandle::removeKey(std::string const& key) +{ + if (auto dict = as_dictionary(strict)) { + dict.removeKey(key); + return; + } + typeWarning("dictionary", "ignoring key removal request"); + QTC::TC("qpdf", "QPDFObjectHandle dictionary ignoring removeKey"); +} + +QPDFObjectHandle +QPDFObjectHandle::removeKeyAndGetOld(std::string const& key) +{ + auto result = QPDFObjectHandle::newNull(); + if (auto dict = as_dictionary(strict)) { + result = dict.getKey(key); + } + removeKey(key); + return result; +} -- libgit2 0.21.4