Commit dcd53ffc7046006dafc561b8cfbccc961ef91831

Authored by m-holger
1 parent 98d8d472

Refactor `QPDFJob::handlePageSpecs`: simplify interface by removing `page_heap` …

…parameter, return success status, and streamline warning handling logic.
include/qpdf/QPDFJob.hh
... ... @@ -468,7 +468,7 @@ class QPDFJob
468 468  
469 469 // Transformations
470 470 void setQPDFOptions(QPDF& pdf);
471   - void handlePageSpecs(QPDF& pdf, std::vector<std::unique_ptr<QPDF>>& page_heap);
  471 + bool handlePageSpecs(QPDF& pdf);
472 472 bool shouldRemoveUnreferencedResources(QPDF& pdf);
473 473 void handleRotations(QPDF& pdf);
474 474 void getUOPagenos(
... ...
libqpdf/QPDFJob.cc
... ... @@ -464,40 +464,37 @@ QPDFJob::createQPDF()
464 464 pdf.updateFromJSON(m->update_from_json);
465 465 }
466 466  
467   - std::vector<std::unique_ptr<QPDF>> page_heap;
468 467 if (!m->page_specs.empty()) {
469   - handlePageSpecs(pdf, page_heap);
  468 + if (!handlePageSpecs(pdf)) {
  469 + m->warnings = true;
  470 + }
470 471 }
471 472 if (!m->rotations.empty()) {
472 473 handleRotations(pdf);
473 474 }
474 475 handleUnderOverlay(pdf);
475 476 handleTransformations(pdf);
  477 +
  478 + auto root = pdf.getRoot();
476 479 if (m->remove_info) {
477 480 auto trailer = pdf.getTrailer();
478   - auto mod_date = trailer.getKey("/Info").getKeyIfDict("/ModDate");
  481 + auto mod_date = trailer["/Info"]["/ModDate"];
479 482 if (mod_date.null()) {
480   - trailer.removeKey("/Info");
  483 + trailer.erase("/Info");
481 484 } else {
482   - auto info = trailer.replaceKeyAndGetNew(
483   - "/Info", pdf.makeIndirectObject(QPDFObjectHandle::newDictionary()));
484   - info.replaceKey("/ModDate", mod_date);
  485 + trailer.replaceKey(
  486 + "/Info", pdf.makeIndirectObject(Dictionary({{"/ModDate", mod_date}})));
485 487 }
486   - pdf.getRoot().removeKey("/Metadata");
  488 + root.erase("/Metadata");
487 489 }
488 490 if (m->remove_metadata) {
489   - pdf.getRoot().removeKey("/Metadata");
  491 + root.erase("/Metadata");
490 492 }
491 493 if (m->remove_structure) {
492   - pdf.getRoot().removeKey("/StructTreeRoot");
493   - pdf.getRoot().removeKey("/MarkInfo");
  494 + root.erase("/StructTreeRoot");
  495 + root.erase("/MarkInfo");
494 496 }
495 497  
496   - for (auto& foreign: page_heap) {
497   - if (foreign->anyWarnings()) {
498   - m->warnings = true;
499   - }
500   - }
501 498 return pdf_sp;
502 499 }
503 500  
... ... @@ -2377,9 +2374,12 @@ added_page(QPDF&amp; pdf, QPDFPageObjectHelper page)
2377 2374 return added_page(pdf, page.getObjectHandle());
2378 2375 }
2379 2376  
2380   -void
2381   -QPDFJob::handlePageSpecs(QPDF& pdf, std::vector<std::unique_ptr<QPDF>>& page_heap)
  2377 +// Handle all page specifications. Return true if it succeeded without warnings.
  2378 +bool
  2379 +QPDFJob::handlePageSpecs(QPDF& pdf)
2382 2380 {
  2381 + std::vector<std::unique_ptr<QPDF>> page_heap;
  2382 +
2383 2383 // Parse all page specifications and translate them into lists of actual pages.
2384 2384  
2385 2385 // Handle "." as a shortcut for the input file
... ... @@ -2649,6 +2649,12 @@ QPDFJob::handlePageSpecs(QPDF&amp; pdf, std::vector&lt;std::unique_ptr&lt;QPDF&gt;&gt;&amp; page_hea
2649 2649 }
2650 2650 }
2651 2651 }
  2652 + for (auto& foreign: page_heap) {
  2653 + if (foreign->anyWarnings()) {
  2654 + return false;
  2655 + }
  2656 + }
  2657 + return true;
2652 2658 }
2653 2659  
2654 2660 void
... ...