2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-06-21 11:22:21 +00:00
qpdf/libqpdf/QPDF_Dictionary.cc
Jay Berkenbilt 4f24617e1e Code clean up: use range-style for loops wherever possible
Where not possible, use "auto" to get the iterator type.

Editorial note: I have avoid this change for a long time because of
not wanting to make gratuitous changes to version history, which can
obscure when certain changes were made, but with having recently
touched every single file to apply automatic code formatting and with
making several broad changes to the API, I decided it was time to take
the plunge and get rid of the older (pre-C++11) verbose iterator
syntax. The new code is just easier to read and understand, and in
many cases, it will be more effecient as fewer temporary copies are
being made.

m-holger, if you're reading, you can see that I've finally come
around. :-)
2022-04-30 13:27:18 -04:00

129 lines
2.8 KiB
C++

#include <qpdf/QPDF_Dictionary.hh>
#include <qpdf/QPDF_Name.hh>
#include <qpdf/QPDF_Null.hh>
QPDF_Dictionary::QPDF_Dictionary(
std::map<std::string, QPDFObjectHandle> const& items) :
items(items)
{
}
void
QPDF_Dictionary::releaseResolved()
{
for (auto& iter: this->items) {
QPDFObjectHandle::ReleaseResolver::releaseResolved(iter.second);
}
}
std::string
QPDF_Dictionary::unparse()
{
std::string result = "<< ";
for (auto& iter: this->items) {
if (!iter.second.isNull()) {
result += QPDF_Name::normalizeName(iter.first) + " " +
iter.second.unparse() + " ";
}
}
result += ">>";
return result;
}
JSON
QPDF_Dictionary::getJSON()
{
JSON j = JSON::makeDictionary();
for (auto& iter: this->items) {
if (!iter.second.isNull()) {
j.addDictionaryMember(
QPDF_Name::normalizeName(iter.first), iter.second.getJSON());
}
}
return j;
}
QPDFObject::object_type_e
QPDF_Dictionary::getTypeCode() const
{
return QPDFObject::ot_dictionary;
}
char const*
QPDF_Dictionary::getTypeName() const
{
return "dictionary";
}
void
QPDF_Dictionary::setDescription(QPDF* qpdf, std::string const& description)
{
this->QPDFObject::setDescription(qpdf, description);
}
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 {
QPDFObjectHandle null = QPDFObjectHandle::newNull();
QPDF* qpdf = 0;
std::string description;
if (getDescription(qpdf, description)) {
null.setObjectDescription(
qpdf, description + " -> dictionary key " + key);
}
return null;
}
}
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()) {
// The PDF spec doesn't distinguish between keys with null
// values and missing keys.
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);
}