From 0a354af02cf51aeb1602596988c8f826b47b3a81 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Tue, 25 Jan 2022 16:37:17 -0500 Subject: QPDFJob: convert AddAttachment handlers --- libqpdf/QPDFJob_argv.cc | 91 ++--------------------------- libqpdf/QPDFJob_config.cc | 124 +++++++++++++++++++++++++++++++++++++++- libqpdf/qpdf/auto_job_decl.hh | 7 --- libqpdf/qpdf/auto_job_init.hh | 14 ++--- libqpdf/qpdf/auto_job_schema.hh | 4 +- 5 files changed, 138 insertions(+), 102 deletions(-) (limited to 'libqpdf') diff --git a/libqpdf/QPDFJob_argv.cc b/libqpdf/QPDFJob_argv.cc index af7ce13f..a32818ba 100644 --- a/libqpdf/QPDFJob_argv.cc +++ b/libqpdf/QPDFJob_argv.cc @@ -45,6 +45,7 @@ namespace QPDFJob& o; std::shared_ptr c_main; std::shared_ptr c_copy_att; + std::shared_ptr c_att; std::vector accumulated_args; // points to member in ap char* pages_password; }; @@ -441,7 +442,7 @@ ArgParser::argRotate(char* parameter) void ArgParser::argAddAttachment() { - o.attachments_to_add.push_back(QPDFJob::AddAttachment()); + this->c_att = c_main->addAttachment(); this->ap.selectOptionTable(O_ATTACHMENT); } @@ -792,100 +793,20 @@ ArgParser::argEndUnderlayOverlay() void ArgParser::argAttPositional(char* arg) { - o.attachments_to_add.back().path = arg; -} - -void -ArgParser::argAttKey(char* parameter) -{ - o.attachments_to_add.back().key = parameter; -} - -void -ArgParser::argAttFilename(char* parameter) -{ - o.attachments_to_add.back().filename = parameter; -} - -void -ArgParser::argAttCreationdate(char* parameter) -{ - if (! QUtil::pdf_time_to_qpdf_time(parameter)) - { - usage(std::string(parameter) + " is not a valid PDF timestamp"); - } - o.attachments_to_add.back().creationdate = parameter; -} - -void -ArgParser::argAttModdate(char* parameter) -{ - if (! QUtil::pdf_time_to_qpdf_time(parameter)) - { - usage(std::string(parameter) + " is not a valid PDF timestamp"); - } - o.attachments_to_add.back().moddate = parameter; -} - -void -ArgParser::argAttMimetype(char* parameter) -{ - if (strchr(parameter, '/') == nullptr) - { - usage("mime type should be specified as type/subtype"); - } - o.attachments_to_add.back().mimetype = parameter; -} - -void -ArgParser::argAttDescription(char* parameter) -{ - o.attachments_to_add.back().description = parameter; -} - -void -ArgParser::argAttReplace() -{ - o.attachments_to_add.back().replace = true; + c_att->path(arg); } void ArgParser::argEndAttachment() { - static std::string now = QUtil::qpdf_time_to_pdf_time( - QUtil::get_current_qpdf_time()); - auto& cur = o.attachments_to_add.back(); - if (cur.path.empty()) - { - usage("add attachment: no path specified"); - } - std::string last_element = QUtil::path_basename(cur.path); - if (last_element.empty()) - { - usage("path for --add-attachment may not be empty"); - } - if (cur.filename.empty()) - { - cur.filename = last_element; - } - if (cur.key.empty()) - { - cur.key = last_element; - } - if (cur.creationdate.empty()) - { - cur.creationdate = now; - } - if (cur.moddate.empty()) - { - cur.moddate = now; - } + c_att->end(); + c_att = nullptr; } void ArgParser::argCopyAttPositional(char* arg) { - c_copy_att->filename(arg); + c_copy_att->path(arg); } void diff --git a/libqpdf/QPDFJob_config.cc b/libqpdf/QPDFJob_config.cc index 34f15b9f..ad970aa4 100644 --- a/libqpdf/QPDFJob_config.cc +++ b/libqpdf/QPDFJob_config.cc @@ -509,7 +509,7 @@ QPDFJob::CopyAttConfig::CopyAttConfig(Config& c) : } QPDFJob::CopyAttConfig& -QPDFJob::CopyAttConfig::filename(char const* parameter) +QPDFJob::CopyAttConfig::path(char const* parameter) { this->caf.path = parameter; return *this; @@ -541,3 +541,125 @@ QPDFJob::CopyAttConfig::end() this->config.o.attachments_to_copy.push_back(this->caf); return this->config; } + +QPDFJob::AttConfig::AttConfig(Config& c) : + config(c) +{ +} + +std::shared_ptr +QPDFJob::Config::addAttachment() +{ + return std::shared_ptr(new AttConfig(*this)); +} + +QPDFJob::AttConfig& +QPDFJob::AttConfig::path(char const* parameter) +{ + this->att.path = parameter; + return *this; +} + +QPDFJob::AttConfig& +QPDFJob::AttConfig::key(char const* parameter) +{ + this->att.key = parameter; + return *this; +} + +QPDFJob::AttConfig& +QPDFJob::AttConfig::filename(char const* parameter) +{ + this->att.filename = parameter; + return *this; +} + +QPDFJob::AttConfig& +QPDFJob::AttConfig::creationdate(char const* parameter) +{ + if (! QUtil::pdf_time_to_qpdf_time(parameter)) + { + // QXXXQ + throw std::runtime_error( + std::string(parameter) + " is not a valid PDF timestamp"); + } + this->att.creationdate = parameter; + return *this; +} + +QPDFJob::AttConfig& +QPDFJob::AttConfig::moddate(char const* parameter) +{ + if (! QUtil::pdf_time_to_qpdf_time(parameter)) + { + // QXXXQ + throw std::runtime_error( + std::string(parameter) + " is not a valid PDF timestamp"); + } + this->att.moddate = parameter; + return *this; +} + +QPDFJob::AttConfig& +QPDFJob::AttConfig::mimetype(char const* parameter) +{ + if (strchr(parameter, '/') == nullptr) + { + // QXXXQ + throw std::runtime_error( + "mime type should be specified as type/subtype"); + } + this->att.mimetype = parameter; + return *this; +} + +QPDFJob::AttConfig& +QPDFJob::AttConfig::description(char const* parameter) +{ + this->att.description = parameter; + return *this; +} + +QPDFJob::AttConfig& +QPDFJob::AttConfig::replace() +{ + this->att.replace = true; + return *this; +} + +QPDFJob::Config& +QPDFJob::AttConfig::end() +{ + // QXXXQ runtime_error + + static std::string now = QUtil::qpdf_time_to_pdf_time( + QUtil::get_current_qpdf_time()); + if (this->att.path.empty()) + { + throw std::runtime_error("add attachment: no path specified"); + } + std::string last_element = QUtil::path_basename(this->att.path); + if (last_element.empty()) + { + throw std::runtime_error("path for --add-attachment may not be empty"); + } + if (this->att.filename.empty()) + { + this->att.filename = last_element; + } + if (this->att.key.empty()) + { + this->att.key = last_element; + } + if (this->att.creationdate.empty()) + { + this->att.creationdate = now; + } + if (this->att.moddate.empty()) + { + this->att.moddate = now; + } + + this->config.o.attachments_to_add.push_back(this->att); + return this->config; +} diff --git a/libqpdf/qpdf/auto_job_decl.hh b/libqpdf/qpdf/auto_job_decl.hh index 1ea25372..98e1d6e3 100644 --- a/libqpdf/qpdf/auto_job_decl.hh +++ b/libqpdf/qpdf/auto_job_decl.hh @@ -65,13 +65,6 @@ void argUORepeat(char *); void argUOPassword(char *); void argEndUnderlayOverlay(); void argAttPositional(char*); -void argAttReplace(); -void argAttKey(char *); -void argAttFilename(char *); -void argAttCreationdate(char *); -void argAttModdate(char *); -void argAttMimetype(char *); -void argAttDescription(char *); void argEndAttachment(); void argCopyAttPositional(char*); void argEndCopyAttachment(); diff --git a/libqpdf/qpdf/auto_job_init.hh b/libqpdf/qpdf/auto_job_init.hh index 7ceff2ae..d6bb63f3 100644 --- a/libqpdf/qpdf/auto_job_init.hh +++ b/libqpdf/qpdf/auto_job_init.hh @@ -143,13 +143,13 @@ this->ap.addRequiredParameter("repeat", p(&ArgParser::argUORepeat), "page-range" this->ap.addRequiredParameter("password", p(&ArgParser::argUOPassword), "password"); this->ap.registerOptionTable("attachment", b(&ArgParser::argEndAttachment)); this->ap.addPositional(p(&ArgParser::argAttPositional)); -this->ap.addBare("replace", b(&ArgParser::argAttReplace)); -this->ap.addRequiredParameter("key", p(&ArgParser::argAttKey), "attachment-key"); -this->ap.addRequiredParameter("filename", p(&ArgParser::argAttFilename), "filename"); -this->ap.addRequiredParameter("creationdate", p(&ArgParser::argAttCreationdate), "creation-date"); -this->ap.addRequiredParameter("moddate", p(&ArgParser::argAttModdate), "modification-date"); -this->ap.addRequiredParameter("mimetype", p(&ArgParser::argAttMimetype), "mime/type"); -this->ap.addRequiredParameter("description", p(&ArgParser::argAttDescription), "description"); +this->ap.addBare("replace", [this](){c_att->replace();}); +this->ap.addRequiredParameter("key", [this](char *x){c_att->key(x);}, "attachment-key"); +this->ap.addRequiredParameter("filename", [this](char *x){c_att->filename(x);}, "filename"); +this->ap.addRequiredParameter("creationdate", [this](char *x){c_att->creationdate(x);}, "creation-date"); +this->ap.addRequiredParameter("moddate", [this](char *x){c_att->moddate(x);}, "modification-date"); +this->ap.addRequiredParameter("mimetype", [this](char *x){c_att->mimetype(x);}, "mime/type"); +this->ap.addRequiredParameter("description", [this](char *x){c_att->description(x);}, "description"); this->ap.registerOptionTable("copy attachment", b(&ArgParser::argEndCopyAttachment)); this->ap.addPositional(p(&ArgParser::argCopyAttPositional)); this->ap.addRequiredParameter("prefix", [this](char *x){c_copy_att->prefix(x);}, "prefix"); diff --git a/libqpdf/qpdf/auto_job_schema.hh b/libqpdf/qpdf/auto_job_schema.hh index 9ecdc507..7fe018af 100644 --- a/libqpdf/qpdf/auto_job_schema.hh +++ b/libqpdf/qpdf/auto_job_schema.hh @@ -122,7 +122,7 @@ static constexpr char const* JOB_SCHEMA_DATA = R"({ "modify": { "addAttachment": [ { - "file": "attachment to add", + "path": "attachment to add", "creationdate": "set attachment's creation date", "description": "set attachment's description", "filename": "set attachment's displayed filename", @@ -135,7 +135,7 @@ static constexpr char const* JOB_SCHEMA_DATA = R"({ "removeAttachment": "remove an embedded file", "copyAttachmentsFrom": [ { - "file": "attachment source filename", + "path": "attachment source filename", "password": "specify password", "prefix": "key prefix for copying attachments" } -- cgit v1.2.3-54-g00ecf