Commit 1496472e1c2f64f46d2d7d76481aef1aa3fff869

Authored by m-holger
1 parent da14ab4d

Add method QPDFValue::setChildDescription

libqpdf/QPDFObjectHandle.cc
... ... @@ -36,6 +36,8 @@
36 36 #include <stdexcept>
37 37 #include <stdlib.h>
38 38  
  39 +using namespace std::literals;
  40 +
39 41 namespace
40 42 {
41 43 class TerminateParsing
... ... @@ -813,13 +815,9 @@ QPDFObjectHandle::getArrayItem(int n)
813 815 typeWarning("array", "returning null");
814 816 QTC::TC("qpdf", "QPDFObjectHandle array null for non-array");
815 817 }
816   - QPDF* context = nullptr;
817   - std::string description;
818   - if (obj->getDescription(context, description)) {
819   - result.setObjectDescription(
820   - context,
821   - description + " -> null returned from invalid array access");
822   - }
  818 + static auto constexpr msg =
  819 + " -> null returned from invalid array access"sv;
  820 + result.obj->setChildDescription(obj, msg, "");
823 821 }
824 822 return result;
825 823 }
... ... @@ -1038,14 +1036,9 @@ QPDFObjectHandle::getKey(std::string const&amp; key)
1038 1036 typeWarning("dictionary", "returning null for attempted key retrieval");
1039 1037 QTC::TC("qpdf", "QPDFObjectHandle dictionary null for getKey");
1040 1038 result = newNull();
1041   - QPDF* qpdf = nullptr;
1042   - std::string description;
1043   - if (obj->getDescription(qpdf, description)) {
1044   - result.setObjectDescription(
1045   - qpdf,
1046   - (description + " -> null returned from getting key " + key +
1047   - " from non-Dictionary"));
1048   - }
  1039 + static auto constexpr msg =
  1040 + " -> null returned from getting key $VD from non-Dictionary"sv;
  1041 + result.obj->setChildDescription(obj, msg, key);
1049 1042 }
1050 1043 return result;
1051 1044 }
... ...
libqpdf/QPDFValue.cc
... ... @@ -17,6 +17,7 @@ QPDFValue::getDescription()
17 17 switch (object_description->index()) {
18 18 case 0:
19 19 {
  20 + // Simple template string
20 21 auto description = std::get<0>(*object_description);
21 22  
22 23 if (auto pos = description.find("$OG");
... ... @@ -36,12 +37,27 @@ QPDFValue::getDescription()
36 37 }
37 38 case 1:
38 39 {
  40 + // QPDF::JSONReactor generated description
39 41 auto j_descr = std::get<1>(*object_description);
40 42 return (
41 43 *j_descr.input +
42 44 (j_descr.object.empty() ? "" : ", " + j_descr.object) +
43 45 " at offset " + std::to_string(parsed_offset));
44 46 }
  47 + case 2:
  48 + {
  49 + // Child object description
  50 + auto j_descr = std::get<2>(*object_description);
  51 + std::string result;
  52 + if (auto p = j_descr.parent.lock()) {
  53 + result = p->getDescription();
  54 + }
  55 + result += j_descr.static_descr;
  56 + if (auto pos = result.find("$VD"); pos != std::string::npos) {
  57 + result.replace(pos, 3, j_descr.var_descr);
  58 + }
  59 + return result;
  60 + }
45 61 }
46 62 } else if (og.isIndirect()) {
47 63 return "object " + og.unparse(' ');
... ...
libqpdf/QPDF_Dictionary.cc
1 1 #include <qpdf/QPDF_Dictionary.hh>
2 2  
  3 +#include <qpdf/QPDFObject_private.hh>
3 4 #include <qpdf/QPDF_Name.hh>
4 5  
  6 +using namespace std::literals;
  7 +
5 8 QPDF_Dictionary::QPDF_Dictionary(
6 9 std::map<std::string, QPDFObjectHandle> const& items) :
7 10 QPDFValue(::ot_dictionary, "dictionary"),
... ... @@ -98,10 +101,8 @@ QPDF_Dictionary::getKey(std::string const&amp; key)
98 101 return item->second;
99 102 } else {
100 103 auto null = QPDFObjectHandle::newNull();
101   - if (qpdf != nullptr) {
102   - null.setObjectDescription(
103   - qpdf, getDescription() + " -> dictionary key " + key);
104   - }
  104 + static auto constexpr msg = " -> dictionary key $VD"sv;
  105 + null.getObj()->setChildDescription(shared_from_this(), msg, key);
105 106 return null;
106 107 }
107 108 }
... ...
libqpdf/qpdf/QPDFObject_private.hh
... ... @@ -12,6 +12,7 @@
12 12 #include <qpdf/Types.h>
13 13  
14 14 #include <string>
  15 +#include <string_view>
