Commit 43d9ee56ea958428ce3a8e395a029035d81f914f

Authored by m-holger
1 parent 4359de90

Split QPDFJob::run into createQPDF and writeQPDF

Also, change QPDFJob to use unique_ptr<QPDF> instead of shared pointers.
include/qpdf/QPDFJob.hh
... ... @@ -397,6 +397,21 @@ class QPDFJob
397 397 QPDF_DLL
398 398 void run();
399 399  
  400 + // The following two methods allow a job to be run in two stages - creation
  401 + // of a QPDF object and writing of the QPDF object. This allows the QPDF
  402 + // object to be modified prior to writing it out. See
  403 + // examples/qpdfjob-remove-annotations for an illustration of its use.
  404 +
  405 + // Run the first stage of the job. Return a nullptr if the configuration is
  406 + // not valid.
  407 + QPDF_DLL
  408 + std::unique_ptr<QPDF> createQPDF();
  409 +
  410 + // Run the second stage of the job. Do nothing if a nullptr is passed as
  411 + // parameter.
  412 + QPDF_DLL
  413 + void writeQPDF(QPDF& qpdf);
  414 +
400 415 // CHECK STATUS -- these methods provide information known after
401 416 // run() is called.
402 417  
... ... @@ -474,7 +489,7 @@ class QPDFJob
474 489 std::string to_nr;
475 490 std::string from_nr;
476 491 std::string repeat_nr;
477   - std::shared_ptr<QPDF> pdf;
  492 + std::unique_ptr<QPDF> pdf;
478 493 std::vector<int> to_pagenos;
479 494 std::vector<int> from_pagenos;
480 495 std::vector<int> repeat_pagenos;
... ... @@ -490,25 +505,25 @@ class QPDFJob
490 505  
491 506 // Basic file processing
492 507 void processFile(
493   - std::shared_ptr<QPDF>&,
  508 + std::unique_ptr<QPDF>&,
494 509 char const* filename,
495 510 char const* password,
496 511 bool used_for_input,
497 512 bool main_input);
498 513 void processInputSource(
499   - std::shared_ptr<QPDF>&,
  514 + std::unique_ptr<QPDF>&,
500 515 std::shared_ptr<InputSource> is,
501 516 char const* password,
502 517 bool used_for_input);
503 518 void doProcess(
504   - std::shared_ptr<QPDF>&,
  519 + std::unique_ptr<QPDF>&,
505 520 std::function<void(QPDF*, char const*)> fn,
506 521 char const* password,
507 522 bool empty,
508 523 bool used_for_input,
509 524 bool main_input);
510 525 void doProcessOnce(
511   - std::shared_ptr<QPDF>&,
  526 + std::unique_ptr<QPDF>&,
512 527 std::function<void(QPDF*, char const*)> fn,
513 528 char const* password,
514 529 bool empty,
... ... @@ -518,7 +533,7 @@ class QPDFJob
518 533 // Transformations
519 534 void setQPDFOptions(QPDF& pdf);
520 535 void
521   - handlePageSpecs(QPDF& pdf, std::vector<std::shared_ptr<QPDF>>& page_heap);
  536 + handlePageSpecs(QPDF& pdf, std::vector<std::unique_ptr<QPDF>>& page_heap);
522 537 bool shouldRemoveUnreferencedResources(QPDF& pdf);
523 538 void handleRotations(QPDF& pdf);
524 539 void
... ...
libqpdf/QPDFJob.cc
... ... @@ -447,11 +447,11 @@ QPDFJob::parseNumrange(char const* range, int max)
447 447 return std::vector<int>();
448 448 }
449 449  
450   -void
451   -QPDFJob::run()
  450 +std::unique_ptr<QPDF>
  451 +QPDFJob::createQPDF()
452 452 {
453 453 checkConfiguration();
454   - std::shared_ptr<QPDF> pdf_sp;
  454 + std::unique_ptr<QPDF> pdf_sp;
455 455 try {
456 456 processFile(pdf_sp, m->infilename.get(), m->password.get(), true, true);
457 457 } catch (QPDFExc& e) {
... ... @@ -461,12 +461,12 @@ QPDFJob::run()
461 461 if (m->check_is_encrypted || m->check_requires_password) {
462 462 this->m->encryption_status =
463 463 qpdf_es_encrypted | qpdf_es_password_incorrect;
464   - return;
  464 + return nullptr;
465 465 }
466 466 if (m->show_encryption && pdf_sp) {
467 467 this->m->log->info("Incorrect password supplied\n");
468 468 showEncryption(*pdf_sp);
469   - return;
  469 + return nullptr;
470 470 }
471 471 }
472 472 throw e;
... ... @@ -477,7 +477,7 @@ QPDFJob::run()
477 477 }
478 478  
479 479 if (m->check_is_encrypted || m->check_requires_password) {
480   - return;
  480 + return nullptr;
481 481 }
482 482  
483 483 // If we are updating from JSON, this has to be done first before
... ... @@ -486,7 +486,7 @@ QPDFJob::run()
486 486 pdf.updateFromJSON(this->m->update_from_json);
487 487 }
488 488  
489   - std::vector<std::shared_ptr<QPDF>> page_heap;
  489 + std::vector<std::unique_ptr<QPDF>> page_heap;
