Commit 7b3cbacf5dc78b6f2b57204796f7dd0880a68b66

Authored by Jay Berkenbilt
1 parent a9ae8cad

Change from QPDF{Array,Dict}Items to aitems() and ditems()

ChangeLog
... ... @@ -137,16 +137,15 @@
137 137  
138 138 2021-01-29 Jay Berkenbilt <ejb@ql.org>
139 139  
140   - * Add wrappers QPDFDictItems and QPDFArrayItems around
141   - QPDFObjectHandle that provide a C++ iterator API, including C++11
142   - range-for iteration, over arrays and dictionaries. With this, you
143   - can do
  140 + * Add methods to QPDFObjectHandle that provide a C++ iterator API,
  141 + including C++11 range-for iteration, over arrays and dictionaries.
  142 + With this, you can do
144 143  
145   - for (auto i: QPDFDictItems(oh))
  144 + for (auto i: dict_oh.ditems())
146 145 {
147 146 // i.first is a string, i.second is a QPDFObjectHandle
148 147 }
149   - for (auto i: QPDFArrayItems(oh))
  148 + for (auto i: array_oh.aitems())
150 149 {
151 150 // i is a QPDFObjectHandle
152 151 }
... ...
examples/pdf-mod-info.cc
... ... @@ -32,7 +32,7 @@ void dumpInfoDict(QPDF&amp; pdf,
32 32 QPDFObjectHandle trailer = pdf.getTrailer();
33 33 if (trailer.hasKey("/Info"))
34 34 {
35   - for (auto& it: QPDFDictItems(trailer.getKey("/Info")))
  35 + for (auto& it: trailer.getKey("/Info").ditems())
36 36 {
37 37 std::string val;
38 38 if (it.second.isString())
... ...
examples/pdf-name-number-tree.cc
... ... @@ -93,7 +93,7 @@ int main(int argc, char* argv[])
93 93 // look at it using dictionary and array iterators.
94 94 std::cout << "Keys in name tree object:" << std::endl;
95 95 QPDFObjectHandle names;
96   - for (auto const& i: QPDFDictItems(name_tree_oh))
  96 + for (auto const& i: name_tree_oh.ditems())
97 97 {
98 98 std::cout << i.first << std::endl;
99 99 if (i.first == "/Names")
... ... @@ -103,7 +103,7 @@ int main(int argc, char* argv[])
103 103 }
104 104 // Values in names array:
105 105 std::cout << "Values in names:" << std::endl;
106   - for (auto& i: QPDFArrayItems(names))
  106 + for (auto& i: names.aitems())
107 107 {
108 108 std::cout << " " << i.unparse() << std::endl;
109 109 }
... ...
include/qpdf/QPDFObjectHandle.hh
... ... @@ -659,8 +659,19 @@ class QPDFObjectHandle
659 659 QPDF_DLL
660 660 std::string getInlineImageValue();
661 661  
662   - // Methods for array objects; see also name and array objects. See
663   - // also QPDFArrayItems later in this file.
  662 + // Methods for array objects; see also name and array objects.
  663 +
  664 + // Return an object that enables iteration over members. You can
  665 + // do
  666 + //
  667 + // for (auto iter: obj.aitems())
  668 + // {
  669 + // // iter is an array element
  670 + // }
  671 + class QPDFArrayItems;
  672 + QPDF_DLL
  673 + QPDFArrayItems aitems();
  674 +
664 675 QPDF_DLL
665 676 int getArrayNItems();
666 677 QPDF_DLL
... ... @@ -684,8 +695,20 @@ class QPDFObjectHandle
684 695 QPDF_DLL
685 696 Matrix getArrayAsMatrix();
686 697  
687   - // Methods for dictionary objects. See also QPDFDictItems later in
688   - // this file.
  698 + // Methods for dictionary objects.
  699 +
  700 + // Return an object that enables iteration over members. You can
  701 + // do
  702 + //
  703 + // for (auto iter: obj.ditems())
  704 + // {
  705 + // // iter.first is the key
  706 + // // iter.second is the value
  707 + // }
  708 + class QPDFDictItems;
  709 + QPDF_DLL
  710 + QPDFDictItems ditems();
  711 +
689 712 QPDF_DLL
690 713 bool hasKey(std::string const&);
691 714 QPDF_DLL
... ... @@ -1288,7 +1311,7 @@ class QPDFObjectHandle
1288 1311 bool reserved;
1289 1312 };
1290 1313  
1291   -class QPDFDictItems
  1314 +class QPDFObjectHandle::QPDFDictItems
1292 1315 {
1293 1316 // This class allows C++-style iteration, including range-for
1294 1317 // iteration, around dictionaries. You can write
... ... @@ -1379,7 +1402,7 @@ class QPDFDictItems
1379 1402 QPDFObjectHandle oh;
1380 1403 };
1381 1404  
1382   -class QPDFArrayItems
  1405 +class QPDFObjectHandle::QPDFArrayItems
1383 1406 {
1384 1407 // This class allows C++-style iteration, including range-for
1385 1408 // iteration, around arrays. You can write
... ...
libqpdf/QPDFAcroFormDocumentHelper.cc
... ... @@ -439,7 +439,7 @@ QPDFAcroFormDocumentHelper::transformAnnotations(
439 439 }
440 440 };
441 441  
442   - for (auto annot: QPDFArrayItems(old_annots))
  442 + for (auto annot: old_annots.aitems())
443 443 {
444 444 if (annot.isStream())
445 445 {
... ... @@ -620,7 +620,7 @@ QPDFAcroFormDocumentHelper::transformAnnotations(
620 620 };
621 621 if (apdict.isDictionary())
622 622 {
623   - for (auto& ap: QPDFDictItems(apdict))
  623 + for (auto& ap: apdict.ditems())
624 624 {
625 625 if (ap.second.isStream())
626 626 {
... ... @@ -629,7 +629,7 @@ QPDFAcroFormDocumentHelper::transformAnnotations(
629 629 }
630 630 else if (ap.second.isDictionary())
631 631 {
632   - for (auto& ap2: QPDFDictItems(ap.second))
  632 + for (auto& ap2: ap.second.ditems())
633 633 {
634 634 if (ap2.second.isStream())
635 635 {
... ...
libqpdf/QPDFObjectHandle.cc
... ... @@ -705,6 +705,12 @@ QPDFObjectHandle::getInlineImageValue()
705 705  
706 706 // Array accessors
707 707  
  708 +QPDFObjectHandle::QPDFArrayItems
  709 +QPDFObjectHandle::aitems()
  710 +{
  711 + return QPDFArrayItems(*this);
  712 +}
  713 +
708 714 int
709 715 QPDFObjectHandle::getArrayNItems()
710 716 {
... ... @@ -931,6 +937,12 @@ QPDFObjectHandle::eraseItem(int at)
931 937  
932 938 // Dictionary accessors
933 939  
  940 +QPDFObjectHandle::QPDFDictItems
  941 +QPDFObjectHandle::ditems()
  942 +{
  943 + return QPDFDictItems(*this);
  944 +}
  945 +
934 946 bool
935 947 QPDFObjectHandle::hasKey(std::string const& key)
936 948 {
... ... @@ -3211,43 +3223,44 @@ QPDFObjectHandle::warn(QPDF* qpdf, QPDFExc const&amp; e)
3211 3223 }
3212 3224 }
3213 3225  
3214   -QPDFDictItems::QPDFDictItems(QPDFObjectHandle const& oh) :
  3226 +QPDFObjectHandle::QPDFDictItems::QPDFDictItems(QPDFObjectHandle const& oh) :
3215 3227 oh(oh)
3216 3228 {
3217 3229 }
3218 3230  
3219   -QPDFDictItems::iterator&
3220   -QPDFDictItems::iterator::operator++()
  3231 +QPDFObjectHandle::QPDFDictItems::iterator&
  3232 +QPDFObjectHandle::QPDFDictItems::iterator::operator++()
3221 3233 {
3222 3234 ++this->m->iter;
3223 3235 updateIValue();
3224 3236 return *this;
3225 3237 }
3226 3238  
3227   -QPDFDictItems::iterator&
3228   -QPDFDictItems::iterator::operator--()
  3239 +QPDFObjectHandle::QPDFDictItems::iterator&
  3240 +QPDFObjectHandle::QPDFDictItems::iterator::operator--()
3229 3241 {
3230 3242 --this->m->iter;
3231 3243 updateIValue();
3232 3244 return *this;
3233 3245 }
3234 3246  
3235   -QPDFDictItems::iterator::reference
3236   -QPDFDictItems::iterator:: operator*()
  3247 +QPDFObjectHandle::QPDFDictItems::iterator::reference
  3248 +QPDFObjectHandle::QPDFDictItems::iterator:: operator*()
3237 3249 {
3238 3250 updateIValue();
3239 3251 return this->ivalue;
3240 3252 }
3241 3253  
3242   -QPDFDictItems::iterator::pointer
3243   -QPDFDictItems::iterator::operator->()
  3254 +QPDFObjectHandle::QPDFDictItems::iterator::pointer
  3255 +QPDFObjectHandle::QPDFDictItems::iterator::operator->()
3244 3256 {
3245 3257 updateIValue();
3246 3258 return &this->ivalue;
3247 3259 }
3248 3260  
3249 3261 bool
3250   -QPDFDictItems::iterator::operator==(iterator const& other) const
  3262 +QPDFObjectHandle::QPDFDictItems::iterator::operator==(
  3263 + iterator const& other) const
3251 3264 {
3252 3265 if (this->m->is_end && other.m->is_end)
3253 3266 {
... ... @@ -3260,14 +3273,15 @@ QPDFDictItems::iterator::operator==(iterator const&amp; other) const
3260 3273 return (this->ivalue.first == other.ivalue.first);
3261 3274 }
3262 3275  
3263   -QPDFDictItems::iterator::iterator(QPDFObjectHandle& oh, bool for_begin) :
  3276 +QPDFObjectHandle::QPDFDictItems::iterator::iterator(
  3277 + QPDFObjectHandle& oh, bool for_begin) :
3264 3278 m(new Members(oh, for_begin))
3265 3279 {
3266 3280 updateIValue();
3267 3281 }
3268 3282  
3269 3283 void
3270   -QPDFDictItems::iterator::updateIValue()
  3284 +QPDFObjectHandle::QPDFDictItems::iterator::updateIValue()
3271 3285 {
3272 3286 this->m->is_end = (this->m->iter == this->m->keys.end());
3273 3287 if (this->m->is_end)
... ... @@ -3282,7 +3296,7 @@ QPDFDictItems::iterator::updateIValue()
3282 3296 }
3283 3297 }
3284 3298  
3285   -QPDFDictItems::iterator::Members::Members(
  3299 +QPDFObjectHandle::QPDFDictItems::iterator::Members::Members(
3286 3300 QPDFObjectHandle& oh, bool for_begin) :
3287 3301 oh(oh)
3288 3302 {
... ... @@ -3290,25 +3304,25 @@ QPDFDictItems::iterator::Members::Members(
3290 3304 this->iter = for_begin ? this->keys.begin() : this->keys.end();
3291 3305 }
3292 3306  
3293   -QPDFDictItems::iterator
3294   -QPDFDictItems::begin()
  3307 +QPDFObjectHandle::QPDFDictItems::iterator
  3308 +QPDFObjectHandle::QPDFDictItems::begin()
3295 3309 {
3296 3310 return iterator(oh, true);
3297 3311 }
3298 3312  
3299   -QPDFDictItems::iterator
3300   -QPDFDictItems::end()
  3313 +QPDFObjectHandle::QPDFDictItems::iterator
  3314 +QPDFObjectHandle::QPDFDictItems::end()
3301 3315 {
3302 3316 return iterator(oh, false);
3303 3317 }
3304 3318  
3305   -QPDFArrayItems::QPDFArrayItems(QPDFObjectHandle const& oh) :
  3319 +QPDFObjectHandle::QPDFArrayItems::QPDFArrayItems(QPDFObjectHandle const& oh) :
3306 3320 oh(oh)
3307 3321 {
3308 3322 }
3309 3323  
3310   -QPDFArrayItems::iterator&
3311   -QPDFArrayItems::iterator::operator++()
  3324 +QPDFObjectHandle::QPDFArrayItems::iterator&
  3325 +QPDFObjectHandle::QPDFArrayItems::iterator::operator++()
3312 3326 {
3313 3327 if (! this->m->is_end)
3314 3328 {
... ... @@ -3318,8 +3332,8 @@ QPDFArrayItems::iterator::operator++()
3318 3332 return *this;
3319 3333 }
3320 3334  
3321   -QPDFArrayItems::iterator&
3322   -QPDFArrayItems::iterator::operator--()
  3335 +QPDFObjectHandle::QPDFArrayItems::iterator&
  3336 +QPDFObjectHandle::QPDFArrayItems::iterator::operator--()
3323 3337 {
3324 3338 if (this->m->item_number > 0)
3325 3339 {
... ... @@ -3329,34 +3343,36 @@ QPDFArrayItems::iterator::operator--()
3329 3343 return *this;
3330 3344 }
3331 3345  
3332   -QPDFArrayItems::iterator::reference
3333   -QPDFArrayItems::iterator:: operator*()
  3346 +QPDFObjectHandle::QPDFArrayItems::iterator::reference
  3347 +QPDFObjectHandle::QPDFArrayItems::iterator:: operator*()
3334 3348 {
3335 3349 updateIValue();
3336 3350 return this->ivalue;
3337 3351 }
3338 3352  
3339   -QPDFArrayItems::iterator::pointer
3340   -QPDFArrayItems::iterator::operator->()
  3353 +QPDFObjectHandle::QPDFArrayItems::iterator::pointer
  3354 +QPDFObjectHandle::QPDFArrayItems::iterator::operator->()
3341 3355 {
3342 3356 updateIValue();
3343 3357 return &this->ivalue;
3344 3358 }
3345 3359  
3346 3360 bool
3347   -QPDFArrayItems::iterator::operator==(iterator const& other) const
  3361 +QPDFObjectHandle::QPDFArrayItems::iterator::operator==(
  3362 + iterator const& other) const
3348 3363 {
3349 3364 return (this->m->item_number == other.m->item_number);
3350 3365 }
3351 3366  
3352   -QPDFArrayItems::iterator::iterator(QPDFObjectHandle& oh, bool for_begin) :
  3367 +QPDFObjectHandle::QPDFArrayItems::iterator::iterator(
  3368 + QPDFObjectHandle& oh, bool for_begin) :
3353 3369 m(new Members(oh, for_begin))
3354 3370 {
3355 3371 updateIValue();
3356 3372 }
3357 3373  
3358 3374 void
3359   -QPDFArrayItems::iterator::updateIValue()
  3375 +QPDFObjectHandle::QPDFArrayItems::iterator::updateIValue()
3360 3376 {
3361 3377 this->m->is_end = (this->m->item_number >= this->m->oh.getArrayNItems());
3362 3378 if (this->m->is_end)
... ... @@ -3369,21 +3385,21 @@ QPDFArrayItems::iterator::updateIValue()
3369 3385 }
3370 3386 }
3371 3387  
3372   -QPDFArrayItems::iterator::Members::Members(
  3388 +QPDFObjectHandle::QPDFArrayItems::iterator::Members::Members(
3373 3389 QPDFObjectHandle& oh, bool for_begin) :
3374 3390 oh(oh)
3375 3391 {
3376 3392 this->item_number = for_begin ? 0 : oh.getArrayNItems();
3377 3393 }
3378 3394  
3379   -QPDFArrayItems::iterator
3380   -QPDFArrayItems::begin()
  3395 +QPDFObjectHandle::QPDFArrayItems::iterator
  3396 +QPDFObjectHandle::QPDFArrayItems::begin()
3381 3397 {
3382 3398 return iterator(oh, true);
3383 3399 }
3384 3400  
3385   -QPDFArrayItems::iterator
3386   -QPDFArrayItems::end()
  3401 +QPDFObjectHandle::QPDFArrayItems::iterator
  3402 +QPDFObjectHandle::QPDFArrayItems::end()
3387 3403 {
3388 3404 return iterator(oh, false);
3389 3405 }
... ...
manual/qpdf-manual.xml
... ... @@ -5190,11 +5190,10 @@ print &quot;\n&quot;;
5190 5190 <itemizedlist>
5191 5191 <listitem>
5192 5192 <para>
5193   - Add <classname>QPDFDictItems</classname> and
5194   - <classname>QPDFArrayItems</classname> wrappers around
5195   - <classname>QPDFObjectHandle</classname>, allowing C++-style
5196   - iteration, including range-for iteration, over dictionary
5197   - and array QPDFObjectHandles. See comments in
  5193 + Add <function>QPDFObjectHandle::ditems()</function> and
  5194 + <function>QPDFObjectHandle::aitems()</function> that enable
  5195 + C++-style iteration, including range-for iteration, over
  5196 + dictionary and array QPDFObjectHandles. See comments in
5198 5197 <filename>include/qpdf/QPDFObjectHandle.hh</filename> and
5199 5198 <filename>examples/pdf-name-number-tree.cc</filename> for
5200 5199 details.
... ...
qpdf/qpdf.cc
... ... @@ -4104,7 +4104,7 @@ static void do_list_attachments(QPDF&amp; pdf, Options&amp; o)
4104 4104 << std::endl;
4105 4105 }
4106 4106 std::cout << " all data streams:" << std::endl;
4107   - for (auto i2: QPDFDictItems(efoh->getEmbeddedFileStreams()))
  4107 + for (auto i2: efoh->getEmbeddedFileStreams().ditems())
4108 4108 {
4109 4109 std::cout << " " << i2.first << " -> "
4110 4110 << i2.second.getObjGen()
... ...
qpdf/test_driver.cc
... ... @@ -352,7 +352,7 @@ void runtest(int n, char const* filename1, char const* arg2)
352 352 std::cout << "/QTest is an array with "
353 353 << qtest.getArrayNItems() << " items" << std::endl;
354 354 int i = 0;
355   - for (auto& iter: QPDFArrayItems(qtest))
  355 + for (auto& iter: qtest.aitems())
356 356 {
357 357 QTC::TC("qpdf", "main QTest array indirect",
358 358 iter.isIndirect() ? 1 : 0);
... ... @@ -366,7 +366,7 @@ void runtest(int n, char const* filename1, char const* arg2)
366 366 {
367 367 QTC::TC("qpdf", "main QTest dictionary");
368 368 std::cout << "/QTest is a dictionary" << std::endl;
369   - for (auto& iter: QPDFDictItems(qtest))
  369 + for (auto& iter: qtest.ditems())
370 370 {
371 371 QTC::TC("qpdf", "main QTest dictionary indirect",
372 372 iter.second.isIndirect() ? 1 : 0);
... ... @@ -1545,7 +1545,7 @@ void runtest(int n, char const* filename1, char const* arg2)
1545 1545 assert(array.isArray());
1546 1546 {
1547 1547 // Exercise iterators directly
1548   - QPDFArrayItems ai(array);
  1548 + auto ai = array.aitems();
1549 1549 auto i = ai.begin();
1550 1550 assert(i->getName() == "/Item0");
1551 1551 auto& i_value = *i;
... ... @@ -1565,7 +1565,7 @@ void runtest(int n, char const* filename1, char const* arg2)
1565 1565 assert(dictionary.isDictionary());
1566 1566 {
1567 1567 // Exercise iterators directly
1568   - QPDFDictItems di(dictionary);
  1568 + auto di = dictionary.ditems();
1569 1569 auto i = di.begin();
1570 1570 assert(i->first == "/Key1");
1571 1571 auto& i_value = *i;
... ...