Commit 655c55f84830190f9fa4777c615b8a622254648a

Authored by Jay Berkenbilt
1 parent d9ec2eb0

implement methods to get dictionary and array contents as map and vector

include/qpdf/QPDFObjectHandle.hh
... ... @@ -172,6 +172,8 @@ class QPDFObjectHandle
172 172 int getArrayNItems();
173 173 QPDF_DLL
174 174 QPDFObjectHandle getArrayItem(int n);
  175 + QPDF_DLL
  176 + std::vector<QPDFObjectHandle> getArrayAsVector();
175 177  
176 178 // Methods for dictionary objects
177 179 QPDF_DLL
... ... @@ -180,6 +182,8 @@ class QPDFObjectHandle
180 182 QPDFObjectHandle getKey(std::string const&);
181 183 QPDF_DLL
182 184 std::set<std::string> getKeys();
  185 + QPDF_DLL
  186 + std::map<std::string, QPDFObjectHandle> getDictAsMap();
183 187  
184 188 // Methods for name and array objects
185 189 QPDF_DLL
... ...
libqpdf/QPDFObjectHandle.cc
... ... @@ -246,6 +246,13 @@ QPDFObjectHandle::getArrayItem(int n)
246 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 256 // Array mutators
250 257  
251 258 void
... ... @@ -278,6 +285,13 @@ QPDFObjectHandle::getKeys()
278 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 295 // Array and Name accessors
282 296 bool
283 297 QPDFObjectHandle::isOrHasName(std::string const& value)
... ...
libqpdf/QPDF_Array.cc
... ... @@ -51,6 +51,12 @@ QPDF_Array::getItem(int n) const
51 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 60 void
55 61 QPDF_Array::setItem(int n, QPDFObjectHandle const& oh)
56 62 {
... ...
libqpdf/QPDF_Dictionary.cc
... ... @@ -78,6 +78,13 @@ QPDF_Dictionary::getKeys()
78 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 88 void
82 89 QPDF_Dictionary::replaceKey(std::string const& key,
83 90 QPDFObjectHandle const& value)
... ...
libqpdf/qpdf/QPDF_Array.hh
... ... @@ -14,6 +14,7 @@ class QPDF_Array: public QPDFObject
14 14 virtual std::string unparse();
15 15 int getNItems() const;
16 16 QPDFObjectHandle getItem(int n) const;
  17 + std::vector<QPDFObjectHandle> const& getAsVector() const;
17 18 void setItem(int, QPDFObjectHandle const&);
18 19  
19 20 protected:
... ...
libqpdf/qpdf/QPDF_Dictionary.hh
... ... @@ -21,6 +21,7 @@ class QPDF_Dictionary: public QPDFObject
21 21 bool hasKey(std::string const&);
22 22 QPDFObjectHandle getKey(std::string const&);
23 23 std::set<std::string> getKeys();
  24 + std::map<std::string, QPDFObjectHandle> const& getAsMap() const;
24 25  
25 26 // Replace value of key, adding it if it does not exist
26 27 void replaceKey(std::string const& key, QPDFObjectHandle const&);
... ...
qpdf/qtest/qpdf/test14.out
... ... @@ -3,4 +3,5 @@ old dict: 1
3 3 old dict: 1
4 4 new dict: 2
5 5 swapped array: /Array
  6 +array and dictionary contents are correct
6 7 test 14 done
... ...
qpdf/test_driver.cc
... ... @@ -570,6 +570,20 @@ void runtest(int n, char const* filename)
570 570 std::cout << "swapped array: " << qdict.getArrayItem(0).getName()
571 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 587 QPDFWriter w(pdf, "a.pdf");
574 588 w.setStaticID(true);
575 589 w.setStreamDataMode(qpdf_s_preserve);
... ...