aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorm-holger <m-holger@kubitscheck.org>2023-02-15 11:11:38 +0100
committerm-holger <m-holger@kubitscheck.org>2023-02-18 09:33:08 +0100
commit32907fc14c663e95df0f7c62905b82389c0024a2 (patch)
tree9e32a7905a436453d5d1920446006d2d4bea2426
parentd58ec90310c93653ab42bcb7373a38e3f8d43362 (diff)
downloadqpdf-32907fc14c663e95df0f7c62905b82389c0024a2.tar.zst
Change type of QPDFValue::object_description to std::shared_ptr<std::variant>
Also, name the type QPDFValue::Description.
-rw-r--r--libqpdf/QPDFObjectHandle.cc3
-rw-r--r--libqpdf/QPDFValue.cc32
-rw-r--r--libqpdf/QPDF_Stream.cc6
-rw-r--r--libqpdf/qpdf/QPDFObject_private.hh2
-rw-r--r--libqpdf/qpdf/QPDFParser.hh7
-rw-r--r--libqpdf/qpdf/QPDFValue.hh12
-rw-r--r--libqpdf/qpdf/QPDF_Stream.hh4
7 files changed, 44 insertions, 22 deletions
diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc
index 2b2ca5db..7a37ffc2 100644
--- a/libqpdf/QPDFObjectHandle.cc
+++ b/libqpdf/QPDFObjectHandle.cc
@@ -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);
}
}
diff --git a/libqpdf/QPDFValue.cc b/libqpdf/QPDFValue.cc
index 19679df2..7c5b30a6 100644
--- a/libqpdf/QPDFValue.cc
+++ b/libqpdf/QPDFValue.cc
@@ -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);
+
+ 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));
+ description.replace(
+ pos, 3, std::to_string(parsed_offset + shift));
+ }
+ return description;
+ }
+ }
}
- return description;
+ return {};
}
diff --git a/libqpdf/QPDF_Stream.cc b/libqpdf/QPDF_Stream.cc
index 66916c21..8e2e16c5 100644
--- a/libqpdf/QPDF_Stream.cc
+++ b/libqpdf/QPDF_Stream.cc
@@ -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();
diff --git a/libqpdf/qpdf/QPDFObject_private.hh b/libqpdf/qpdf/QPDFObject_private.hh
index 4d388503..612949da 100644
--- a/libqpdf/qpdf/QPDFObject_private.hh
+++ b/libqpdf/qpdf/QPDFObject_private.hh
@@ -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);
diff --git a/libqpdf/qpdf/QPDFParser.hh b/libqpdf/qpdf/QPDFParser.hh
index 65279700..c2bf6bbe 100644
--- a/libqpdf/qpdf/QPDFParser.hh
+++ b/libqpdf/qpdf/QPDFParser.hh
@@ -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
diff --git a/libqpdf/qpdf/QPDFValue.hh b/libqpdf/qpdf/QPDFValue.hh
index b9c3cdb1..eef088f9 100644
--- a/libqpdf/qpdf/QPDFValue.hh
+++ b/libqpdf/qpdf/QPDFValue.hh
@@ -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"};
diff --git a/libqpdf/qpdf/QPDF_Stream.hh b/libqpdf/qpdf/QPDF_Stream.hh
index c0694c60..cf0a2288 100644
--- a/libqpdf/qpdf/QPDF_Stream.hh
+++ b/libqpdf/qpdf/QPDF_Stream.hh
@@ -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;