aboutsummaryrefslogtreecommitdiffstats
path: root/src/buffer.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2023-05-04 14:58:45 -0400
committerDavid Robillard <d@drobilla.net>2023-12-02 18:49:08 -0500
commit551faf54e35c757144204bf7a7949c0f7d0a20a3 (patch)
treee8f761fc6361c75dd443d6e91806d27025d4102a /src/buffer.c
parentc3bc111f7fb2a44e068e5250f7823352e44c76dc (diff)
downloadserd-551faf54e35c757144204bf7a7949c0f7d0a20a3.tar.gz
serd-551faf54e35c757144204bf7a7949c0f7d0a20a3.tar.bz2
serd-551faf54e35c757144204bf7a7949c0f7d0a20a3.zip
Make SerdBuffer an output stream
Diffstat (limited to 'src/buffer.c')
-rw-r--r--src/buffer.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/buffer.c b/src/buffer.c
new file mode 100644
index 00000000..12f343c2
--- /dev/null
+++ b/src/buffer.c
@@ -0,0 +1,39 @@
+// Copyright 2011-2021 David Robillard <d@drobilla.net>
+// SPDX-License-Identifier: ISC
+
+#include "serd/serd.h"
+
+#include <assert.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+
+size_t
+serd_buffer_write(const void* const buf,
+ const size_t size,
+ const size_t nmemb,
+ void* const stream)
+{
+ assert(buf);
+ assert(stream);
+
+ SerdBuffer* const buffer = (SerdBuffer*)stream;
+ const size_t n_bytes = size * nmemb;
+
+ char* const new_buf = (char*)realloc(buffer->buf, buffer->len + n_bytes);
+ if (new_buf) {
+ memcpy(new_buf + buffer->len, buf, n_bytes);
+ buffer->buf = new_buf;
+ buffer->len += nmemb;
+ }
+
+ return new_buf ? nmemb : 0;
+}
+
+int
+serd_buffer_close(void* const stream)
+{
+ serd_buffer_write("", 1, 1, stream); // Write null terminator
+
+ return 0;
+}