From d8fdf632a979a7adb4a6423fd55ef1971fb06afd Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Fri, 29 Apr 2022 20:21:07 -0400 Subject: Use replaceKeyAndGet in a few places in existing code --- libqpdf/QPDFAcroFormDocumentHelper.cc | 18 ++++++++---------- libqpdf/QPDFEFStreamObjectHelper.cc | 4 ++-- libqpdf/QPDFEmbeddedFileDocumentHelper.cc | 4 ++-- libqpdf/QPDFJob.cc | 4 ++-- libqpdf/QPDFObjectHandle.cc | 3 +-- libqpdf/QPDFPageObjectHelper.cc | 7 +++---- libqpdf/QPDFWriter.cc | 7 +++---- 7 files changed, 21 insertions(+), 26 deletions(-) (limited to 'libqpdf') diff --git a/libqpdf/QPDFAcroFormDocumentHelper.cc b/libqpdf/QPDFAcroFormDocumentHelper.cc index 6d4b3b83..2bb52f44 100644 --- a/libqpdf/QPDFAcroFormDocumentHelper.cc +++ b/libqpdf/QPDFAcroFormDocumentHelper.cc @@ -40,9 +40,9 @@ QPDFAcroFormDocumentHelper::getOrCreateAcroForm() { auto acroform = this->qpdf.getRoot().getKey("/AcroForm"); if (!acroform.isDictionary()) { - acroform = - this->qpdf.makeIndirectObject(QPDFObjectHandle::newDictionary()); - this->qpdf.getRoot().replaceKey("/AcroForm", acroform); + acroform = this->qpdf.getRoot().replaceKeyAndGet( + "/AcroForm", + this->qpdf.makeIndirectObject(QPDFObjectHandle::newDictionary())); } return acroform; } @@ -53,8 +53,8 @@ QPDFAcroFormDocumentHelper::addFormField(QPDFFormFieldObjectHelper ff) auto acroform = getOrCreateAcroForm(); auto fields = acroform.getKey("/Fields"); if (!fields.isArray()) { - fields = QPDFObjectHandle::newArray(); - acroform.replaceKey("/Fields", fields); + fields = + acroform.replaceKeyAndGet("/Fields", QPDFObjectHandle::newArray()); } fields.appendItem(ff.getObjectHandle()); std::set visited; @@ -877,8 +877,8 @@ QPDFAcroFormDocumentHelper::transformAnnotations( } dr.makeResourcesIndirect(this->qpdf); if (!dr.isIndirect()) { - dr = this->qpdf.makeIndirectObject(dr); - acroform.replaceKey("/DR", dr); + dr = acroform.replaceKeyAndGet( + "/DR", this->qpdf.makeIndirectObject(dr)); } // Merge the other document's /DR, creating a conflict // map. mergeResources checks to make sure both objects @@ -1099,9 +1099,7 @@ QPDFAcroFormDocumentHelper::transformAnnotations( auto apdict = ah.getAppearanceDictionary(); std::vector streams; auto replace_stream = [](auto& dict, auto& key, auto& old) { - auto new_stream = old.copyStream(); - dict.replaceKey(key, new_stream); - return new_stream; + return dict.replaceKeyAndGet(key, old.copyStream()); }; if (apdict.isDictionary()) { for (auto& ap : apdict.ditems()) { diff --git a/libqpdf/QPDFEFStreamObjectHelper.cc b/libqpdf/QPDFEFStreamObjectHelper.cc index fe27a86e..5810cf37 100644 --- a/libqpdf/QPDFEFStreamObjectHelper.cc +++ b/libqpdf/QPDFEFStreamObjectHelper.cc @@ -28,8 +28,8 @@ QPDFEFStreamObjectHelper::setParam( { auto params = this->oh.getDict().getKey("/Params"); if (!params.isDictionary()) { - params = QPDFObjectHandle::newDictionary(); - this->oh.getDict().replaceKey("/Params", params); + params = this->oh.getDict().replaceKeyAndGet( + "/Params", QPDFObjectHandle::newDictionary()); } params.replaceKey(pkey, pval); } diff --git a/libqpdf/QPDFEmbeddedFileDocumentHelper.cc b/libqpdf/QPDFEmbeddedFileDocumentHelper.cc index 826e0d87..4b61dfe3 100644 --- a/libqpdf/QPDFEmbeddedFileDocumentHelper.cc +++ b/libqpdf/QPDFEmbeddedFileDocumentHelper.cc @@ -62,8 +62,8 @@ QPDFEmbeddedFileDocumentHelper::initEmbeddedFiles() auto root = qpdf.getRoot(); auto names = root.getKey("/Names"); if (!names.isDictionary()) { - names = QPDFObjectHandle::newDictionary(); - root.replaceKey("/Names", names); + names = + root.replaceKeyAndGet("/Names", QPDFObjectHandle::newDictionary()); } auto embedded_files = names.getKey("/EmbeddedFiles"); if (!embedded_files.isDictionary()) { diff --git a/libqpdf/QPDFJob.cc b/libqpdf/QPDFJob.cc index 692e6420..08b5b9d7 100644 --- a/libqpdf/QPDFJob.cc +++ b/libqpdf/QPDFJob.cc @@ -1958,8 +1958,8 @@ QPDFJob::doUnderOverlayForPage( QPDFObjectHandle resources = dest_page.getAttribute("/Resources", true); if (!resources.isDictionary()) { QTC::TC("qpdf", "QPDFJob overlay page with no resources"); - resources = QPDFObjectHandle::newDictionary(); - dest_page.getObjectHandle().replaceKey("/Resources", resources); + resources = dest_page.getObjectHandle().replaceKeyAndGet( + "/Resources", QPDFObjectHandle::newDictionary()); } for (std::vector::iterator iter = pagenos[pageno].begin(); iter != pagenos[pageno].end(); diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index 68849f46..9bdf1a64 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -1172,8 +1172,7 @@ QPDFObjectHandle::mergeResources( // subdictionaries just to get this shallow copy // functionality. QTC::TC("qpdf", "QPDFObjectHandle replace with copy"); - this_val = this_val.shallowCopy(); - replaceKey(rtype, this_val); + this_val = replaceKeyAndGet(rtype, this_val.shallowCopy()); } std::map og_to_name; std::set rnames; diff --git a/libqpdf/QPDFPageObjectHelper.cc b/libqpdf/QPDFPageObjectHelper.cc index b4e6a13a..a6773e70 100644 --- a/libqpdf/QPDFPageObjectHelper.cc +++ b/libqpdf/QPDFPageObjectHelper.cc @@ -597,8 +597,7 @@ QPDFPageObjectHelper::removeUnreferencedResourcesHelper( for (auto const& iter : to_filter) { QPDFObjectHandle dict = resources.getKey(iter); if (dict.isDictionary()) { - dict = dict.shallowCopy(); - resources.replaceKey(iter, dict); + dict = resources.replaceKeyAndGet(iter, dict.shallowCopy()); rdicts.push_back(dict); auto keys = dict.getKeys(); known_names.insert(keys.begin(), keys.end()); @@ -1113,8 +1112,8 @@ QPDFPageObjectHelper::copyAnnotations( afdh->addAndRenameFormFields(new_fields); auto annots = this->oh.getKey("/Annots"); if (!annots.isArray()) { - annots = QPDFObjectHandle::newArray(); - this->oh.replaceKey("/Annots", annots); + annots = + this->oh.replaceKeyAndGet("/Annots", QPDFObjectHandle::newArray()); } for (auto const& annot : new_annots) { annots.appendItem(annot); diff --git a/libqpdf/QPDFWriter.cc b/libqpdf/QPDFWriter.cc index 9f3eacbe..d06a82b0 100644 --- a/libqpdf/QPDFWriter.cc +++ b/libqpdf/QPDFWriter.cc @@ -1662,8 +1662,8 @@ QPDFWriter::unparseObject( "qpdf", "QPDFWriter create Extensions", this->m->qdf_mode ? 0 : 1); - extensions = QPDFObjectHandle::newDictionary(); - object.replaceKey("/Extensions", extensions); + extensions = object.replaceKeyAndGet( + "/Extensions", QPDFObjectHandle::newDictionary()); } } else if (!have_extensions_other) { // We have Extensions dictionary and don't want one. @@ -2387,8 +2387,7 @@ QPDFWriter::prepareFileForWrite() if (oh.isIndirect()) { QTC::TC("qpdf", "QPDFWriter make Extensions direct"); extensions_indirect = true; - oh = oh.shallowCopy(); - root.replaceKey(key, oh); + oh = root.replaceKeyAndGet(key, oh.shallowCopy()); } if (oh.hasKey("/ADBE")) { QPDFObjectHandle adbe = oh.getKey("/ADBE"); -- cgit v1.2.3-54-g00ecf