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 ++- libqpdf/QPDFValue.cc | 32 ++++++++++++++++++++++---------- libqpdf/QPDF_Stream.cc | 6 ++++-- libqpdf/qpdf/QPDFObject_private.hh | 2 +- libqpdf/qpdf/QPDFParser.hh | 7 ++++--- libqpdf/qpdf/QPDFValue.hh | 12 ++++++++---- libqpdf/qpdf/QPDF_Stream.hh | 4 +++- 7 files changed, 44 insertions(+), 22 deletions(-) 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); } } diff --git a/libqpdf/QPDFValue.cc b/libqpdf/QPDFValue.cc index 19679df2..7c5b30a6 100644 --- a/libqpdf/QPDFValue.cc +++ b/libqpdf/QPDFValue.cc @@ -13,16 +13,28 @@ QPDFValue::do_create(QPDFValue* object) std::string QPDFValue::getDescription() { - auto description = object_description ? *object_description : ""; - if (auto pos = description.find("$OG"); pos != std::string::npos) { - description.replace(pos, 3, og.unparse(' ')); - } - if (auto pos = description.find("$PO"); pos != std::string::npos) { - qpdf_offset_t shift = (type_code == ::ot_dictionary) ? 2 - : (type_code == ::ot_array) ? 1 - : 0; + if (object_description) { + switch (object_description->index()) { + case 0: + { + auto description = std::get<0>(*object_description); + + if (auto pos = description.find("$OG"); + pos != std::string::npos) { + description.replace(pos, 3, og.unparse(' ')); + } + if (auto pos = description.find("$PO"); + pos != std::string::npos) { + qpdf_offset_t shift = (type_code == ::ot_dictionary) ? 2 + : (type_code == ::ot_array) ? 1 + : 0; - description.replace(pos, 3, std::to_string(parsed_offset + shift)); + description.replace( + pos, 3, std::to_string(parsed_offset + shift)); + } + return description; + } + } } - return description; + return {}; } diff --git a/libqpdf/QPDF_Stream.cc b/libqpdf/QPDF_Stream.cc index 66916c21..8e2e16c5 100644 --- a/libqpdf/QPDF_Stream.cc +++ b/libqpdf/QPDF_Stream.cc @@ -123,7 +123,7 @@ QPDF_Stream::QPDF_Stream( throw std::logic_error("stream object instantiated with non-dictionary " "object for dictionary"); } - auto descr = std::make_shared( + auto descr = std::make_shared( qpdf->getFilename() + ", stream object " + og.unparse(' ')); setDescription(qpdf, descr, offset); } @@ -283,7 +283,9 @@ QPDF_Stream::getStreamJSON( void QPDF_Stream::setDescription( - QPDF* qpdf, std::shared_ptr& description, qpdf_offset_t offset) + QPDF* qpdf, + std::shared_ptr& description, + qpdf_offset_t offset) { this->QPDFValue::setDescription(qpdf, description, offset); setDictDescription(); diff --git a/libqpdf/qpdf/QPDFObject_private.hh b/libqpdf/qpdf/QPDFObject_private.hh index 4d388503..612949da 100644 --- a/libqpdf/qpdf/QPDFObject_private.hh +++ b/libqpdf/qpdf/QPDFObject_private.hh @@ -71,7 +71,7 @@ class QPDFObject void setDescription( QPDF* qpdf, - std::shared_ptr& description, + std::shared_ptr& description, qpdf_offset_t offset = -1) { return value->setDescription(qpdf, description, offset); diff --git a/libqpdf/qpdf/QPDFParser.hh b/libqpdf/qpdf/QPDFParser.hh index 65279700..c2bf6bbe 100644 --- a/libqpdf/qpdf/QPDFParser.hh +++ b/libqpdf/qpdf/QPDFParser.hh @@ -2,6 +2,7 @@ #define QPDFPARSER_HH #include +#include #include #include @@ -21,8 +22,8 @@ class QPDFParser tokenizer(tokenizer), decrypter(decrypter), context(context), - description(std::make_shared( - input->getName() + ", " + object_description + " at offset $PO")) + description(std::make_shared(std::string( + input->getName() + ", " + object_description + " at offset $PO"))) { } virtual ~QPDFParser() = default; @@ -49,7 +50,7 @@ class QPDFParser QPDFTokenizer& tokenizer; QPDFObjectHandle::StringDecrypter* decrypter; QPDF* context; - std::shared_ptr description; + std::shared_ptr description; }; #endif // QPDFPARSER_HH diff --git a/libqpdf/qpdf/QPDFValue.hh b/libqpdf/qpdf/QPDFValue.hh index b9c3cdb1..eef088f9 100644 --- a/libqpdf/qpdf/QPDFValue.hh +++ b/libqpdf/qpdf/QPDFValue.hh @@ -8,6 +8,7 @@ #include #include +#include class QPDF; class QPDFObjectHandle; @@ -23,10 +24,13 @@ class QPDFValue virtual std::shared_ptr copy(bool shallow = false) = 0; virtual std::string unparse() = 0; virtual JSON getJSON(int json_version) = 0; + + using Description = std::variant; + virtual void setDescription( QPDF* qpdf_p, - std::shared_ptr& description, + std::shared_ptr& description, qpdf_offset_t offset) { qpdf = qpdf_p; @@ -37,7 +41,7 @@ class QPDFValue setDefaultDescription(QPDF* a_qpdf, QPDFObjGen const& a_og) { static auto default_description{ - std::make_shared("object $OG")}; + std::make_shared("object $OG")}; if (!object_description) { object_description = default_description; } @@ -49,7 +53,7 @@ class QPDFValue hasDescription() { return qpdf != nullptr && object_description && - !object_description->empty(); + !getDescription().empty(); } void setParsedOffset(qpdf_offset_t offset) @@ -108,7 +112,7 @@ class QPDFValue private: QPDFValue(QPDFValue const&) = delete; QPDFValue& operator=(QPDFValue const&) = delete; - std::shared_ptr object_description; + std::shared_ptr object_description; const qpdf_object_type_e type_code{::ot_uninitialized}; char const* type_name{"uninitialized"}; diff --git a/libqpdf/qpdf/QPDF_Stream.hh b/libqpdf/qpdf/QPDF_Stream.hh index c0694c60..cf0a2288 100644 --- a/libqpdf/qpdf/QPDF_Stream.hh +++ b/libqpdf/qpdf/QPDF_Stream.hh @@ -27,7 +27,9 @@ class QPDF_Stream: public QPDFValue virtual std::string unparse(); virtual JSON getJSON(int json_version); virtual void setDescription( - QPDF*, std::shared_ptr& description, qpdf_offset_t offset); + QPDF*, + std::shared_ptr& description, + qpdf_offset_t offset); virtual void disconnect(); QPDFObjectHandle getDict() const; bool isDataModified() const; -- cgit v1.2.3-54-g00ecf