2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-11-18 10:25:12 +00:00
qpdf/libqpdf/QPDF_Name.cc
Jay Berkenbilt 6c61be00e8 Rename QPDFObject -> QPDFValueProxy
This is in preparation for restoring a QPDFObject.hh to ease the
transition on qpdf_object_type_e.

This commit was created by
* Renaming QPDFObject.cc and QPDFObject.hh
* Replacing QPDFObject\b with QPDFValueProxy (where \b is word
  boundary)
* Running format-code
* Manually resorting files in libqpdf/CMakeLists.txt
* Manually refilling the comment in QPDF.hh near class Resolver
2022-09-05 18:52:59 -04:00

70 lines
1.4 KiB
C++

#include <qpdf/QPDF_Name.hh>
#include <qpdf/QUtil.hh>
#include <stdio.h>
#include <string.h>
QPDF_Name::QPDF_Name(std::string const& name) :
QPDFValue(::ot_name, "name"),
name(name)
{
}
std::shared_ptr<QPDFValueProxy>
QPDF_Name::create(std::string const& name)
{
return do_create(new QPDF_Name(name));
}
std::shared_ptr<QPDFValueProxy>
QPDF_Name::shallowCopy()
{
return create(name);
}
std::string
QPDF_Name::normalizeName(std::string const& name)
{
if (name.empty()) {
return name;
}
std::string result;
result += name.at(0);
for (size_t i = 1; i < name.length(); ++i) {
char ch = name.at(i);
// Don't use locale/ctype here; follow PDF spec guidelines.
if (ch == '\0') {
// QPDFTokenizer embeds a null character to encode an
// invalid #.
result += "#";
} else if (strchr("#()<>[]{}/%", ch) || (ch < 33) || (ch > 126)) {
result += "#" + QUtil::hex_encode(std::string(&ch, 1));
} else {
result += ch;
}
}
return result;
}
std::string
QPDF_Name::unparse()
{
return normalizeName(this->name);
}
JSON
QPDF_Name::getJSON(int json_version)
{
if (json_version == 1) {
return JSON::makeString(normalizeName(this->name));
} else {
return JSON::makeString(this->name);
}
}
std::string
QPDF_Name::getName() const
{
return this->name;
}