From cd3d9986f40fd4e605ac2e8168512065439173e2 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 1 Oct 2022 20:12:13 -0400 Subject: Split up public API header --- include/serd/buffer.h | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 include/serd/buffer.h (limited to 'include/serd/buffer.h') diff --git a/include/serd/buffer.h b/include/serd/buffer.h new file mode 100644 index 00000000..90b51a48 --- /dev/null +++ b/include/serd/buffer.h @@ -0,0 +1,56 @@ +// Copyright 2011-2022 David Robillard +// SPDX-License-Identifier: ISC + +#ifndef SERD_BUFFER_H +#define SERD_BUFFER_H + +#include "serd/attributes.h" + +#include + +SERD_BEGIN_DECLS + +/** + @defgroup serd_buffer Dynamic Memory Buffers + @ingroup serd_memory + + The #SerdBuffer type represents a writable area of memory with a known size. + + @{ +*/ + +/// A mutable buffer in memory +typedef struct { + void* SERD_NULLABLE buf; ///< Buffer + size_t len; ///< Size of buffer in bytes +} SerdBuffer; + +/** + A convenience sink function for writing to a string. + + This function can be used as a #SerdSink 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(). +*/ +SERD_API size_t +serd_buffer_sink(const void* SERD_NONNULL buf, + size_t len, + void* SERD_NONNULL stream); + +/** + Finish writing to a buffer with serd_buffer_sink(). + + The returned string is the result of the serialisation, which is null + terminated (by this function) and owned by the caller. +*/ +SERD_API char* SERD_NONNULL +serd_buffer_sink_finish(SerdBuffer* SERD_NONNULL stream); + +/** + @} +*/ + +SERD_END_DECLS + +#endif // SERD_BUFFER_H -- cgit v1.2.1