aboutsummaryrefslogtreecommitdiffstats
path: root/include/serd/buffer.h
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2022-10-01 20:12:13 -0400
committerDavid Robillard <d@drobilla.net>2023-12-02 16:27:02 -0500
commitcd3d9986f40fd4e605ac2e8168512065439173e2 (patch)
tree72d0418cbd8b8b5c7e43880c68831819e12b1982 /include/serd/buffer.h
parent1f09497ae009daade56c450e73bf6425b27cea24 (diff)
downloadserd-cd3d9986f40fd4e605ac2e8168512065439173e2.tar.gz
serd-cd3d9986f40fd4e605ac2e8168512065439173e2.tar.bz2
serd-cd3d9986f40fd4e605ac2e8168512065439173e2.zip
Split up public API header
Diffstat (limited to 'include/serd/buffer.h')
-rw-r--r--include/serd/buffer.h56
1 files changed, 56 insertions, 0 deletions
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 <d@drobilla.net>
+// SPDX-License-Identifier: ISC
+
+#ifndef SERD_BUFFER_H
+#define SERD_BUFFER_H
+
+#include "serd/attributes.h"
+
+#include <stddef.h>
+
+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