Commit 637c33a7912065f0cc2efd6759cad1385cb09560

Authored by m-holger
1 parent ac039c85

Refactor `QPDFJob`: move section processing logic to `Selection::process`, centr…

…alize handling in `Files::process_all`, and simplify `handlePageSpecs`.
libqpdf/QPDFJob.cc
@@ -2390,6 +2390,10 @@ QPDFJob::Inputs::process(std::string const& filename, QPDFJob::Input& input) @@ -2390,6 +2390,10 @@ QPDFJob::Inputs::process(std::string const& filename, QPDFJob::Input& input)
2390 void 2390 void
2391 QPDFJob::Inputs::process_all() 2391 QPDFJob::Inputs::process_all()
2392 { 2392 {
  2393 + for (auto& selection: selections) {
  2394 + selection.process(*this);
  2395 + }
  2396 +
2393 if (!keep_files_open_set) { 2397 if (!keep_files_open_set) {
2394 // Count the number of distinct files to determine whether we should keep files open or not. 2398 // Count the number of distinct files to determine whether we should keep files open or not.
2395 // Rather than trying to code some portable heuristic based on OS limits, just hard-code 2399 // Rather than trying to code some portable heuristic based on OS limits, just hard-code
@@ -2407,6 +2411,27 @@ QPDFJob::Inputs::process_all() @@ -2407,6 +2411,27 @@ QPDFJob::Inputs::process_all()
2407 process(filename, input); 2411 process(filename, input);
2408 } 2412 }
2409 } 2413 }
  2414 +
  2415 + for (auto& selection: selections) {
  2416 + // Read original pages from the PDF, and parse the page range associated with this
  2417 + // occurrence of the file.
  2418 + auto const& file_spec = files[selection.filename];
  2419 + selection.qpdf = file_spec.qpdf;
  2420 + if (selection.range.empty()) {
  2421 + selection.selected_pages.reserve(static_cast<size_t>(file_spec.n_pages));
  2422 + for (int i = 1; i <= file_spec.n_pages; ++i) {
  2423 + selection.selected_pages.push_back(i);
  2424 + }
  2425 + continue;
  2426 + }
  2427 + try {
  2428 + selection.selected_pages =
  2429 + QUtil::parse_numrange(selection.range.data(), files[selection.filename].n_pages);
  2430 + } catch (std::runtime_error& e) {
  2431 + throw std::runtime_error(
  2432 + "parsing numeric range for " + selection.filename + ": " + e.what());
  2433 + }
  2434 + }
2410 } 2435 }
2411 2436
2412 bool 2437 bool
@@ -2422,6 +2447,19 @@ QPDFJob::Inputs::clear() @@ -2422,6 +2447,19 @@ QPDFJob::Inputs::clear()
2422 return any_warnings; 2447 return any_warnings;
2423 } 2448 }
2424 2449
  2450 +void
  2451 +QPDFJob::Selection::process(QPDFJob::Inputs& in)
  2452 +{
  2453 + // Handle "." as a shortcut for the input file.
  2454 + if (filename == ".") {
  2455 + filename = in.infile_name();
  2456 + }
  2457 + auto& input = in.files[filename];
  2458 + if (!password.empty()) {
  2459 + input.password = password;
  2460 + }
  2461 +}
  2462 +
2425 // Handle all page specifications. 2463 // Handle all page specifications.
2426 void 2464 void
2427 QPDFJob::handlePageSpecs(QPDF& pdf) 2465 QPDFJob::handlePageSpecs(QPDF& pdf)
@@ -2432,37 +2470,10 @@ QPDFJob::handlePageSpecs(QPDF&amp; pdf) @@ -2432,37 +2470,10 @@ QPDFJob::handlePageSpecs(QPDF&amp; pdf)
2432 auto& main_input = m->inputs.files[m->infile_name()]; 2470 auto& main_input = m->inputs.files[m->infile_name()];
2433 main_input.initialize(m->inputs, &pdf); 2471 main_input.initialize(m->inputs, &pdf);
2434 2472
2435 - // Handle "." as a shortcut for the input file.  
2436 - for (auto& selection: m->inputs.selections) {  
2437 - if (selection.filename == ".") {  
2438 - selection.filename = m->infile_name();  
2439 - } else {  
2440 - // Force insertion  
2441 - (void)m->inputs.files[selection.filename];  
2442 - }  
2443 - if (!selection.password.empty()) {  
2444 - m->inputs.files[selection.filename].password = selection.password;  
2445 - }  
2446 - if (selection.range.empty()) {  
2447 - selection.range = "1-z";  
2448 - }  
2449 - }  
2450 - 2473 + // Parse all section and translate them into lists of actual pages.
2451 m->inputs.process_all(); 2474 m->inputs.process_all();
2452 2475
2453 std::map<unsigned long long, std::set<QPDFObjGen>> copied_pages; 2476 std::map<unsigned long long, std::set<QPDFObjGen>> copied_pages;
2454 - for (auto& selection: m->inputs.selections) {  
2455 - // Read original pages from the PDF, and parse the page range associated with this  
2456 - // occurrence of the file.  
2457 - auto const& input = m->inputs.files[selection.filename];  
2458 - selection.qpdf = input.qpdf;  
2459 - try {  
2460 - selection.selected_pages = QUtil::parse_numrange(selection.range.data(), input.n_pages);  
2461 - } catch (std::runtime_error& e) {  
2462 - throw std::runtime_error(  
2463 - "parsing numeric range for " + selection.filename + ": " + e.what());  
2464 - }  
2465 - }  
2466 2477
2467 // Clear all pages out of the primary QPDF's pages tree but leave the objects in place in the 2478 // Clear all pages out of the primary QPDF's pages tree but leave the objects in place in the
2468 // file so they can be re-added without changing their object numbers. This enables other things 2479 // file so they can be re-added without changing their object numbers. This enables other things
libqpdf/qpdf/QPDFJob_private.hh
@@ -25,6 +25,8 @@ struct QPDFJob::Selection @@ -25,6 +25,8 @@ struct QPDFJob::Selection
25 { 25 {
26 } 26 }
27 27
  28 + void process(Inputs& in);
  29 +
28 std::string filename; 30 std::string filename;
29 std::string password; 31 std::string password;
30 std::string range; 32 std::string range;