diff options
author | David Robillard <d@drobilla.net> | 2023-03-27 05:39:26 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 18:49:08 -0500 |
commit | 7ffa2c0488fcd96c3c12713e5650633eb03e91f7 (patch) | |
tree | 9ca96bb40f71b6c37789ae641af75e39a2751c18 /src | |
parent | 5e4538756d601e6a941c5290777af95ea8848e1a (diff) | |
download | serd-7ffa2c0488fcd96c3c12713e5650633eb03e91f7.tar.gz serd-7ffa2c0488fcd96c3c12713e5650633eb03e91f7.tar.bz2 serd-7ffa2c0488fcd96c3c12713e5650633eb03e91f7.zip |
[WIP] Move SerdEnv mutation from writer to reader
Writing having side-effects seems questionable in general, and this prepares
things for expanding URIs in the reader.
Diffstat (limited to 'src')
-rw-r--r-- | src/read_turtle.c | 7 | ||||
-rw-r--r-- | src/reader.c | 3 | ||||
-rw-r--r-- | src/reader.h | 2 | ||||
-rw-r--r-- | src/writer.c | 21 |
4 files changed, 20 insertions, 13 deletions
diff --git a/src/read_turtle.c b/src/read_turtle.c index 22269741..8d9ec78a 100644 --- a/src/read_turtle.c +++ b/src/read_turtle.c @@ -14,6 +14,7 @@ #include "turtle.h" #include "serd/caret.h" +#include "serd/env.h" #include "serd/node.h" #include "serd/reader.h" #include "serd/sink.h" @@ -891,6 +892,7 @@ read_turtle_base(SerdReader* const reader, const bool sparql, const bool token) } serd_node_zero_pad(uri); + TRY(st, serd_env_set_base_uri(reader->env, serd_node_string_view(uri))); TRY(st, serd_sink_write_base(reader->sink, uri)); read_turtle_ws_star(reader); @@ -934,6 +936,11 @@ read_turtle_prefixID(SerdReader* const reader, serd_node_zero_pad(name); serd_node_zero_pad(uri); + + TRY(st, + serd_env_set_prefix( + reader->env, serd_node_string_view(name), serd_node_string_view(uri))); + st = serd_sink_write_prefix(reader->sink, name, uri); if (!sparql) { diff --git a/src/reader.c b/src/reader.c index 7e0864cc..8b74836f 100644 --- a/src/reader.c +++ b/src/reader.c @@ -229,9 +229,11 @@ SerdReader* serd_reader_new(SerdWorld* const world, const SerdSyntax syntax, const SerdReaderFlags flags, + SerdEnv* const env, const SerdSink* const sink) { assert(world); + assert(env); assert(sink); const size_t stack_size = world->limits.reader_stack_size; @@ -243,6 +245,7 @@ serd_reader_new(SerdWorld* const world, me->world = world; me->sink = sink; + me->env = env; me->stack = serd_stack_new(stack_size, serd_node_align); me->syntax = syntax; me->flags = flags; diff --git a/src/reader.h b/src/reader.h index 132d038e..302f8c6f 100644 --- a/src/reader.h +++ b/src/reader.h @@ -11,6 +11,7 @@ #include "serd/attributes.h" #include "serd/caret.h" +#include "serd/env.h" #include "serd/node.h" #include "serd/reader.h" #include "serd/sink.h" @@ -40,6 +41,7 @@ struct SerdReaderImpl { SerdNode* rdf_rest; SerdNode* rdf_nil; SerdByteSource* source; + SerdEnv* env; SerdStack stack; SerdSyntax syntax; SerdReaderFlags flags; diff --git a/src/writer.c b/src/writer.c index 329a29ad..05be0500 100644 --- a/src/writer.c +++ b/src/writer.c @@ -131,7 +131,7 @@ struct SerdWriterImpl { SerdSink iface; SerdSyntax syntax; SerdWriterFlags flags; - SerdEnv* env; + const SerdEnv* env; SerdNode* root_node; SerdURIView root_uri; WriteContext* anon_stack; @@ -1367,7 +1367,7 @@ SerdWriter* serd_writer_new(SerdWorld* world, SerdSyntax syntax, SerdWriterFlags flags, - SerdEnv* env, + const SerdEnv* env, SerdOutputStream* output, size_t block_size) { @@ -1426,20 +1426,15 @@ serd_writer_set_base_uri(SerdWriter* writer, const SerdNode* uri) { assert(writer); + 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; - } - 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)); @@ -1476,16 +1471,16 @@ serd_writer_set_prefix(SerdWriter* writer, { SerdStatus st = SERD_SUCCESS; - TRY(st, - serd_env_set_prefix( - writer->env, serd_node_string_view(name), serd_node_string_view(uri))); + if (name->type != SERD_LITERAL || uri->type != SERD_URI) { + return SERD_BAD_ARG; + } if (writer->syntax == SERD_TURTLE || writer->syntax == SERD_TRIG) { TRY(st, terminate_context(writer)); TRY(st, esink("@prefix ", 8, writer)); TRY(st, esink(serd_node_string(name), name->length, writer)); TRY(st, esink(": <", 3, writer)); - TRY(st, ewrite_uri(writer, serd_node_string(uri), uri->length)); + TRY(st, write_uri_from_node(writer, uri)); TRY(st, esink(">", 1, writer)); writer->last_sep = SEP_NODE; TRY(st, write_sep(writer, writer->context.flags, SEP_END_DIRECT)); |