aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog3
-rw-r--r--TODO3
-rw-r--r--include/qpdf/Pl_String.hh63
-rw-r--r--libqpdf/CMakeLists.txt1
-rw-r--r--libqpdf/Pl_String.cc33
-rw-r--r--manual/release-notes.rst3
-rw-r--r--qpdf/sizes.cc2
-rw-r--r--qpdf/test_driver.cc10
8 files changed, 109 insertions, 9 deletions
diff --git a/ChangeLog b/ChangeLog
index 38d5aaa1..dfcadf49 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
2022-05-03 Jay Berkenbilt <ejb@ql.org>
+ * 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 <qpdf/Pipeline.hh>
+
+#include <string>
+
+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<Members> 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 <qpdf/Pl_String.hh>
+
+#include <qpdf/QUtil.hh>
+#include <errno.h>
+#include <stdexcept>
+
+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<char const*>(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 <qpdf/Pl_QPDFTokenizer.hh>
#include <qpdf/Pl_RunLength.hh>
#include <qpdf/Pl_StdioFile.hh>
+#include <qpdf/Pl_String.hh>
#include <qpdf/QPDF.hh>
#include <qpdf/QPDFAcroFormDocumentHelper.hh>
#include <qpdf/QPDFAnnotationObjectHelper.hh>
@@ -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 <qpdf/Pl_Discard.hh>
#include <qpdf/Pl_Flate.hh>
#include <qpdf/Pl_StdioFile.hh>
+#include <qpdf/Pl_String.hh>
#include <qpdf/QIntC.hh>
#include <qpdf/QPDFAcroFormDocumentHelper.hh>
#include <qpdf/QPDFEmbeddedFileDocumentHelper.hh>
@@ -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<char const*>(data), "<?xpacket", 9) == 0)) {
+ if (buf.substr(0, 9) == "<?xpacket") {
cleartext = true;
}
- delete buf;
std::cout << "encrypted=" << (pdf.isEncrypted() ? 1 : 0)
<< "; cleartext=" << (cleartext ? 1 : 0) << std::endl;
}