From a3c99803954ad0f3762d986c953666c6517cad0c Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Fri, 6 May 2022 17:37:25 -0400 Subject: Add next to Pl_String and fix comments --- include/qpdf/Pl_String.hh | 18 +++++++++++++++--- libqpdf/JSON.cc | 2 +- libqpdf/Pl_String.cc | 10 ++++++++-- qpdf/test_driver.cc | 2 +- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/include/qpdf/Pl_String.hh b/include/qpdf/Pl_String.hh index a858257c..327598da 100644 --- a/include/qpdf/Pl_String.hh +++ b/include/qpdf/Pl_String.hh @@ -19,11 +19,23 @@ // continue to consider qpdf to be licensed under those terms. Please // see the manual for additional information. -// End-of-line pipeline that simply writes its data to a stdio FILE* object. - #ifndef PL_STRING_HH #define PL_STRING_HH +// This pipeline accumulates the data passed to it into a std::string, +// a reference to which is passed in at construction. Each subsequent +// use of this pipeline appends to the data accumulated so far. +// +// For this pipeline, "next" may be null. If a next pointer is +// provided, this pipeline will also pass the data through to it and +// will forward finish() to it. +// +// It is okay to not call finish() on this pipeline. This makes it +// easy to stick this in front of another pipeline to capture data +// that is written to the other pipeline without interfering with when +// finish is called on the other pipeline and without having to put a +// Pl_Concatenate after it. + #include #include @@ -32,7 +44,7 @@ class QPDF_DLL_CLASS Pl_String: public Pipeline { public: QPDF_DLL - Pl_String(char const* identifier, std::string& s); + Pl_String(char const* identifier, Pipeline* next, std::string& s); QPDF_DLL virtual ~Pl_String(); diff --git a/libqpdf/JSON.cc b/libqpdf/JSON.cc index 8549b7ed..31675a42 100644 --- a/libqpdf/JSON.cc +++ b/libqpdf/JSON.cc @@ -200,7 +200,7 @@ std::string JSON::unparse() const { std::string s; - Pl_String p("unparse", s); + Pl_String p("unparse", nullptr, s); write(&p, 0); return s; } diff --git a/libqpdf/Pl_String.cc b/libqpdf/Pl_String.cc index c9392821..d946a652 100644 --- a/libqpdf/Pl_String.cc +++ b/libqpdf/Pl_String.cc @@ -9,8 +9,8 @@ Pl_String::Members::Members(std::string& s) : { } -Pl_String::Pl_String(char const* identifier, std::string& s) : - Pipeline(identifier, 0), +Pl_String::Pl_String(char const* identifier, Pipeline* next, std::string& s) : + Pipeline(identifier, next), m(new Members(s)) { } @@ -25,9 +25,15 @@ void Pl_String::write(unsigned char const* buf, size_t len) { this->m->s.append(reinterpret_cast(buf), len); + if (getNext(true)) { + getNext()->write(buf, len); + } } void Pl_String::finish() { + if (getNext(true)) { + getNext()->finish(); + } } diff --git a/qpdf/test_driver.cc b/qpdf/test_driver.cc index 79744162..849eb4a0 100644 --- a/qpdf/test_driver.cc +++ b/qpdf/test_driver.cc @@ -437,7 +437,7 @@ test_6(QPDF& pdf, char const* arg2) throw std::logic_error("test 6 run on file with no metadata"); } std::string buf; - Pl_String bufpl("buffer", buf); + Pl_String bufpl("buffer", nullptr, buf); metadata.pipeStreamData(&bufpl, 0, qpdf_dl_none); bool cleartext = false; if (buf.substr(0, 9) == "