2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-11-18 02:25:08 +00:00
qpdf/libqpdf/QPDF_Array.cc

139 lines
2.9 KiB
C++
Raw Normal View History

#include <qpdf/QPDF_Array.hh>
#include <qpdf/QIntC.hh>
#include <qpdf/QUtil.hh>
#include <stdexcept>
QPDF_Array::QPDF_Array(std::vector<QPDFObjectHandle> const& v) :
QPDFValue(::ot_array, "array")
{
setFromVector(v);
}
2019-08-18 02:13:37 +00:00
QPDF_Array::QPDF_Array(SparseOHArray const& items) :
QPDFValue(::ot_array, "array"),
2019-08-18 02:13:37 +00:00
elements(items)
{
}
std::shared_ptr<QPDFObject>
QPDF_Array::create(std::vector<QPDFObjectHandle> const& items)
{
return do_create(new QPDF_Array(items));
}
std::shared_ptr<QPDFObject>
QPDF_Array::create(SparseOHArray const& items)
{
return do_create(new QPDF_Array(items));
}
std::shared_ptr<QPDFObject>
QPDF_Array::copy(bool shallow)
{
2022-11-14 22:06:04 +00:00
return create(shallow ? elements : elements.copy());
}
void
QPDF_Array::disconnect()
{
elements.disconnect();
}
std::string
QPDF_Array::unparse()
{
std::string result = "[ ";
2019-08-18 02:13:37 +00:00
size_t size = this->elements.size();
for (size_t i = 0; i < size; ++i) {
result += this->elements.at(i).unparse();
result += " ";
}
result += "]";
return result;
}
2018-12-17 22:40:29 +00:00
JSON
QPDF_Array::getJSON(int json_version)
2018-12-17 22:40:29 +00:00
{
JSON j = JSON::makeArray();
2019-08-18 02:13:37 +00:00
size_t size = this->elements.size();
for (size_t i = 0; i < size; ++i) {
j.addArrayElement(this->elements.at(i).getJSON(json_version));
2018-12-17 22:40:29 +00:00
}
return j;
}
int
QPDF_Array::getNItems() const
{
// This should really return a size_t, but changing it would break
// a lot of code.
2019-08-18 02:13:37 +00:00
return QIntC::to_int(this->elements.size());
}
QPDFObjectHandle
QPDF_Array::getItem(int n) const
{
if ((n < 0) || (n >= QIntC::to_int(elements.size()))) {
throw std::logic_error(
"INTERNAL ERROR: bounds error accessing QPDF_Array element");
}
2019-08-18 02:13:37 +00:00
return this->elements.at(QIntC::to_size(n));
}
void
QPDF_Array::getAsVector(std::vector<QPDFObjectHandle>& v) const
{
2019-08-18 02:13:37 +00:00
size_t size = this->elements.size();
for (size_t i = 0; i < size; ++i) {
2019-08-18 02:13:37 +00:00
v.push_back(this->elements.at(i));
}
}
void
QPDF_Array::setItem(int n, QPDFObjectHandle const& oh)
{
2019-08-18 02:13:37 +00:00
this->elements.setAt(QIntC::to_size(n), oh);
}
void
QPDF_Array::setFromVector(std::vector<QPDFObjectHandle> const& v)
{
2019-08-18 02:13:37 +00:00
this->elements = SparseOHArray();
for (auto const& iter: v) {
this->elements.append(iter);
}
}
void
QPDF_Array::insertItem(int at, QPDFObjectHandle const& item)
{
// As special case, also allow insert beyond the end
if ((at < 0) || (at > QIntC::to_int(this->elements.size()))) {
throw std::logic_error(
"INTERNAL ERROR: bounds error accessing QPDF_Array element");
}
2019-08-18 02:13:37 +00:00
this->elements.insert(QIntC::to_size(at), item);
}
void
QPDF_Array::appendItem(QPDFObjectHandle const& item)
{
2019-08-18 02:13:37 +00:00
this->elements.append(item);
}
void
QPDF_Array::eraseItem(int at)
{
2019-08-18 02:13:37 +00:00
this->elements.erase(QIntC::to_size(at));
}
void
QPDF_Array::addExplicitElementsToList(std::list<QPDFObjectHandle>& l) const
{
for (auto const& iter: this->elements) {
l.push_back(iter.second);
}
}