Commit 5b0030ff5978a4958bec625d2f8a4db34f34ca1a
Committed by
Jay Berkenbilt
1 parent
956a272d
Tidy pdf-bookmarks example
Convert loops to use range-based for statements. Simplify the extract_bookmarks function.
Showing
1 changed file
with
17 additions
and
20 deletions
examples/pdf-bookmarks.cc
| @@ -5,11 +5,14 @@ | @@ -5,11 +5,14 @@ | ||
| 5 | #include <qpdf/QPDFPageDocumentHelper.hh> | 5 | #include <qpdf/QPDFPageDocumentHelper.hh> |
| 6 | #include <qpdf/QPDFOutlineDocumentHelper.hh> | 6 | #include <qpdf/QPDFOutlineDocumentHelper.hh> |
| 7 | #include <qpdf/QUtil.hh> | 7 | #include <qpdf/QUtil.hh> |
| 8 | +#include <qpdf/QIntC.hh> | ||
| 8 | #include <qpdf/QTC.hh> | 9 | #include <qpdf/QTC.hh> |
| 9 | 10 | ||
| 10 | // This program demonstrates extraction of bookmarks using the qpdf | 11 | // This program demonstrates extraction of bookmarks using the qpdf |
| 11 | // outlines API. Note that all the information shown by this program | 12 | // outlines API. Note that all the information shown by this program |
| 12 | // can also be obtained from a PDF file using qpdf's --json option. | 13 | // can also be obtained from a PDF file using qpdf's --json option. |
| 14 | +// | ||
| 15 | +// Ignore calls to QTC::TC - they are for qpdf CI testing only. | ||
| 13 | 16 | ||
| 14 | static char const* whoami = 0; | 17 | static char const* whoami = 0; |
| 15 | static enum { st_none, st_numbers, st_lines } style = st_none; | 18 | static enum { st_none, st_numbers, st_lines } style = st_none; |
| @@ -51,13 +54,10 @@ void print_lines(std::vector<int>& numbers) | @@ -51,13 +54,10 @@ void print_lines(std::vector<int>& numbers) | ||
| 51 | void generate_page_map(QPDF& qpdf) | 54 | void generate_page_map(QPDF& qpdf) |
| 52 | { | 55 | { |
| 53 | QPDFPageDocumentHelper dh(qpdf); | 56 | QPDFPageDocumentHelper dh(qpdf); |
| 54 | - std::vector<QPDFPageObjectHelper> pages = dh.getAllPages(); | ||
| 55 | int n = 0; | 57 | int n = 0; |
| 56 | - for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin(); | ||
| 57 | - iter != pages.end(); ++iter) | 58 | + for (auto const& page : dh.getAllPages()) |
| 58 | { | 59 | { |
| 59 | - QPDFObjectHandle oh = (*iter).getObjectHandle(); | ||
| 60 | - page_map[oh.getObjGen()] = ++n; | 60 | + page_map[page.getObjectHandle().getObjGen()] = ++n; |
| 61 | } | 61 | } |
| 62 | } | 62 | } |
| 63 | 63 | ||
| @@ -73,10 +73,9 @@ void show_bookmark_details(QPDFOutlineObjectHelper outline, | @@ -73,10 +73,9 @@ void show_bookmark_details(QPDFOutlineObjectHelper outline, | ||
| 73 | 73 | ||
| 74 | case st_numbers: | 74 | case st_numbers: |
| 75 | QTC::TC("examples", "pdf-bookmarks numbers"); | 75 | QTC::TC("examples", "pdf-bookmarks numbers"); |
| 76 | - for (std::vector<int>::iterator iter = numbers.begin(); | ||
| 77 | - iter != numbers.end(); ++iter) | 76 | + for (auto const& number : numbers) |
| 78 | { | 77 | { |
| 79 | - std::cout << *iter << "."; | 78 | + std::cout << number << "."; |
| 80 | } | 79 | } |
| 81 | std::cout << " "; | 80 | std::cout << " "; |
| 82 | break; | 81 | break; |
| @@ -138,20 +137,18 @@ void show_bookmark_details(QPDFOutlineObjectHelper outline, | @@ -138,20 +137,18 @@ void show_bookmark_details(QPDFOutlineObjectHelper outline, | ||
| 138 | void extract_bookmarks(std::vector<QPDFOutlineObjectHelper> outlines, | 137 | void extract_bookmarks(std::vector<QPDFOutlineObjectHelper> outlines, |
| 139 | std::vector<int>& numbers) | 138 | std::vector<int>& numbers) |
| 140 | { | 139 | { |
| 141 | - numbers.push_back(0); | ||
| 142 | - for (std::vector<QPDFOutlineObjectHelper>::iterator iter = outlines.begin(); | ||
| 143 | - iter != outlines.end(); ++iter) | 140 | + // For style == st_numbers, numbers.at(n) contains the numerical |
| 141 | + // label for the outline, so we count up from 1. | ||
| 142 | + // For style == st_lines, numbers.at(n) == 0 indicates the last | ||
| 143 | + // outline at level n, and we don't otherwise care what the value | ||
| 144 | + // is, so we count up to zero. | ||
| 145 | + numbers.push_back( | ||
| 146 | + (style == st_lines) ? -QIntC::to_int(outlines.size()) : 0); | ||
| 147 | + for (auto& outline : outlines) | ||
| 144 | { | 148 | { |
| 145 | ++(numbers.back()); | 149 | ++(numbers.back()); |
| 146 | - show_bookmark_details(*iter, numbers); | ||
| 147 | - std::vector<QPDFOutlineObjectHelper>::iterator next = iter; | ||
| 148 | - ++next; | ||
| 149 | - bool has_next = (next != outlines.end()); | ||
| 150 | - if ((style == st_lines) && (! has_next)) | ||
| 151 | - { | ||
| 152 | - numbers.back() = 0; | ||
| 153 | - } | ||
| 154 | - extract_bookmarks((*iter).getKids(), numbers); | 150 | + show_bookmark_details(outline, numbers); |
| 151 | + extract_bookmarks(outline.getKids(), numbers); | ||
| 155 | } | 152 | } |
| 156 | numbers.pop_back(); | 153 | numbers.pop_back(); |
| 157 | } | 154 | } |