Tue, 10 Dec 2024 00:07:16 +0100
add cxBufferTerminate()
src/buffer.c | file | annotate | diff | comparison | revisions | |
src/cx/buffer.h | file | annotate | diff | comparison | revisions |
--- a/src/buffer.c Mon Dec 09 23:31:42 2024 +0100 +++ b/src/buffer.c Tue Dec 10 00:07:16 2024 +0100 @@ -316,6 +316,17 @@ } } +int cxBufferTerminate(CxBuffer *buffer) { + bool success = 0 == cxBufferPut(buffer, 0); + if (success) { + buffer->pos--; + buffer->size--; + return 0; + } else { + return 1; + } +} + size_t cxBufferPutString( CxBuffer *buffer, const char *str
--- a/src/cx/buffer.h Mon Dec 09 23:31:42 2024 +0100 +++ b/src/cx/buffer.h Tue Dec 10 00:07:16 2024 +0100 @@ -427,14 +427,14 @@ * * The least significant byte of the argument is written to the buffer. If the * end of the buffer is reached and #CX_BUFFER_AUTO_EXTEND feature is enabled, - * the buffer capacity is extended by cxBufferMinimumCapacity(). If the feature is - * disabled or buffer extension fails, \c EOF is returned. + * the buffer capacity is extended by cxBufferMinimumCapacity(). If the feature + * is disabled or buffer extension fails, \c EOF is returned. * * On successful write, the position of the buffer is increased. * * @param buffer the buffer to write to * @param c the character to write - * @return the byte that has bean written or \c EOF when the end of the stream is + * @return the byte that has been written or \c EOF when the end of the stream is * reached and automatic extension is not enabled or not possible */ cx_attr_nonnull @@ -444,6 +444,21 @@ ); /** + * Writes a terminating zero to a buffer. + * + * On successful write, \em neither the position \em nor the size of the buffer is + * increased. + * + * The purpose of this function is to have the written data ready to be used as + * a C string. + * + * @param buffer the buffer to write to + * @return zero, if the terminator could be written, non-zero otherwise + */ +cx_attr_nonnull +int cxBufferTerminate(CxBuffer *buffer); + +/** * Writes a string to a buffer. * * @param buffer the buffer