From 32907fc14c663e95df0f7c62905b82389c0024a2 Mon Sep 17 00:00:00 2001 From: m-holger Date: Wed, 15 Feb 2023 10:11:38 +0000 Subject: Change type of QPDFValue::object_description to std::shared_ptr Also, name the type QPDFValue::Description. --- libqpdf/QPDFObjectHandle.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'libqpdf/QPDFObjectHandle.cc') diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index 2b2ca5db..7a37ffc2 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -2176,7 +2176,8 @@ QPDFObjectHandle::setObjectDescription( // This is called during parsing on newly created direct objects, // so we can't call dereference() here. if (isInitialized() && obj.get()) { - auto descr = std::make_shared(object_description); + auto descr = + std::make_shared(object_description); obj->setDescription(owning_qpdf, descr); } } -- cgit v1.2.3-54-g00ecf From 1496472e1c2f64f46d2d7d76481aef1aa3fff869 Mon Sep 17 00:00:00 2001 From: m-holger Date: Fri, 17 Feb 2023 13:58:21 +0000 Subject: Add method QPDFValue::setChildDescription --- libqpdf/QPDFObjectHandle.cc | 23 ++++++++--------------- libqpdf/QPDFValue.cc | 16 ++++++++++++++++ libqpdf/QPDF_Dictionary.cc | 9 +++++---- libqpdf/qpdf/QPDFObject_private.hh | 20 ++++++++++++++++++++ libqpdf/qpdf/QPDFValue.hh | 33 +++++++++++++++++++++++++++++++-- 5 files changed, 80 insertions(+), 21 deletions(-) (limited to 'libqpdf/QPDFObjectHandle.cc') diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index 7a37ffc2..cbe42995 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -36,6 +36,8 @@ #include #include +using namespace std::literals; + namespace { class TerminateParsing @@ -813,13 +815,9 @@ QPDFObjectHandle::getArrayItem(int n) typeWarning("array", "returning null"); QTC::TC("qpdf", "QPDFObjectHandle array null for non-array"); } - QPDF* context = nullptr; - std::string description; - if (obj->getDescription(context, description)) { - result.setObjectDescription( - context, - description + " -> null returned from invalid array access"); - } + static auto constexpr msg = + " -> null returned from invalid array access"sv; + result.obj->setChildDescription(obj, msg, ""); } return result; } @@ -1038,14 +1036,9 @@ QPDFObjectHandle::getKey(std::string const& key) typeWarning("dictionary", "returning null for attempted key retrieval"); QTC::TC("qpdf", "QPDFObjectHandle dictionary null for getKey"); result = newNull(); - QPDF* qpdf = nullptr; - std::string description; - if (obj->getDescription(qpdf, description)) { - result.setObjectDescription( - qpdf, - (description + " -> null returned from getting key " + key + - " from non-Dictionary")); - } + static auto constexpr msg = + " -> null returned from getting key $VD from non-Dictionary"sv; + result.obj->setChildDescription(obj, msg, key); } return result; } diff --git a/libqpdf/QPDFValue.cc b/libqpdf/QPDFValue.cc index a89afd55..30d534dc 100644 --- a/libqpdf/QPDFValue.cc +++ b/libqpdf/QPDFValue.cc @@ -17,6 +17,7 @@ QPDFValue::getDescription() switch (object_description->index()) { case 0: { + // Simple template string auto description = std::get<0>(*object_description); if (auto pos = description.find("$OG"); @@ -36,12 +37,27 @@ QPDFValue::getDescription() } case 1: { + // QPDF::JSONReactor generated description auto j_descr = std::get<1>(*object_description); return ( *j_descr.input + (j_descr.object.empty() ? "" : ", " + j_descr.object) + " at offset " + std::to_string(parsed_offset)); } + case 2: + { + // Child object description + auto j_descr = std::get<2>(*object_description); + std::string result; + if (auto p = j_descr.parent.lock()) { + result = p->getDescription(); + } + result += j_descr.static_descr; + if (auto pos = result.find("$VD"); pos != std::string::npos) { + result.replace(pos, 3, j_descr.var_descr); + } + return result; + } } } else if (og.isIndirect()) { return "object " + og.unparse(' '); diff --git a/libqpdf/QPDF_Dictionary.cc b/libqpdf/QPDF_Dictionary.cc index 5349a2a8..f45c00c6 100644 --- a/libqpdf/QPDF_Dictionary.cc +++ b/libqpdf/QPDF_Dictionary.cc @@ -1,7 +1,10 @@ #include +#include #include +using namespace std::literals; + QPDF_Dictionary::QPDF_Dictionary( std::map const& items) : QPDFValue(::ot_dictionary, "dictionary"), @@ -98,10 +101,8 @@ QPDF_Dictionary::getKey(std::string const& key) return item->second; } else { auto null = QPDFObjectHandle::newNull(); - if (qpdf != nullptr) { - null.setObjectDescription( - qpdf, getDescription() + " -> dictionary key " + key); - } + static auto constexpr msg = " -> dictionary key $VD"sv; + null.getObj()->setChildDescription(shared_from_this(), msg, key); return null; } } diff --git a/libqpdf/qpdf/QPDFObject_private.hh b/libqpdf/qpdf/QPDFObject_private.hh index 612949da..0dc04699 100644 --- a/libqpdf/qpdf/QPDFObject_private.hh +++ b/libqpdf/qpdf/QPDFObject_private.hh @@ -12,6 +12,7 @@ #include #include +#include class QPDF; class QPDFObjectHandle; @@ -76,6 +77,25 @@ class QPDFObject { return value->setDescription(qpdf, description, offset); } + void + setChildDescription( + std::shared_ptr parent, + std::string_view const& static_descr, + std::string var_descr) + { + auto qpdf = parent ? parent->value->qpdf : nullptr; + value->setChildDescription( + qpdf, parent->value, static_descr, var_descr); + } + void + setChildDescription( + std::shared_ptr parent, + std::string_view const& static_descr, + std::string var_descr) + { + auto qpdf = parent ? parent->qpdf : nullptr; + value->setChildDescription(qpdf, parent, static_descr, var_descr); + } bool getDescription(QPDF*& qpdf, std::string& description) { diff --git a/libqpdf/qpdf/QPDFValue.hh b/libqpdf/qpdf/QPDFValue.hh index 9a1a6df1..e8a1834f 100644 --- a/libqpdf/qpdf/QPDFValue.hh +++ b/libqpdf/qpdf/QPDFValue.hh @@ -8,13 +8,14 @@ #include #include +#include #include class QPDF; class QPDFObjectHandle; class QPDFObject; -class QPDFValue +class QPDFValue: public std::enable_shared_from_this { friend class QPDFObject; @@ -38,7 +39,24 @@ class QPDFValue std::string object; }; - using Description = std::variant; + struct ChildDescr + { + ChildDescr( + std::shared_ptr parent, + std::string_view const& static_descr, + std::string var_descr) : + parent(parent), + static_descr(static_descr), + var_descr(var_descr) + { + } + + std::weak_ptr parent; + std::string_view const& static_descr; + std::string var_descr; + }; + + using Description = std::variant; virtual void setDescription( @@ -56,6 +74,17 @@ class QPDFValue qpdf = a_qpdf; og = a_og; } + void + setChildDescription( + QPDF* a_qpdf, + std::shared_ptr parent, + std::string_view const& static_descr, + std::string var_descr) + { + object_description = std::make_shared( + ChildDescr(parent, static_descr, var_descr)); + qpdf = a_qpdf; + } std::string getDescription(); bool hasDescription() -- cgit v1.2.3-54-g00ecf From 07bb5c3dd6213af9c9a64e17ae2d457cf4fc7190 Mon Sep 17 00:00:00 2001 From: m-holger Date: Fri, 17 Feb 2023 14:59:33 +0000 Subject: Overload QPDF_Null::create to take a child object description --- libqpdf/QPDFObjectHandle.cc | 17 +++++------------ libqpdf/QPDF_Dictionary.cc | 5 ++--- libqpdf/QPDF_Null.cc | 24 ++++++++++++++++++++++++ libqpdf/qpdf/QPDF_Null.hh | 8 ++++++++ 4 files changed, 39 insertions(+), 15 deletions(-) (limited to 'libqpdf/QPDFObjectHandle.cc') diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index cbe42995..d474dcce 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -802,12 +802,10 @@ QPDFObjectHandle::getArrayNItems() QPDFObjectHandle QPDFObjectHandle::getArrayItem(int n) { - QPDFObjectHandle result; auto array = asArray(); if (array && (n < array->getNItems()) && (n >= 0)) { - result = array->getItem(n); + return array->getItem(n); } else { - result = newNull(); if (array) { objectWarning("returning null for out of bounds array access"); QTC::TC("qpdf", "QPDFObjectHandle array bounds"); @@ -817,9 +815,8 @@ QPDFObjectHandle::getArrayItem(int n) } static auto constexpr msg = " -> null returned from invalid array access"sv; - result.obj->setChildDescription(obj, msg, ""); + return QPDF_Null::create(obj, msg, ""); } - return result; } bool @@ -1028,19 +1025,15 @@ QPDFObjectHandle::hasKey(std::string const& key) QPDFObjectHandle QPDFObjectHandle::getKey(std::string const& key) { - QPDFObjectHandle result; - auto dict = asDictionary(); - if (dict) { - result = dict->getKey(key); + if (auto dict = asDictionary()) { + return dict->getKey(key); } else { typeWarning("dictionary", "returning null for attempted key retrieval"); QTC::TC("qpdf", "QPDFObjectHandle dictionary null for getKey"); - result = newNull(); static auto constexpr msg = " -> null returned from getting key $VD from non-Dictionary"sv; - result.obj->setChildDescription(obj, msg, key); + return QPDF_Null::create(obj, msg, ""); } - return result; } QPDFObjectHandle diff --git a/libqpdf/QPDF_Dictionary.cc b/libqpdf/QPDF_Dictionary.cc index f45c00c6..43ad8a85 100644 --- a/libqpdf/QPDF_Dictionary.cc +++ b/libqpdf/QPDF_Dictionary.cc @@ -2,6 +2,7 @@ #include #include +#include using namespace std::literals; @@ -100,10 +101,8 @@ QPDF_Dictionary::getKey(std::string const& key) // May be a null object return item->second; } else { - auto null = QPDFObjectHandle::newNull(); static auto constexpr msg = " -> dictionary key $VD"sv; - null.getObj()->setChildDescription(shared_from_this(), msg, key); - return null; + return QPDF_Null::create(shared_from_this(), msg, key); } } diff --git a/libqpdf/QPDF_Null.cc b/libqpdf/QPDF_Null.cc index 6ec4556c..a82f23c0 100644 --- a/libqpdf/QPDF_Null.cc +++ b/libqpdf/QPDF_Null.cc @@ -1,5 +1,7 @@ #include +#include + QPDF_Null::QPDF_Null() : QPDFValue(::ot_null, "null") { @@ -11,6 +13,28 @@ QPDF_Null::create() return do_create(new QPDF_Null()); } +std::shared_ptr +QPDF_Null::create( + std::shared_ptr parent, + std::string_view const& static_descr, + std::string var_descr) +{ + auto n = do_create(new QPDF_Null()); + n->setChildDescription(parent, static_descr, var_descr); + return n; +} + +std::shared_ptr +QPDF_Null::create( + std::shared_ptr parent, + std::string_view const& static_descr, + std::string var_descr) +{ + auto n = do_create(new QPDF_Null()); + n->setChildDescription(parent, static_descr, var_descr); + return n; +} + std::shared_ptr QPDF_Null::copy(bool shallow) { diff --git a/libqpdf/qpdf/QPDF_Null.hh b/libqpdf/qpdf/QPDF_Null.hh index 1a92b214..2bbb3db5 100644 --- a/libqpdf/qpdf/QPDF_Null.hh +++ b/libqpdf/qpdf/QPDF_Null.hh @@ -8,6 +8,14 @@ class QPDF_Null: public QPDFValue public: virtual ~QPDF_Null() = default; static std::shared_ptr create(); + static std::shared_ptr create( + std::shared_ptr parent, + std::string_view const& static_descr, + std::string var_descr); + static std::shared_ptr create( + std::shared_ptr parent, + std::string_view const& static_descr, + std::string var_descr); virtual std::shared_ptr copy(bool shallow = false); virtual std::string unparse(); virtual JSON getJSON(int json_version); -- cgit v1.2.3-54-g00ecf