diff options
author | David Robillard <d@drobilla.net> | 2018-04-29 18:29:03 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2021-03-07 15:32:24 -0500 |
commit | 471ef4960aba6b5e1c9445229cddc0b3eb9ca148 (patch) | |
tree | ae6ff30025ec510624c50aa6364a9171aca879bc /src/writer.c | |
parent | 941b14a0ab8f7c80f94e04762e65a48f9ed02f6e (diff) | |
download | serd-471ef4960aba6b5e1c9445229cddc0b3eb9ca148.tar.gz serd-471ef4960aba6b5e1c9445229cddc0b3eb9ca148.tar.bz2 serd-471ef4960aba6b5e1c9445229cddc0b3eb9ca148.zip |
Simplify node construction API
Diffstat (limited to 'src/writer.c')
-rw-r--r-- | src/writer.c | 51 |
1 files changed, 26 insertions, 25 deletions
diff --git a/src/writer.c b/src/writer.c index 5205c7d1..75734c0c 100644 --- a/src/writer.c +++ b/src/writer.c @@ -15,6 +15,7 @@ */ #include "byte_sink.h" +#include "env.h" #include "node.h" #include "serd_internal.h" #include "stack.h" @@ -96,7 +97,6 @@ struct SerdWriterImpl { SerdEnv* env; SerdNode* root_node; SerdURIView root_uri; - SerdURIView base_uri; SerdStack anon_stack; SerdByteSink byte_sink; SerdErrorSink error_sink; @@ -604,7 +604,7 @@ write_uri_node(SerdWriter* const writer, } if (!has_scheme && !supports_uriref(writer) && - !serd_env_base_uri(writer->env, NULL)) { + !serd_env_base_uri(writer->env)) { w_err(writer, SERD_ERR_BAD_ARG, "syntax does not support URI reference <%s>\n", @@ -613,22 +613,18 @@ write_uri_node(SerdWriter* const writer, } write_sep(writer, SEP_URI_BEGIN); - if (writer->flags & SERD_WRITE_RESOLVED) { - const SerdURIView uri = serd_parse_uri(node_str); - - SerdURIView in_base_uri; - serd_env_base_uri(writer->env, &in_base_uri); - - const SerdURIView abs_uri = serd_resolve_uri(uri, in_base_uri); - const bool rooted = uri_is_under(&in_base_uri, &writer->root_uri); - const SerdURIView* root = rooted ? &writer->root_uri : &in_base_uri; + if ((writer->flags & SERD_WRITE_RESOLVED) && serd_env_base_uri(writer->env)) { + const SerdURIView base_uri = serd_env_base_uri_view(writer->env); + SerdURIView uri = serd_parse_uri(node_str); + SerdURIView abs_uri = serd_resolve_uri(uri, base_uri); + bool rooted = uri_is_under(&base_uri, &writer->root_uri); + const SerdURIView* root = rooted ? &writer->root_uri : &base_uri; if (writer->syntax == SERD_NTRIPLES || writer->syntax == SERD_NQUADS || - !uri_is_under(&abs_uri, root) || - !uri_is_related(&abs_uri, &in_base_uri)) { + !uri_is_under(&abs_uri, root) || !uri_is_related(&abs_uri, &base_uri)) { serd_write_uri(abs_uri, uri_sink, writer); } else { - serd_write_uri(serd_relative_uri(uri, in_base_uri), uri_sink, writer); + serd_write_uri(serd_relative_uri(uri, base_uri), uri_sink, writer); } } else { write_uri_from_node(writer, node); @@ -957,12 +953,11 @@ serd_writer_finish(SerdWriter* writer) } SerdWriter* -serd_writer_new(SerdSyntax syntax, - SerdWriterFlags flags, - SerdEnv* env, - const SerdURIView* base_uri, - SerdSink ssink, - void* stream) +serd_writer_new(SerdSyntax syntax, + SerdWriterFlags flags, + SerdEnv* env, + SerdSink ssink, + void* stream) { const WriteContext context = WRITE_CONTEXT_NULL; SerdWriter* writer = (SerdWriter*)calloc(1, sizeof(SerdWriter)); @@ -972,7 +967,6 @@ serd_writer_new(SerdSyntax syntax, writer->env = env; writer->root_node = NULL; writer->root_uri = SERD_URI_NULL; - writer->base_uri = base_uri ? *base_uri : SERD_URI_NULL; writer->anon_stack = serd_stack_new(4 * sizeof(WriteContext)); writer->context = context; writer->list_subj = NULL; @@ -1010,9 +1004,11 @@ serd_writer_chop_blank_prefix(SerdWriter* writer, const char* prefix) SerdStatus serd_writer_set_base_uri(SerdWriter* writer, const SerdNode* uri) { - if (!serd_env_set_base_uri(writer->env, uri)) { - serd_env_base_uri(writer->env, &writer->base_uri); + if (uri->type != SERD_URI) { + return SERD_ERR_BAD_ARG; + } + if (!serd_env_set_base_uri(writer->env, serd_node_string_view(uri))) { if (writer->syntax == SERD_TURTLE || writer->syntax == SERD_TRIG) { if (ctx(writer, FIELD_GRAPH) || ctx(writer, FIELD_SUBJECT)) { sink(" .\n\n", 4, writer); @@ -1034,7 +1030,7 @@ serd_writer_set_root_uri(SerdWriter* writer, const SerdNode* uri) serd_node_free(writer->root_node); if (uri) { writer->root_node = serd_node_copy(uri); - writer->root_uri = serd_parse_uri(serd_node_string(writer->root_node)); + writer->root_uri = serd_node_uri_view(writer->root_node); } else { writer->root_node = NULL; writer->root_uri = SERD_URI_NULL; @@ -1047,7 +1043,12 @@ serd_writer_set_prefix(SerdWriter* writer, const SerdNode* name, const SerdNode* uri) { - if (!serd_env_set_prefix(writer->env, name, uri)) { + if (name->type != SERD_LITERAL || uri->type != SERD_URI) { + return SERD_ERR_BAD_ARG; + } + + if (!serd_env_set_prefix( + writer->env, serd_node_string_view(name), serd_node_string_view(uri))) { if (writer->syntax == SERD_TURTLE || writer->syntax == SERD_TRIG) { if (ctx(writer, FIELD_GRAPH) || ctx(writer, FIELD_SUBJECT)) { sink(" .\n\n", 4, writer); |