Commit 655c55f84830190f9fa4777c615b8a622254648a
1 parent
d9ec2eb0
implement methods to get dictionary and array contents as map and vector
Showing
8 changed files
with
48 additions
and
0 deletions
include/qpdf/QPDFObjectHandle.hh
| @@ -172,6 +172,8 @@ class QPDFObjectHandle | @@ -172,6 +172,8 @@ class QPDFObjectHandle | ||
| 172 | int getArrayNItems(); | 172 | int getArrayNItems(); |
| 173 | QPDF_DLL | 173 | QPDF_DLL |
| 174 | QPDFObjectHandle getArrayItem(int n); | 174 | QPDFObjectHandle getArrayItem(int n); |
| 175 | + QPDF_DLL | ||
| 176 | + std::vector<QPDFObjectHandle> getArrayAsVector(); | ||
| 175 | 177 | ||
| 176 | // Methods for dictionary objects | 178 | // Methods for dictionary objects |
| 177 | QPDF_DLL | 179 | QPDF_DLL |
| @@ -180,6 +182,8 @@ class QPDFObjectHandle | @@ -180,6 +182,8 @@ class QPDFObjectHandle | ||
| 180 | QPDFObjectHandle getKey(std::string const&); | 182 | QPDFObjectHandle getKey(std::string const&); |
| 181 | QPDF_DLL | 183 | QPDF_DLL |
| 182 | std::set<std::string> getKeys(); | 184 | std::set<std::string> getKeys(); |
| 185 | + QPDF_DLL | ||
| 186 | + std::map<std::string, QPDFObjectHandle> getDictAsMap(); | ||
| 183 | 187 | ||
| 184 | // Methods for name and array objects | 188 | // Methods for name and array objects |
| 185 | QPDF_DLL | 189 | QPDF_DLL |
libqpdf/QPDFObjectHandle.cc
| @@ -246,6 +246,13 @@ QPDFObjectHandle::getArrayItem(int n) | @@ -246,6 +246,13 @@ QPDFObjectHandle::getArrayItem(int n) | ||
| 246 | return dynamic_cast<QPDF_Array*>(obj.getPointer())->getItem(n); | 246 | return dynamic_cast<QPDF_Array*>(obj.getPointer())->getItem(n); |
| 247 | } | 247 | } |
| 248 | 248 | ||
| 249 | +std::vector<QPDFObjectHandle> | ||
| 250 | +QPDFObjectHandle::getArrayAsVector() | ||
| 251 | +{ | ||
| 252 | + assertType("Array", isArray()); | ||
| 253 | + return dynamic_cast<QPDF_Array*>(obj.getPointer())->getAsVector(); | ||
| 254 | +} | ||
| 255 | + | ||
| 249 | // Array mutators | 256 | // Array mutators |
| 250 | 257 | ||
| 251 | void | 258 | void |
| @@ -278,6 +285,13 @@ QPDFObjectHandle::getKeys() | @@ -278,6 +285,13 @@ QPDFObjectHandle::getKeys() | ||
| 278 | return dynamic_cast<QPDF_Dictionary*>(obj.getPointer())->getKeys(); | 285 | return dynamic_cast<QPDF_Dictionary*>(obj.getPointer())->getKeys(); |
| 279 | } | 286 | } |
| 280 | 287 | ||
| 288 | +std::map<std::string, QPDFObjectHandle> | ||
| 289 | +QPDFObjectHandle::getDictAsMap() | ||
| 290 | +{ | ||
| 291 | + assertType("Dictionary", isDictionary()); | ||
| 292 | + return dynamic_cast<QPDF_Dictionary*>(obj.getPointer())->getAsMap(); | ||
| 293 | +} | ||
| 294 | + | ||
| 281 | // Array and Name accessors | 295 | // Array and Name accessors |
| 282 | bool | 296 | bool |
| 283 | QPDFObjectHandle::isOrHasName(std::string const& value) | 297 | QPDFObjectHandle::isOrHasName(std::string const& value) |
libqpdf/QPDF_Array.cc
| @@ -51,6 +51,12 @@ QPDF_Array::getItem(int n) const | @@ -51,6 +51,12 @@ QPDF_Array::getItem(int n) const | ||
| 51 | return this->items[n]; | 51 | return this->items[n]; |
| 52 | } | 52 | } |
| 53 | 53 | ||
| 54 | +std::vector<QPDFObjectHandle> const& | ||
| 55 | +QPDF_Array::getAsVector() const | ||
| 56 | +{ | ||
| 57 | + return this->items; | ||
| 58 | +} | ||
| 59 | + | ||
| 54 | void | 60 | void |
| 55 | QPDF_Array::setItem(int n, QPDFObjectHandle const& oh) | 61 | QPDF_Array::setItem(int n, QPDFObjectHandle const& oh) |
| 56 | { | 62 | { |
libqpdf/QPDF_Dictionary.cc
| @@ -78,6 +78,13 @@ QPDF_Dictionary::getKeys() | @@ -78,6 +78,13 @@ QPDF_Dictionary::getKeys() | ||
| 78 | return result; | 78 | return result; |
| 79 | } | 79 | } |
| 80 | 80 | ||
| 81 | +std::map<std::string, QPDFObjectHandle> const& | ||
| 82 | +QPDF_Dictionary::getAsMap() const | ||
| 83 | +{ | ||
| 84 | + | ||
| 85 | + return this->items; | ||
| 86 | +} | ||
| 87 | + | ||
| 81 | void | 88 | void |
| 82 | QPDF_Dictionary::replaceKey(std::string const& key, | 89 | QPDF_Dictionary::replaceKey(std::string const& key, |
| 83 | QPDFObjectHandle const& value) | 90 | QPDFObjectHandle const& value) |
libqpdf/qpdf/QPDF_Array.hh
| @@ -14,6 +14,7 @@ class QPDF_Array: public QPDFObject | @@ -14,6 +14,7 @@ class QPDF_Array: public QPDFObject | ||
| 14 | virtual std::string unparse(); | 14 | virtual std::string unparse(); |
| 15 | int getNItems() const; | 15 | int getNItems() const; |
| 16 | QPDFObjectHandle getItem(int n) const; | 16 | QPDFObjectHandle getItem(int n) const; |
| 17 | + std::vector<QPDFObjectHandle> const& getAsVector() const; | ||
| 17 | void setItem(int, QPDFObjectHandle const&); | 18 | void setItem(int, QPDFObjectHandle const&); |
| 18 | 19 | ||
| 19 | protected: | 20 | protected: |
libqpdf/qpdf/QPDF_Dictionary.hh
| @@ -21,6 +21,7 @@ class QPDF_Dictionary: public QPDFObject | @@ -21,6 +21,7 @@ class QPDF_Dictionary: public QPDFObject | ||
| 21 | bool hasKey(std::string const&); | 21 | bool hasKey(std::string const&); |
| 22 | QPDFObjectHandle getKey(std::string const&); | 22 | QPDFObjectHandle getKey(std::string const&); |
| 23 | std::set<std::string> getKeys(); | 23 | std::set<std::string> getKeys(); |
| 24 | + std::map<std::string, QPDFObjectHandle> const& getAsMap() const; | ||
| 24 | 25 | ||
| 25 | // Replace value of key, adding it if it does not exist | 26 | // Replace value of key, adding it if it does not exist |
| 26 | void replaceKey(std::string const& key, QPDFObjectHandle const&); | 27 | void replaceKey(std::string const& key, QPDFObjectHandle const&); |
qpdf/qtest/qpdf/test14.out
qpdf/test_driver.cc
| @@ -570,6 +570,20 @@ void runtest(int n, char const* filename) | @@ -570,6 +570,20 @@ void runtest(int n, char const* filename) | ||
| 570 | std::cout << "swapped array: " << qdict.getArrayItem(0).getName() | 570 | std::cout << "swapped array: " << qdict.getArrayItem(0).getName() |
| 571 | << std::endl; | 571 | << std::endl; |
| 572 | 572 | ||
| 573 | + // Exercise getAsMap and getAsArray | ||
| 574 | + std::vector<QPDFObjectHandle> array_elements = | ||
| 575 | + qdict.getArrayAsVector(); | ||
| 576 | + std::map<std::string, QPDFObjectHandle> dict_items = | ||
| 577 | + qarray.getDictAsMap(); | ||
| 578 | + if ((array_elements.size() == 1) && | ||
| 579 | + (array_elements[0].getName() == "/Array") && | ||
| 580 | + (dict_items.size() == 1) && | ||
| 581 | + (dict_items["/NewDict"].getIntValue() == 2)) | ||
| 582 | + { | ||
| 583 | + std::cout << "array and dictionary contents are correct" | ||
| 584 | + << std::endl; | ||
| 585 | + } | ||
| 586 | + | ||
| 573 | QPDFWriter w(pdf, "a.pdf"); | 587 | QPDFWriter w(pdf, "a.pdf"); |
| 574 | w.setStaticID(true); | 588 | w.setStaticID(true); |
| 575 | w.setStreamDataMode(qpdf_s_preserve); | 589 | w.setStreamDataMode(qpdf_s_preserve); |