Commit 200213727a24b40563885950eeed51549ce64ad4

Authored by m-holger
1 parent 7fa74c4f

Refactor `QPDFJob`: replace `page_spec_cfis` map with `cfis` in `Input`, streaml…

…ine file handling and resource management logic.
libqpdf/QPDFJob.cc
@@ -2376,7 +2376,6 @@ QPDFJob::handlePageSpecs(QPDF& pdf) @@ -2376,7 +2376,6 @@ QPDFJob::handlePageSpecs(QPDF& pdf)
2376 } 2376 }
2377 2377
2378 // Create a QPDF object for each file that we may take pages from. 2378 // Create a QPDF object for each file that we may take pages from.
2379 - std::map<std::string, ClosedFileInputSource*> page_spec_cfis;  
2380 std::map<unsigned long long, std::set<QPDFObjGen>> copied_pages; 2379 std::map<unsigned long long, std::set<QPDFObjGen>> copied_pages;
2381 for (auto& selection: m->selections) { 2380 for (auto& selection: m->selections) {
2382 auto& input = m->inputs.files[selection.filename]; 2381 auto& input = m->inputs.files[selection.filename];
@@ -2396,21 +2395,22 @@ QPDFJob::handlePageSpecs(QPDF&amp; pdf) @@ -2396,21 +2395,22 @@ QPDFJob::handlePageSpecs(QPDF&amp; pdf)
2396 doIfVerbose([&](Pipeline& v, std::string const& prefix) { 2395 doIfVerbose([&](Pipeline& v, std::string const& prefix) {
2397 v << prefix << ": processing " << selection.filename << "\n"; 2396 v << prefix << ": processing " << selection.filename << "\n";
2398 }); 2397 });
2399 - std::shared_ptr<InputSource> is;  
2400 - ClosedFileInputSource* cis = nullptr;  
2401 if (!m->keep_files_open) { 2398 if (!m->keep_files_open) {
2402 - cis = new ClosedFileInputSource(selection.filename.data());  
2403 - is = std::shared_ptr<InputSource>(cis); 2399 + auto cis = std::make_shared<ClosedFileInputSource>(selection.filename.data());
2404 cis->stayOpen(true); 2400 cis->stayOpen(true);
  2401 + processInputSource(input.qpdf_p, cis, password.data(), true);
  2402 + cis->stayOpen(false);
  2403 + input.cfis = cis.get();
2405 } else { 2404 } else {
2406 - FileInputSource* fis = new FileInputSource(selection.filename.data());  
2407 - is = std::shared_ptr<InputSource>(fis); 2405 + processInputSource(
  2406 + input.qpdf_p,
  2407 + std::make_shared<FileInputSource>(selection.filename.data()),
  2408 + password.data(),
  2409 + true);
2408 } 2410 }
2409 - processInputSource(input.qpdf_p, is, password.data(), true);  
2410 input.qpdf = input.qpdf_p.get(); 2411 input.qpdf = input.qpdf_p.get();
2411 - if (cis) {  
2412 - cis->stayOpen(false);  
2413 - page_spec_cfis[selection.filename] = cis; 2412 + if (input.cfis) {
  2413 + input.cfis->stayOpen(false);
2414 } 2414 }
2415 } 2415 }
2416 2416
@@ -2430,18 +2430,16 @@ QPDFJob::handlePageSpecs(QPDF&amp; pdf) @@ -2430,18 +2430,16 @@ QPDFJob::handlePageSpecs(QPDF&amp; pdf)
2430 std::map<unsigned long long, bool> remove_unreferenced; 2430 std::map<unsigned long long, bool> remove_unreferenced;
2431 if (m->remove_unreferenced_page_resources != QPDFJob::re_no) { 2431 if (m->remove_unreferenced_page_resources != QPDFJob::re_no) {
2432 for (auto const& [filename, input]: m->inputs.files) { 2432 for (auto const& [filename, input]: m->inputs.files) {
2433 - ClosedFileInputSource* cis = nullptr;  
2434 - if (page_spec_cfis.contains(filename)) {  
2435 - cis = page_spec_cfis[filename];  
2436 - cis->stayOpen(true); 2433 + if (input.cfis) {
  2434 + input.cfis->stayOpen(true);
2437 } 2435 }
2438 QPDF& other(*input.qpdf); 2436 QPDF& other(*input.qpdf);
2439 auto other_uuid = other.getUniqueId(); 2437 auto other_uuid = other.getUniqueId();
2440 if (!remove_unreferenced.contains(other_uuid)) { 2438 if (!remove_unreferenced.contains(other_uuid)) {
2441 remove_unreferenced[other_uuid] = shouldRemoveUnreferencedResources(other); 2439 remove_unreferenced[other_uuid] = shouldRemoveUnreferencedResources(other);
2442 } 2440 }
2443 - if (cis) {  
2444 - cis->stayOpen(false); 2441 + if (input.cfis) {
  2442 + input.cfis->stayOpen(false);
2445 } 2443 }
2446 } 2444 }
2447 } 2445 }
@@ -2502,10 +2500,9 @@ QPDFJob::handlePageSpecs(QPDF&amp; pdf) @@ -2502,10 +2500,9 @@ QPDFJob::handlePageSpecs(QPDF&amp; pdf)
2502 auto& this_afdh = pdf.acroform(); 2500 auto& this_afdh = pdf.acroform();
2503 std::set<QPDFObjGen> referenced_fields; 2501 std::set<QPDFObjGen> referenced_fields;
2504 for (auto& page_data: page_specs) { 2502 for (auto& page_data: page_specs) {
2505 - ClosedFileInputSource* cis = nullptr;  
2506 - if (page_spec_cfis.contains(page_data.filename)) {  
2507 - cis = page_spec_cfis[page_data.filename];  
2508 - cis->stayOpen(true); 2503 + auto& input = m->inputs.files[page_data.filename];
  2504 + if (input.cfis) {
  2505 + input.cfis->stayOpen(true);
2509 } 2506 }
2510 auto& pldh = page_data.qpdf->page_labels(); 2507 auto& pldh = page_data.qpdf->page_labels();
2511 auto& other_afdh = page_data.qpdf->acroform(); 2508 auto& other_afdh = page_data.qpdf->acroform();
@@ -2570,8 +2567,8 @@ QPDFJob::handlePageSpecs(QPDF&amp; pdf) @@ -2570,8 +2567,8 @@ QPDFJob::handlePageSpecs(QPDF&amp; pdf)
2570 } 2567 }
2571 } 2568 }
2572 } 2569 }
2573 - if (cis) {  
2574 - cis->stayOpen(false); 2570 + if (input.cfis) {
  2571 + input.cfis->stayOpen(false);
2575 } 2572 }
2576 } 2573 }
2577 if (any_page_labels) { 2574 if (any_page_labels) {
libqpdf/qpdf/QPDFJob_private.hh
@@ -42,6 +42,7 @@ struct QPDFJob::Input @@ -42,6 +42,7 @@ struct QPDFJob::Input
42 { 42 {
43 std::unique_ptr<QPDF> qpdf_p; 43 std::unique_ptr<QPDF> qpdf_p;
44 QPDF* qpdf; 44 QPDF* qpdf;
  45 + ClosedFileInputSource* cfis{};
45 }; 46 };
46 47
47 // All PDF input files for a job. 48 // All PDF input files for a job.