aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2022-01-30 20:39:38 +0100
committerJay Berkenbilt <ejb@ql.org>2022-01-31 21:57:45 +0100
commit1355d95d0811942758036e23d64ed6b9fb7c8317 (patch)
tree449fc6664446e21bcea22f7c50aa816c8ef1e432
parentcd30f626fedef7c577766d32e84a65a7f0fbcb7a (diff)
downloadqpdf-1355d95d0811942758036e23d64ed6b9fb7c8317.tar.zst
QPDFJob: partial mode for initializeFromJson
-rw-r--r--include/qpdf/QPDFJob.hh14
-rw-r--r--libqpdf/QPDFJob_config.cc2
-rw-r--r--libqpdf/QPDFJob_json.cc17
3 files changed, 26 insertions, 7 deletions
diff --git a/include/qpdf/QPDFJob.hh b/include/qpdf/QPDFJob.hh
index 5178c4c1..77ef6068 100644
--- a/include/qpdf/QPDFJob.hh
+++ b/include/qpdf/QPDFJob.hh
@@ -73,8 +73,20 @@ class QPDFJob
void initializeFromArgv(int argc, char* argv[],
char const* progname_env = nullptr);
+ // Initialize a QPDFJob from json. Passing partial = true prevents
+ // this method from doing the final checks (calling
+ // checkConfiguration) after processing the json file. This makes
+ // it possible to initialze QPDFJob in stages using multiple json
+ // files or to have a json file that can be processed from the CLI
+ // with --job-json-file and be combined with other arguments. For
+ // example, you might include only encryption parameters, leaving
+ // it up to the rest of the command-line arguments to provide
+ // input and output files. initializeFromJson is called with
+ // partial = true when invoked from the command line. To make sure
+ // that the json file is fully valid on its own, just don't
+ // specify any other command-line flags.
QPDF_DLL
- void initializeFromJson(std::string const& json);
+ void initializeFromJson(std::string const& json, bool partial = false);
// Set name that is used to prefix verbose messages, progress
// messages, and other things that the library writes to output
diff --git a/libqpdf/QPDFJob_config.cc b/libqpdf/QPDFJob_config.cc
index bf33820a..02f1b69e 100644
--- a/libqpdf/QPDFJob_config.cc
+++ b/libqpdf/QPDFJob_config.cc
@@ -730,7 +730,7 @@ QPDFJob::Config::jobJsonFile(char const* parameter)
QUtil::read_file_into_memory(parameter, file_buf, size);
try
{
- o.initializeFromJson(std::string(file_buf.getPointer(), size));
+ o.initializeFromJson(std::string(file_buf.getPointer(), size), true);
}
catch (std::exception& e)
{
diff --git a/libqpdf/QPDFJob_json.cc b/libqpdf/QPDFJob_json.cc
index 02e851b0..6525b761 100644
--- a/libqpdf/QPDFJob_json.cc
+++ b/libqpdf/QPDFJob_json.cc
@@ -15,7 +15,7 @@ namespace
class Handlers
{
public:
- Handlers(std::shared_ptr<QPDFJob::Config> c_main);
+ Handlers(bool partial, std::shared_ptr<QPDFJob::Config> c_main);
void handle(JSON&);
private:
@@ -47,6 +47,7 @@ namespace
setup_handler_t bindSetup(void (Handlers::*f)(std::string const&));
std::list<std::shared_ptr<JSONHandler>> json_handlers;
+ bool partial;
JSONHandler* jh; // points to last of json_handlers
std::shared_ptr<QPDFJob::Config> c_main;
std::shared_ptr<QPDFJob::CopyAttConfig> c_copy_att;
@@ -57,7 +58,8 @@ namespace
};
}
-Handlers::Handlers(std::shared_ptr<QPDFJob::Config> c_main) :
+Handlers::Handlers(bool partial, std::shared_ptr<QPDFJob::Config> c_main) :
+ partial(partial),
jh(nullptr),
c_main(c_main)
{
@@ -95,7 +97,12 @@ Handlers::initHandlers()
this->jh = this->json_handlers.back().get();
jh->addDictHandlers(
[](std::string const&, JSON){},
- [this](std::string const&){c_main->checkConfiguration();});
+ [this](std::string const&){
+ if (! this->partial)
+ {
+ c_main->checkConfiguration();
+ }
+ });
# include <qpdf/auto_job_json_init.hh>
@@ -623,7 +630,7 @@ Handlers::setupOptionsUnderlayPassword(std::string const& key)
}
void
-QPDFJob::initializeFromJson(std::string const& json)
+QPDFJob::initializeFromJson(std::string const& json, bool partial)
{
std::list<std::string> errors;
JSON j = JSON::parse(json);
@@ -639,5 +646,5 @@ QPDFJob::initializeFromJson(std::string const& json)
throw std::runtime_error(msg.str());
}
- Handlers(config()).handle(j);
+ Handlers(partial, config()).handle(j);
}