From b983d2a94b0f84b4c06889be77fbdacc502f2d7b Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 20 Feb 2021 19:18:28 -0500 Subject: Replace multiple stream callbacks with SerdEvent This makes plumbing easier since everything goes through the same "stream" and only one callback is required to handling everything. It's also more easily extensible in case more event types need to be added in the future. --- src/writer.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'src/writer.c') diff --git a/src/writer.c b/src/writer.c index 4160c09d..9b10848f 100644 --- a/src/writer.c +++ b/src/writer.c @@ -935,6 +935,25 @@ serd_writer_end_anon(SerdWriter* writer, const SerdNode* node) return SERD_SUCCESS; } +static SerdStatus +serd_writer_on_event(SerdWriter* writer, const SerdEvent* event) +{ + switch (event->type) { + case SERD_BASE: + return serd_writer_set_base_uri(writer, event->base.uri); + case SERD_PREFIX: + return serd_writer_set_prefix( + writer, event->prefix.name, event->prefix.uri); + case SERD_STATEMENT: + return serd_writer_write_statement( + writer, event->statement.flags, event->statement.statement); + case SERD_END: + return serd_writer_end_anon(writer, event->end.node); + } + + return SERD_SUCCESS; +} + SerdStatus serd_writer_finish(SerdWriter* writer) { @@ -975,11 +994,8 @@ serd_writer_new(SerdWorld* world, writer->byte_sink = serd_byte_sink_new( ssink, stream, (flags & SERD_WRITE_BULK) ? SERD_PAGE_SIZE : 1); - writer->iface.handle = writer; - writer->iface.base = (SerdBaseFunc)serd_writer_set_base_uri; - writer->iface.prefix = (SerdPrefixFunc)serd_writer_set_prefix; - writer->iface.statement = (SerdStatementFunc)serd_writer_write_statement; - writer->iface.end = (SerdEndFunc)serd_writer_end_anon; + writer->iface.handle = writer; + writer->iface.on_event = (SerdEventFunc)serd_writer_on_event; return writer; } -- cgit v1.2.1