2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-06-16 17:02:22 +00:00

Add method QPDFValue::setChildDescription

This commit is contained in:
m-holger 2023-02-17 13:58:21 +00:00
parent da14ab4dc7
commit 1496472e1c
5 changed files with 80 additions and 21 deletions

View File

@ -36,6 +36,8 @@
#include <stdexcept> #include <stdexcept>
#include <stdlib.h> #include <stdlib.h>
using namespace std::literals;
namespace namespace
{ {
class TerminateParsing class TerminateParsing
@ -813,13 +815,9 @@ QPDFObjectHandle::getArrayItem(int n)
typeWarning("array", "returning null"); typeWarning("array", "returning null");
QTC::TC("qpdf", "QPDFObjectHandle array null for non-array"); QTC::TC("qpdf", "QPDFObjectHandle array null for non-array");
} }
QPDF* context = nullptr; static auto constexpr msg =
std::string description; " -> null returned from invalid array access"sv;
if (obj->getDescription(context, description)) { result.obj->setChildDescription(obj, msg, "");
result.setObjectDescription(
context,
description + " -> null returned from invalid array access");
}
} }
return result; return result;
} }
@ -1038,14 +1036,9 @@ QPDFObjectHandle::getKey(std::string const& key)
typeWarning("dictionary", "returning null for attempted key retrieval"); typeWarning("dictionary", "returning null for attempted key retrieval");
QTC::TC("qpdf", "QPDFObjectHandle dictionary null for getKey"); QTC::TC("qpdf", "QPDFObjectHandle dictionary null for getKey");
result = newNull(); result = newNull();
QPDF* qpdf = nullptr; static auto constexpr msg =
std::string description; " -> null returned from getting key $VD from non-Dictionary"sv;
if (obj->getDescription(qpdf, description)) { result.obj->setChildDescription(obj, msg, key);
result.setObjectDescription(
qpdf,
(description + " -> null returned from getting key " + key +
" from non-Dictionary"));
}
} }
return result; return result;
} }

View File

@ -17,6 +17,7 @@ QPDFValue::getDescription()
switch (object_description->index()) { switch (object_description->index()) {
case 0: case 0:
{ {
// Simple template string
auto description = std::get<0>(*object_description); auto description = std::get<0>(*object_description);
if (auto pos = description.find("$OG"); if (auto pos = description.find("$OG");
@ -36,12 +37,27 @@ QPDFValue::getDescription()
} }
case 1: case 1:
{ {
// QPDF::JSONReactor generated description
auto j_descr = std::get<1>(*object_description); auto j_descr = std::get<1>(*object_description);
return ( return (
*j_descr.input + *j_descr.input +
(j_descr.object.empty() ? "" : ", " + j_descr.object) + (j_descr.object.empty() ? "" : ", " + j_descr.object) +
" at offset " + std::to_string(parsed_offset)); " at offset " + std::to_string(parsed_offset));
} }
case 2:
{
// Child object description
auto j_descr = std::get<2>(*object_description);
std::string result;
if (auto p = j_descr.parent.lock()) {
result = p->getDescription();
}
result += j_descr.static_descr;
if (auto pos = result.find("$VD"); pos != std::string::npos) {
result.replace(pos, 3, j_descr.var_descr);
}
return result;
}
} }
} else if (og.isIndirect()) { } else if (og.isIndirect()) {
return "object " + og.unparse(' '); return "object " + og.unparse(' ');

View File

@ -1,7 +1,10 @@
#include <qpdf/QPDF_Dictionary.hh> #include <qpdf/QPDF_Dictionary.hh>
#include <qpdf/QPDFObject_private.hh>
#include <qpdf/QPDF_Name.hh> #include <qpdf/QPDF_Name.hh>
using namespace std::literals;
QPDF_Dictionary::QPDF_Dictionary( QPDF_Dictionary::QPDF_Dictionary(
std::map<std::string, QPDFObjectHandle> const& items) : std::map<std::string, QPDFObjectHandle> const& items) :
QPDFValue(::ot_dictionary, "dictionary"), QPDFValue(::ot_dictionary, "dictionary"),
@ -98,10 +101,8 @@ QPDF_Dictionary::getKey(std::string const& key)
return item->second; return item->second;
} else { } else {
auto null = QPDFObjectHandle::newNull(); auto null = QPDFObjectHandle::newNull();
if (qpdf != nullptr) { static auto constexpr msg = " -> dictionary key $VD"sv;
null.setObjectDescription( null.getObj()->setChildDescription(shared_from_this(), msg, key);
qpdf, getDescription() + " -> dictionary key " + key);
}
return null; return null;
} }
} }

View File

@ -12,6 +12,7 @@
#include <qpdf/Types.h> #include <qpdf/Types.h>
#include <string> #include <string>
#include <string_view>
class QPDF; class QPDF;
class QPDFObjectHandle; class QPDFObjectHandle;
@ -76,6 +77,25 @@ class QPDFObject
{ {
return value->setDescription(qpdf, description, offset); return value->setDescription(qpdf, description, offset);
} }
void
setChildDescription(
std::shared_ptr<QPDFObject> parent,
std::string_view const& static_descr,
std::string var_descr)
{
auto qpdf = parent ? parent->value->qpdf : nullptr;
value->setChildDescription(
qpdf, parent->value, static_descr, var_descr);
}
void
setChildDescription(
std::shared_ptr<QPDFValue> parent,
std::string_view const& static_descr,
std::string var_descr)
{
auto qpdf = parent ? parent->qpdf : nullptr;
value->setChildDescription(qpdf, parent, static_descr, var_descr);
}
bool bool
getDescription(QPDF*& qpdf, std::string& description) getDescription(QPDF*& qpdf, std::string& description)
{ {

View File

@ -8,13 +8,14 @@
#include <qpdf/Types.h> #include <qpdf/Types.h>
#include <string> #include <string>
#include <string_view>
#include <variant> #include <variant>
class QPDF; class QPDF;
class QPDFObjectHandle; class QPDFObjectHandle;
class QPDFObject; class QPDFObject;
class QPDFValue class QPDFValue: public std::enable_shared_from_this<QPDFValue>
{ {
friend class QPDFObject; friend class QPDFObject;
@ -38,7 +39,24 @@ class QPDFValue
std::string object; std::string object;
}; };
using Description = std::variant<std::string, JSON_Descr>; struct ChildDescr
{
ChildDescr(
std::shared_ptr<QPDFValue> parent,
std::string_view const& static_descr,
std::string var_descr) :
parent(parent),
static_descr(static_descr),
var_descr(var_descr)
{
}
std::weak_ptr<QPDFValue> parent;
std::string_view const& static_descr;
std::string var_descr;
};
using Description = std::variant<std::string, JSON_Descr, ChildDescr>;
virtual void virtual void
setDescription( setDescription(
@ -56,6 +74,17 @@ class QPDFValue
qpdf = a_qpdf; qpdf = a_qpdf;
og = a_og; og = a_og;
} }
void
setChildDescription(
QPDF* a_qpdf,
std::shared_ptr<QPDFValue> parent,
std::string_view const& static_descr,
std::string var_descr)
{
object_description = std::make_shared<Description>(
ChildDescr(parent, static_descr, var_descr));
qpdf = a_qpdf;
}
std::string getDescription(); std::string getDescription();
bool bool
hasDescription() hasDescription()