2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-11-10 07:11:07 +00:00
qpdf/libqpdf/QPDF_Dictionary.cc

141 lines
3.5 KiB
C++
Raw Normal View History

#include <qpdf/QPDF_Dictionary.hh>
#include <qpdf/QPDFObject_private.hh>
#include <qpdf/QPDF_Name.hh>
#include <qpdf/QPDF_Null.hh>
using namespace std::literals;
2023-05-21 17:35:09 +00:00
QPDF_Dictionary::QPDF_Dictionary(std::map<std::string, QPDFObjectHandle> const& items) :
QPDFValue(::ot_dictionary, "dictionary"),
items(items)
{
}
2023-05-21 17:35:09 +00:00
QPDF_Dictionary::QPDF_Dictionary(std::map<std::string, QPDFObjectHandle>&& items) :
QPDFValue(::ot_dictionary, "dictionary"),
items(items)
{
}
std::shared_ptr<QPDFObject>
QPDF_Dictionary::create(std::map<std::string, QPDFObjectHandle> const& items)
{
return do_create(new QPDF_Dictionary(items));
}
std::shared_ptr<QPDFObject>
QPDF_Dictionary::create(std::map<std::string, QPDFObjectHandle>&& items)
{
return do_create(new QPDF_Dictionary(items));
}
std::shared_ptr<QPDFObject>
QPDF_Dictionary::copy(bool shallow)
{
2022-11-14 22:06:04 +00:00
if (shallow) {
return create(items);
} else {
std::map<std::string, QPDFObjectHandle> new_items;
for (auto const& item: this->items) {
auto value = item.second;
2023-05-21 17:35:09 +00:00
new_items[item.first] = value.isIndirect() ? value : value.shallowCopy();
2022-11-14 22:06:04 +00:00
}
return create(new_items);
}
}
void
QPDF_Dictionary::disconnect()
{
for (auto& iter: this->items) {
QPDFObjectHandle::DisconnectAccess::disconnect(iter.second);
}
}
std::string
QPDF_Dictionary::unparse()
{
std::string result = "<< ";
for (auto& iter: this->items) {
if (!iter.second.isNull()) {
2023-05-21 17:35:09 +00:00
result += QPDF_Name::normalizeName(iter.first) + " " + iter.second.unparse() + " ";
}
}
result += ">>";
return result;
}
2018-12-17 22:40:29 +00:00
JSON
QPDF_Dictionary::getJSON(int json_version)
2018-12-17 22:40:29 +00:00
{
JSON j = JSON::makeDictionary();
for (auto& iter: this->items) {
if (!iter.second.isNull()) {
std::string key =
2023-05-21 17:35:09 +00:00
(json_version == 1 ? QPDF_Name::normalizeName(iter.first) : iter.first);
j.addDictionaryMember(key, iter.second.getJSON(json_version));
}
2018-12-17 22:40:29 +00:00
}
return j;
}
bool
QPDF_Dictionary::hasKey(std::string const& key)
{
return ((this->items.count(key) > 0) && (!this->items[key].isNull()));
}
QPDFObjectHandle
QPDF_Dictionary::getKey(std::string const& key)
{
// PDF spec says fetching a non-existent key from a dictionary returns the null object.
auto item = this->items.find(key);
if (item != this->items.end()) {
// May be a null object
return item->second;
} else {
static auto constexpr msg = " -> dictionary key $VD"sv;
return QPDF_Null::create(shared_from_this(), msg, key);
}
}
std::set<std::string>
QPDF_Dictionary::getKeys()
{
std::set<std::string> result;
for (auto& iter: this->items) {
if (!iter.second.isNull()) {
result.insert(iter.first);
}
}
return result;
}
std::map<std::string, QPDFObjectHandle> const&
QPDF_Dictionary::getAsMap() const
{
return this->items;
}
void
QPDF_Dictionary::replaceKey(std::string const& key, QPDFObjectHandle value)
{
if (value.isNull() && !value.isIndirect()) {
// The PDF spec doesn't distinguish between keys with null values and missing keys. Allow
// indirect nulls which are equivalent to a dangling reference, which is permitted by the
// spec.
removeKey(key);
} else {
// add or replace value
this->items[key] = value;
}
}
void
QPDF_Dictionary::removeKey(std::string const& key)
{
// no-op if key does not exist
this->items.erase(key);
}