2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-12-22 10:58:58 +00:00

Refactor QPDF::getXRefTable

This commit is contained in:
m-holger 2024-08-20 15:36:37 +01:00
parent 965f0fcd63
commit 0f0747b3ae
3 changed files with 25 additions and 32 deletions

View File

@ -827,7 +827,6 @@ class QPDF
// For QPDFWriter: // For QPDFWriter:
std::map<QPDFObjGen, QPDFXRefEntry> getXRefTableInternal();
template <typename T> template <typename T>
void optimize_internal( void optimize_internal(
T const& object_stream_data, T const& object_stream_data,

View File

@ -1410,6 +1410,30 @@ QPDF::Xref_table::at_offset(qpdf_offset_t offset) const noexcept
return QPDFObjGen(id, gen); return QPDFObjGen(id, gen);
} }
std::map<QPDFObjGen, QPDFXRefEntry>
QPDF::Xref_table::as_map() const
{
std::map<QPDFObjGen, QPDFXRefEntry> 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 void
QPDF::showXRefTable() QPDF::showXRefTable()
{ {
@ -2601,17 +2625,10 @@ QPDF::getRoot()
std::map<QPDFObjGen, QPDFXRefEntry> std::map<QPDFObjGen, QPDFXRefEntry>
QPDF::getXRefTable() QPDF::getXRefTable()
{
return getXRefTableInternal();
}
std::map<QPDFObjGen, QPDFXRefEntry>
QPDF::getXRefTableInternal()
{ {
if (!m->xref_table.initialized()) { if (!m->xref_table.initialized()) {
throw std::logic_error("QPDF::getXRefTable called before parsing."); throw std::logic_error("QPDF::getXRefTable called before parsing.");
} }
return m->xref_table.as_map(); return m->xref_table.as_map();
} }

View File

@ -89,30 +89,7 @@ class QPDF::Xref_table
QPDFObjGen at_offset(qpdf_offset_t offset) const noexcept; QPDFObjGen at_offset(qpdf_offset_t offset) const noexcept;
// Temporary access to underlying map std::map<QPDFObjGen, QPDFXRefEntry> as_map() const;
std::map<QPDFObjGen, QPDFXRefEntry>
as_map()
{
std::map<QPDFObjGen, QPDFXRefEntry> 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;
}
bool bool
object_streams() const noexcept object_streams() const noexcept