Commit 41b5c46497997d45ba2e7521b66a262ea7a1afd4
1 parent
b3c91f64
refactor: split write_outfile and do_split_pages
Showing
1 changed file
with
86 additions
and
84 deletions
qpdf/qpdf.cc
| ... | ... | @@ -5024,107 +5024,105 @@ static void set_writer_options(QPDF& pdf, Options& o, QPDFWriter& w) |
| 5024 | 5024 | } |
| 5025 | 5025 | } |
| 5026 | 5026 | |
| 5027 | -static void write_outfile(QPDF& pdf, Options& o) | |
| 5027 | +static void do_split_pages(QPDF& pdf, Options& o) | |
| 5028 | 5028 | { |
| 5029 | - if (o.split_pages) | |
| 5029 | + // Generate output file pattern | |
| 5030 | + std::string before; | |
| 5031 | + std::string after; | |
| 5032 | + size_t len = strlen(o.outfilename); | |
| 5033 | + char* num_spot = strstr(const_cast<char*>(o.outfilename), "%d"); | |
| 5034 | + if (num_spot != 0) | |
| 5030 | 5035 | { |
| 5031 | - // Generate output file pattern | |
| 5032 | - std::string before; | |
| 5033 | - std::string after; | |
| 5034 | - size_t len = strlen(o.outfilename); | |
| 5035 | - char* num_spot = strstr(const_cast<char*>(o.outfilename), "%d"); | |
| 5036 | - if (num_spot != 0) | |
| 5037 | - { | |
| 5038 | - QTC::TC("qpdf", "qpdf split-pages %d"); | |
| 5039 | - before = std::string(o.outfilename, | |
| 5040 | - QIntC::to_size(num_spot - o.outfilename)); | |
| 5041 | - after = num_spot + 2; | |
| 5042 | - } | |
| 5043 | - else if ((len >= 4) && | |
| 5044 | - (QUtil::str_compare_nocase( | |
| 5045 | - o.outfilename + len - 4, ".pdf") == 0)) | |
| 5046 | - { | |
| 5047 | - QTC::TC("qpdf", "qpdf split-pages .pdf"); | |
| 5048 | - before = std::string(o.outfilename, len - 4) + "-"; | |
| 5049 | - after = o.outfilename + len - 4; | |
| 5050 | - } | |
| 5051 | - else | |
| 5036 | + QTC::TC("qpdf", "qpdf split-pages %d"); | |
| 5037 | + before = std::string(o.outfilename, | |
| 5038 | + QIntC::to_size(num_spot - o.outfilename)); | |
| 5039 | + after = num_spot + 2; | |
| 5040 | + } | |
| 5041 | + else if ((len >= 4) && | |
| 5042 | + (QUtil::str_compare_nocase( | |
| 5043 | + o.outfilename + len - 4, ".pdf") == 0)) | |
| 5044 | + { | |
| 5045 | + QTC::TC("qpdf", "qpdf split-pages .pdf"); | |
| 5046 | + before = std::string(o.outfilename, len - 4) + "-"; | |
| 5047 | + after = o.outfilename + len - 4; | |
| 5048 | + } | |
| 5049 | + else | |
| 5050 | + { | |
| 5051 | + QTC::TC("qpdf", "qpdf split-pages other"); | |
| 5052 | + before = std::string(o.outfilename) + "-"; | |
| 5053 | + } | |
| 5054 | + | |
| 5055 | + if (! o.preserve_unreferenced_page_resources) | |
| 5056 | + { | |
| 5057 | + QPDFPageDocumentHelper dh(pdf); | |
| 5058 | + dh.removeUnreferencedResources(); | |
| 5059 | + } | |
| 5060 | + QPDFPageLabelDocumentHelper pldh(pdf); | |
| 5061 | + std::vector<QPDFObjectHandle> const& pages = pdf.getAllPages(); | |
| 5062 | + size_t pageno_len = QUtil::uint_to_string(pages.size()).length(); | |
| 5063 | + size_t num_pages = pages.size(); | |
| 5064 | + for (size_t i = 0; i < num_pages; i += QIntC::to_size(o.split_pages)) | |
| 5065 | + { | |
| 5066 | + size_t first = i + 1; | |
| 5067 | + size_t last = i + QIntC::to_size(o.split_pages); | |
| 5068 | + if (last > num_pages) | |
| 5052 | 5069 | { |
| 5053 | - QTC::TC("qpdf", "qpdf split-pages other"); | |
| 5054 | - before = std::string(o.outfilename) + "-"; | |
| 5070 | + last = num_pages; | |
| 5055 | 5071 | } |
| 5056 | - | |
| 5057 | - if (! o.preserve_unreferenced_page_resources) | |
| 5072 | + QPDF outpdf; | |
| 5073 | + outpdf.emptyPDF(); | |
| 5074 | + for (size_t pageno = first; pageno <= last; ++pageno) | |
| 5058 | 5075 | { |
| 5059 | - QPDFPageDocumentHelper dh(pdf); | |
| 5060 | - dh.removeUnreferencedResources(); | |
| 5076 | + QPDFObjectHandle page = pages.at(pageno - 1); | |
| 5077 | + outpdf.addPage(page, false); | |
| 5061 | 5078 | } |
| 5062 | - QPDFPageLabelDocumentHelper pldh(pdf); | |
| 5063 | - std::vector<QPDFObjectHandle> const& pages = pdf.getAllPages(); | |
| 5064 | - size_t pageno_len = QUtil::uint_to_string(pages.size()).length(); | |
| 5065 | - size_t num_pages = pages.size(); | |
| 5066 | - for (size_t i = 0; i < num_pages; i += QIntC::to_size(o.split_pages)) | |
| 5079 | + if (pldh.hasPageLabels()) | |
| 5067 | 5080 | { |
| 5068 | - size_t first = i + 1; | |
| 5069 | - size_t last = i + QIntC::to_size(o.split_pages); | |
| 5070 | - if (last > num_pages) | |
| 5071 | - { | |
| 5072 | - last = num_pages; | |
| 5073 | - } | |
| 5074 | - QPDF outpdf; | |
| 5075 | - outpdf.emptyPDF(); | |
| 5076 | - for (size_t pageno = first; pageno <= last; ++pageno) | |
| 5077 | - { | |
| 5078 | - QPDFObjectHandle page = pages.at(pageno - 1); | |
| 5079 | - outpdf.addPage(page, false); | |
| 5080 | - } | |
| 5081 | - if (pldh.hasPageLabels()) | |
| 5082 | - { | |
| 5083 | - std::vector<QPDFObjectHandle> labels; | |
| 5084 | - pldh.getLabelsForPageRange( | |
| 5085 | - QIntC::to_longlong(first - 1), | |
| 5086 | - QIntC::to_longlong(last - 1), | |
| 5087 | - 0, labels); | |
| 5088 | - QPDFObjectHandle page_labels = | |
| 5089 | - QPDFObjectHandle::newDictionary(); | |
| 5090 | - page_labels.replaceKey( | |
| 5091 | - "/Nums", QPDFObjectHandle::newArray(labels)); | |
| 5092 | - outpdf.getRoot().replaceKey("/PageLabels", page_labels); | |
| 5093 | - } | |
| 5094 | - std::string page_range = | |
| 5095 | - QUtil::uint_to_string(first, QIntC::to_int(pageno_len)); | |
| 5096 | - if (o.split_pages > 1) | |
| 5097 | - { | |
| 5098 | - page_range += "-" + | |
| 5099 | - QUtil::uint_to_string(last, QIntC::to_int(pageno_len)); | |
| 5100 | - } | |
| 5101 | - std::string outfile = before + page_range + after; | |
| 5102 | - QPDFWriter w(outpdf, outfile.c_str()); | |
| 5103 | - set_writer_options(outpdf, o, w); | |
| 5104 | - w.write(); | |
| 5105 | - if (o.verbose) | |
| 5106 | - { | |
| 5107 | - std::cout << whoami << ": wrote file " << outfile << std::endl; | |
| 5108 | - } | |
| 5081 | + std::vector<QPDFObjectHandle> labels; | |
| 5082 | + pldh.getLabelsForPageRange( | |
| 5083 | + QIntC::to_longlong(first - 1), | |
| 5084 | + QIntC::to_longlong(last - 1), | |
| 5085 | + 0, labels); | |
| 5086 | + QPDFObjectHandle page_labels = | |
| 5087 | + QPDFObjectHandle::newDictionary(); | |
| 5088 | + page_labels.replaceKey( | |
| 5089 | + "/Nums", QPDFObjectHandle::newArray(labels)); | |
| 5090 | + outpdf.getRoot().replaceKey("/PageLabels", page_labels); | |
| 5109 | 5091 | } |
| 5110 | - } | |
| 5111 | - else | |
| 5112 | - { | |
| 5113 | - if (strcmp(o.outfilename, "-") == 0) | |
| 5092 | + std::string page_range = | |
| 5093 | + QUtil::uint_to_string(first, QIntC::to_int(pageno_len)); | |
| 5094 | + if (o.split_pages > 1) | |
| 5114 | 5095 | { |
| 5115 | - o.outfilename = 0; | |
| 5096 | + page_range += "-" + | |
| 5097 | + QUtil::uint_to_string(last, QIntC::to_int(pageno_len)); | |
| 5116 | 5098 | } |
| 5117 | - QPDFWriter w(pdf, o.outfilename); | |
| 5118 | - set_writer_options(pdf, o, w); | |
| 5099 | + std::string outfile = before + page_range + after; | |
| 5100 | + QPDFWriter w(outpdf, outfile.c_str()); | |
| 5101 | + set_writer_options(outpdf, o, w); | |
| 5119 | 5102 | w.write(); |
| 5120 | 5103 | if (o.verbose) |
| 5121 | 5104 | { |
| 5122 | - std::cout << whoami << ": wrote file " | |
| 5123 | - << o.outfilename << std::endl; | |
| 5105 | + std::cout << whoami << ": wrote file " << outfile << std::endl; | |
| 5124 | 5106 | } |
| 5125 | 5107 | } |
| 5126 | 5108 | } |
| 5127 | 5109 | |
| 5110 | +static void write_outfile(QPDF& pdf, Options& o) | |
| 5111 | +{ | |
| 5112 | + if (strcmp(o.outfilename, "-") == 0) | |
| 5113 | + { | |
| 5114 | + o.outfilename = 0; | |
| 5115 | + } | |
| 5116 | + QPDFWriter w(pdf, o.outfilename); | |
| 5117 | + set_writer_options(pdf, o, w); | |
| 5118 | + w.write(); | |
| 5119 | + if (o.verbose) | |
| 5120 | + { | |
| 5121 | + std::cout << whoami << ": wrote file " | |
| 5122 | + << o.outfilename << std::endl; | |
| 5123 | + } | |
| 5124 | +} | |
| 5125 | + | |
| 5128 | 5126 | int realmain(int argc, char* argv[]) |
| 5129 | 5127 | { |
| 5130 | 5128 | whoami = QUtil::getWhoami(argv[0]); |
| ... | ... | @@ -5162,6 +5160,10 @@ int realmain(int argc, char* argv[]) |
| 5162 | 5160 | { |
| 5163 | 5161 | do_inspection(pdf, o); |
| 5164 | 5162 | } |
| 5163 | + else if (o.split_pages) | |
| 5164 | + { | |
| 5165 | + do_split_pages(pdf, o); | |
| 5166 | + } | |
| 5165 | 5167 | else |
| 5166 | 5168 | { |
| 5167 | 5169 | write_outfile(pdf, o); | ... | ... |