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