Add new Pl_OStream Pipeline

This commit is contained in:
Jay Berkenbilt 2022-05-03 18:40:42 -04:00
parent 21d6e3231f
commit 16139d97c8
8 changed files with 115 additions and 3 deletions

View File

@ -1,5 +1,8 @@
2022-05-03 Jay Berkenbilt <ejb@ql.org>
* Add new Pipeline class Pl_OStream, similar to Pl_StdioFile but
takes a std::ostream instead of a FILE*.
* Add new convenience methods to Pipeline: writeCStr and
writeString. Also add a limit << operator that takes C strings and
std::strings. Also add an overloaded version of write that takes

1
TODO
View File

@ -47,7 +47,6 @@ Output JSON v2
notes from 5/2:
Need new pipelines:
* Pl_OStream(std::ostream) with semantics like Pl_StdioFile
* Pl_String to std::string with semantics like Pl_Buffer
See if I can change all output and error messages issued by the

View File

@ -0,0 +1,69 @@
// 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_OSTREAM_HH
#define PL_OSTREAM_HH
#include <qpdf/Pipeline.hh>
#include <iostream>
//
// This pipeline is reusable.
//
class QPDF_DLL_CLASS Pl_OStream: public Pipeline
{
public:
// os is externally maintained; this class just writes to and
// flushes it. It does not close it.
QPDF_DLL
Pl_OStream(char const* identifier, std::ostream& os);
QPDF_DLL
virtual ~Pl_OStream();
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_OStream;
public:
QPDF_DLL
~Members() = default;
private:
Members(std::ostream&);
Members(Members const&) = delete;
std::ostream& os;
};
std::shared_ptr<Members> m;
};
#endif // PL_OSTREAM_HH

View File

@ -44,6 +44,7 @@ set(libqpdf_SOURCES
Pl_Flate.cc
Pl_LZWDecoder.cc
Pl_MD5.cc
Pl_OStream.cc
Pl_PNGFilter.cc
Pl_QPDFTokenizer.cc
Pl_RC4.cc

35
libqpdf/Pl_OStream.cc Normal file
View File

@ -0,0 +1,35 @@
#include <qpdf/Pl_OStream.hh>
#include <qpdf/QUtil.hh>
#include <errno.h>
#include <stdexcept>
Pl_OStream::Members::Members(std::ostream& os) :
os(os)
{
}
Pl_OStream::Pl_OStream(char const* identifier, std::ostream& os) :
Pipeline(identifier, 0),
m(new Members(os))
{
}
Pl_OStream::~Pl_OStream()
{
// Must be explicit and not inline -- see QPDF_DLL_CLASS in
// README-maintainer
}
void
Pl_OStream::write(unsigned char const* buf, size_t len)
{
this->m->os.write(
reinterpret_cast<char const*>(buf), static_cast<std::streamsize>(len));
}
void
Pl_OStream::finish()
{
this->m->os.flush();
}

View File

@ -2,7 +2,7 @@
#include <qpdf/Pl_Base64.hh>
#include <qpdf/Pl_StdioFile.hh>
#include <qpdf/Pl_OStream.hh>
#include <qpdf/QUtil.hh>
#include <cstdlib>
#include <cstring>
@ -52,7 +52,7 @@ main(int argc, char* argv[])
}
try {
Pl_StdioFile out("stdout", stdout);
Pl_OStream out("stdout", std::cout);
Pl_Base64 decode("decode", &out, action);
// The comments are "n: n%4 n%3", where n is the number of
// bytes read at the end of the call, and are there to

View File

@ -117,6 +117,9 @@ For a detailed list of changes, please see the file
- ``operator <<``: for null-terminated C strings and std::strings
- Add new ``Pipeline`` type ``Pl_OStream`` to write to a
``std::ostream``.
- Other changes
- A new chapter on contributing to qpdf has been added to the

View File

@ -16,6 +16,7 @@
#include <qpdf/Pl_DCT.hh>
#include <qpdf/Pl_Discard.hh>
#include <qpdf/Pl_Flate.hh>
#include <qpdf/Pl_OStream.hh>
#include <qpdf/Pl_QPDFTokenizer.hh>
#include <qpdf/Pl_RunLength.hh>
#include <qpdf/Pl_StdioFile.hh>
@ -75,6 +76,7 @@ main()
print_size(Pl_DCT);
print_size(Pl_Discard);
print_size(Pl_Flate);
print_size(Pl_OStream);
print_size(Pl_QPDFTokenizer);
print_size(Pl_RunLength);
print_size(Pl_StdioFile);