15 16  
16 17 class QPDF;
17 18 class QPDFObjectHandle;
... ... @@ -76,6 +77,25 @@ class QPDFObject
76 77 {
77 78 return value->setDescription(qpdf, description, offset);
78 79 }
  80 + void
  81 + setChildDescription(
  82 + std::shared_ptr<QPDFObject> parent,
  83 + std::string_view const& static_descr,
  84 + std::string var_descr)
  85 + {
  86 + auto qpdf = parent ? parent->value->qpdf : nullptr;
  87 + value->setChildDescription(
  88 + qpdf, parent->value, static_descr, var_descr);
  89 + }
  90 + void
  91 + setChildDescription(
  92 + std::shared_ptr<QPDFValue> parent,
  93 + std::string_view const& static_descr,
  94 + std::string var_descr)
  95 + {
  96 + auto qpdf = parent ? parent->qpdf : nullptr;
  97 + value->setChildDescription(qpdf, parent, static_descr, var_descr);
  98 + }
79 99 bool
80 100 getDescription(QPDF*& qpdf, std::string& description)
81 101 {
... ...
libqpdf/qpdf/QPDFValue.hh
... ... @@ -8,13 +8,14 @@
8 8 #include <qpdf/Types.h>
9 9  
10 10 #include <string>
  11 +#include <string_view>
11 12 #include <variant>
12 13  
13 14 class QPDF;
14 15 class QPDFObjectHandle;
15 16 class QPDFObject;
16 17  
17   -class QPDFValue
  18 +class QPDFValue: public std::enable_shared_from_this<QPDFValue>
18 19 {
19 20 friend class QPDFObject;
20 21  
... ... @@ -38,7 +39,24 @@ class QPDFValue
38 39 std::string object;
39 40 };
40 41  
41   - using Description = std::variant<std::string, JSON_Descr>;
  42 + struct ChildDescr
  43 + {
  44 + ChildDescr(
  45 + std::shared_ptr<QPDFValue> parent,
  46 + std::string_view const& static_descr,
  47 + std::string var_descr) :
  48 + parent(parent),
  49 + static_descr(static_descr),
  50 + var_descr(var_descr)
  51 + {
  52 + }
  53 +
  54 + std::weak_ptr<QPDFValue> parent;
  55 + std::string_view const& static_descr;
  56 + std::string var_descr;
  57 + };
  58 +
  59 + using Description = std::variant<std::string, JSON_Descr, ChildDescr>;
42 60  
43 61 virtual void
44 62 setDescription(
... ... @@ -56,6 +74,17 @@ class QPDFValue
56 74 qpdf = a_qpdf;
57 75 og = a_og;
58 76 }
  77 + void
  78 + setChildDescription(
  79 + QPDF* a_qpdf,
  80 + std::shared_ptr<QPDFValue> parent,
  81 + std::string_view const& static_descr,
  82 + std::string var_descr)
  83 + {
  84 + object_description = std::make_shared<Description>(
  85 + ChildDescr(parent, static_descr, var_descr));
  86 + qpdf = a_qpdf;
  87 + }
59 88 std::string getDescription();
60 89 bool
61 90 hasDescription()
... ...