From f4206a0938318984f2e7ca8709154598addcfa64 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Tue, 3 May 2022 18:54:44 -0400 Subject: [PATCH] Add new Pl_String Pipeline --- ChangeLog | 3 ++ TODO | 3 -- include/qpdf/Pl_String.hh | 63 +++++++++++++++++++++++++++++++++++++++ libqpdf/CMakeLists.txt | 1 + libqpdf/Pl_String.cc | 33 ++++++++++++++++++++ manual/release-notes.rst | 3 ++ qpdf/sizes.cc | 2 ++ qpdf/test_driver.cc | 10 +++---- 8 files changed, 109 insertions(+), 9 deletions(-) create mode 100644 include/qpdf/Pl_String.hh create mode 100644 libqpdf/Pl_String.cc diff --git a/ChangeLog b/ChangeLog index 38d5aaa1..dfcadf49 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2022-05-03 Jay Berkenbilt + * Add new Pipeline class Pl_String which appends to a std::string& + passed to it at construction. + * Add new Pipeline class Pl_OStream, similar to Pl_StdioFile but takes a std::ostream instead of a FILE*. diff --git a/TODO b/TODO index 98f3dbd6..ec110f34 100644 --- a/TODO +++ b/TODO @@ -46,9 +46,6 @@ Output JSON v2 ---- notes from 5/2: -Need new pipelines: -* Pl_String to std::string with semantics like Pl_Buffer - See if I can change all output and error messages issued by the library, when context is available, to have a pipeline rather than a FILE* or std::ostream. This makes it possible for people to capture diff --git a/include/qpdf/Pl_String.hh b/include/qpdf/Pl_String.hh new file mode 100644 index 00000000..a858257c --- /dev/null +++ b/include/qpdf/Pl_String.hh @@ -0,0 +1,63 @@ +// Copyright (c) 2005-2022 Jay Berkenbilt +// +// This file is part of qpdf. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Versions of qpdf prior to version 7 were released under the terms +// of version 2.0 of the Artistic License. At your option, you may +// 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 + +#include + +#include + +class QPDF_DLL_CLASS Pl_String: public Pipeline +{ + public: + QPDF_DLL + Pl_String(char const* identifier, std::string& s); + QPDF_DLL + virtual ~Pl_String(); + + QPDF_DLL + virtual void write(unsigned char const* buf, size_t len); + QPDF_DLL + virtual void finish(); + + private: + class QPDF_DLL_PRIVATE Members + { + friend class Pl_String; + + public: + QPDF_DLL + ~Members() = default; + + private: + Members(std::string&); + Members(Members const&) = delete; + + std::string& s; + }; + + std::shared_ptr m; +}; + +#endif // PL_STRING_HH diff --git a/libqpdf/CMakeLists.txt b/libqpdf/CMakeLists.txt index 2d5685b4..aa7b1fd0 100644 --- a/libqpdf/CMakeLists.txt +++ b/libqpdf/CMakeLists.txt @@ -51,6 +51,7 @@ set(libqpdf_SOURCES Pl_RunLength.cc Pl_SHA2.cc Pl_StdioFile.cc + Pl_String.cc Pl_TIFFPredictor.cc QPDF.cc QPDFAcroFormDocumentHelper.cc diff --git a/libqpdf/Pl_String.cc b/libqpdf/Pl_String.cc new file mode 100644 index 00000000..c9392821 --- /dev/null +++ b/libqpdf/Pl_String.cc @@ -0,0 +1,33 @@ +#include + +#include +#include +#include + +Pl_String::Members::Members(std::string& s) : + s(s) +{ +} + +Pl_String::Pl_String(char const* identifier, std::string& s) : + Pipeline(identifier, 0), + m(new Members(s)) +{ +} + +Pl_String::~Pl_String() +{ + // Must be explicit and not inline -- see QPDF_DLL_CLASS in + // README-maintainer +} + +void +Pl_String::write(unsigned char const* buf, size_t len) +{ + this->m->s.append(reinterpret_cast(buf), len); +} + +void +Pl_String::finish() +{ +} diff --git a/manual/release-notes.rst b/manual/release-notes.rst index fdbd21b7..08e2fd52 100644 --- a/manual/release-notes.rst +++ b/manual/release-notes.rst @@ -120,6 +120,9 @@ For a detailed list of changes, please see the file - Add new ``Pipeline`` type ``Pl_OStream`` to write to a ``std::ostream``. + - Add new ``Pipeline`` type ``Pl_String`` to append to a + ``std::string``. + - Other changes - A new chapter on contributing to qpdf has been added to the diff --git a/qpdf/sizes.cc b/qpdf/sizes.cc index dc5715ab..ac4bae6e 100644 --- a/qpdf/sizes.cc +++ b/qpdf/sizes.cc @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -80,6 +81,7 @@ main() print_size(Pl_QPDFTokenizer); print_size(Pl_RunLength); print_size(Pl_StdioFile); + print_size(Pl_String); print_size(QPDF); print_size(QPDFAcroFormDocumentHelper); print_size(QPDFAnnotationObjectHelper); diff --git a/qpdf/test_driver.cc b/qpdf/test_driver.cc index ed603c3b..79744162 100644 --- a/qpdf/test_driver.cc +++ b/qpdf/test_driver.cc @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -435,16 +436,13 @@ test_6(QPDF& pdf, char const* arg2) if (!metadata.isStream()) { throw std::logic_error("test 6 run on file with no metadata"); } - Pl_Buffer bufpl("buffer"); + std::string buf; + Pl_String bufpl("buffer", buf); metadata.pipeStreamData(&bufpl, 0, qpdf_dl_none); - Buffer* buf = bufpl.getBuffer(); - unsigned char const* data = buf->getBuffer(); bool cleartext = false; - if ((buf->getSize() > 9) && - (strncmp(reinterpret_cast(data), "