diff --git a/ChangeLog b/ChangeLog index 662bad6d..d8f5ad91 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2022-05-03 Jay Berkenbilt + * 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 + "char const*". + * API change: Pipeline::write now takes "unsigned char const *" instead of "unsigned char*". Callers shouldn't have to change anything, though can stop using writable strings or diff --git a/TODO b/TODO index 8eae1090..d4ffa3fc 100644 --- a/TODO +++ b/TODO @@ -50,14 +50,6 @@ Need new pipelines: * Pl_OStream(std::ostream) with semantics like Pl_StdioFile * Pl_String to std::string with semantics like Pl_Buffer -New Pipeline methods: -* writeString(std::string const&) -* writeCString(char*) -* writeChars(char*, size_t) - -* Consider templated operator<< which could specialize for char* and - std::string and could use std::ostringstream otherwise - 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/Pipeline.hh b/include/qpdf/Pipeline.hh index 8c1dfe17..c3eb787e 100644 --- a/include/qpdf/Pipeline.hh +++ b/include/qpdf/Pipeline.hh @@ -71,6 +71,26 @@ class QPDF_DLL_CLASS Pipeline QPDF_DLL std::string getIdentifier() const; + // These are convenience methods for making it easier to write + // certain other types of data to pipelines without having to + // cast. The methods that take char const* expect null-terminated + // C strings and do not write the null terminators. + QPDF_DLL + void writeCStr(char const* cstr); + QPDF_DLL + void writeString(std::string const&); + // This allows *p << "x" << "y" but is not intended to be a + // general purpose << compatible with ostream and does not have + // local awareness or the ability to be "imbued" with properties. + QPDF_DLL + Pipeline& operator<<(char const* cstr); + QPDF_DLL + Pipeline& operator<<(std::string const&); + + // Overloaded write to reduce casting + QPDF_DLL + void write(char const* data, size_t len); + protected: QPDF_DLL Pipeline* getNext(bool allow_null = false); diff --git a/libqpdf/Pipeline.cc b/libqpdf/Pipeline.cc index b63dff9f..12a98d04 100644 --- a/libqpdf/Pipeline.cc +++ b/libqpdf/Pipeline.cc @@ -25,3 +25,35 @@ Pipeline::getIdentifier() const { return this->identifier; } + +void +Pipeline::writeCStr(char const* cstr) +{ + this->write(cstr, strlen(cstr)); +} + +void +Pipeline::writeString(std::string const& str) +{ + this->write(str.c_str(), str.length()); +} + +Pipeline& +Pipeline::operator<<(char const* cstr) +{ + this->writeCStr(cstr); + return *this; +} + +Pipeline& +Pipeline::operator<<(std::string const& str) +{ + this->writeString(str); + return *this; +} + +void +Pipeline::write(char const* data, size_t len) +{ + this->write(reinterpret_cast(data), len); +} diff --git a/manual/release-notes.rst b/manual/release-notes.rst index b378301e..990a0188 100644 --- a/manual/release-notes.rst +++ b/manual/release-notes.rst @@ -105,6 +105,18 @@ For a detailed list of changes, please see the file ``appendItemAndGet``, ``eraseItemAndGet``, ``replaceKeyAndGet``, and ``removeKeyAndGet`` return the newly added or removed object. + - Add new ``Pipeline`` methods to reduce the amount of casting that is + needed: + + - ``write``: overloaded version that takes `char const*` in + addition to the one that takes `unsigned char const*` + + - ``writeCstr``: writes a null-terminated C string + + - ``writeString``: writes a std::string + + - ``operator <<``: for null-terminated C strings and std::strings + - Other changes - A new chapter on contributing to qpdf has been added to the