Commit f1c6bb97db659faf84e59dbe973b969e9fc1a066

Authored by Jay Berkenbilt
1 parent 59f3e09e

Add new Pipeline convenience methods

ChangeLog
1 2022-05-03 Jay Berkenbilt <ejb@ql.org> 1 2022-05-03 Jay Berkenbilt <ejb@ql.org>
2 2
  3 + * Add new convenience methods to Pipeline: writeCStr and
  4 + writeString. Also add a limit << operator that takes C strings and
  5 + std::strings. Also add an overloaded version of write that takes
  6 + "char const*".
  7 +
3 * API change: Pipeline::write now takes "unsigned char const *" 8 * API change: Pipeline::write now takes "unsigned char const *"
4 instead of "unsigned char*". Callers shouldn't have to change 9 instead of "unsigned char*". Callers shouldn't have to change
5 anything, though can stop using writable strings or 10 anything, though can stop using writable strings or
@@ -50,14 +50,6 @@ Need new pipelines: @@ -50,14 +50,6 @@ Need new pipelines:
50 * Pl_OStream(std::ostream) with semantics like Pl_StdioFile 50 * Pl_OStream(std::ostream) with semantics like Pl_StdioFile
51 * Pl_String to std::string with semantics like Pl_Buffer 51 * Pl_String to std::string with semantics like Pl_Buffer
52 52
53 -New Pipeline methods:  
54 -* writeString(std::string const&)  
55 -* writeCString(char*)  
56 -* writeChars(char*, size_t)  
57 -  
58 -* Consider templated operator<< which could specialize for char* and  
59 - std::string and could use std::ostringstream otherwise  
60 -  
61 See if I can change all output and error messages issued by the 53 See if I can change all output and error messages issued by the
62 library, when context is available, to have a pipeline rather than a 54 library, when context is available, to have a pipeline rather than a
63 FILE* or std::ostream. This makes it possible for people to capture 55 FILE* or std::ostream. This makes it possible for people to capture
include/qpdf/Pipeline.hh
@@ -71,6 +71,26 @@ class QPDF_DLL_CLASS Pipeline @@ -71,6 +71,26 @@ class QPDF_DLL_CLASS Pipeline
71 QPDF_DLL 71 QPDF_DLL
72 std::string getIdentifier() const; 72 std::string getIdentifier() const;
73 73
  74 + // These are convenience methods for making it easier to write
  75 + // certain other types of data to pipelines without having to
  76 + // cast. The methods that take char const* expect null-terminated
  77 + // C strings and do not write the null terminators.
  78 + QPDF_DLL
  79 + void writeCStr(char const* cstr);
  80 + QPDF_DLL
  81 + void writeString(std::string const&);
  82 + // This allows *p << "x" << "y" but is not intended to be a
  83 + // general purpose << compatible with ostream and does not have
  84 + // local awareness or the ability to be "imbued" with properties.
  85 + QPDF_DLL
  86 + Pipeline& operator<<(char const* cstr);
  87 + QPDF_DLL
  88 + Pipeline& operator<<(std::string const&);
  89 +
  90 + // Overloaded write to reduce casting
  91 + QPDF_DLL
  92 + void write(char const* data, size_t len);
  93 +
74 protected: 94 protected:
75 QPDF_DLL 95 QPDF_DLL
76 Pipeline* getNext(bool allow_null = false); 96 Pipeline* getNext(bool allow_null = false);
libqpdf/Pipeline.cc
@@ -25,3 +25,35 @@ Pipeline::getIdentifier() const @@ -25,3 +25,35 @@ Pipeline::getIdentifier() const
25 { 25 {
26 return this->identifier; 26 return this->identifier;
27 } 27 }
  28 +
  29 +void
  30 +Pipeline::writeCStr(char const* cstr)
  31 +{
  32 + this->write(cstr, strlen(cstr));
  33 +}
  34 +
  35 +void
  36 +Pipeline::writeString(std::string const& str)
  37 +{
  38 + this->write(str.c_str(), str.length());
  39 +}
  40 +
  41 +Pipeline&
  42 +Pipeline::operator<<(char const* cstr)
  43 +{
  44 + this->writeCStr(cstr);
  45 + return *this;
  46 +}
  47 +
  48 +Pipeline&
  49 +Pipeline::operator<<(std::string const& str)
  50 +{
  51 + this->writeString(str);
  52 + return *this;
  53 +}
  54 +
  55 +void
  56 +Pipeline::write(char const* data, size_t len)
  57 +{
  58 + this->write(reinterpret_cast<unsigned char const*>(data), len);
  59 +}
manual/release-notes.rst
@@ -105,6 +105,18 @@ For a detailed list of changes, please see the file @@ -105,6 +105,18 @@ For a detailed list of changes, please see the file
105 ``appendItemAndGet``, ``eraseItemAndGet``, ``replaceKeyAndGet``, 105 ``appendItemAndGet``, ``eraseItemAndGet``, ``replaceKeyAndGet``,
106 and ``removeKeyAndGet`` return the newly added or removed object. 106 and ``removeKeyAndGet`` return the newly added or removed object.
107 107
  108 + - Add new ``Pipeline`` methods to reduce the amount of casting that is
  109 + needed:
  110 +
  111 + - ``write``: overloaded version that takes `char const*` in
  112 + addition to the one that takes `unsigned char const*`
  113 +
  114 + - ``writeCstr``: writes a null-terminated C string
  115 +
  116 + - ``writeString``: writes a std::string
  117 +
  118 + - ``operator <<``: for null-terminated C strings and std::strings
  119 +
108 - Other changes 120 - Other changes
109 121
110 - A new chapter on contributing to qpdf has been added to the 122 - A new chapter on contributing to qpdf has been added to the