2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-05-29 00:10:54 +00:00

Change type of QPDFValue::object_description to std::shared_ptr<std::variant>

Also, name the type QPDFValue::Description.
This commit is contained in:
m-holger 2023-02-15 10:11:38 +00:00
parent d58ec90310
commit 32907fc14c
7 changed files with 44 additions and 22 deletions

View File

@ -2176,7 +2176,8 @@ QPDFObjectHandle::setObjectDescription(
// This is called during parsing on newly created direct objects,
// so we can't call dereference() here.
if (isInitialized() && obj.get()) {
auto descr = std::make_shared<std::string>(object_description);
auto descr =
std::make_shared<QPDFValue::Description>(object_description);
obj->setDescription(owning_qpdf, descr);
}
}

View File

@ -13,16 +13,28 @@ QPDFValue::do_create(QPDFValue* object)
std::string
QPDFValue::getDescription()
{
auto description = object_description ? *object_description : "";
if (auto pos = description.find("$OG"); pos != std::string::npos) {
description.replace(pos, 3, og.unparse(' '));
}
if (auto pos = description.find("$PO"); pos != std::string::npos) {
qpdf_offset_t shift = (type_code == ::ot_dictionary) ? 2
: (type_code == ::ot_array) ? 1
: 0;
if (object_description) {
switch (object_description->index()) {
case 0:
{
auto description = std::get<0>(*object_description);
description.replace(pos, 3, std::to_string(parsed_offset + shift));
if (auto pos = description.find("$OG");
pos != std::string::npos) {
description.replace(pos, 3, og.unparse(' '));
}
if (auto pos = description.find("$PO");
pos != std::string::npos) {
qpdf_offset_t shift = (type_code == ::ot_dictionary) ? 2
: (type_code == ::ot_array) ? 1
: 0;
description.replace(
pos, 3, std::to_string(parsed_offset + shift));
}
return description;
}
}
}
return description;
return {};
}

View File

@ -123,7 +123,7 @@ QPDF_Stream::QPDF_Stream(
throw std::logic_error("stream object instantiated with non-dictionary "
"object for dictionary");
}
auto descr = std::make_shared<std::string>(
auto descr = std::make_shared<QPDFValue::Description>(
qpdf->getFilename() + ", stream object " + og.unparse(' '));
setDescription(qpdf, descr, offset);
}
@ -283,7 +283,9 @@ QPDF_Stream::getStreamJSON(
void
QPDF_Stream::setDescription(
QPDF* qpdf, std::shared_ptr<std::string>& description, qpdf_offset_t offset)
QPDF* qpdf,
std::shared_ptr<QPDFValue::Description>& description,
qpdf_offset_t offset)
{
this->QPDFValue::setDescription(qpdf, description, offset);
setDictDescription();

View File

@ -71,7 +71,7 @@ class QPDFObject
void
setDescription(
QPDF* qpdf,
std::shared_ptr<std::string>& description,
std::shared_ptr<QPDFValue::Description>& description,
qpdf_offset_t offset = -1)
{
return value->setDescription(qpdf, description, offset);

View File

@ -2,6 +2,7 @@
#define QPDFPARSER_HH
#include <qpdf/QPDFObjectHandle.hh>
#include <qpdf/QPDFValue.hh>
#include <memory>
#include <string>
@ -21,8 +22,8 @@ class QPDFParser
tokenizer(tokenizer),
decrypter(decrypter),
context(context),
description(std::make_shared<std::string>(
input->getName() + ", " + object_description + " at offset $PO"))
description(std::make_shared<QPDFValue::Description>(std::string(
input->getName() + ", " + object_description + " at offset $PO")))
{
}
virtual ~QPDFParser() = default;
@ -49,7 +50,7 @@ class QPDFParser
QPDFTokenizer& tokenizer;
QPDFObjectHandle::StringDecrypter* decrypter;
QPDF* context;
std::shared_ptr<std::string> description;
std::shared_ptr<QPDFValue::Description> description;
};
#endif // QPDFPARSER_HH

View File

@ -8,6 +8,7 @@
#include <qpdf/Types.h>
#include <string>
#include <variant>
class QPDF;
class QPDFObjectHandle;
@ -23,10 +24,13 @@ class QPDFValue
virtual std::shared_ptr<QPDFObject> copy(bool shallow = false) = 0;
virtual std::string unparse() = 0;
virtual JSON getJSON(int json_version) = 0;
using Description = std::variant<std::string>;
virtual void
setDescription(
QPDF* qpdf_p,
std::shared_ptr<std::string>& description,
std::shared_ptr<Description>& description,
qpdf_offset_t offset)
{
qpdf = qpdf_p;
@ -37,7 +41,7 @@ class QPDFValue
setDefaultDescription(QPDF* a_qpdf, QPDFObjGen const& a_og)
{
static auto default_description{
std::make_shared<std::string>("object $OG")};
std::make_shared<Description>("object $OG")};
if (!object_description) {
object_description = default_description;
}
@ -49,7 +53,7 @@ class QPDFValue
hasDescription()
{
return qpdf != nullptr && object_description &&
!object_description->empty();
!getDescription().empty();
}
void
setParsedOffset(qpdf_offset_t offset)
@ -108,7 +112,7 @@ class QPDFValue
private:
QPDFValue(QPDFValue const&) = delete;
QPDFValue& operator=(QPDFValue const&) = delete;
std::shared_ptr<std::string> object_description;
std::shared_ptr<Description> object_description;
const qpdf_object_type_e type_code{::ot_uninitialized};
char const* type_name{"uninitialized"};

View File

@ -27,7 +27,9 @@ class QPDF_Stream: public QPDFValue
virtual std::string unparse();
virtual JSON getJSON(int json_version);
virtual void setDescription(
QPDF*, std::shared_ptr<std::string>& description, qpdf_offset_t offset);
QPDF*,
std::shared_ptr<QPDFValue::Description>& description,
qpdf_offset_t offset);
virtual void disconnect();
QPDFObjectHandle getDict() const;
bool isDataModified() const;