2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-12-22 19:08:59 +00:00

Added additional array mutators

Added methods to append to arrays, insert items into arrays, and
replace array contents with a vector of items.
This commit is contained in:
Tobias Hoffmann 2012-06-18 22:38:59 +02:00 committed by Jay Berkenbilt
parent b2e6818935
commit db7474e0fa
4 changed files with 79 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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);
}

View File

@ -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();