diff options
author | David Robillard <d@drobilla.net> | 2018-04-29 18:29:03 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 16:27:02 -0500 |
commit | 5c02da92038072f28408854e862fc2d4edf765d4 (patch) | |
tree | 34577415801279981b626b18c1dc69dc79afea7f /src/writer.c | |
parent | 672e90382da08efa8f593fdc9081e31d0e548fa0 (diff) | |
download | serd-5c02da92038072f28408854e862fc2d4edf765d4.tar.gz serd-5c02da92038072f28408854e862fc2d4edf765d4.tar.bz2 serd-5c02da92038072f28408854e862fc2d4edf765d4.zip |
Simplify node construction API
Diffstat (limited to 'src/writer.c')
-rw-r--r-- | src/writer.c | 42 |
1 files changed, 26 insertions, 16 deletions
diff --git a/src/writer.c b/src/writer.c index 5aad6d29..cea75aec 100644 --- a/src/writer.c +++ b/src/writer.c @@ -2,6 +2,7 @@ // SPDX-License-Identifier: ISC #include "byte_sink.h" +#include "env.h" #include "node.h" #include "serd_internal.h" #include "stack.h" @@ -759,7 +760,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)) { return w_err(writer, SERD_BAD_ARG, "syntax does not support URI reference <%s>\n", @@ -768,14 +769,13 @@ write_uri_node(SerdWriter* const writer, TRY(st, esink("<", 1, writer)); - SerdURIView base_uri = SERD_URI_NULL; - if ((writer->flags & SERD_WRITE_RESOLVED) && - serd_env_base_uri(writer->env, &base_uri)) { - 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; - UriSinkContext ctx = {writer, SERD_SUCCESS}; + 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; + UriSinkContext ctx = {writer, SERD_SUCCESS}; if (!supports_abbrev(writer) || !uri_is_under(&abs_uri, root)) { serd_write_uri(abs_uri, uri_sink, &ctx); @@ -1126,7 +1126,6 @@ SerdWriter* serd_writer_new(SerdSyntax syntax, SerdWriterFlags flags, SerdEnv* env, - const SerdNode* base_uri, SerdSink ssink, void* stream) { @@ -1143,7 +1142,6 @@ serd_writer_new(SerdSyntax syntax, writer->byte_sink = serd_byte_sink_new( ssink, stream, (flags & SERD_WRITE_BULK) ? SERD_PAGE_SIZE : 1); - serd_env_set_base_uri(writer->env, base_uri); return writer; } @@ -1174,14 +1172,24 @@ serd_writer_chop_blank_prefix(SerdWriter* writer, const char* prefix) SerdStatus serd_writer_set_base_uri(SerdWriter* writer, const SerdNode* uri) { - SerdStatus st = SERD_SUCCESS; + if (uri && serd_node_type(uri) != SERD_URI) { + return SERD_BAD_ARG; + } + + if (serd_node_equals(serd_env_base_uri(writer->env), uri)) { + return SERD_SUCCESS; + } - TRY(st, serd_env_set_base_uri(writer->env, uri)); + const SerdStringView uri_string = + uri ? serd_node_string_view(uri) : serd_empty_string(); + + SerdStatus st = SERD_SUCCESS; + TRY(st, serd_env_set_base_uri(writer->env, uri_string)); if (uri && (writer->syntax == SERD_TURTLE || writer->syntax == SERD_TRIG)) { TRY(st, terminate_context(writer)); TRY(st, esink("@base <", 7, writer)); - TRY(st, esink(serd_node_string(uri), serd_node_length(uri), writer)); + TRY(st, esink(uri_string.data, uri_string.length, writer)); TRY(st, esink(">", 1, writer)); writer->last_sep = SEP_NODE; TRY(st, write_sep(writer, SEP_END_DIRECT)); @@ -1199,7 +1207,7 @@ serd_writer_set_root_uri(SerdWriter* writer, const SerdNode* uri) 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); } return SERD_SUCCESS; @@ -1212,7 +1220,9 @@ serd_writer_set_prefix(SerdWriter* writer, { SerdStatus st = SERD_SUCCESS; - TRY(st, serd_env_set_prefix(writer->env, name, uri)); + TRY(st, + 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) { TRY(st, terminate_context(writer)); |