Commit 0f67d851398d0647ad2b24abebb1e92ddf72cbdc

Authored by m-holger
1 parent a43b8312

Refactor `QPDFJob::Inputs`: add `clear` method to centralize QPDF object cleanup…

…, replace redundant file warning logic in `handlePageSpecs`, and simplify resource management.
include/qpdf/QPDFJob.hh
@@ -461,7 +461,7 @@ class QPDFJob @@ -461,7 +461,7 @@ class QPDFJob
461 461
462 // Transformations 462 // Transformations
463 void setQPDFOptions(QPDF& pdf); 463 void setQPDFOptions(QPDF& pdf);
464 - bool handlePageSpecs(QPDF& pdf); 464 + void handlePageSpecs(QPDF& pdf);
465 bool shouldRemoveUnreferencedResources(QPDF& pdf); 465 bool shouldRemoveUnreferencedResources(QPDF& pdf);
466 void new_selection( 466 void new_selection(
467 std::string const& filename, std::string const& password, std::string const& range); 467 std::string const& filename, std::string const& password, std::string const& range);
libqpdf/QPDFJob.cc
@@ -426,15 +426,14 @@ QPDFJob::createQPDF() @@ -426,15 +426,14 @@ QPDFJob::createQPDF()
426 } 426 }
427 427
428 if (!m->selections.empty()) { 428 if (!m->selections.empty()) {
429 - if (!handlePageSpecs(pdf)) {  
430 - m->warnings = true;  
431 - } 429 + handlePageSpecs(pdf);
432 } 430 }
433 if (!m->rotations.empty()) { 431 if (!m->rotations.empty()) {
434 handleRotations(pdf); 432 handleRotations(pdf);
435 } 433 }
436 handleUnderOverlay(pdf); 434 handleUnderOverlay(pdf);
437 handleTransformations(pdf); 435 handleTransformations(pdf);
  436 + m->warnings |= m->inputs.clear();
438 437
439 auto root = pdf.getRoot(); 438 auto root = pdf.getRoot();
440 if (m->remove_info) { 439 if (m->remove_info) {
@@ -2414,8 +2413,21 @@ QPDFJob::Inputs::process_all() @@ -2414,8 +2413,21 @@ QPDFJob::Inputs::process_all()
2414 } 2413 }
2415 } 2414 }
2416 2415
2417 -// Handle all page specifications. Return true if it succeeded without warnings.  
2418 bool 2416 bool
  2417 +QPDFJob::Inputs::clear()
  2418 +{
  2419 + bool any_warnings = false;
  2420 + for (auto& [filename, file_spec]: files) {
  2421 + if (auto& pdf = file_spec.qpdf_p) {
  2422 + any_warnings |= pdf->anyWarnings();
  2423 + pdf = nullptr;
  2424 + }
  2425 + }
  2426 + return any_warnings;
  2427 +}
  2428 +
  2429 +// Handle all page specifications.
  2430 +void
2419 QPDFJob::handlePageSpecs(QPDF& pdf) 2431 QPDFJob::handlePageSpecs(QPDF& pdf)
2420 { 2432 {
2421 auto& main_input = m->inputs.files[m->infilename]; 2433 auto& main_input = m->inputs.files[m->infilename];
@@ -2615,14 +2627,6 @@ QPDFJob::handlePageSpecs(QPDF& pdf) @@ -2615,14 +2627,6 @@ QPDFJob::handlePageSpecs(QPDF& pdf)
2615 } 2627 }
2616 } 2628 }
2617 } 2629 }
2618 - for (auto& foreign: m->inputs.files) {  
2619 - if (foreign.second.qpdf_p) { // exclude main input  
2620 - if (foreign.second.qpdf->anyWarnings()) {  
2621 - return false;  
2622 - }  
2623 - }  
2624 - }  
2625 - return true;  
2626 } 2630 }
2627 2631
2628 void 2632 void
libqpdf/qpdf/QPDFJob_private.hh
@@ -62,6 +62,10 @@ struct QPDFJob::Inputs @@ -62,6 +62,10 @@ struct QPDFJob::Inputs
62 } 62 }
63 void process(std::string const& filename, QPDFJob::Input& file_spec); 63 void process(std::string const& filename, QPDFJob::Input& file_spec);
64 void process_all(); 64 void process_all();
  65 +
  66 + // Destroy all owned QPDF objects. Return false if any of the QPDF objects recorded warnings.
  67 + bool clear();
  68 +
65 std::string encryption_file; 69 std::string encryption_file;
66 std::string encryption_file_password; 70 std::string encryption_file_password;
67 bool keep_files_open{true}; 71 bool keep_files_open{true};