diff options
author | Jay Berkenbilt <ejb@ql.org> | 2022-02-01 00:15:10 +0100 |
---|---|---|
committer | Jay Berkenbilt <ejb@ql.org> | 2022-02-01 00:16:09 +0100 |
commit | 21b9290785fb03477784cf6312f57cfb96dbe53d (patch) | |
tree | ba51fcff3ed2982623d4ce9cd88c490eee34f95d /libqpdf | |
parent | ea96330bb615791de58a4f6beb6203137fe1ba35 (diff) | |
download | qpdf-21b9290785fb03477784cf6312f57cfb96dbe53d.tar.zst |
QPDFJob json: make bare arguments expect the empty string
Changing from bool requiring true to string requiring the empty string
is more consistent with the CLI and makes it possible to add an
optional parameter or choices later without breaking compatibility.
Diffstat (limited to 'libqpdf')
-rw-r--r-- | libqpdf/QPDFJob_config.cc | 4 | ||||
-rw-r--r-- | libqpdf/QPDFJob_json.cc | 33 | ||||
-rw-r--r-- | libqpdf/qpdf/auto_job_json_init.hh | 64 |
3 files changed, 54 insertions, 47 deletions
diff --git a/libqpdf/QPDFJob_config.cc b/libqpdf/QPDFJob_config.cc index 2066e608..ed8943f6 100644 --- a/libqpdf/QPDFJob_config.cc +++ b/libqpdf/QPDFJob_config.cc @@ -108,7 +108,7 @@ QPDFJob::Config::coalesceContents() QPDFJob::Config* QPDFJob::Config::collate(char const* parameter) { - auto n = ((parameter == 0) ? 1 : + auto n = (((parameter == 0) || (strlen(parameter) == 0)) ? 1 : QUtil::string_to_uint(parameter)); o.m->collate = QIntC::to_size(n); return this; @@ -519,7 +519,7 @@ QPDFJob::Config::showXref() QPDFJob::Config* QPDFJob::Config::splitPages(char const* parameter) { - int n = ((parameter == 0) ? 1 : + int n = (((parameter == 0) || (strlen(parameter) == 0)) ? 1 : QUtil::string_to_int(parameter)); o.m->split_pages = n; return this; diff --git a/libqpdf/QPDFJob_json.cc b/libqpdf/QPDFJob_json.cc index 3091c7b1..cc4e2ff7 100644 --- a/libqpdf/QPDFJob_json.cc +++ b/libqpdf/QPDFJob_json.cc @@ -31,7 +31,7 @@ namespace void addBare(bare_handler_t); void addParameter(param_handler_t); - void addChoices(char const** choices, param_handler_t); + void addChoices(char const** choices, bool required, param_handler_t); void pushKey(std::string const& key); void beginDict(json_handler_t start_fn, bare_handler_t end_fn); @@ -106,11 +106,12 @@ Handlers::initHandlers() void Handlers::addBare(bare_handler_t fn) { - jh->addBoolHandler([this, fn](std::string const& path, bool v){ - if (! v) + jh->addStringHandler( + [this, fn](std::string const& path, std::string const& parameter){ + if (! parameter.empty()) { - QTC::TC("qpdf", "QPDFJob json bare not true"); - usage(path + ": value must be true"); + QTC::TC("qpdf", "QPDFJob json bare not empty"); + usage(path + ": value must be the empty string"); } else { @@ -129,22 +130,28 @@ Handlers::addParameter(param_handler_t fn) } void -Handlers::addChoices(char const** choices, - param_handler_t fn) +Handlers::addChoices(char const** choices, bool required, param_handler_t fn) { jh->addStringHandler( - [fn, choices, this]( + [fn, choices, required, this]( std::string const& path, std::string const& parameter){ char const* p = parameter.c_str(); bool matches = false; - for (char const** i = choices; *i; ++i) + if ((! required) && (parameter.empty())) + { + matches = true; + } + if (! matches) { - if (strcmp(*i, p) == 0) + for (char const** i = choices; *i; ++i) { - QTC::TC("qpdf", "QPDFJob json choice match"); - matches = true; - break; + if (strcmp(*i, p) == 0) + { + QTC::TC("qpdf", "QPDFJob json choice match"); + matches = true; + break; + } } } if (! matches) diff --git a/libqpdf/qpdf/auto_job_json_init.hh b/libqpdf/qpdf/auto_job_json_init.hh index 3d19c76e..833d5471 100644 --- a/libqpdf/qpdf/auto_job_json_init.hh +++ b/libqpdf/qpdf/auto_job_json_init.hh @@ -43,19 +43,19 @@ pushKey("newlineBeforeEndstream"); addBare([this]() { c_main->newlineBeforeEndstream(); }); popHandler(); // key: newlineBeforeEndstream pushKey("normalizeContent"); -addChoices(yn_choices, [this](char const* p) { c_main->normalizeContent(p); }); +addChoices(yn_choices, true, [this](char const* p) { c_main->normalizeContent(p); }); popHandler(); // key: normalizeContent pushKey("streamData"); -addChoices(stream_data_choices, [this](char const* p) { c_main->streamData(p); }); +addChoices(stream_data_choices, true, [this](char const* p) { c_main->streamData(p); }); popHandler(); // key: streamData pushKey("compressStreams"); -addChoices(yn_choices, [this](char const* p) { c_main->compressStreams(p); }); +addChoices(yn_choices, true, [this](char const* p) { c_main->compressStreams(p); }); popHandler(); // key: compressStreams pushKey("recompressFlate"); addBare([this]() { c_main->recompressFlate(); }); popHandler(); // key: recompressFlate pushKey("decodeLevel"); -addChoices(decode_level_choices, [this](char const* p) { c_main->decodeLevel(p); }); +addChoices(decode_level_choices, true, [this](char const* p) { c_main->decodeLevel(p); }); popHandler(); // key: decodeLevel pushKey("decrypt"); addBare([this]() { c_main->decrypt(); }); @@ -85,7 +85,7 @@ pushKey("linearizePass1"); addParameter([this](char const* p) { c_main->linearizePass1(p); }); popHandler(); // key: linearizePass1 pushKey("objectStreams"); -addChoices(object_streams_choices, [this](char const* p) { c_main->objectStreams(p); }); +addChoices(object_streams_choices, true, [this](char const* p) { c_main->objectStreams(p); }); popHandler(); // key: objectStreams pushKey("minVersion"); addParameter([this](char const* p) { c_main->minVersion(p); }); @@ -110,82 +110,82 @@ popHandler(); // key: ownerPassword pushKey("40bit"); beginDict(bindJSON(&Handlers::beginEncrypt40bit), bindBare(&Handlers::endEncrypt40bit)); // .encrypt.40bit pushKey("annotate"); -addChoices(yn_choices, [this](char const* p) { c_enc->annotate(p); }); +addChoices(yn_choices, true, [this](char const* p) { c_enc->annotate(p); }); popHandler(); // key: annotate pushKey("extract"); -addChoices(yn_choices, [this](char const* p) { c_enc->extract(p); }); +addChoices(yn_choices, true, [this](char const* p) { c_enc->extract(p); }); popHandler(); // key: extract pushKey("modify"); -addChoices(modify128_choices, [this](char const* p) { c_enc->modify(p); }); +addChoices(modify128_choices, true, [this](char const* p) { c_enc->modify(p); }); popHandler(); // key: modify pushKey("print"); -addChoices(print128_choices, [this](char const* p) { c_enc->print(p); }); +addChoices(print128_choices, true, [this](char const* p) { c_enc->print(p); }); popHandler(); // key: print popHandler(); // key: 40bit pushKey("128bit"); beginDict(bindJSON(&Handlers::beginEncrypt128bit), bindBare(&Handlers::endEncrypt128bit)); // .encrypt.128bit pushKey("accessibility"); -addChoices(yn_choices, [this](char const* p) { c_enc->accessibility(p); }); +addChoices(yn_choices, true, [this](char const* p) { c_enc->accessibility(p); }); popHandler(); // key: accessibility pushKey("annotate"); -addChoices(yn_choices, [this](char const* p) { c_enc->annotate(p); }); +addChoices(yn_choices, true, [this](char const* p) { c_enc->annotate(p); }); popHandler(); // key: annotate pushKey("assemble"); -addChoices(yn_choices, [this](char const* p) { c_enc->assemble(p); }); +addChoices(yn_choices, true, [this](char const* p) { c_enc->assemble(p); }); popHandler(); // key: assemble pushKey("cleartextMetadata"); addBare([this]() { c_enc->cleartextMetadata(); }); popHandler(); // key: cleartextMetadata pushKey("extract"); -addChoices(yn_choices, [this](char const* p) { c_enc->extract(p); }); +addChoices(yn_choices, true, [this](char const* p) { c_enc->extract(p); }); popHandler(); // key: extract pushKey("form"); -addChoices(yn_choices, [this](char const* p) { c_enc->form(p); }); +addChoices(yn_choices, true, [this](char const* p) { c_enc->form(p); }); popHandler(); // key: form pushKey("modifyOther"); -addChoices(yn_choices, [this](char const* p) { c_enc->modifyOther(p); }); +addChoices(yn_choices, true, [this](char const* p) { c_enc->modifyOther(p); }); popHandler(); // key: modifyOther pushKey("modify"); -addChoices(modify128_choices, [this](char const* p) { c_enc->modify(p); }); +addChoices(modify128_choices, true, [this](char const* p) { c_enc->modify(p); }); popHandler(); // key: modify pushKey("print"); -addChoices(print128_choices, [this](char const* p) { c_enc->print(p); }); +addChoices(print128_choices, true, [this](char const* p) { c_enc->print(p); }); popHandler(); // key: print pushKey("forceV4"); addBare([this]() { c_enc->forceV4(); }); popHandler(); // key: forceV4 pushKey("useAes"); -addChoices(yn_choices, [this](char const* p) { c_enc->useAes(p); }); +addChoices(yn_choices, true, [this](char const* p) { c_enc->useAes(p); }); popHandler(); // key: useAes popHandler(); // key: 128bit pushKey("256bit"); beginDict(bindJSON(&Handlers::beginEncrypt256bit), bindBare(&Handlers::endEncrypt256bit)); // .encrypt.256bit pushKey("accessibility"); -addChoices(yn_choices, [this](char const* p) { c_enc->accessibility(p); }); +addChoices(yn_choices, true, [this](char const* p) { c_enc->accessibility(p); }); popHandler(); // key: accessibility pushKey("annotate"); -addChoices(yn_choices, [this](char const* p) { c_enc->annotate(p); }); +addChoices(yn_choices, true, [this](char const* p) { c_enc->annotate(p); }); popHandler(); // key: annotate pushKey("assemble"); -addChoices(yn_choices, [this](char const* p) { c_enc->assemble(p); }); +addChoices(yn_choices, true, [this](char const* p) { c_enc->assemble(p); }); popHandler(); // key: assemble pushKey("cleartextMetadata"); addBare([this]() { c_enc->cleartextMetadata(); }); popHandler(); // key: cleartextMetadata pushKey("extract"); -addChoices(yn_choices, [this](char const* p) { c_enc->extract(p); }); +addChoices(yn_choices, true, [this](char const* p) { c_enc->extract(p); }); popHandler(); // key: extract pushKey("form"); -addChoices(yn_choices, [this](char const* p) { c_enc->form(p); }); +addChoices(yn_choices, true, [this](char const* p) { c_enc->form(p); }); popHandler(); // key: form pushKey("modifyOther"); -addChoices(yn_choices, [this](char const* p) { c_enc->modifyOther(p); }); +addChoices(yn_choices, true, [this](char const* p) { c_enc->modifyOther(p); }); popHandler(); // key: modifyOther pushKey("modify"); -addChoices(modify128_choices, [this](char const* p) { c_enc->modify(p); }); +addChoices(modify128_choices, true, [this](char const* p) { c_enc->modify(p); }); popHandler(); // key: modify pushKey("print"); -addChoices(print128_choices, [this](char const* p) { c_enc->print(p); }); +addChoices(print128_choices, true, [this](char const* p) { c_enc->print(p); }); popHandler(); // key: print pushKey("allowInsecure"); addBare([this]() { c_enc->allowInsecure(); }); @@ -238,11 +238,11 @@ pushKey("showAttachment"); addParameter([this](char const* p) { c_main->showAttachment(p); }); popHandler(); // key: showAttachment pushKey("json"); -addChoices(json_version_choices, [this](char const* p) { c_main->json(p); }); +addChoices(json_version_choices, false, [this](char const* p) { c_main->json(p); }); popHandler(); // key: json pushKey("jsonKey"); beginArray(bindJSON(&Handlers::beginJsonKeyArray), bindBare(&Handlers::endJsonKeyArray)); // .jsonKey[] -addChoices(json_key_choices, [this](char const* p) { c_main->jsonKey(p); }); +addChoices(json_key_choices, true, [this](char const* p) { c_main->jsonKey(p); }); popHandler(); // array: .jsonKey[] popHandler(); // key: jsonKey pushKey("jsonObject"); @@ -254,7 +254,7 @@ pushKey("allowWeakCrypto"); addBare([this]() { c_main->allowWeakCrypto(); }); popHandler(); // key: allowWeakCrypto pushKey("keepFilesOpen"); -addChoices(yn_choices, [this](char const* p) { c_main->keepFilesOpen(p); }); +addChoices(yn_choices, true, [this](char const* p) { c_main->keepFilesOpen(p); }); popHandler(); // key: keepFilesOpen pushKey("keepFilesOpenThreshold"); addParameter([this](char const* p) { c_main->keepFilesOpenThreshold(p); }); @@ -272,7 +272,7 @@ pushKey("passwordIsHexKey"); addBare([this]() { c_main->passwordIsHexKey(); }); popHandler(); // key: passwordIsHexKey pushKey("passwordMode"); -addChoices(password_mode_choices, [this](char const* p) { c_main->passwordMode(p); }); +addChoices(password_mode_choices, true, [this](char const* p) { c_main->passwordMode(p); }); popHandler(); // key: passwordMode pushKey("suppressPasswordRecovery"); addBare([this]() { c_main->suppressPasswordRecovery(); }); @@ -293,7 +293,7 @@ pushKey("iiMinBytes"); addParameter([this](char const* p) { c_main->iiMinBytes(p); }); popHandler(); // key: iiMinBytes pushKey("removeUnreferencedResources"); -addChoices(remove_unref_choices, [this](char const* p) { c_main->removeUnreferencedResources(p); }); +addChoices(remove_unref_choices, true, [this](char const* p) { c_main->removeUnreferencedResources(p); }); popHandler(); // key: removeUnreferencedResources pushKey("addAttachment"); beginArray(bindJSON(&Handlers::beginAddAttachmentArray), bindBare(&Handlers::endAddAttachmentArray)); // .addAttachment[] @@ -345,7 +345,7 @@ pushKey("collate"); addParameter([this](char const* p) { c_main->collate(p); }); popHandler(); // key: collate pushKey("flattenAnnotations"); -addChoices(flatten_choices, [this](char const* p) { c_main->flattenAnnotations(p); }); +addChoices(flatten_choices, true, [this](char const* p) { c_main->flattenAnnotations(p); }); popHandler(); // key: flattenAnnotations pushKey("flattenRotation"); addBare([this]() { c_main->flattenRotation(); }); |