2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-06-08 05:10:53 +00:00

Refactor QPDF::writeJSONStream

Use QPDF_Stream::writeStreamJSON.
Factor out the json_stream_data == qpdf_sj_file case.
This commit is contained in:
m-holger 2024-02-15 14:47:08 +00:00
parent b15d0bf6e1
commit b9bc05356a
2 changed files with 41 additions and 38 deletions

View File

@ -1411,19 +1411,6 @@ class QPDF
// JSON import // JSON import
void importJSON(std::shared_ptr<InputSource>, bool must_be_complete); void importJSON(std::shared_ptr<InputSource>, bool must_be_complete);
// JSON write
void writeJSONStream(
int version,
Pipeline* p,
bool& first,
std::string const& key,
QPDFObjectHandle&,
qpdf_stream_decode_level_e,
qpdf_json_stream_data_e,
std::string const& file_prefix);
void writeJSONObject(
int version, Pipeline* p, bool& first, std::string const& key, QPDFObjectHandle&);
// Type conversion helper methods // Type conversion helper methods
template <typename T> template <typename T>
static qpdf_offset_t static qpdf_offset_t

View File

@ -8,6 +8,7 @@
#include <qpdf/QPDFObject_private.hh> #include <qpdf/QPDFObject_private.hh>
#include <qpdf/QPDFValue.hh> #include <qpdf/QPDFValue.hh>
#include <qpdf/QPDF_Null.hh> #include <qpdf/QPDF_Null.hh>
#include <qpdf/QPDF_Stream.hh>
#include <qpdf/QTC.hh> #include <qpdf/QTC.hh>
#include <qpdf/QUtil.hh> #include <qpdf/QUtil.hh>
#include <algorithm> #include <algorithm>
@ -443,7 +444,9 @@ void
QPDF::JSONReactor::replaceObject(QPDFObjectHandle&& replacement, JSON const& value) QPDF::JSONReactor::replaceObject(QPDFObjectHandle&& replacement, JSON const& value)
{ {
if (replacement.isIndirect()) { if (replacement.isIndirect()) {
error(replacement.getParsedOffset(), "the value of an object may not be an indirect object reference"); error(
replacement.getParsedOffset(),
"the value of an object may not be an indirect object reference");
return; return;
} }
auto& tos = stack.back(); auto& tos = stack.back();
@ -829,40 +832,51 @@ QPDF::importJSON(std::shared_ptr<InputSource> is, bool must_be_complete)
} }
void void
QPDF::writeJSONStream( writeJSONStreamFile(
int version,
JSON::Writer& jw,
QPDF_Stream& stream,
int id,
qpdf_stream_decode_level_e decode_level,
std::string const& file_prefix)
{
auto filename = file_prefix + "-" + std::to_string(id);
auto* f = QUtil::safe_fopen(filename.c_str(), "wb");
Pl_StdioFile f_pl{"stream data", f};
stream.writeStreamJSON(version, jw, qpdf_sj_file, decode_level, &f_pl, filename);
f_pl.finish();
fclose(f);
}
void
writeJSONStream(
int version, int version,
Pipeline* p, Pipeline* p,
bool& first, bool& first,
std::string const& key, std::string const& key,
QPDFObjectHandle& obj, QPDF_Stream& stream,
int id,
qpdf_stream_decode_level_e decode_level, qpdf_stream_decode_level_e decode_level,
qpdf_json_stream_data_e json_stream_data, qpdf_json_stream_data_e json_stream_data,
std::string const& file_prefix) std::string const& file_prefix)
{ {
Pipeline* stream_p = nullptr; if (first) {
FILE* f = nullptr; *p << "\n \"" << key << "\": {\n \"stream\": ";
std::shared_ptr<Pl_StdioFile> f_pl; first = false;
std::string filename; } else {
*p << ",\n \"" << key << "\": {\n \"stream\": ";
}
JSON::Writer jw{p, 4};
if (json_stream_data == qpdf_sj_file) { if (json_stream_data == qpdf_sj_file) {
filename = file_prefix + "-" + std::to_string(obj.getObjectID()); writeJSONStreamFile(version, jw, stream, id, decode_level, file_prefix);
f = QUtil::safe_fopen(filename.c_str(), "wb"); } else {
f_pl = std::make_shared<Pl_StdioFile>("stream data", f); stream.writeStreamJSON(version, jw, json_stream_data, decode_level, nullptr, "");
stream_p = f_pl.get();
}
auto j = JSON::makeDictionary();
j.addDictionaryMember(
"stream", obj.getStreamJSON(version, json_stream_data, decode_level, stream_p, filename));
JSON::writeDictionaryItem(p, first, key, j, 3);
if (f) {
f_pl->finish();
f_pl = nullptr;
fclose(f);
} }
*p << "\n }";
} }
void void
QPDF::writeJSONObject( writeJSONObject(
int version, Pipeline* p, bool& first, std::string const& key, QPDFObjectHandle& obj) int version, Pipeline* p, bool& first, std::string const& key, QPDFObjectHandle& obj)
{ {
if (first) { if (first) {
@ -947,15 +961,17 @@ QPDF::writeJSON(
JSON::writeDictionaryOpen(p, first_qpdf_inner, depth_qpdf); JSON::writeDictionaryOpen(p, first_qpdf_inner, depth_qpdf);
bool all_objects = wanted_objects.empty(); bool all_objects = wanted_objects.empty();
for (auto& obj: getAllObjects()) { for (auto& obj: getAllObjects()) {
std::string key = "obj:" + obj.unparse(); auto const og = obj.getObjGen();
std::string key = "obj:" + og.unparse(' ') + " R";
if (all_objects || wanted_objects.count(key)) { if (all_objects || wanted_objects.count(key)) {
if (obj.isStream()) { if (auto* stream = obj.getObjectPtr()->as<QPDF_Stream>()) {
writeJSONStream( writeJSONStream(
version, version,
p, p,
first_qpdf_inner, first_qpdf_inner,
key, key,
obj, *stream,
og.getObj(),
decode_level, decode_level,
json_stream_data, json_stream_data,
file_prefix); file_prefix);