diff options
author | David Robillard <d@drobilla.net> | 2018-12-29 17:36:15 -0500 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2018-12-31 11:37:49 -0500 |
commit | 904d3d9b0bcdbb1507adccdde831db0d5f7ae251 (patch) | |
tree | be485f9684819534972a40ced925138f5e4d850c /src | |
parent | beb2eba61195d6ef838cd3bfd363ae74785aaff9 (diff) | |
download | serd-904d3d9b0bcdbb1507adccdde831db0d5f7ae251.tar.gz serd-904d3d9b0bcdbb1507adccdde831db0d5f7ae251.tar.bz2 serd-904d3d9b0bcdbb1507adccdde831db0d5f7ae251.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/sink.c | 10 | ||||
-rw-r--r-- | src/sink.h | 1 | ||||
-rw-r--r-- | src/writer.c | 19 |
3 files changed, 20 insertions, 10 deletions
@@ -24,11 +24,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; } @@ -38,6 +40,12 @@ serd_sink_free(SerdSink* sink) free(sink); } +SerdEnv* +serd_sink_get_env(SerdSink* sink) +{ + return sink->env; +} + SerdStatus serd_sink_set_base_func(SerdSink* sink, SerdBaseSink base_func) { @@ -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 48979423..ad9d3ccc 100644 --- a/src/writer.c +++ b/src/writer.c @@ -104,7 +104,6 @@ struct SerdWriterImpl { SerdSink iface; SerdSyntax syntax; SerdWriterFlags flags; - SerdEnv* env; SerdNode* root_node; SerdURI root_uri; SerdStack anon_stack; @@ -519,6 +518,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); @@ -536,7 +537,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(suffix.buf, suffix.len)) { write_uri_from_node(writer, prefix); sink(":", 1, writer); @@ -545,8 +546,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); @@ -585,7 +586,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", @@ -915,7 +916,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); @@ -925,6 +925,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; @@ -951,7 +952,7 @@ SerdStatus serd_writer_set_base_uri(SerdWriter* writer, const SerdNode* uri) { - if (!serd_env_set_base_uri(writer->env, uri)) { + if (!serd_env_set_base_uri(writer->iface.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); @@ -988,7 +989,7 @@ serd_writer_set_prefix(SerdWriter* writer, const SerdNode* name, const SerdNode* uri) { - if (!serd_env_set_prefix(writer->env, name, uri)) { + if (!serd_env_set_prefix(writer->iface.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); @@ -1025,7 +1026,7 @@ serd_writer_get_sink(SerdWriter* writer) SerdEnv* serd_writer_get_env(SerdWriter* writer) { - return writer->env; + return writer->iface.env; } size_t |