aboutsummaryrefslogtreecommitdiffstats
path: root/src/block_dumper.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-08-13 19:31:26 -0400
committerDavid Robillard <d@drobilla.net>2023-12-02 18:49:08 -0500
commit828c1018f38bab9a930cecce64646366d051d39b (patch)
tree38a60777520efb17017ed62fe3b299ba94aaccf2 /src/block_dumper.c
parenta083c64f506175029280ff76defa0ad7d7ae2ea0 (diff)
downloadserd-828c1018f38bab9a930cecce64646366d051d39b.tar.gz
serd-828c1018f38bab9a930cecce64646366d051d39b.tar.bz2
serd-828c1018f38bab9a930cecce64646366d051d39b.zip
Simplify output stream API
Diffstat (limited to 'src/block_dumper.c')
-rw-r--r--src/block_dumper.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/block_dumper.c b/src/block_dumper.c
new file mode 100644
index 00000000..174f0215
--- /dev/null
+++ b/src/block_dumper.c
@@ -0,0 +1,54 @@
+// Copyright 2011-2021 David Robillard <d@drobilla.net>
+// SPDX-License-Identifier: ISC
+
+#include "block_dumper.h"
+#include "system.h"
+
+#include <stddef.h>
+
+SerdStatus
+serd_block_dumper_open(SerdBlockDumper* const dumper,
+ SerdOutputStream* const output,
+ const size_t block_size)
+{
+ if (!block_size) {
+ return SERD_BAD_ARG;
+ }
+
+ dumper->out = output;
+ dumper->buf = NULL;
+ dumper->size = 0U;
+ dumper->block_size = block_size;
+
+ if (block_size == 1) {
+ return SERD_SUCCESS;
+ }
+
+ dumper->buf = (char*)serd_allocate_buffer(block_size);
+ return dumper->buf ? SERD_SUCCESS : SERD_BAD_ALLOC;
+}
+
+SerdStatus
+serd_block_dumper_flush(SerdBlockDumper* const dumper)
+{
+ SerdStatus st = SERD_SUCCESS;
+
+ if (dumper->out->stream && dumper->block_size > 1 && dumper->size > 0) {
+ const size_t written =
+ dumper->out->write(dumper->buf, 1, dumper->size, dumper->out->stream);
+
+ if (written != dumper->size) {
+ st = SERD_BAD_WRITE;
+ }
+
+ dumper->size = 0;
+ }
+
+ return st;
+}
+
+void
+serd_block_dumper_close(SerdBlockDumper* const dumper)
+{
+ serd_free_aligned(dumper->buf);
+}