2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QPDF_Array.hh>
|
2022-02-04 16:31:31 -05:00
|
|
|
|
2019-06-20 23:35:23 -04:00
|
|
|
#include <qpdf/QIntC.hh>
|
2022-04-02 17:14:10 -04:00
|
|
|
#include <qpdf/QUtil.hh>
|
2009-09-26 18:36:04 +00:00
|
|
|
#include <stdexcept>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-08-02 22:57:33 +01:00
|
|
|
QPDF_Array::QPDF_Array(std::vector<QPDFObjectHandle> const& v) :
|
|
|
|
QPDFValue(::ot_array, "array")
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2019-08-17 19:01:32 -04:00
|
|
|
setFromVector(v);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-08-17 22:13:37 -04:00
|
|
|
QPDF_Array::QPDF_Array(SparseOHArray const& items) :
|
2022-08-02 22:57:33 +01:00
|
|
|
QPDFValue(::ot_array, "array"),
|
2019-08-17 22:13:37 -04:00
|
|
|
elements(items)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-09-08 11:29:23 -04:00
|
|
|
std::shared_ptr<QPDFObject>
|
2022-06-16 17:45:04 +01:00
|
|
|
QPDF_Array::create(std::vector<QPDFObjectHandle> const& items)
|
|
|
|
{
|
|
|
|
return do_create(new QPDF_Array(items));
|
|
|
|
}
|
|
|
|
|
2022-09-08 11:29:23 -04:00
|
|
|
std::shared_ptr<QPDFObject>
|
2022-06-16 17:45:04 +01:00
|
|
|
QPDF_Array::create(SparseOHArray const& items)
|
|
|
|
{
|
|
|
|
return do_create(new QPDF_Array(items));
|
|
|
|
}
|
|
|
|
|
2022-09-08 11:29:23 -04:00
|
|
|
std::shared_ptr<QPDFObject>
|
2022-06-16 17:45:04 +01:00
|
|
|
QPDF_Array::shallowCopy()
|
|
|
|
{
|
|
|
|
return create(elements);
|
|
|
|
}
|
|
|
|
|
2022-09-07 16:49:31 -04:00
|
|
|
void
|
2022-09-08 11:06:15 -04:00
|
|
|
QPDF_Array::disconnect()
|
2022-09-07 16:49:31 -04:00
|
|
|
{
|
2022-09-08 11:06:15 -04:00
|
|
|
elements.disconnect();
|
2022-09-07 16:49:31 -04:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string
|
|
|
|
QPDF_Array::unparse()
|
|
|
|
{
|
|
|
|
std::string result = "[ ";
|
2019-08-17 22:13:37 -04:00
|
|
|
size_t size = this->elements.size();
|
2022-04-02 17:14:10 -04:00
|
|
|
for (size_t i = 0; i < size; ++i) {
|
2022-02-08 09:18:08 -05:00
|
|
|
result += this->elements.at(i).unparse();
|
|
|
|
result += " ";
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
result += "]";
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-12-17 17:40:29 -05:00
|
|
|
JSON
|
2022-05-07 07:53:45 -04:00
|
|
|
QPDF_Array::getJSON(int json_version)
|
2018-12-17 17:40:29 -05:00
|
|
|
{
|
|
|
|
JSON j = JSON::makeArray();
|
2019-08-17 22:13:37 -04:00
|
|
|
size_t size = this->elements.size();
|
2022-04-02 17:14:10 -04:00
|
|
|
for (size_t i = 0; i < size; ++i) {
|
2022-05-07 07:53:45 -04:00
|
|
|
j.addArrayElement(this->elements.at(i).getJSON(json_version));
|
2018-12-17 17:40:29 -05:00
|
|
|
}
|
|
|
|
return j;
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
int
|
|
|
|
QPDF_Array::getNItems() const
|
|
|
|
{
|
2019-06-20 23:35:23 -04:00
|
|
|
// This should really return a size_t, but changing it would break
|
|
|
|
// a lot of code.
|
2019-08-17 22:13:37 -04:00
|
|
|
return QIntC::to_int(this->elements.size());
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF_Array::getItem(int n) const
|
|
|
|
{
|
2022-04-02 17:14:10 -04:00
|
|
|
if ((n < 0) || (n >= QIntC::to_int(elements.size()))) {
|
2022-02-08 09:18:08 -05:00
|
|
|
throw std::logic_error(
|
|
|
|
"INTERNAL ERROR: bounds error accessing QPDF_Array element");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2019-08-17 22:13:37 -04:00
|
|
|
return this->elements.at(QIntC::to_size(n));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-08-17 19:01:32 -04:00
|
|
|
void
|
|
|
|
QPDF_Array::getAsVector(std::vector<QPDFObjectHandle>& v) const
|
2011-08-10 13:33:58 -04:00
|
|
|
{
|
2019-08-17 22:13:37 -04:00
|
|
|
size_t size = this->elements.size();
|
2022-04-02 17:14:10 -04:00
|
|
|
for (size_t i = 0; i < size; ++i) {
|
2019-08-17 22:13:37 -04:00
|
|
|
v.push_back(this->elements.at(i));
|
2019-08-17 19:01:32 -04:00
|
|
|
}
|
2011-08-10 13:33:58 -04:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
|
|
|
QPDF_Array::setItem(int n, QPDFObjectHandle const& oh)
|
|
|
|
{
|
2019-08-17 22:13:37 -04:00
|
|
|
this->elements.setAt(QIntC::to_size(n), oh);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2012-06-18 22:38:59 +02:00
|
|
|
|
|
|
|
void
|
2019-08-17 19:01:32 -04:00
|
|
|
QPDF_Array::setFromVector(std::vector<QPDFObjectHandle> const& v)
|
2012-06-18 22:38:59 +02:00
|
|
|
{
|
2019-08-17 22:13:37 -04:00
|
|
|
this->elements = SparseOHArray();
|
2022-04-30 13:23:18 -04:00
|
|
|
for (auto const& iter: v) {
|
|
|
|
this->elements.append(iter);
|
2019-08-17 19:01:32 -04:00
|
|
|
}
|
2012-06-18 22:38:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF_Array::insertItem(int at, QPDFObjectHandle const& item)
|
|
|
|
{
|
|
|
|
// As special case, also allow insert beyond the end
|
2022-04-02 17:14:10 -04:00
|
|
|
if ((at < 0) || (at > QIntC::to_int(this->elements.size()))) {
|
2022-02-08 09:18:08 -05:00
|
|
|
throw std::logic_error(
|
|
|
|
"INTERNAL ERROR: bounds error accessing QPDF_Array element");
|
2012-06-18 22:38:59 +02:00
|
|
|
}
|
2019-08-17 22:13:37 -04:00
|
|
|
this->elements.insert(QIntC::to_size(at), item);
|
2012-06-18 22:38:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF_Array::appendItem(QPDFObjectHandle const& item)
|
|
|
|
{
|
2019-08-17 22:13:37 -04:00
|
|
|
this->elements.append(item);
|
2012-06-18 22:38:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF_Array::eraseItem(int at)
|
|
|
|
{
|
2019-08-17 22:13:37 -04:00
|
|
|
this->elements.erase(QIntC::to_size(at));
|
2012-06-18 22:38:59 +02:00
|
|
|
}
|
2019-08-17 22:27:55 -04:00
|
|
|
|
2019-08-17 22:28:19 -04:00
|
|
|
void
|
|
|
|
QPDF_Array::addExplicitElementsToList(std::list<QPDFObjectHandle>& l) const
|
|
|
|
{
|
2022-04-30 09:43:07 -04:00
|
|
|
for (auto const& iter: this->elements) {
|
2020-04-08 20:14:04 -04:00
|
|
|
l.push_back(iter.second);
|
2019-08-17 22:28:19 -04:00
|
|
|
}
|
|
|
|
}
|