Commit 16139d97c83ba48ab31b0f7619bab77d6cdb79e6

Authored by Jay Berkenbilt
1 parent 21d6e323

Add new Pl_OStream Pipeline

ChangeLog
1 1 2022-05-03 Jay Berkenbilt <ejb@ql.org>
2 2  
  3 + * Add new Pipeline class Pl_OStream, similar to Pl_StdioFile but
  4 + takes a std::ostream instead of a FILE*.
  5 +
3 6 * Add new convenience methods to Pipeline: writeCStr and
4 7 writeString. Also add a limit << operator that takes C strings and
5 8 std::strings. Also add an overloaded version of write that takes
... ...
... ... @@ -47,7 +47,6 @@ Output JSON v2
47 47 notes from 5/2:
48 48  
49 49 Need new pipelines:
50   -* Pl_OStream(std::ostream) with semantics like Pl_StdioFile
51 50 * Pl_String to std::string with semantics like Pl_Buffer
52 51  
53 52 See if I can change all output and error messages issued by the
... ...
include/qpdf/Pl_OStream.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_OSTREAM_HH
  25 +#define PL_OSTREAM_HH
  26 +
  27 +#include <qpdf/Pipeline.hh>
  28 +
  29 +#include <iostream>
  30 +
  31 +//
  32 +// This pipeline is reusable.
  33 +//
  34 +
  35 +class QPDF_DLL_CLASS Pl_OStream: public Pipeline
  36 +{
  37 + public:
  38 + // os is externally maintained; this class just writes to and
  39 + // flushes it. It does not close it.
  40 + QPDF_DLL
  41 + Pl_OStream(char const* identifier, std::ostream& os);
  42 + QPDF_DLL
  43 + virtual ~Pl_OStream();
  44 +
  45 + QPDF_DLL
  46 + virtual void write(unsigned char const* buf, size_t len);
  47 + QPDF_DLL
  48 + virtual void finish();
  49 +
  50 + private:
  51 + class QPDF_DLL_PRIVATE Members
  52 + {
  53 + friend class Pl_OStream;
  54 +
  55 + public:
  56 + QPDF_DLL
  57 + ~Members() = default;
  58 +
  59 + private:
  60 + Members(std::ostream&);
  61 + Members(Members const&) = delete;
  62 +
  63 + std::ostream& os;
  64 + };
  65 +
  66 + std::shared_ptr<Members> m;
  67 +};
  68 +
  69 +#endif // PL_OSTREAM_HH
... ...
libqpdf/CMakeLists.txt
... ... @@ -44,6 +44,7 @@ set(libqpdf_SOURCES
44 44 Pl_Flate.cc
45 45 Pl_LZWDecoder.cc
46 46 Pl_MD5.cc
  47 + Pl_OStream.cc
47 48 Pl_PNGFilter.cc
48 49 Pl_QPDFTokenizer.cc
49 50 Pl_RC4.cc
... ...
libqpdf/Pl_OStream.cc 0 → 100644
  1 +#include <qpdf/Pl_OStream.hh>
  2 +
  3 +#include <qpdf/QUtil.hh>
  4 +#include <errno.h>
  5 +#include <stdexcept>
  6 +
  7 +Pl_OStream::Members::Members(std::ostream& os) :
  8 + os(os)
  9 +{
  10 +}
  11 +
  12 +Pl_OStream::Pl_OStream(char const* identifier, std::ostream& os) :
  13 + Pipeline(identifier, 0),
  14 + m(new Members(os))
  15 +{
  16 +}
  17 +
  18 +Pl_OStream::~Pl_OStream()
  19 +{
  20 + // Must be explicit and not inline -- see QPDF_DLL_CLASS in
  21 + // README-maintainer
  22 +}
  23 +
  24 +void
  25 +Pl_OStream::write(unsigned char const* buf, size_t len)
  26 +{
  27 + this->m->os.write(
  28 + reinterpret_cast<char const*>(buf), static_cast<std::streamsize>(len));
  29 +}
  30 +
  31 +void
  32 +Pl_OStream::finish()
  33 +{
  34 + this->m->os.flush();
  35 +}
... ...
libtests/base64.cc
... ... @@ -2,7 +2,7 @@
2 2  
3 3 #include <qpdf/Pl_Base64.hh>
4 4  
5   -#include <qpdf/Pl_StdioFile.hh>
  5 +#include <qpdf/Pl_OStream.hh>
6 6 #include <qpdf/QUtil.hh>
7 7 #include <cstdlib>
8 8 #include <cstring>
... ... @@ -52,7 +52,7 @@ main(int argc, char* argv[])
52 52 }
53 53  
54 54 try {
55   - Pl_StdioFile out("stdout", stdout);
  55 + Pl_OStream out("stdout", std::cout);
56 56 Pl_Base64 decode("decode", &out, action);
57 57 // The comments are "n: n%4 n%3", where n is the number of
58 58 // bytes read at the end of the call, and are there to
... ...
manual/release-notes.rst
... ... @@ -117,6 +117,9 @@ For a detailed list of changes, please see the file
117 117  
118 118 - ``operator <<``: for null-terminated C strings and std::strings
119 119  
  120 + - Add new ``Pipeline`` type ``Pl_OStream`` to write to a
  121 + ``std::ostream``.
  122 +
120 123 - Other changes
121 124  
122 125 - A new chapter on contributing to qpdf has been added to the
... ...
qpdf/sizes.cc
... ... @@ -16,6 +16,7 @@
16 16 #include <qpdf/Pl_DCT.hh>
17 17 #include <qpdf/Pl_Discard.hh>
18 18 #include <qpdf/Pl_Flate.hh>
  19 +#include <qpdf/Pl_OStream.hh>
19 20 #include <qpdf/Pl_QPDFTokenizer.hh>
20 21 #include <qpdf/Pl_RunLength.hh>
21 22 #include <qpdf/Pl_StdioFile.hh>
... ... @@ -75,6 +76,7 @@ main()
75 76 print_size(Pl_DCT);
76 77 print_size(Pl_Discard);
77 78 print_size(Pl_Flate);
  79 + print_size(Pl_OStream);
78 80 print_size(Pl_QPDFTokenizer);
79 81 print_size(Pl_RunLength);
80 82 print_size(Pl_StdioFile);
... ...