Commit db7474e0facd82577edfc1cc4883bce3fa588584
Committed by
Jay Berkenbilt
1 parent
b2e68189
Added additional array mutators
Added methods to append to arrays, insert items into arrays, and replace array contents with a vector of items.
Showing
4 changed files
with
79 additions
and
0 deletions
include/qpdf/QPDFObjectHandle.hh
| ... | ... | @@ -200,6 +200,19 @@ class QPDFObjectHandle |
| 200 | 200 | // Mutator methods for array objects |
| 201 | 201 | QPDF_DLL |
| 202 | 202 | void setArrayItem(int, QPDFObjectHandle const&); |
| 203 | + QPDF_DLL | |
| 204 | + void setArrayFromVector(std::vector<QPDFObjectHandle> const& items); | |
| 205 | + // Insert an item before the item at the given position ("at") so | |
| 206 | + // that it has that position after insertion. If "at" is equal to | |
| 207 | + // the size of the array, insert the item at the end. | |
| 208 | + QPDF_DLL | |
| 209 | + void insertItem(int at, QPDFObjectHandle const& item); | |
| 210 | + QPDF_DLL | |
| 211 | + void appendItem(QPDFObjectHandle const& item); | |
| 212 | + // Remove the item at that position, reducing the size of the | |
| 213 | + // array by one. | |
| 214 | + QPDF_DLL | |
| 215 | + void eraseItem(int at); | |
| 203 | 216 | |
| 204 | 217 | // Mutator methods for dictionary objects |
| 205 | 218 | ... | ... |
libqpdf/QPDFObjectHandle.cc
| ... | ... | @@ -262,6 +262,34 @@ QPDFObjectHandle::setArrayItem(int n, QPDFObjectHandle const& item) |
| 262 | 262 | return dynamic_cast<QPDF_Array*>(obj.getPointer())->setItem(n, item); |
| 263 | 263 | } |
| 264 | 264 | |
| 265 | +void | |
| 266 | +QPDFObjectHandle::setArrayFromVector(std::vector<QPDFObjectHandle> const& items) | |
| 267 | +{ | |
| 268 | + assertType("Array", isArray()); | |
| 269 | + return dynamic_cast<QPDF_Array*>(obj.getPointer())->setFromVector(items); | |
| 270 | +} | |
| 271 | + | |
| 272 | +void | |
| 273 | +QPDFObjectHandle::insertItem(int at, QPDFObjectHandle const& item) | |
| 274 | +{ | |
| 275 | + assertType("Array", isArray()); | |
| 276 | + return dynamic_cast<QPDF_Array*>(obj.getPointer())->insertItem(at, item); | |
| 277 | +} | |
| 278 | + | |
| 279 | +void | |
| 280 | +QPDFObjectHandle::appendItem(QPDFObjectHandle const& item) | |
| 281 | +{ | |
| 282 | + assertType("Array", isArray()); | |
| 283 | + return dynamic_cast<QPDF_Array*>(obj.getPointer())->appendItem(item); | |
| 284 | +} | |
| 285 | + | |
| 286 | +void | |
| 287 | +QPDFObjectHandle::eraseItem(int at) | |
| 288 | +{ | |
| 289 | + assertType("Array", isArray()); | |
| 290 | + return dynamic_cast<QPDF_Array*>(obj.getPointer())->eraseItem(at); | |
| 291 | +} | |
| 292 | + | |
| 265 | 293 | // Dictionary accessors |
| 266 | 294 | |
| 267 | 295 | bool | ... | ... |
libqpdf/QPDF_Array.cc
| ... | ... | @@ -64,3 +64,35 @@ QPDF_Array::setItem(int n, QPDFObjectHandle const& oh) |
| 64 | 64 | (void) getItem(n); |
| 65 | 65 | this->items[n] = oh; |
| 66 | 66 | } |
| 67 | + | |
| 68 | +void | |
| 69 | +QPDF_Array::setFromVector(std::vector<QPDFObjectHandle> const& items) | |
| 70 | +{ | |
| 71 | + this->items = items; | |
| 72 | +} | |
| 73 | + | |
| 74 | +void | |
| 75 | +QPDF_Array::insertItem(int at, QPDFObjectHandle const& item) | |
| 76 | +{ | |
| 77 | + // As special case, also allow insert beyond the end | |
| 78 | + if ((at < 0) || (at > (int)this->items.size())) | |
| 79 | + { | |
| 80 | + throw std::logic_error( | |
| 81 | + "INTERNAL ERROR: bounds error accessing QPDF_Array element"); | |
| 82 | + } | |
| 83 | + this->items.insert(this->items.begin() + at, item); | |
| 84 | +} | |
| 85 | + | |
| 86 | +void | |
| 87 | +QPDF_Array::appendItem(QPDFObjectHandle const& item) | |
| 88 | +{ | |
| 89 | + this->items.push_back(item); | |
| 90 | +} | |
| 91 | + | |
| 92 | +void | |
| 93 | +QPDF_Array::eraseItem(int at) | |
| 94 | +{ | |
| 95 | + // Call getItem for bounds checking | |
| 96 | + (void) getItem(at); | |
| 97 | + this->items.erase(this->items.begin() + at); | |
| 98 | +} | ... | ... |
libqpdf/qpdf/QPDF_Array.hh
| ... | ... | @@ -12,10 +12,16 @@ class QPDF_Array: public QPDFObject |
| 12 | 12 | QPDF_Array(std::vector<QPDFObjectHandle> const& items); |
| 13 | 13 | virtual ~QPDF_Array(); |
| 14 | 14 | virtual std::string unparse(); |
| 15 | + | |
| 15 | 16 | int getNItems() const; |
| 16 | 17 | QPDFObjectHandle getItem(int n) const; |
| 17 | 18 | std::vector<QPDFObjectHandle> const& getAsVector() const; |
| 19 | + | |
| 18 | 20 | void setItem(int, QPDFObjectHandle const&); |
| 21 | + void setFromVector(std::vector<QPDFObjectHandle> const& items); | |
| 22 | + void insertItem(int at, QPDFObjectHandle const& item); | |
| 23 | + void appendItem(QPDFObjectHandle const& item); | |
| 24 | + void eraseItem(int at); | |
| 19 | 25 | |
| 20 | 26 | protected: |
| 21 | 27 | virtual void releaseResolved(); | ... | ... |