diff options
author | David Robillard <d@drobilla.net> | 2018-12-29 17:36:15 -0500 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2019-04-13 19:15:32 +0200 |
commit | 2b775031fc4d239cbece7f0911f4be9e50e690d7 (patch) | |
tree | 0a58b292c1bae93b40e4e912450aa883d7bd2190 /src | |
parent | d6060f62d7dd3c9906fb4c58a75874f57d846162 (diff) | |
download | serd-2b775031fc4d239cbece7f0911f4be9e50e690d7.tar.gz serd-2b775031fc4d239cbece7f0911f4be9e50e690d7.tar.bz2 serd-2b775031fc4d239cbece7f0911f4be9e50e690d7.zip |
Add Env to Sink
This isn't technically necessary for any sink, but is a pattern common to every
sink implementation in serd, so having it here reduces boilerplate and
parameters in user code.
Diffstat (limited to 'src')
-rw-r--r-- | src/n3.c | 20 | ||||
-rw-r--r-- | src/reader.c | 9 | ||||
-rw-r--r-- | src/sink.c | 20 | ||||
-rw-r--r-- | src/sink.h | 1 | ||||
-rw-r--r-- | src/writer.c | 61 |
5 files changed, 57 insertions, 54 deletions
@@ -18,7 +18,6 @@ #include "node.h" #include "reader.h" #include "serd_internal.h" -#include "sink.h" #include "stack.h" #include "string_utils.h" #include "uri_utils.h" @@ -958,8 +957,8 @@ read_anon(SerdReader* reader, ReadContext ctx, bool subject, SerdNode** dest) *ctx.flags = old_flags; } - if (reader->sink->end && (!subject || !empty)) { - reader->sink->end(reader->sink->handle, *dest); + if (!subject || !empty) { + serd_sink_write_end(reader->sink, *dest); } return (eat_byte_check(reader, ']') == ']') ? SERD_SUCCESS @@ -1262,10 +1261,8 @@ read_base(SerdReader* reader, bool sparql, bool token) read_ws_star(reader); SerdNode* uri = NULL; TRY(st, read_IRIREF(reader, &uri)); - if (reader->sink->base) { - serd_node_zero_pad(uri); - reader->sink->base(reader->sink->handle, uri); - } + serd_node_zero_pad(uri); + serd_sink_write_base(reader->sink, uri); read_ws_star(reader); if (!sparql) { @@ -1301,11 +1298,10 @@ read_prefixID(SerdReader* reader, bool sparql, bool token) SerdNode* uri = NULL; TRY(st, read_IRIREF(reader, &uri)); - if (reader->sink->prefix) { - serd_node_zero_pad(name); - serd_node_zero_pad(uri); - st = reader->sink->prefix(reader->sink->handle, name, uri); - } + serd_node_zero_pad(name); + serd_node_zero_pad(uri); + st = serd_sink_write_prefix(reader->sink, name, uri); + if (!sparql) { read_ws_star(reader); st = eat_byte_check(reader, '.') ? SERD_SUCCESS : SERD_ERR_BAD_SYNTAX; diff --git a/src/reader.c b/src/reader.c index ab6b3611..4235168a 100644 --- a/src/reader.c +++ b/src/reader.c @@ -20,7 +20,6 @@ #include "node.h" #include "serd/serd.h" #include "serd_internal.h" -#include "sink.h" #include "stack.h" #include "statement.h" #include "system.h" @@ -121,9 +120,6 @@ SerdStatus emit_statement(SerdReader* reader, ReadContext ctx, SerdNode* o) { SerdNode* graph = ctx.graph; - if (!reader->sink->statement) { - return SERD_SUCCESS; - } /* Zero the pad of the object node on the top of the stack. Lower nodes (subject and predicate) were already zeroed by subsequent pushes. */ @@ -134,8 +130,9 @@ emit_statement(SerdReader* reader, ReadContext ctx, SerdNode* o) &reader->source.cur }; - const SerdStatus st = reader->sink->statement( - reader->sink->handle, *ctx.flags, &statement); + const SerdStatus st = serd_sink_write_statement( + reader->sink, *ctx.flags, &statement); + *ctx.flags = 0; return st; } @@ -23,11 +23,13 @@ #include <stdlib.h> SerdSink* -serd_sink_new(void* handle) +serd_sink_new(void* handle, SerdEnv* env) { SerdSink* sink = (SerdSink*)calloc(1, sizeof(SerdSink)); sink->handle = handle; + sink->env = env; + return sink; } @@ -37,6 +39,12 @@ serd_sink_free(SerdSink* sink) free(sink); } +const SerdEnv* +serd_sink_get_env(const SerdSink* sink) +{ + return sink->env; +} + SerdStatus serd_sink_set_base_func(SerdSink* sink, SerdBaseSink base_func) { @@ -68,7 +76,10 @@ serd_sink_set_end_func(SerdSink* sink, SerdEndSink end_func) SerdStatus serd_sink_write_base(const SerdSink* sink, const SerdNode* uri) { - return sink->base ? sink->base(sink->handle, uri) : SERD_SUCCESS; + const SerdStatus st = (sink->env ? serd_env_set_base_uri(sink->env, uri) + : SERD_SUCCESS); + + return (!st && sink->base) ? sink->base(sink->handle, uri) : st; } SerdStatus @@ -76,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 SerdStatus st = (sink->env ? serd_env_set_prefix(sink->env, name, uri) + : SERD_SUCCESS); + + return (!st && sink->prefix) ? sink->prefix(sink->handle, name, uri) : st; } SerdStatus @@ -24,6 +24,7 @@ */ struct SerdSinkImpl { void* handle; + SerdEnv* env; SerdBaseSink base; SerdPrefixSink prefix; SerdStatementSink statement; diff --git a/src/writer.c b/src/writer.c index 72ecc4ed..953de1fb 100644 --- a/src/writer.c +++ b/src/writer.c @@ -105,7 +105,6 @@ struct SerdWriterImpl { SerdSink iface; SerdSyntax syntax; SerdWriterFlags flags; - SerdEnv* env; SerdNode* root_node; SerdURI root_uri; SerdStack anon_stack; @@ -520,6 +519,8 @@ write_uri_node(SerdWriter* const writer, const SerdField field, const SerdStatementFlags flags) { + SerdEnv* const env = writer->iface.env; + writer->last_sep = SEP_NONE; if (is_inline_start(writer, field, flags)) { write_sep(writer, SEP_ANON_BEGIN); @@ -537,7 +538,7 @@ write_uri_node(SerdWriter* const writer, serd_node_equals(node, writer->world->rdf_nil)) { return sink("()", 2, writer) == 2; } else if (has_scheme && supports_abbrev(writer) && - serd_env_qualify_in_place(writer->env, node, &prefix, &suffix) && + serd_env_qualify_in_place(env, node, &prefix, &suffix) && is_name(serd_node_get_string(prefix), serd_node_get_length(prefix)) && is_name(suffix.buf, suffix.len)) { write_uri_from_node(writer, prefix); @@ -547,8 +548,8 @@ write_uri_node(SerdWriter* const writer, } sink("<", 1, writer); - if (serd_env_get_base_uri(writer->env)) { - const SerdURI* base_uri = serd_env_get_parsed_base_uri(writer->env); + if (serd_env_get_base_uri(env)) { + const SerdURI* base_uri = serd_env_get_parsed_base_uri(env); SerdURI uri, abs_uri; serd_uri_parse(node_str, &uri); serd_uri_resolve(&uri, base_uri, &abs_uri); @@ -587,7 +588,7 @@ write_curie(SerdWriter* const writer, case SERD_NTRIPLES: case SERD_NQUADS: if ((st = serd_env_expand_in_place( - writer->env, node, &prefix, &suffix))) { + writer->iface.env, node, &prefix, &suffix))) { serd_world_errorf(writer->world, st, "undefined namespace prefix `%s'\n", @@ -910,7 +911,6 @@ serd_writer_new(SerdWorld* world, writer->world = world; writer->syntax = syntax; writer->flags = flags; - writer->env = env; writer->root_node = NULL; writer->root_uri = SERD_URI_NULL; writer->anon_stack = serd_stack_new(SERD_PAGE_SIZE); @@ -920,6 +920,7 @@ serd_writer_new(SerdWorld* world, writer->empty = true; writer->iface.handle = writer; + writer->iface.env = env; writer->iface.base = (SerdBaseSink)serd_writer_set_base_uri; writer->iface.prefix = (SerdPrefixSink)serd_writer_set_prefix; writer->iface.statement = (SerdStatementSink)serd_writer_write_statement; @@ -946,20 +947,17 @@ SerdStatus serd_writer_set_base_uri(SerdWriter* writer, const SerdNode* uri) { - if (!serd_env_set_base_uri(writer->env, uri)) { - if (writer->syntax == SERD_TURTLE || writer->syntax == SERD_TRIG) { - if (ctx(writer, SERD_GRAPH) || ctx(writer, SERD_SUBJECT)) { - sink(" .\n\n", 4, writer); - reset_context(writer, true); - } - sink("@base <", 7, writer); - sink(serd_node_get_string(uri), uri->n_bytes, writer); - sink("> .\n", 4, writer); + if (writer->syntax == SERD_TURTLE || writer->syntax == SERD_TRIG) { + if (ctx(writer, SERD_GRAPH) || ctx(writer, SERD_SUBJECT)) { + sink(" .\n\n", 4, writer); + reset_context(writer, true); } - writer->indent = 0; - return reset_context(writer, true); + sink("@base <", 7, writer); + sink(serd_node_get_string(uri), uri->n_bytes, writer); + sink("> .\n", 4, writer); } - return SERD_ERR_UNKNOWN; + writer->indent = 0; + return reset_context(writer, true); } SerdStatus @@ -983,22 +981,19 @@ serd_writer_set_prefix(SerdWriter* writer, const SerdNode* name, const SerdNode* uri) { - if (!serd_env_set_prefix(writer->env, name, uri)) { - if (writer->syntax == SERD_TURTLE || writer->syntax == SERD_TRIG) { - if (ctx(writer, SERD_GRAPH) || ctx(writer, SERD_SUBJECT)) { - sink(" .\n\n", 4, writer); - reset_context(writer, true); - } - sink("@prefix ", 8, writer); - sink(serd_node_get_string(name), name->n_bytes, writer); - sink(": <", 3, writer); - write_uri_from_node(writer, uri); - sink("> .\n", 4, writer); + if (writer->syntax == SERD_TURTLE || writer->syntax == SERD_TRIG) { + if (ctx(writer, SERD_GRAPH) || ctx(writer, SERD_SUBJECT)) { + sink(" .\n\n", 4, writer); + reset_context(writer, true); } - writer->indent = 0; - return reset_context(writer, true); + sink("@prefix ", 8, writer); + sink(serd_node_get_string(name), name->n_bytes, writer); + sink(": <", 3, writer); + write_uri_from_node(writer, uri); + sink("> .\n", 4, writer); } - return SERD_ERR_UNKNOWN; + writer->indent = 0; + return reset_context(writer, true); } void @@ -1020,7 +1015,7 @@ serd_writer_get_sink(SerdWriter* writer) SerdEnv* serd_writer_get_env(SerdWriter* writer) { - return writer->env; + return writer->iface.env; } size_t |