Commit dab27c9bb35c26e30e22f2e53299ee9566cadefd

Authored by m-holger
1 parent fe74f28d

Refactor setting of object descriptions in QPDF::JSONReactor

include/qpdf/QPDF.hh
@@ -32,6 +32,7 @@ @@ -32,6 +32,7 @@
32 #include <memory> 32 #include <memory>
33 #include <stdio.h> 33 #include <stdio.h>
34 #include <string> 34 #include <string>
  35 +#include <variant>
35 #include <vector> 36 #include <vector>
36 37
37 #include <qpdf/Buffer.hh> 38 #include <qpdf/Buffer.hh>
@@ -50,6 +51,7 @@ class BitStream; @@ -50,6 +51,7 @@ class BitStream;
50 class BitWriter; 51 class BitWriter;
51 class QPDFLogger; 52 class QPDFLogger;
52 class QPDFParser; 53 class QPDFParser;
  54 +struct JSON_Descr;
53 55
54 class QPDF 56 class QPDF
55 { 57 {
@@ -1152,6 +1154,7 @@ class QPDF @@ -1152,6 +1154,7 @@ class QPDF
1152 QPDF& pdf; 1154 QPDF& pdf;
1153 std::shared_ptr<InputSource> is; 1155 std::shared_ptr<InputSource> is;
1154 bool must_be_complete; 1156 bool must_be_complete;
  1157 + std::shared_ptr<std::variant<std::string, JSON_Descr>> descr;
1155 bool errors; 1158 bool errors;
1156 bool parse_error; 1159 bool parse_error;
1157 bool saw_qpdf; 1160 bool saw_qpdf;
libqpdf/QPDFValue.cc
@@ -34,6 +34,14 @@ QPDFValue::getDescription() @@ -34,6 +34,14 @@ QPDFValue::getDescription()
34 } 34 }
35 return description; 35 return description;
36 } 36 }
  37 + case 1:
  38 + {
  39 + auto j_descr = std::get<1>(*object_description);
  40 + return (
  41 + *j_descr.input +
  42 + (j_descr.object.empty() ? "" : ", " + j_descr.object) +
  43 + " at offset " + std::to_string(parsed_offset));
  44 + }
37 } 45 }
38 } else if (og.isIndirect()) { 46 } else if (og.isIndirect()) {
39 return "object " + og.unparse(' '); 47 return "object " + og.unparse(' ');
libqpdf/QPDF_json.cc
@@ -4,6 +4,8 @@ @@ -4,6 +4,8 @@
4 #include <qpdf/Pl_Base64.hh> 4 #include <qpdf/Pl_Base64.hh>
5 #include <qpdf/Pl_StdioFile.hh> 5 #include <qpdf/Pl_StdioFile.hh>
6 #include <qpdf/QIntC.hh> 6 #include <qpdf/QIntC.hh>
  7 +#include <qpdf/QPDFObject_private.hh>
  8 +#include <qpdf/QPDFValue.hh>
7 #include <qpdf/QTC.hh> 9 #include <qpdf/QTC.hh>
8 #include <qpdf/QUtil.hh> 10 #include <qpdf/QUtil.hh>
9 #include <algorithm> 11 #include <algorithm>
@@ -226,6 +228,8 @@ QPDF::JSONReactor::JSONReactor( @@ -226,6 +228,8 @@ QPDF::JSONReactor::JSONReactor(
226 pdf(pdf), 228 pdf(pdf),
227 is(is), 229 is(is),
228 must_be_complete(must_be_complete), 230 must_be_complete(must_be_complete),
  231 + descr(std::make_shared<std::variant<std::string, JSON_Descr>>(
  232 + JSON_Descr(std::make_shared<std::string>(is->getName()), ""))),
229 errors(false), 233 errors(false),
230 parse_error(false), 234 parse_error(false),
231 saw_qpdf(false), 235 saw_qpdf(false),
@@ -675,12 +679,13 @@ QPDF::JSONReactor::arrayItem(JSON const&amp; value) @@ -675,12 +679,13 @@ QPDF::JSONReactor::arrayItem(JSON const&amp; value)
675 void 679 void
676 QPDF::JSONReactor::setObjectDescription(QPDFObjectHandle& oh, JSON const& value) 680 QPDF::JSONReactor::setObjectDescription(QPDFObjectHandle& oh, JSON const& value)
677 { 681 {
678 - std::string description = this->is->getName();  
679 - if (!this->cur_object.empty()) {  
680 - description += ", " + this->cur_object; 682 + auto j_descr = std::get<JSON_Descr>(*descr);
  683 + if (j_descr.object != cur_object) {
  684 + descr = std::make_shared<QPDFValue::Description>(
  685 + JSON_Descr(j_descr.input, cur_object));
681 } 686 }
682 - description += " at offset " + std::to_string(value.getStart());  
683 - oh.setObjectDescription(&this->pdf, description); 687 +
  688 + oh.getObjectPtr()->setDescription(&pdf, descr, value.getStart());
684 } 689 }
685 690
686 QPDFObjectHandle 691 QPDFObjectHandle
libqpdf/qpdf/QPDFValue.hh
@@ -14,6 +14,18 @@ class QPDF; @@ -14,6 +14,18 @@ class QPDF;
14 class QPDFObjectHandle; 14 class QPDFObjectHandle;
15 class QPDFObject; 15 class QPDFObject;
16 16
  17 +struct JSON_Descr
  18 +{
  19 + JSON_Descr(std::shared_ptr<std::string> input, std::string const& object) :
  20 + input(input),
  21 + object(object)
  22 + {
  23 + }
  24 +
  25 + std::shared_ptr<std::string> input;
  26 + std::string object;
  27 +};
  28 +
17 class QPDFValue 29 class QPDFValue
18 { 30 {
19 friend class QPDFObject; 31 friend class QPDFObject;
@@ -25,7 +37,7 @@ class QPDFValue @@ -25,7 +37,7 @@ class QPDFValue
25 virtual std::string unparse() = 0; 37 virtual std::string unparse() = 0;
26 virtual JSON getJSON(int json_version) = 0; 38 virtual JSON getJSON(int json_version) = 0;
27 39
28 - using Description = std::variant<std::string>; 40 + using Description = std::variant<std::string, JSON_Descr>;
29 41
30 virtual void 42 virtual void
31 setDescription( 43 setDescription(