Commit f4206a0938318984f2e7ca8709154598addcfa64

Authored by Jay Berkenbilt
1 parent 16139d97

Add new Pl_String Pipeline

ChangeLog
1 2022-05-03 Jay Berkenbilt <ejb@ql.org> 1 2022-05-03 Jay Berkenbilt <ejb@ql.org>
2 2
  3 + * Add new Pipeline class Pl_String which appends to a std::string&
  4 + passed to it at construction.
  5 +
3 * Add new Pipeline class Pl_OStream, similar to Pl_StdioFile but 6 * Add new Pipeline class Pl_OStream, similar to Pl_StdioFile but
4 takes a std::ostream instead of a FILE*. 7 takes a std::ostream instead of a FILE*.
5 8
@@ -46,9 +46,6 @@ Output JSON v2 @@ -46,9 +46,6 @@ Output JSON v2
46 ---- 46 ----
47 notes from 5/2: 47 notes from 5/2:
48 48
49 -Need new pipelines:  
50 -* Pl_String to std::string with semantics like Pl_Buffer  
51 -  
52 See if I can change all output and error messages issued by the 49 See if I can change all output and error messages issued by the
53 library, when context is available, to have a pipeline rather than a 50 library, when context is available, to have a pipeline rather than a
54 FILE* or std::ostream. This makes it possible for people to capture 51 FILE* or std::ostream. This makes it possible for people to capture
include/qpdf/Pl_String.hh 0 → 100644
  1 +// Copyright (c) 2005-2022 Jay Berkenbilt
  2 +//
  3 +// This file is part of qpdf.
  4 +//
  5 +// Licensed under the Apache License, Version 2.0 (the "License");
  6 +// you may not use this file except in compliance with the License.
  7 +// You may obtain a copy of the License at
  8 +//
  9 +// http://www.apache.org/licenses/LICENSE-2.0
  10 +//
  11 +// Unless required by applicable law or agreed to in writing, software
  12 +// distributed under the License is distributed on an "AS IS" BASIS,
  13 +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14 +// See the License for the specific language governing permissions and
  15 +// limitations under the License.
  16 +//
  17 +// Versions of qpdf prior to version 7 were released under the terms
  18 +// of version 2.0 of the Artistic License. At your option, you may
  19 +// continue to consider qpdf to be licensed under those terms. Please
  20 +// see the manual for additional information.
  21 +
  22 +// End-of-line pipeline that simply writes its data to a stdio FILE* object.
  23 +
  24 +#ifndef PL_STRING_HH
  25 +#define PL_STRING_HH
  26 +
  27 +#include <qpdf/Pipeline.hh>
  28 +
  29 +#include <string>
  30 +
  31 +class QPDF_DLL_CLASS Pl_String: public Pipeline
  32 +{
  33 + public:
  34 + QPDF_DLL
  35 + Pl_String(char const* identifier, std::string& s);
  36 + QPDF_DLL
  37 + virtual ~Pl_String();
  38 +
  39 + QPDF_DLL
  40 + virtual void write(unsigned char const* buf, size_t len);
  41 + QPDF_DLL
  42 + virtual void finish();
  43 +
  44 + private:
  45 + class QPDF_DLL_PRIVATE Members
  46 + {
  47 + friend class Pl_String;
  48 +
  49 + public:
  50 + QPDF_DLL
  51 + ~Members() = default;
  52 +
  53 + private:
  54 + Members(std::string&);
  55 + Members(Members const&) = delete;
  56 +
  57 + std::string& s;
  58 + };
  59 +
  60 + std::shared_ptr<Members> m;
  61 +};
  62 +
  63 +#endif // PL_STRING_HH
