diff options
author | Tobias Hoffmann <thobi@worker> | 2012-06-18 22:38:59 +0200 |
---|---|---|
committer | Jay Berkenbilt <ejb@ql.org> | 2012-06-20 21:29:44 +0200 |
commit | db7474e0facd82577edfc1cc4883bce3fa588584 (patch) | |
tree | b1f472ddadab0c95747b3963d84682f9d5b17b06 | |
parent | b2e681893503d7f51679b96a3b7be580bb6d362b (diff) | |
download | qpdf-db7474e0facd82577edfc1cc4883bce3fa588584.tar.zst |
Added additional array mutators
Added methods to append to arrays, insert items into arrays, and
replace array contents with a vector of items.
-rw-r--r-- | include/qpdf/QPDFObjectHandle.hh | 13 | ||||
-rw-r--r-- | libqpdf/QPDFObjectHandle.cc | 28 | ||||
-rw-r--r-- | libqpdf/QPDF_Array.cc | 32 | ||||
-rw-r--r-- | libqpdf/qpdf/QPDF_Array.hh | 6 |
4 files changed, 79 insertions, 0 deletions
diff --git a/include/qpdf/QPDFObjectHandle.hh b/include/qpdf/QPDFObjectHandle.hh index f2ed22b7..557ee869 100644 --- a/include/qpdf/QPDFObjectHandle.hh +++ b/include/qpdf/QPDFObjectHandle.hh @@ -200,6 +200,19 @@ class QPDFObjectHandle // Mutator methods for array objects QPDF_DLL void setArrayItem(int, QPDFObjectHandle const&); + QPDF_DLL + void setArrayFromVector(std::vector<QPDFObjectHandle> const& items); + // Insert an item before the item at the given position ("at") so + // that it has that position after insertion. If "at" is equal to + // the size of the array, insert the item at the end. + QPDF_DLL + void insertItem(int at, QPDFObjectHandle const& item); + QPDF_DLL + void appendItem(QPDFObjectHandle const& item); + // Remove the item at that position, reducing the size of the + // array by one. + QPDF_DLL + void eraseItem(int at); // Mutator methods for dictionary objects diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index 55f1bf6f..53d9414c 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -262,6 +262,34 @@ QPDFObjectHandle::setArrayItem(int n, QPDFObjectHandle const& item) return dynamic_cast<QPDF_Array*>(obj.getPointer())->setItem(n, item); } +void +QPDFObjectHandle::setArrayFromVector(std::vector<QPDFObjectHandle> const& items) +{ + assertType("Array", isArray()); + return dynamic_cast<QPDF_Array*>(obj.getPointer())->setFromVector(items); +} + +void +QPDFObjectHandle::insertItem(int at, QPDFObjectHandle const& item) +{ + assertType("Array", isArray()); + return dynamic_cast<QPDF_Array*>(obj.getPointer())->insertItem(at, item); +} + +void +QPDFObjectHandle::appendItem(QPDFObjectHandle const& item) +{ + assertType("Array", isArray()); + return dynamic_cast<QPDF_Array*>(obj.getPointer())->appendItem(item); +} + +void +QPDFObjectHandle::eraseItem(int at) +{ + assertType("Array", isArray()); + return dynamic_cast<QPDF_Array*>(obj.getPointer())->eraseItem(at); +} + // Dictionary accessors bool diff --git a/libqpdf/QPDF_Array.cc b/libqpdf/QPDF_Array.cc index 91e34eec..9f720287 100644 --- a/libqpdf/QPDF_Array.cc +++ b/libqpdf/QPDF_Array.cc @@ -64,3 +64,35 @@ QPDF_Array::setItem(int n, QPDFObjectHandle const& oh) (void) getItem(n); this->items[n] = oh; } + +void +QPDF_Array::setFromVector(std::vector<QPDFObjectHandle> const& items) +{ + this->items = items; +} + +void +QPDF_Array::insertItem(int at, QPDFObjectHandle const& item) +{ + // As special case, also allow insert beyond the end + if ((at < 0) || (at > (int)this->items.size())) + { + throw std::logic_error( + "INTERNAL ERROR: bounds error accessing QPDF_Array element"); + } + this->items.insert(this->items.begin() + at, item); +} + +void +QPDF_Array::appendItem(QPDFObjectHandle const& item) +{ + this->items.push_back(item); +} + +void +QPDF_Array::eraseItem(int at) +{ + // Call getItem for bounds checking + (void) getItem(at); + this->items.erase(this->items.begin() + at); +} diff --git a/libqpdf/qpdf/QPDF_Array.hh b/libqpdf/qpdf/QPDF_Array.hh index a57834c8..338f6eb2 100644 --- a/libqpdf/qpdf/QPDF_Array.hh +++ b/libqpdf/qpdf/QPDF_Array.hh @@ -12,10 +12,16 @@ class QPDF_Array: public QPDFObject QPDF_Array(std::vector<QPDFObjectHandle> const& items); virtual ~QPDF_Array(); virtual std::string unparse(); + int getNItems() const; QPDFObjectHandle getItem(int n) const; std::vector<QPDFObjectHandle> const& getAsVector() const; + void setItem(int, QPDFObjectHandle const&); + void setFromVector(std::vector<QPDFObjectHandle> const& items); + void insertItem(int at, QPDFObjectHandle const& item); + void appendItem(QPDFObjectHandle const& item); + void eraseItem(int at); protected: virtual void releaseResolved(); |