From 0f0747b3ae9b2b74d337f92c2861a11e0d0573d4 Mon Sep 17 00:00:00 2001 From: m-holger Date: Tue, 20 Aug 2024 15:36:37 +0100 Subject: [PATCH] Refactor QPDF::getXRefTable --- include/qpdf/QPDF.hh | 1 - libqpdf/QPDF.cc | 31 ++++++++++++++++++++++++------- libqpdf/qpdf/QPDF_private.hh | 25 +------------------------ 3 files changed, 25 insertions(+), 32 deletions(-) diff --git a/include/qpdf/QPDF.hh b/include/qpdf/QPDF.hh index 8bb7120a..4d1cde7f 100644 --- a/include/qpdf/QPDF.hh +++ b/include/qpdf/QPDF.hh @@ -827,7 +827,6 @@ class QPDF // For QPDFWriter: - std::map getXRefTableInternal(); template void optimize_internal( T const& object_stream_data, diff --git a/libqpdf/QPDF.cc b/libqpdf/QPDF.cc index d0a2725d..7ea69f52 100644 --- a/libqpdf/QPDF.cc +++ b/libqpdf/QPDF.cc @@ -1410,6 +1410,30 @@ QPDF::Xref_table::at_offset(qpdf_offset_t offset) const noexcept return QPDFObjGen(id, gen); } +std::map +QPDF::Xref_table::as_map() const +{ + std::map result; + int i{0}; + for (auto const& item: table) { + switch (item.type()) { + case 0: + break; + case 1: + result.emplace(QPDFObjGen(i, item.gen()), item.offset()); + break; + case 2: + result.emplace( + QPDFObjGen(i, 0), QPDFXRefEntry(item.stream_number(), item.stream_index())); + break; + default: + throw std::logic_error("Xref_table: invalid entry type"); + } + ++i; + } + return result; +} + void QPDF::showXRefTable() { @@ -2601,17 +2625,10 @@ QPDF::getRoot() std::map QPDF::getXRefTable() -{ - return getXRefTableInternal(); -} - -std::map -QPDF::getXRefTableInternal() { if (!m->xref_table.initialized()) { throw std::logic_error("QPDF::getXRefTable called before parsing."); } - return m->xref_table.as_map(); } diff --git a/libqpdf/qpdf/QPDF_private.hh b/libqpdf/qpdf/QPDF_private.hh index 3c4330b1..60931dfa 100644 --- a/libqpdf/qpdf/QPDF_private.hh +++ b/libqpdf/qpdf/QPDF_private.hh @@ -89,30 +89,7 @@ class QPDF::Xref_table QPDFObjGen at_offset(qpdf_offset_t offset) const noexcept; - // Temporary access to underlying map - std::map - as_map() - { - std::map result; - int i{0}; - for (auto const& item: table) { - switch (item.type()) { - case 0: - break; - case 1: - result.emplace(QPDFObjGen(i, item.gen()), item.offset()); - break; - case 2: - result.emplace( - QPDFObjGen(i, 0), QPDFXRefEntry(item.stream_number(), item.stream_index())); - break; - default: - throw std::logic_error("Xref_table: invalid entry type"); - } - ++i; - } - return result; - } + std::map as_map() const; bool object_streams() const noexcept