diff options
author | David Robillard <d@drobilla.net> | 2023-05-04 14:58:45 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 18:49:08 -0500 |
commit | 551faf54e35c757144204bf7a7949c0f7d0a20a3 (patch) | |
tree | e8f761fc6361c75dd443d6e91806d27025d4102a /include/serd/buffer.h | |
parent | c3bc111f7fb2a44e068e5250f7823352e44c76dc (diff) | |
download | serd-551faf54e35c757144204bf7a7949c0f7d0a20a3.tar.gz serd-551faf54e35c757144204bf7a7949c0f7d0a20a3.tar.bz2 serd-551faf54e35c757144204bf7a7949c0f7d0a20a3.zip |
Make SerdBuffer an output stream
Diffstat (limited to 'include/serd/buffer.h')
-rw-r--r-- | include/serd/buffer.h | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/include/serd/buffer.h b/include/serd/buffer.h index d4fd63fb..556e7e1f 100644 --- a/include/serd/buffer.h +++ b/include/serd/buffer.h @@ -17,6 +17,9 @@ SERD_BEGIN_DECLS The #SerdBuffer type represents a writable area of memory with a known size. + A #SerdWriteFunc function is provided which enable writing output to a + memory buffer (as `fwrite` does for files). + @{ */ @@ -27,27 +30,29 @@ typedef struct { } SerdBuffer; /** - A convenience sink function for writing to a string. + A function for writing to a buffer, resizing it if necessary. + + This function can be used as a #SerdWriteFunc to write to a #SerdBuffer + which is resized as necessary with realloc(). The `stream` parameter must + point to an initialized #SerdBuffer. - This function can be used as a #SerdWriteFunc to write to a SerdBuffer which - is resized as necessary with realloc(). The `stream` parameter must point to - an initialized #SerdBuffer. When the write is finished, the string should be - retrieved with serd_buffer_sink_finish(). + Note that when writing a string, the string in the buffer will not be + null-terminated until serd_buffer_close() is called. */ SERD_API size_t -serd_buffer_sink(const void* ZIX_NONNULL buf, - size_t size, - size_t nmemb, - void* ZIX_NONNULL stream); +serd_buffer_write(const void* ZIX_NONNULL buf, + size_t size, + size_t nmemb, + void* ZIX_NONNULL stream); /** - Finish writing to a buffer with serd_buffer_sink(). + Close the buffer for writing. - The returned string is the result of the serialisation, which is null - terminated (by this function) and owned by the caller. + This writes a terminating null byte, so the contents of the buffer are safe + to read as a string after this call. */ -SERD_API char* ZIX_NONNULL -serd_buffer_sink_finish(SerdBuffer* ZIX_NONNULL stream); +SERD_API int +serd_buffer_close(void* ZIX_NONNULL stream); /** @} |