From dab27c9bb35c26e30e22f2e53299ee9566cadefd Mon Sep 17 00:00:00 2001 From: m-holger Date: Thu, 9 Feb 2023 12:43:56 +0000 Subject: [PATCH] Refactor setting of object descriptions in QPDF::JSONReactor --- include/qpdf/QPDF.hh | 3 +++ libqpdf/QPDFValue.cc | 8 ++++++++ libqpdf/QPDF_json.cc | 15 ++++++++++----- libqpdf/qpdf/QPDFValue.hh | 14 +++++++++++++- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/include/qpdf/QPDF.hh b/include/qpdf/QPDF.hh index d6b32fe..b227bb3 100644 --- a/include/qpdf/QPDF.hh +++ b/include/qpdf/QPDF.hh @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -50,6 +51,7 @@ class BitStream; class BitWriter; class QPDFLogger; class QPDFParser; +struct JSON_Descr; class QPDF { @@ -1152,6 +1154,7 @@ class QPDF QPDF& pdf; std::shared_ptr is; bool must_be_complete; + std::shared_ptr> descr; bool errors; bool parse_error; bool saw_qpdf; diff --git a/libqpdf/QPDFValue.cc b/libqpdf/QPDFValue.cc index 41a00fa..a89afd5 100644 --- a/libqpdf/QPDFValue.cc +++ b/libqpdf/QPDFValue.cc @@ -34,6 +34,14 @@ QPDFValue::getDescription() } return description; } + case 1: + { + 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)); + } } } else if (og.isIndirect()) { return "object " + og.unparse(' '); diff --git a/libqpdf/QPDF_json.cc b/libqpdf/QPDF_json.cc index 02dc57e..fb85855 100644 --- a/libqpdf/QPDF_json.cc +++ b/libqpdf/QPDF_json.cc @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include #include #include @@ -226,6 +228,8 @@ QPDF::JSONReactor::JSONReactor( pdf(pdf), is(is), must_be_complete(must_be_complete), + descr(std::make_shared>( + JSON_Descr(std::make_shared(is->getName()), ""))), errors(false), parse_error(false), saw_qpdf(false), @@ -675,12 +679,13 @@ QPDF::JSONReactor::arrayItem(JSON const& value) void QPDF::JSONReactor::setObjectDescription(QPDFObjectHandle& oh, JSON const& value) { - std::string description = this->is->getName(); - if (!this->cur_object.empty()) { - description += ", " + this->cur_object; + auto j_descr = std::get(*descr); + if (j_descr.object != cur_object) { + descr = std::make_shared( + JSON_Descr(j_descr.input, cur_object)); } - description += " at offset " + std::to_string(value.getStart()); - oh.setObjectDescription(&this->pdf, description); + + oh.getObjectPtr()->setDescription(&pdf, descr, value.getStart()); } QPDFObjectHandle diff --git a/libqpdf/qpdf/QPDFValue.hh b/libqpdf/qpdf/QPDFValue.hh index 8e9d4ea..2e0c5e8 100644 --- a/libqpdf/qpdf/QPDFValue.hh +++ b/libqpdf/qpdf/QPDFValue.hh @@ -14,6 +14,18 @@ class QPDF; class QPDFObjectHandle; class QPDFObject; +struct JSON_Descr +{ + JSON_Descr(std::shared_ptr input, std::string const& object) : + input(input), + object(object) + { + } + + std::shared_ptr input; + std::string object; +}; + class QPDFValue { friend class QPDFObject; @@ -25,7 +37,7 @@ class QPDFValue virtual std::string unparse() = 0; virtual JSON getJSON(int json_version) = 0; - using Description = std::variant; + using Description = std::variant; virtual void setDescription( -- libgit2 0.21.4