libqpdf/CMakeLists.txt
@@ -51,6 +51,7 @@ set(libqpdf_SOURCES @@ -51,6 +51,7 @@ set(libqpdf_SOURCES
51 Pl_RunLength.cc 51 Pl_RunLength.cc
52 Pl_SHA2.cc 52 Pl_SHA2.cc
53 Pl_StdioFile.cc 53 Pl_StdioFile.cc
  54 + Pl_String.cc
54 Pl_TIFFPredictor.cc 55 Pl_TIFFPredictor.cc
55 QPDF.cc 56 QPDF.cc
56 QPDFAcroFormDocumentHelper.cc 57 QPDFAcroFormDocumentHelper.cc
libqpdf/Pl_String.cc 0 → 100644
  1 +#include <qpdf/Pl_String.hh>
  2 +
  3 +#include <qpdf/QUtil.hh>
  4 +#include <errno.h>
  5 +#include <stdexcept>
  6 +
  7 +Pl_String::Members::Members(std::string& s) :
  8 + s(s)
  9 +{
  10 +}
  11 +
  12 +Pl_String::Pl_String(char const* identifier, std::string& s) :
  13 + Pipeline(identifier, 0),
  14 + m(new Members(s))
  15 +{
  16 +}
  17 +
  18 +Pl_String::~Pl_String()
  19 +{
  20 + // Must be explicit and not inline -- see QPDF_DLL_CLASS in
  21 + // README-maintainer
  22 +}
  23 +
  24 +void
  25 +Pl_String::write(unsigned char const* buf, size_t len)
  26 +{
  27 + this->m->s.append(reinterpret_cast<char const*>(buf), len);
  28 +}
  29 +
  30 +void
  31 +Pl_String::finish()
  32 +{
  33 +}
manual/release-notes.rst
@@ -120,6 +120,9 @@ For a detailed list of changes, please see the file @@ -120,6 +120,9 @@ For a detailed list of changes, please see the file
120 - Add new ``Pipeline`` type ``Pl_OStream`` to write to a 120 - Add new ``Pipeline`` type ``Pl_OStream`` to write to a
121 ``std::ostream``. 121 ``std::ostream``.
122 122
  123 + - Add new ``Pipeline`` type ``Pl_String`` to append to a
  124 + ``std::string``.
  125 +
123 - Other changes 126 - Other changes
124 127
125 - A new chapter on contributing to qpdf has been added to the 128 - A new chapter on contributing to qpdf has been added to the
qpdf/sizes.cc
@@ -20,6 +20,7 @@ @@ -20,6 +20,7 @@
20 #include <qpdf/Pl_QPDFTokenizer.hh> 20 #include <qpdf/Pl_QPDFTokenizer.hh>
21 #include <qpdf/Pl_RunLength.hh> 21 #include <qpdf/Pl_RunLength.hh>
22 #include <qpdf/Pl_StdioFile.hh> 22 #include <qpdf/Pl_StdioFile.hh>
  23 +#include <qpdf/Pl_String.hh>
23 #include <qpdf/QPDF.hh> 24 #include <qpdf/QPDF.hh>
24 #include <qpdf/QPDFAcroFormDocumentHelper.hh> 25 #include <qpdf/QPDFAcroFormDocumentHelper.hh>
25 #include <qpdf/QPDFAnnotationObjectHelper.hh> 26 #include <qpdf/QPDFAnnotationObjectHelper.hh>
@@ -80,6 +81,7 @@ main() @@ -80,6 +81,7 @@ main()
80 print_size(Pl_QPDFTokenizer); 81 print_size(Pl_QPDFTokenizer);
81 print_size(Pl_RunLength); 82 print_size(Pl_RunLength);
82 print_size(Pl_StdioFile); 83 print_size(Pl_StdioFile);
  84 + print_size(Pl_String);
83 print_size(QPDF); 85 print_size(QPDF);
84 print_size(QPDFAcroFormDocumentHelper); 86 print_size(QPDFAcroFormDocumentHelper);
85 print_size(QPDFAnnotationObjectHelper); 87 print_size(QPDFAnnotationObjectHelper);
qpdf/test_driver.cc
@@ -10,6 +10,7 @@ @@ -10,6 +10,7 @@
10 #include <qpdf/Pl_Discard.hh> 10 #include <qpdf/Pl_Discard.hh>
11 #include <qpdf/Pl_Flate.hh> 11 #include <qpdf/Pl_Flate.hh>
12 #include <qpdf/Pl_StdioFile.hh> 12 #include <qpdf/Pl_StdioFile.hh>
  13 +#include <qpdf/Pl_String.hh>
13 #include <qpdf/QIntC.hh> 14 #include <qpdf/QIntC.hh>
14 #include <qpdf/QPDFAcroFormDocumentHelper.hh> 15 #include <qpdf/QPDFAcroFormDocumentHelper.hh>
15 #include <qpdf/QPDFEmbeddedFileDocumentHelper.hh> 16 #include <qpdf/QPDFEmbeddedFileDocumentHelper.hh>
@@ -435,16 +436,13 @@ test_6(QPDF&amp; pdf, char const* arg2) @@ -435,16 +436,13 @@ test_6(QPDF&amp; pdf, char const* arg2)
435 if (!metadata.isStream()) { 436 if (!metadata.isStream()) {
436 throw std::logic_error("test 6 run on file with no metadata"); 437 throw std::logic_error("test 6 run on file with no metadata");
437 } 438 }
438 - Pl_Buffer bufpl("buffer"); 439 + std::string buf;
  440 + Pl_String bufpl("buffer", buf);
439 metadata.pipeStreamData(&bufpl, 0, qpdf_dl_none); 441 metadata.pipeStreamData(&bufpl, 0, qpdf_dl_none);
440 - Buffer* buf = bufpl.getBuffer();  
441 - unsigned char const* data = buf->getBuffer();  
442 bool cleartext = false; 442 bool cleartext = false;
443 - if ((buf->getSize() > 9) &&  
444 - (strncmp(reinterpret_cast<char const*>(data), "<?xpacket", 9) == 0)) { 443 + if (buf.substr(0, 9) == "<?xpacket") {
445 cleartext = true; 444 cleartext = true;
446 } 445 }
447 - delete buf;  
448 std::cout << "encrypted=" << (pdf.isEncrypted() ? 1 : 0) 446 std::cout << "encrypted=" << (pdf.isEncrypted() ? 1 : 0)
449 << "; cleartext=" << (cleartext ? 1 : 0) << std::endl; 447 << "; cleartext=" << (cleartext ? 1 : 0) << std::endl;
450 } 448 }