aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-12-29 17:36:15 -0500
committerDavid Robillard <d@drobilla.net>2018-12-31 11:37:49 -0500
commit904d3d9b0bcdbb1507adccdde831db0d5f7ae251 (patch)
treebe485f9684819534972a40ced925138f5e4d850c
parentbeb2eba61195d6ef838cd3bfd363ae74785aaff9 (diff)
downloadserd-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.
-rw-r--r--serd/serd.h10
-rw-r--r--src/sink.c10
-rw-r--r--src/sink.h1
-rw-r--r--src/writer.c19
-rw-r--r--tests/overflow_test.c2
-rw-r--r--tests/read_chunk_test.c2
-rw-r--r--tests/serd_test.c4
7 files changed, 33 insertions, 15 deletions
diff --git a/serd/serd.h b/serd/serd.h
index 0346d32e..c0b22504 100644
--- a/serd/serd.h
+++ b/serd/serd.h
@@ -1025,10 +1025,11 @@ serd_env_send_prefixes(const SerdEnv* env, SerdSink* sink);
serd_sink_set_*_func functions to set handlers for various events.
@param handle Opaque handle that will be passed to sink functions.
+ @param env Environment for sink, updated as base uri or prefixes change.
*/
SERD_API
SerdSink*
-serd_sink_new(void* handle);
+serd_sink_new(void* handle, SerdEnv* env);
/**
Free `sink`.
@@ -1038,6 +1039,13 @@ void
serd_sink_free(SerdSink* sink);
/**
+ Return the env used by `writer`.
+*/
+SERD_API
+SerdEnv*
+serd_sink_get_env(SerdSink* sink);
+
+/**
Set a function to be called when the base URI changes.
*/
SERD_API
diff --git a/src/sink.c b/src/sink.c
index 0f6faf85..b7e93768 100644
--- a/src/sink.c
+++ b/src/sink.c
@@ -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)
{
diff --git a/src/sink.h b/src/sink.h
index 03523370..bab46ef6 100644
--- a/src/sink.h
+++ b/src/sink.h
@@ -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
diff --git a/tests/overflow_test.c b/tests/overflow_test.c
index d3a1816e..5c2c4ef1 100644
--- a/tests/overflow_test.c
+++ b/tests/overflow_test.c
@@ -59,7 +59,7 @@ main(void)
{NULL, 0}};
SerdWorld* world = serd_world_new();
- SerdSink* sink = serd_sink_new(NULL);
+ SerdSink* sink = serd_sink_new(NULL, NULL);
for (const Test* t = tests; t->str; ++t) {
const SerdStatus st = test(world, sink, t->str, t->stack_size);
diff --git a/tests/read_chunk_test.c b/tests/read_chunk_test.c
index f02fbe92..264bb0e8 100644
--- a/tests/read_chunk_test.c
+++ b/tests/read_chunk_test.c
@@ -73,7 +73,7 @@ int
main(void)
{
SerdWorld* world = serd_world_new();
- SerdSink* sink = serd_sink_new(NULL);
+ SerdSink* sink = serd_sink_new(NULL, NULL);
serd_sink_set_base_func(sink, on_base);
serd_sink_set_prefix_func(sink, on_prefix);
serd_sink_set_statement_func(sink, on_statement);
diff --git a/tests/serd_test.c b/tests/serd_test.c
index 640d81dc..86911b0c 100644
--- a/tests/serd_test.c
+++ b/tests/serd_test.c
@@ -451,7 +451,7 @@ main(void)
assert(serd_env_set_prefix(env, b, lit));
size_t n_prefixes = 0;
- SerdSink* count_prefixes_sink = serd_sink_new(&n_prefixes);
+ SerdSink* count_prefixes_sink = serd_sink_new(&n_prefixes, NULL);
serd_sink_set_prefix_func(count_prefixes_sink, count_prefixes);
serd_env_set_prefix(env, pre, eg);
serd_env_send_prefixes(env, count_prefixes_sink);
@@ -600,7 +600,7 @@ main(void)
fseek(fd, 0, SEEK_SET);
size_t n_statements = 0;
- SerdSink* sink = serd_sink_new(&n_statements);
+ SerdSink* sink = serd_sink_new(&n_statements, NULL);
serd_sink_set_statement_func(sink, count_statements);
SerdReader* reader = serd_reader_new(world, SERD_TURTLE, sink, 4096);