diff options
author | David Robillard <d@drobilla.net> | 2021-08-13 19:31:26 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-01-28 21:57:07 -0500 |
commit | 63e7e57237a79d0447b0450a7fd3148c43052299 (patch) | |
tree | b4c45eca1c208f8bd70a7a50632d423aec39e118 /src/buffer.c | |
parent | 547ef6e0600b703dcd42a10622563d7b91434669 (diff) | |
download | serd-63e7e57237a79d0447b0450a7fd3148c43052299.tar.gz serd-63e7e57237a79d0447b0450a7fd3148c43052299.tar.bz2 serd-63e7e57237a79d0447b0450a7fd3148c43052299.zip |
Provide a full output stream implementation for SerdBuffer
Essentially replaces serd_buffer_sink_finish() with serd_buffer_close(), which
makes writing to a buffer consistent with writing to a file or anything else.
Diffstat (limited to 'src/buffer.c')
-rw-r--r-- | src/buffer.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/buffer.c b/src/buffer.c new file mode 100644 index 00000000..85ec09de --- /dev/null +++ b/src/buffer.c @@ -0,0 +1,52 @@ +/* + Copyright 2011-2021 David Robillard <d@drobilla.net> + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#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; +} |