490 490 if (!m->page_specs.empty()) {
491 491 handlePageSpecs(pdf, page_heap);
492 492 }
... ... @@ -495,7 +495,12 @@ QPDFJob::run()
495 495 }
496 496 handleUnderOverlay(pdf);
497 497 handleTransformations(pdf);
  498 + return pdf_sp;
  499 +}
498 500  
  501 +void
  502 +QPDFJob::writeQPDF(QPDF& pdf)
  503 +{
499 504 if (!createsOutput()) {
500 505 doInspection(pdf);
501 506 } else if (m->split_pages) {
... ... @@ -527,6 +532,15 @@ QPDFJob::run()
527 532 }
528 533 }
529 534  
  535 +void
  536 +QPDFJob::run()
  537 +{
  538 + auto pdf = createQPDF();
  539 + if (pdf) {
  540 + writeQPDF(*pdf);
  541 + }
  542 +}
  543 +
530 544 bool
531 545 QPDFJob::hasWarnings() const
532 546 {
... ... @@ -1868,14 +1882,14 @@ QPDFJob::doInspection(QPDF&amp; pdf)
1868 1882  
1869 1883 void
1870 1884 QPDFJob::doProcessOnce(
1871   - std::shared_ptr<QPDF>& pdf,
  1885 + std::unique_ptr<QPDF>& pdf,
1872 1886 std::function<void(QPDF*, char const*)> fn,
1873 1887 char const* password,
1874 1888 bool empty,
1875 1889 bool used_for_input,
1876 1890 bool main_input)
1877 1891 {
1878   - pdf = QPDF::create();
  1892 + pdf = std::make_unique<QPDF>();
1879 1893 setQPDFOptions(*pdf);
1880 1894 if (empty) {
1881 1895 pdf->emptyPDF();
... ... @@ -1892,7 +1906,7 @@ QPDFJob::doProcessOnce(
1892 1906  
1893 1907 void
1894 1908 QPDFJob::doProcess(
1895   - std::shared_ptr<QPDF>& pdf,
  1909 + std::unique_ptr<QPDF>& pdf,
1896 1910 std::function<void(QPDF*, char const*)> fn,
1897 1911 char const* password,
1898 1912 bool empty,
... ... @@ -1976,7 +1990,7 @@ QPDFJob::doProcess(
1976 1990  
1977 1991 void
1978 1992 QPDFJob::processFile(
1979   - std::shared_ptr<QPDF>& pdf,
  1993 + std::unique_ptr<QPDF>& pdf,
1980 1994 char const* filename,
1981 1995 char const* password,
1982 1996 bool used_for_input,
... ... @@ -1996,7 +2010,7 @@ QPDFJob::processFile(
1996 2010  
1997 2011 void
1998 2012 QPDFJob::processInputSource(
1999   - std::shared_ptr<QPDF>& pdf,
  2013 + std::unique_ptr<QPDF>& pdf,
2000 2014 std::shared_ptr<InputSource> is,
2001 2015 char const* password,
2002 2016 bool used_for_input)
... ... @@ -2278,7 +2292,7 @@ QPDFJob::copyAttachments(QPDF&amp; pdf)
2278 2292 v << prefix << ": copying attachments from " << to_copy.path
2279 2293 << "\n";
2280 2294 });
2281   - std::shared_ptr<QPDF> other;
  2295 + std::unique_ptr<QPDF> other;
2282 2296 processFile(
2283 2297 other,
2284 2298 to_copy.path.c_str(),
... ... @@ -2540,7 +2554,7 @@ added_page(QPDF&amp; pdf, QPDFPageObjectHelper page)
2540 2554  
2541 2555 void
2542 2556 QPDFJob::handlePageSpecs(
2543   - QPDF& pdf, std::vector<std::shared_ptr<QPDF>>& page_heap)
  2557 + QPDF& pdf, std::vector<std::unique_ptr<QPDF>>& page_heap)
2544 2558 {
2545 2559 // Parse all page specifications and translate them into lists of
2546 2560 // actual pages.
... ... @@ -2612,10 +2626,10 @@ QPDFJob::handlePageSpecs(
2612 2626 new FileInputSource(page_spec.filename.c_str());
2613 2627 is = std::shared_ptr<InputSource>(fis);
2614 2628 }
2615   - std::shared_ptr<QPDF> qpdf_sp;
  2629 + std::unique_ptr<QPDF> qpdf_sp;
2616 2630 processInputSource(qpdf_sp, is, password, true);
2617   - page_heap.push_back(qpdf_sp);
2618 2631 page_spec_qpdfs[page_spec.filename] = qpdf_sp.get();
  2632 + page_heap.push_back(std::move(qpdf_sp));
2619 2633 if (cis) {
2620 2634 cis->stayOpen(false);
2621 2635 page_spec_cfis[page_spec.filename] = cis;
... ... @@ -3116,7 +3130,7 @@ QPDFJob::setWriterOptions(QPDF&amp; pdf, QPDFWriter&amp; w)
3116 3130 w.setSuppressOriginalObjectIDs(true);
3117 3131 }
3118 3132 if (m->copy_encryption) {
3119   - std::shared_ptr<QPDF> encryption_pdf;
  3133 + std::unique_ptr<QPDF> encryption_pdf;
3120 3134 processFile(
3121 3135 encryption_pdf,
3122 3136 m->encryption_file.c_str(),
... ...