2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QPDF_Array.hh>
|
2022-02-04 21:31:31 +00:00
|
|
|
|
2024-02-09 13:09:08 +00:00
|
|
|
#include <qpdf/JSON_writer.hh>
|
2023-03-29 18:52:03 +00:00
|
|
|
#include <qpdf/QPDFObjectHandle.hh>
|
2022-12-19 11:41:09 +00:00
|
|
|
#include <qpdf/QPDFObject_private.hh>
|
2024-02-04 21:11:53 +00:00
|
|
|
#include <qpdf/QTC.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-03-25 16:59:49 +00:00
|
|
|
static const QPDFObjectHandle null_oh = QPDFObjectHandle::newNull();
|
|
|
|
|
2022-11-17 17:35:13 +00:00
|
|
|
inline void
|
|
|
|
QPDF_Array::checkOwnership(QPDFObjectHandle const& item) const
|
|
|
|
{
|
|
|
|
if (auto obj = item.getObjectPtr()) {
|
|
|
|
if (qpdf) {
|
|
|
|
if (auto item_qpdf = obj->getQPDF()) {
|
|
|
|
if (qpdf != item_qpdf) {
|
|
|
|
throw std::logic_error(
|
|
|
|
"Attempting to add an object from a different QPDF. Use "
|
|
|
|
"QPDF::copyForeignObject to add objects from another file.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw std::logic_error("Attempting to add an uninitialized object to a QPDF_Array.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-29 18:52:03 +00:00
|
|
|
QPDF_Array::QPDF_Array() :
|
|
|
|
QPDFValue(::ot_array, "array")
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDF_Array::QPDF_Array(QPDF_Array const& other) :
|
|
|
|
QPDFValue(::ot_array, "array"),
|
2024-01-09 15:19:48 +00:00
|
|
|
sp(other.sp ? std::make_unique<Sparse>(*other.sp) : nullptr)
|
2023-03-29 18:52:03 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-08-02 21:57:33 +00: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 23:01:32 +00:00
|
|
|
setFromVector(v);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-03-26 19:02:49 +00:00
|
|
|
QPDF_Array::QPDF_Array(std::vector<std::shared_ptr<QPDFObject>>&& v, bool sparse) :
|
2024-01-09 15:19:48 +00:00
|
|
|
QPDFValue(::ot_array, "array")
|
2022-12-19 11:41:09 +00:00
|
|
|
{
|
2023-03-30 13:16:07 +00:00
|
|
|
if (sparse) {
|
2024-01-09 15:19:48 +00:00
|
|
|
sp = std::make_unique<Sparse>();
|
2023-03-30 13:16:07 +00:00
|
|
|
for (auto&& item: v) {
|
|
|
|
if (item->getTypeCode() != ::ot_null || item->getObjGen().isIndirect()) {
|
2024-01-09 15:19:48 +00:00
|
|
|
sp->elements[sp->size] = std::move(item);
|
2023-03-30 13:16:07 +00:00
|
|
|
}
|
2024-01-09 15:19:48 +00:00
|
|
|
++sp->size;
|
2023-03-30 13:16:07 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
elements = std::move(v);
|
|
|
|
}
|
2022-12-19 11:41:09 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 15:29:23 +00:00
|
|
|
std::shared_ptr<QPDFObject>
|
2022-06-16 16:45:04 +00:00
|
|
|
QPDF_Array::create(std::vector<QPDFObjectHandle> const& items)
|
|
|
|
{
|
|
|
|
return do_create(new QPDF_Array(items));
|
|
|
|
}
|
|
|
|
|
2022-12-19 11:41:09 +00:00
|
|
|
std::shared_ptr<QPDFObject>
|
2023-03-26 19:02:49 +00:00
|
|
|
QPDF_Array::create(std::vector<std::shared_ptr<QPDFObject>>&& items, bool sparse)
|
2022-12-19 11:41:09 +00:00
|
|
|
{
|
2023-03-26 19:02:49 +00:00
|
|
|
return do_create(new QPDF_Array(std::move(items), sparse));
|
2022-12-19 11:41:09 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 15:29:23 +00:00
|
|
|
std::shared_ptr<QPDFObject>
|
2022-11-14 17:54:12 +00:00
|
|
|
QPDF_Array::copy(bool shallow)
|
2022-06-16 16:45:04 +00:00
|
|
|
{
|
2022-12-11 16:49:41 +00:00
|
|
|
if (shallow) {
|
2023-03-29 18:52:03 +00:00
|
|
|
return do_create(new QPDF_Array(*this));
|
2022-12-11 16:49:41 +00:00
|
|
|
} else {
|
2024-01-12 12:11:46 +00:00
|
|
|
QTC::TC("qpdf", "QPDF_Array copy", sp ? 0 : 1);
|
2024-01-09 15:19:48 +00:00
|
|
|
if (sp) {
|
2023-05-20 12:24:10 +00:00
|
|
|
auto* result = new QPDF_Array();
|
2024-01-12 12:11:46 +00:00
|
|
|
result->sp = std::make_unique<Sparse>();
|
2024-01-09 15:19:48 +00:00
|
|
|
result->sp->size = sp->size;
|
|
|
|
for (auto const& element: sp->elements) {
|
2022-12-11 16:49:41 +00:00
|
|
|
auto const& obj = element.second;
|
2024-01-09 15:19:48 +00:00
|
|
|
result->sp->elements[element.first] =
|
2022-12-11 16:49:41 +00:00
|
|
|
obj->getObjGen().isIndirect() ? obj : obj->copy();
|
|
|
|
}
|
2023-03-29 18:52:03 +00:00
|
|
|
return do_create(result);
|
2023-03-25 18:05:54 +00:00
|
|
|
} else {
|
2023-03-25 18:23:39 +00:00
|
|
|
std::vector<std::shared_ptr<QPDFObject>> result;
|
|
|
|
result.reserve(elements.size());
|
|
|
|
for (auto const& element: elements) {
|
|
|
|
result.push_back(
|
2023-03-25 18:05:54 +00:00
|
|
|
element ? (element->getObjGen().isIndirect() ? element : element->copy())
|
|
|
|
: element);
|
|
|
|
}
|
2023-03-29 18:52:03 +00:00
|
|
|
return create(std::move(result), false);
|
2023-03-25 18:05:54 +00:00
|
|
|
}
|
2023-03-25 15:30:52 +00:00
|
|
|
}
|
2022-06-16 16:45:04 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 20:49:31 +00:00
|
|
|
void
|
2022-09-08 15:06:15 +00:00
|
|
|
QPDF_Array::disconnect()
|
2022-09-07 20:49:31 +00:00
|
|
|
{
|
2024-01-09 15:19:48 +00:00
|
|
|
if (sp) {
|
|
|
|
for (auto& item: sp->elements) {
|
2022-12-11 16:13:19 +00:00
|
|
|
auto& obj = item.second;
|
|
|
|
if (!obj->getObjGen().isIndirect()) {
|
|
|
|
obj->disconnect();
|
|
|
|
}
|
|
|
|
}
|
2023-03-25 15:30:52 +00:00
|
|
|
} else {
|
2022-12-11 16:13:19 +00:00
|
|
|
for (auto& obj: elements) {
|
|
|
|
if (!obj->getObjGen().isIndirect()) {
|
|
|
|
obj->disconnect();
|
2023-03-25 17:23:19 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-25 15:30:52 +00:00
|
|
|
}
|
2022-09-07 20:49:31 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string
|
|
|
|
QPDF_Array::unparse()
|
|
|
|
{
|
2022-12-25 11:03:54 +00:00
|
|
|
std::string result = "[ ";
|
2024-01-09 15:19:48 +00:00
|
|
|
if (sp) {
|
2022-12-25 11:03:54 +00:00
|
|
|
int next = 0;
|
2024-01-09 15:19:48 +00:00
|
|
|
for (auto& item: sp->elements) {
|
2022-12-25 11:03:54 +00:00
|
|
|
int key = item.first;
|
|
|
|
for (int j = next; j < key; ++j) {
|
|
|
|
result += "null ";
|
|
|
|
}
|
|
|
|
item.second->resolve();
|
|
|
|
auto og = item.second->getObjGen();
|
|
|
|
result += og.isIndirect() ? og.unparse(' ') + " R " : item.second->unparse() + " ";
|
|
|
|
next = ++key;
|
|
|
|
}
|
2024-01-09 15:19:48 +00:00
|
|
|
for (int j = next; j < sp->size; ++j) {
|
2022-12-25 11:03:54 +00:00
|
|
|
result += "null ";
|
2023-03-25 15:30:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-12-25 11:03:54 +00:00
|
|
|
for (auto const& item: elements) {
|
|
|
|
item->resolve();
|
|
|
|
auto og = item->getObjGen();
|
|
|
|
result += og.isIndirect() ? og.unparse(' ') + " R " : item->unparse() + " ";
|
2023-03-25 15:30:52 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2022-12-25 11:03:54 +00:00
|
|
|
result += "]";
|
|
|
|
return result;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2024-02-09 13:09:08 +00:00
|
|
|
void
|
|
|
|
QPDF_Array::writeJSON(int json_version, JSON::Writer& p)
|
|
|
|
{
|
|
|
|
p.writeStart('[');
|
|
|
|
if (sp) {
|
|
|
|
int next = 0;
|
|
|
|
for (auto& item: sp->elements) {
|
|
|
|
int key = item.first;
|
|
|
|
for (int j = next; j < key; ++j) {
|
|
|
|
p.writeNext() << "null";
|
|
|
|
}
|
|
|
|
p.writeNext();
|
|
|
|
auto og = item.second->getObjGen();
|
|
|
|
if (og.isIndirect()) {
|
|
|
|
p << "\"" << og.unparse(' ') << " R\"";
|
|
|
|
} else {
|
|
|
|
item.second->writeJSON(json_version, p);
|
|
|
|
}
|
|
|
|
next = ++key;
|
|
|
|
}
|
|
|
|
for (int j = next; j < sp->size; ++j) {
|
|
|
|
p.writeNext() << "null";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (auto const& item: elements) {
|
|
|
|
p.writeNext();
|
|
|
|
auto og = item->getObjGen();
|
|
|
|
if (og.isIndirect()) {
|
|
|
|
p << "\"" << og.unparse(' ') << " R\"";
|
|
|
|
} else {
|
|
|
|
item->writeJSON(json_version, p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.writeEnd(']');
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFObjectHandle
|
2023-03-24 15:01:40 +00:00
|
|
|
QPDF_Array::at(int n) const noexcept
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-03-24 15:01:40 +00:00
|
|
|
if (n < 0 || n >= size()) {
|
|
|
|
return {};
|
2024-01-09 15:19:48 +00:00
|
|
|
} else if (sp) {
|
|
|
|
auto const& iter = sp->elements.find(n);
|
|
|
|
return iter == sp->elements.end() ? null_oh : (*iter).second;
|
2023-03-25 15:30:52 +00:00
|
|
|
} else {
|
2023-03-24 15:01:40 +00:00
|
|
|
return elements[size_t(n)];
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-02 19:49:42 +00:00
|
|
|
std::vector<QPDFObjectHandle>
|
|
|
|
QPDF_Array::getAsVector() const
|
2011-08-10 17:33:58 +00:00
|
|
|
{
|
2024-01-09 15:19:48 +00:00
|
|
|
if (sp) {
|
2023-01-02 19:49:42 +00:00
|
|
|
std::vector<QPDFObjectHandle> v;
|
|
|
|
v.reserve(size_t(size()));
|
2024-01-09 15:19:48 +00:00
|
|
|
for (auto const& item: sp->elements) {
|
2023-01-02 19:49:42 +00:00
|
|
|
v.resize(size_t(item.first), null_oh);
|
2023-05-27 22:49:18 +00:00
|
|
|
v.emplace_back(item.second);
|
2023-03-25 15:30:52 +00:00
|
|
|
}
|
2023-01-02 19:49:42 +00:00
|
|
|
v.resize(size_t(size()), null_oh);
|
|
|
|
return v;
|
2023-03-25 15:30:52 +00:00
|
|
|
} else {
|
2023-01-02 19:49:42 +00:00
|
|
|
return {elements.cbegin(), elements.cend()};
|
2019-08-17 23:01:32 +00:00
|
|
|
}
|
2011-08-10 17:33:58 +00:00
|
|
|
}
|
|
|
|
|
2023-01-01 12:47:03 +00:00
|
|
|
bool
|
|
|
|
QPDF_Array::setAt(int at, QPDFObjectHandle const& oh)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-01-01 12:47:03 +00:00
|
|
|
if (at < 0 || at >= size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
checkOwnership(oh);
|
2024-01-09 15:19:48 +00:00
|
|
|
if (sp) {
|
|
|
|
sp->elements[at] = oh.getObj();
|
2023-03-25 15:30:52 +00:00
|
|
|
} else {
|
2023-01-01 12:47:03 +00:00
|
|
|
elements[size_t(at)] = oh.getObj();
|
2023-03-25 15:30:52 +00:00
|
|
|
}
|
2023-01-01 12:47:03 +00:00
|
|
|
return true;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2012-06-18 20:38:59 +00:00
|
|
|
|
|
|
|
void
|
2019-08-17 23:01:32 +00:00
|
|
|
QPDF_Array::setFromVector(std::vector<QPDFObjectHandle> const& v)
|
2012-06-18 20:38:59 +00:00
|
|
|
{
|
2023-03-30 13:16:07 +00:00
|
|
|
elements.resize(0);
|
|
|
|
elements.reserve(v.size());
|
|
|
|
for (auto const& item: v) {
|
|
|
|
checkOwnership(item);
|
|
|
|
elements.push_back(item.getObj());
|
2022-12-19 11:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-10 19:48:07 +00:00
|
|
|
bool
|
|
|
|
QPDF_Array::insert(int at, QPDFObjectHandle const& item)
|
2012-06-18 20:38:59 +00:00
|
|
|
{
|
2022-12-10 19:48:07 +00:00
|
|
|
int sz = size();
|
|
|
|
if (at < 0 || at > sz) {
|
2023-03-25 15:30:52 +00:00
|
|
|
// As special case, also allow insert beyond the end
|
2022-12-10 19:48:07 +00:00
|
|
|
return false;
|
|
|
|
} else if (at == sz) {
|
|
|
|
push_back(item);
|
2023-03-25 15:30:52 +00:00
|
|
|
} else {
|
2022-12-10 19:48:07 +00:00
|
|
|
checkOwnership(item);
|
2024-01-09 15:19:48 +00:00
|
|
|
if (sp) {
|
|
|
|
auto iter = sp->elements.crbegin();
|
|
|
|
while (iter != sp->elements.crend()) {
|
2023-03-29 14:45:12 +00:00
|
|
|
auto key = (iter++)->first;
|
|
|
|
if (key >= at) {
|
2024-01-09 15:19:48 +00:00
|
|
|
auto nh = sp->elements.extract(key);
|
2023-03-29 14:45:12 +00:00
|
|
|
++nh.key();
|
2024-01-09 15:19:48 +00:00
|
|
|
sp->elements.insert(std::move(nh));
|
2023-03-29 14:45:12 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-01-09 15:19:48 +00:00
|
|
|
sp->elements[at] = item.getObj();
|
|
|
|
++sp->size;
|
2023-03-25 18:05:54 +00:00
|
|
|
} else {
|
2022-12-10 19:48:07 +00:00
|
|
|
elements.insert(elements.cbegin() + at, item.getObj());
|
2023-03-25 18:05:54 +00:00
|
|
|
}
|
2012-06-18 20:38:59 +00:00
|
|
|
}
|
2022-12-10 19:48:07 +00:00
|
|
|
return true;
|
2012-06-18 20:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-12-10 19:05:12 +00:00
|
|
|
QPDF_Array::push_back(QPDFObjectHandle const& item)
|
2012-06-18 20:38:59 +00:00
|
|
|
{
|
2022-12-10 19:05:12 +00:00
|
|
|
checkOwnership(item);
|
2024-01-09 15:19:48 +00:00
|
|
|
if (sp) {
|
|
|
|
sp->elements[(sp->size)++] = item.getObj();
|
2023-03-25 15:30:52 +00:00
|
|
|
} else {
|
2023-03-25 18:23:39 +00:00
|
|
|
elements.push_back(item.getObj());
|
2023-03-25 15:30:52 +00:00
|
|
|
}
|
2012-06-18 20:38:59 +00:00
|
|
|
}
|
|
|
|
|
2022-12-12 13:29:52 +00:00
|
|
|
bool
|
|
|
|
QPDF_Array::erase(int at)
|
2012-06-18 20:38:59 +00:00
|
|
|
{
|
2022-12-12 13:29:52 +00:00
|
|
|
if (at < 0 || at >= size()) {
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-09 15:19:48 +00:00
|
|
|
if (sp) {
|
|
|
|
auto end = sp->elements.end();
|
|
|
|
if (auto iter = sp->elements.lower_bound(at); iter != end) {
|
2023-03-29 14:53:34 +00:00
|
|
|
if (iter->first == at) {
|
|
|
|
iter++;
|
2024-01-09 15:19:48 +00:00
|
|
|
sp->elements.erase(at);
|
2023-03-29 14:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (iter != end) {
|
2024-01-09 15:19:48 +00:00
|
|
|
auto nh = sp->elements.extract(iter++);
|
2023-03-29 14:53:34 +00:00
|
|
|
--nh.key();
|
2024-01-09 15:19:48 +00:00
|
|
|
sp->elements.insert(std::move(nh));
|
2023-03-29 14:53:34 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-09 15:19:48 +00:00
|
|
|
--(sp->size);
|
2023-03-25 15:30:52 +00:00
|
|
|
} else {
|
2022-12-12 13:29:52 +00:00
|
|
|
elements.erase(elements.cbegin() + at);
|
2023-03-25 15:30:52 +00:00
|
|
|
}
|
2022-12-12 13:29:52 +00:00
|
|
|
return true;
|
2012-06-18 20:38:59 +00:00
|
|
|
}
|