From db7474e0facd82577edfc1cc4883bce3fa588584 Mon Sep 17 00:00:00 2001 From: Tobias Hoffmann Date: Mon, 18 Jun 2012 22:38:59 +0200 Subject: [PATCH] Added additional array mutators Added methods to append to arrays, insert items into arrays, and replace array contents with a vector of items. --- include/qpdf/QPDFObjectHandle.hh | 13 +++++++++++++ libqpdf/QPDFObjectHandle.cc | 28 ++++++++++++++++++++++++++++ libqpdf/QPDF_Array.cc | 32 ++++++++++++++++++++++++++++++++ libqpdf/qpdf/QPDF_Array.hh | 6 ++++++ 4 files changed, 79 insertions(+) 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 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(obj.getPointer())->setItem(n, item); } +void +QPDFObjectHandle::setArrayFromVector(std::vector const& items) +{ + assertType("Array", isArray()); + return dynamic_cast(obj.getPointer())->setFromVector(items); +} + +void +QPDFObjectHandle::insertItem(int at, QPDFObjectHandle const& item) +{ + assertType("Array", isArray()); + return dynamic_cast(obj.getPointer())->insertItem(at, item); +} + +void +QPDFObjectHandle::appendItem(QPDFObjectHandle const& item) +{ + assertType("Array", isArray()); + return dynamic_cast(obj.getPointer())->appendItem(item); +} + +void +QPDFObjectHandle::eraseItem(int at) +{ + assertType("Array", isArray()); + return dynamic_cast(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 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 const& items); virtual ~QPDF_Array(); virtual std::string unparse(); + int getNItems() const; QPDFObjectHandle getItem(int n) const; std::vector const& getAsVector() const; + void setItem(int, QPDFObjectHandle const&); + void setFromVector(std::vector const& items); + void insertItem(int at, QPDFObjectHandle const& item); + void appendItem(QPDFObjectHandle const& item); + void eraseItem(int at); protected: virtual void releaseResolved();