From 12f7a4461b18b4be94002fa0043fd0e98e80a274 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Tue, 9 Jan 2024 16:54:11 -0500 Subject: Handle pages/under/overlay JSON file in begin ...since they have to be handled before other options. It was working because, in both cases, `file` was alphabetically before the other keys, but this implementation gives a stronger guarantee. --- libqpdf/QPDFJob_json.cc | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) (limited to 'libqpdf/QPDFJob_json.cc') diff --git a/libqpdf/QPDFJob_json.cc b/libqpdf/QPDFJob_json.cc index 797ee81d..35963169 100644 --- a/libqpdf/QPDFJob_json.cc +++ b/libqpdf/QPDFJob_json.cc @@ -57,6 +57,8 @@ namespace bare_handler_t bindBare(void (Handlers::*f)()); json_handler_t bindJSON(void (Handlers::*f)(JSON)); + void beginUnderOverlay(JSON const& j); + std::list> json_handlers; bool partial; JSONHandler* jh{nullptr}; // points to last of json_handlers @@ -225,6 +227,24 @@ Handlers::handle(JSON& j) this->json_handlers.back()->handle(".", j); } +void +Handlers::beginUnderOverlay(JSON const& j) +{ + // File has to be processed before items, so handle it here. + bool file_seen = false; + std::string file; + j.forEachDictItem([&](std::string const& key, JSON const& value) { + if (key == "file") { + file_seen = value.getString(file); + } + }); + if (!file_seen) { + QTC::TC("qpdf", "QPDFJob json over/under no file"); + usage("file is required in underlay/overlay specification"); + } + c_uo->file(file); +} + void Handlers::setupInputFile() { @@ -468,16 +488,17 @@ void Handlers::beginPages(JSON j) { bool file_seen = false; + std::string file; j.forEachDictItem([&](std::string const& key, JSON const& value) { if (key == "file") { - std::string v; - file_seen = value.getString(v); + file_seen = value.getString(file); } }); if (!file_seen) { QTC::TC("qpdf", "QPDFJob json pages no file"); usage("file is required in page specification"); } + c_pages->file(file); } void @@ -489,7 +510,8 @@ Handlers::endPages() void Handlers::setupPagesFile() { - addParameter([this](char const* p) { c_pages->file(p); }); + // This is handled in beginPages since file() has to be called first. + ignoreItem(); } void @@ -499,9 +521,10 @@ Handlers::setupPagesPassword() } void -Handlers::beginOverlay(JSON) +Handlers::beginOverlay(JSON j) { this->c_uo = c_main->overlay(); + beginUnderOverlay(j); } void @@ -514,7 +537,8 @@ Handlers::endOverlay() void Handlers::setupOverlayFile() { - addParameter([this](char const* p) { c_uo->file(p); }); + // This is handled in beginOverlay since file() has to be called first. + ignoreItem(); } void @@ -524,9 +548,10 @@ Handlers::setupOverlayPassword() } void -Handlers::beginUnderlay(JSON) +Handlers::beginUnderlay(JSON j) { this->c_uo = c_main->underlay(); + beginUnderOverlay(j); } void @@ -539,7 +564,8 @@ Handlers::endUnderlay() void Handlers::setupUnderlayFile() { - addParameter([this](char const* p) { c_uo->file(p); }); + // This is handled in beginUnderlay since file() has to be called first. + ignoreItem(); } void -- cgit v1.2.3-54-g00ecf