diff options
author | David Robillard <d@drobilla.net> | 2020-05-30 21:32:37 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2020-10-27 13:13:59 +0100 |
commit | 7026fb72f85e349eea64a62bd924358c608520cc (patch) | |
tree | 857cef38b39ceb3687216e91904b5edc7bf48a0c /src | |
parent | 797eb91498ea83003922b70aa943925b4fb81bb5 (diff) | |
download | serd-7026fb72f85e349eea64a62bd924358c608520cc.tar.gz serd-7026fb72f85e349eea64a62bd924358c608520cc.tar.bz2 serd-7026fb72f85e349eea64a62bd924358c608520cc.zip |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/inserter.c | 40 | ||||
-rw-r--r-- | src/sink.c | 55 | ||||
-rw-r--r-- | src/sink.h | 9 | ||||
-rw-r--r-- | src/writer.c | 28 |
4 files changed, 81 insertions, 51 deletions
diff --git a/src/inserter.c b/src/inserter.c index e2f95896..5ee5d2c2 100644 --- a/src/inserter.c +++ b/src/inserter.c @@ -36,20 +36,6 @@ manage_or_intern(SerdNodes* nodes, SerdNode* manage, const SerdNode* intern) } static SerdStatus -serd_inserter_on_base(SerdInserterData* data, const SerdNode* uri) -{ - return serd_env_set_base_uri(data->env, uri); -} - -static SerdStatus -serd_inserter_on_prefix(SerdInserterData* data, - const SerdNode* name, - const SerdNode* uri) -{ - return serd_env_set_prefix(data->env, name, uri); -} - -static SerdStatus serd_inserter_on_statement(SerdInserterData* data, const SerdStatementFlags flags, const SerdStatement* statement) @@ -85,6 +71,27 @@ serd_inserter_on_statement(SerdInserterData* data, return st > SERD_FAILURE ? st : SERD_SUCCESS; } +static SerdStatus +serd_inserter_on_event(SerdInserterData* data, const SerdEvent* event) +{ + switch (event->type) { + case SERD_BASE: + return serd_env_set_base_uri(data->env, event->base.uri); + case SERD_PREFIX: + return serd_env_set_prefix(data->env, + event->prefix.name, + event->prefix.uri); + case SERD_STATEMENT: + return serd_inserter_on_statement(data, + event->statement.flags, + event->statement.statement); + case SERD_END: + break; + } + + return SERD_SUCCESS; +} + static void free_data(void* handle) { @@ -108,10 +115,7 @@ serd_inserter_new(SerdModel* model, SerdEnv* env, const SerdNode* default_graph) SerdSink* const sink = serd_sink_new(data, free_data); - serd_sink_set_base_func(sink, (SerdBaseFunc)serd_inserter_on_base); - serd_sink_set_prefix_func(sink, (SerdPrefixFunc)serd_inserter_on_prefix); - serd_sink_set_statement_func(sink, - (SerdStatementFunc)serd_inserter_on_statement); + serd_sink_set_event_func(sink, (SerdEventFunc)serd_inserter_on_event); return sink; } @@ -46,37 +46,40 @@ serd_sink_free(SerdSink* sink) } SerdStatus -serd_sink_set_base_func(SerdSink* sink, SerdBaseFunc base_func) +serd_sink_set_event_func(SerdSink* sink, SerdEventFunc event_func) { - sink->base = base_func; + sink->on_event = event_func; return SERD_SUCCESS; } SerdStatus -serd_sink_set_prefix_func(SerdSink* sink, SerdPrefixFunc prefix_func) +serd_sink_write_event(const SerdSink* sink, const SerdEvent* event) { - sink->prefix = prefix_func; - return SERD_SUCCESS; -} - -SerdStatus -serd_sink_set_statement_func(SerdSink* sink, SerdStatementFunc statement_func) -{ - sink->statement = statement_func; - return SERD_SUCCESS; -} + switch (event->type) { + case SERD_BASE: + return serd_sink_write_base(sink, event->base.uri); + case SERD_PREFIX: + return serd_sink_write_prefix(sink, + event->prefix.name, + event->prefix.uri); + case SERD_STATEMENT: + return serd_sink_write_statement(sink, + event->statement.flags, + event->statement.statement); + case SERD_END: + return serd_sink_write_end(sink, event->end.node); + } -SerdStatus -serd_sink_set_end_func(SerdSink* sink, SerdEndFunc end_func) -{ - sink->end = end_func; return SERD_SUCCESS; } SerdStatus serd_sink_write_base(const SerdSink* sink, const SerdNode* uri) { - return sink->base ? sink->base(sink->handle, uri) : SERD_SUCCESS; + const SerdBaseEvent ev = { SERD_BASE, uri }; + + return sink->on_event ? sink->on_event(sink->handle, (const SerdEvent*)&ev) + : SERD_SUCCESS; } SerdStatus @@ -84,7 +87,10 @@ serd_sink_write_prefix(const SerdSink* sink, const SerdNode* name, const SerdNode* uri) { - return sink->prefix ? sink->prefix(sink->handle, name, uri) : SERD_SUCCESS; + const SerdPrefixEvent ev = { SERD_PREFIX, name, uri }; + + return sink->on_event ? sink->on_event(sink->handle, (const SerdEvent*)&ev) + : SERD_SUCCESS; } SerdStatus @@ -92,8 +98,10 @@ serd_sink_write_statement(const SerdSink* sink, const SerdStatementFlags flags, const SerdStatement* statement) { - return sink->statement ? sink->statement(sink->handle, flags, statement) - : SERD_SUCCESS; + const SerdStatementEvent ev = { SERD_STATEMENT, flags, statement }; + + return sink->on_event ? sink->on_event(sink->handle, (const SerdEvent*)&ev) + : SERD_SUCCESS; } SerdStatus @@ -112,5 +120,8 @@ serd_sink_write(const SerdSink* sink, SerdStatus serd_sink_write_end(const SerdSink* sink, const SerdNode* node) { - return sink->end ? sink->end(sink->handle, node) : SERD_SUCCESS; + const SerdEndEvent ev = {SERD_END, node}; + + return sink->on_event ? sink->on_event(sink->handle, (const SerdEvent*)&ev) + : SERD_SUCCESS; } @@ -23,12 +23,9 @@ An interface that receives a stream of RDF data. */ struct SerdSinkImpl { - void* handle; - SerdFreeFunc free_handle; - SerdBaseFunc base; - SerdPrefixFunc prefix; - SerdStatementFunc statement; - SerdEndFunc end; + void* handle; + SerdFreeFunc free_handle; + SerdEventFunc on_event; }; #endif // SERD_SINK_H diff --git a/src/writer.c b/src/writer.c index 2686cc8a..29d9e902 100644 --- a/src/writer.c +++ b/src/writer.c @@ -996,6 +996,27 @@ serd_writer_end_anon(SerdWriter* writer, const SerdNode* node) return st; } +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) { @@ -1037,11 +1058,8 @@ serd_writer_new(SerdWorld* world, writer->context = context; writer->empty = true; - 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; } |