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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

308 lines
8.0 KiB
C++
Raw Normal View History

#include <qpdf/QPDF_Array.hh>
2023-03-29 18:52:03 +00:00
#include <qpdf/QPDFObjectHandle.hh>
#include <qpdf/QPDFObject_private.hh>
2023-03-25 16:59:49 +00:00
static const QPDFObjectHandle null_oh = QPDFObjectHandle::newNull();
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"),
sparse(other.sparse),
sp_size(other.sp_size),
sp_elements(other.sp_elements),
elements(other.elements)
{
}
QPDF_Array::QPDF_Array(std::vector<QPDFObjectHandle> const& v) :
QPDFValue(::ot_array, "array")
{
setFromVector(v);
}
QPDF_Array::QPDF_Array(std::vector<std::shared_ptr<QPDFObject>>&& v, bool sparse) :
QPDFValue(::ot_array, "array"),
sparse(sparse)
{
2023-03-30 13:16:07 +00:00
if (sparse) {
for (auto&& item: v) {
if (item->getTypeCode() != ::ot_null || item->getObjGen().isIndirect()) {
2023-03-29 18:52:03 +00:00
sp_elements[sp_size] = std::move(item);
2023-03-30 13:16:07 +00:00
}
2023-03-29 18:52:03 +00:00
++sp_size;
2023-03-30 13:16:07 +00:00
}
} else {
elements = std::move(v);
}
}
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(std::vector<std::shared_ptr<QPDFObject>>&& items, bool sparse)
{
return do_create(new QPDF_Array(std::move(items), sparse));
}
std::shared_ptr<QPDFObject>
QPDF_Array::copy(bool shallow)
{
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 {
if (sparse) {
2023-05-20 12:24:10 +00:00
auto* result = new QPDF_Array();
2023-03-29 18:52:03 +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;
2023-03-29 18:52:03 +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);
} 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(
element ? (element->getObjGen().isIndirect() ? element : element->copy())
: element);
}
2023-03-29 18:52:03 +00:00
return create(std::move(result), false);
}
}
}
void
QPDF_Array::disconnect()
{
if (sparse) {
2023-03-29 18:52:03 +00:00
for (auto& item: sp_elements) {
2022-12-11 16:13:19 +00:00
auto& obj = item.second;
if (!obj->getObjGen().isIndirect()) {
obj->disconnect();
}
}
} else {
2022-12-11 16:13:19 +00:00
for (auto& obj: elements) {
if (!obj->getObjGen().isIndirect()) {
obj->disconnect();
}
}
}
}
std::string
QPDF_Array::unparse()
{
2022-12-25 11:03:54 +00:00
std::string result = "[ ";
if (sparse) {
2022-12-25 11:03:54 +00:00
int next = 0;
for (auto& item: sp_elements) {
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;
}
for (int j = next; j < sp_size; ++j) {
result += "null ";
}
} 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() + " ";
}
}
2022-12-25 11:03:54 +00:00
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
{
2022-12-25 09:52:43 +00:00
static const JSON j_null = JSON::makeNull();
JSON j_array = JSON::makeArray();
if (sparse) {
2022-12-25 09:52:43 +00:00
int next = 0;
for (auto& item: sp_elements) {
int key = item.first;
for (int j = next; j < key; ++j) {
j_array.addArrayElement(j_null);
}
auto og = item.second->getObjGen();
j_array.addArrayElement(
og.isIndirect() ? JSON::makeString(og.unparse(' ') + " R")
: item.second->getJSON(json_version));
next = ++key;
}
for (int j = next; j < sp_size; ++j) {
j_array.addArrayElement(j_null);
}
} else {
2022-12-25 09:52:43 +00:00
for (auto const& item: elements) {
auto og = item->getObjGen();
j_array.addArrayElement(
og.isIndirect() ? JSON::makeString(og.unparse(' ') + " R")
: item->getJSON(json_version));
}
2018-12-17 22:40:29 +00:00
}
2022-12-25 09:52:43 +00:00
return j_array;
2018-12-17 22:40:29 +00:00
}
QPDFObjectHandle
QPDF_Array::at(int n) const noexcept
{
if (n < 0 || n >= size()) {
return {};
} else if (sparse) {
2023-03-29 18:52:03 +00:00
auto const& iter = sp_elements.find(n);
return iter == sp_elements.end() ? null_oh : (*iter).second;
} else {
return elements[size_t(n)];
}
}
2023-01-02 19:49:42 +00:00
std::vector<QPDFObjectHandle>
QPDF_Array::getAsVector() const
{
if (sparse) {
2023-01-02 19:49:42 +00:00
std::vector<QPDFObjectHandle> v;
v.reserve(size_t(size()));
2023-03-29 18:52:03 +00:00
for (auto const& item: sp_elements) {
2023-01-02 19:49:42 +00:00
v.resize(size_t(item.first), null_oh);
v.push_back(item.second);
}
2023-01-02 19:49:42 +00:00
v.resize(size_t(size()), null_oh);
return v;
} else {
2023-01-02 19:49:42 +00:00
return {elements.cbegin(), elements.cend()};
}
}
bool
QPDF_Array::setAt(int at, QPDFObjectHandle const& oh)
{
if (at < 0 || at >= size()) {
return false;
}
checkOwnership(oh);
if (sparse) {
2023-03-29 18:52:03 +00:00
sp_elements[at] = oh.getObj();
} else {
elements[size_t(at)] = oh.getObj();
}
return true;
}
void
QPDF_Array::setFromVector(std::vector<QPDFObjectHandle> const& v)
{
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());
}
}
bool
QPDF_Array::insert(int at, QPDFObjectHandle const& item)
{
int sz = size();
if (at < 0 || at > sz) {
// As special case, also allow insert beyond the end
return false;
} else if (at == sz) {
push_back(item);
} else {
checkOwnership(item);
if (sparse) {
2023-03-29 18:52:03 +00:00
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) {
2023-03-29 18:52:03 +00:00
auto nh = sp_elements.extract(key);
2023-03-29 14:45:12 +00:00
++nh.key();
2023-03-29 18:52:03 +00:00
sp_elements.insert(std::move(nh));
2023-03-29 14:45:12 +00:00
} else {
break;
}
}
2023-03-29 18:52:03 +00:00
sp_elements[at] = item.getObj();
++sp_size;
} else {
elements.insert(elements.cbegin() + at, item.getObj());
}
}
return true;
}
void
QPDF_Array::push_back(QPDFObjectHandle const& item)
{
checkOwnership(item);
if (sparse) {
2023-03-29 18:52:03 +00:00
sp_elements[sp_size++] = item.getObj();
} else {
2023-03-25 18:23:39 +00:00
elements.push_back(item.getObj());
}
}
bool
QPDF_Array::erase(int at)
{
if (at < 0 || at >= size()) {
return false;
}
if (sparse) {
2023-03-29 18:52:03 +00:00
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++;
2023-03-29 18:52:03 +00:00
sp_elements.erase(at);
2023-03-29 14:53:34 +00:00
}
while (iter != end) {
2023-03-29 18:52:03 +00:00
auto nh = sp_elements.extract(iter++);
2023-03-29 14:53:34 +00:00
--nh.key();
2023-03-29 18:52:03 +00:00
sp_elements.insert(std::move(nh));
2023-03-29 14:53:34 +00:00
}
}
2023-03-29 18:52:03 +00:00
--sp_size;
} else {
elements.erase(elements.cbegin() + at);
}
return true;
}