2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-11-10 23:20:58 +00:00
qpdf/libqpdf/QPDF_Dictionary.cc
Jay Berkenbilt cb769c62e5 WHITESPACE ONLY -- expand tabs in source code
This comment expands all tabs using an 8-character tab-width. You
should ignore this commit when using git blame or use git blame -w.

In the early days, I used to use tabs where possible for indentation,
since emacs did this automatically. In recent years, I have switched
to only using spaces, which means qpdf source code has been a mixture
of spaces and tabs. I have avoided cleaning this up because of not
wanting gratuitous whitespaces change to cloud the output of git
blame, but I changed my mind after discussing with users who view qpdf
source code in editors/IDEs that have other tab widths by default and
in light of the fact that I am planning to start applying automatic
code formatting soon.
2022-02-08 11:51:15 -05:00

155 lines
3.3 KiB
C++

#include <qpdf/QPDF_Dictionary.hh>
#include <qpdf/QPDF_Null.hh>
#include <qpdf/QPDF_Name.hh>
QPDF_Dictionary::QPDF_Dictionary(
std::map<std::string, QPDFObjectHandle> const& items) :
items(items)
{
}
QPDF_Dictionary::~QPDF_Dictionary()
{
}
void
QPDF_Dictionary::releaseResolved()
{
for (std::map<std::string, QPDFObjectHandle>::iterator iter =
this->items.begin();
iter != this->items.end(); ++iter)
{
QPDFObjectHandle::ReleaseResolver::releaseResolved((*iter).second);
}
}
std::string
QPDF_Dictionary::unparse()
{
std::string result = "<< ";
for (std::map<std::string, QPDFObjectHandle>::iterator iter =
this->items.begin();
iter != this->items.end(); ++iter)
{
result += QPDF_Name::normalizeName((*iter).first) +
" " + (*iter).second.unparse() + " ";
}
result += ">>";
return result;
}
JSON
QPDF_Dictionary::getJSON()
{
JSON j = JSON::makeDictionary();
for (std::map<std::string, QPDFObjectHandle>::iterator iter =
this->items.begin();
iter != this->items.end(); ++iter)
{
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.
if (this->items.count(key))
{
// May be a null object
return (*(this->items.find(key))).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 (std::map<std::string, QPDFObjectHandle>::const_iterator iter =
this->items.begin();
iter != this->items.end(); ++iter)
{
if (hasKey((*iter).first))
{
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)
{
// 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);
}
void
QPDF_Dictionary::replaceOrRemoveKey(std::string const& key,
QPDFObjectHandle value)
{
if (value.isNull())
{
removeKey(key);
}
else
{
replaceKey(key, value);
}
}