From 904d3d9b0bcdbb1507adccdde831db0d5f7ae251 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 29 Dec 2018 17:36:15 -0500 Subject: 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. --- src/writer.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'src/writer.c') 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 -- cgit v1.2.1