diff options
-rw-r--r-- | include/serd/serd.h | 139 | ||||
-rw-r--r-- | meson.build | 1 | ||||
-rw-r--r-- | src/n3.c | 15 | ||||
-rw-r--r-- | src/reader.c | 51 | ||||
-rw-r--r-- | src/reader.h | 37 | ||||
-rw-r--r-- | src/serdi.c | 8 | ||||
-rw-r--r-- | src/sink.c | 106 | ||||
-rw-r--r-- | src/sink.h | 34 | ||||
-rw-r--r-- | src/writer.c | 23 | ||||
-rw-r--r-- | test/test_free_null.c | 1 | ||||
-rw-r--r-- | test/test_read_chunk.c | 10 | ||||
-rw-r--r-- | test/test_reader_writer.c | 63 |
12 files changed, 334 insertions, 154 deletions
diff --git a/include/serd/serd.h b/include/serd/serd.h index 08b81269..9c205d26 100644 --- a/include/serd/serd.h +++ b/include/serd/serd.h @@ -77,12 +77,15 @@ extern "C" { /// Lexical environment for relative URIs or CURIEs (base URI and namespaces) typedef struct SerdEnvImpl SerdEnv; -/// Streaming parser that reads a text stream and writes to a statement sink +/// Streaming parser that reads a text stream and writes to a sink typedef struct SerdReaderImpl SerdReader; /// Streaming serialiser that writes a text stream as statements are pushed typedef struct SerdWriterImpl SerdWriter; +/// An interface that receives a stream of RDF data +typedef struct SerdSinkImpl SerdSink; + /// Return status code typedef enum { SERD_SUCCESS, ///< No error @@ -812,6 +815,87 @@ serd_env_foreach(const SerdEnv* SERD_NONNULL env, /** @} + @defgroup serd_sink Sink + @{ +*/ + +/// Function to free an opaque handle +typedef void (*SerdFreeFunc)(void* SERD_NULLABLE ptr); + +/** + Create a new sink. + + Initially, the sink has no set functions and will do nothing. Use the + serd_sink_set_*_func functions to set handlers for various events. + + @param handle Opaque handle that will be passed to sink functions. + @param free_handle Free function to call on handle in serd_sink_free(). +*/ +SERD_API +SerdSink* SERD_ALLOCATED +serd_sink_new(void* SERD_NULLABLE handle, + SerdFreeFunc SERD_NULLABLE free_handle); + +/// Free `sink` +SERD_API +void +serd_sink_free(SerdSink* SERD_NULLABLE sink); + +/// Set a function to be called when the base URI changes +SERD_API +SerdStatus +serd_sink_set_base_func(SerdSink* SERD_NONNULL sink, + SerdBaseFunc SERD_NULLABLE base_func); + +/// Set a function to be called when a namespace prefix is defined +SERD_API +SerdStatus +serd_sink_set_prefix_func(SerdSink* SERD_NONNULL sink, + SerdPrefixFunc SERD_NULLABLE prefix_func); + +/// Set a function to be called when a statement is emitted +SERD_API +SerdStatus +serd_sink_set_statement_func(SerdSink* SERD_NONNULL sink, + SerdStatementFunc SERD_NULLABLE statement_func); + +/// Set a function to be called when an anonymous node ends +SERD_API +SerdStatus +serd_sink_set_end_func(SerdSink* SERD_NONNULL sink, + SerdEndFunc SERD_NULLABLE end_func); + +/// Set the base URI +SERD_API +SerdStatus +serd_sink_write_base(const SerdSink* SERD_NONNULL sink, + const SerdNode* SERD_NONNULL uri); + +/// Set a namespace prefix +SERD_API +SerdStatus +serd_sink_write_prefix(const SerdSink* SERD_NONNULL sink, + const SerdNode* SERD_NONNULL name, + const SerdNode* SERD_NONNULL uri); + +/// Write a statement from individual nodes +SERD_API +SerdStatus +serd_sink_write(const SerdSink* SERD_NONNULL sink, + SerdStatementFlags flags, + const SerdNode* SERD_NONNULL subject, + const SerdNode* SERD_NONNULL predicate, + const SerdNode* SERD_NONNULL object, + const SerdNode* SERD_NULLABLE graph); + +/// Mark the end of an anonymous node +SERD_API +SerdStatus +serd_sink_write_end(const SerdSink* SERD_NONNULL sink, + const SerdNode* SERD_NONNULL node); + +/** + @} @defgroup serd_reader Reader @{ */ @@ -819,13 +903,7 @@ serd_env_foreach(const SerdEnv* SERD_NONNULL env, /// Create a new RDF reader SERD_API SerdReader* SERD_ALLOCATED -serd_reader_new(SerdSyntax syntax, - void* SERD_NULLABLE handle, - void (*SERD_NULLABLE free_handle)(void* SERD_NULLABLE), - SerdBaseFunc SERD_NULLABLE base_func, - SerdPrefixFunc SERD_NULLABLE prefix_func, - SerdStatementFunc SERD_NULLABLE statement_func, - SerdEndFunc SERD_NULLABLE end_func); +serd_reader_new(SerdSyntax syntax, const SerdSink* SERD_NONNULL sink); /** Enable or disable strict parsing @@ -850,11 +928,6 @@ serd_reader_set_error_sink(SerdReader* SERD_NONNULL reader, SerdErrorFunc SERD_NULLABLE error_func, void* SERD_NULLABLE error_handle); -/// Return the `handle` passed to serd_reader_new() -SERD_PURE_API -void* SERD_NULLABLE -serd_reader_handle(const SerdReader* SERD_NONNULL reader); - /** Set a prefix to be added to all blank node identifiers. @@ -967,6 +1040,11 @@ SERD_API void serd_writer_free(SerdWriter* SERD_NULLABLE writer); +/// Return a sink interface that emits statements via `writer` +SERD_CONST_API +const SerdSink* SERD_NONNULL +serd_writer_sink(SerdWriter* SERD_NONNULL writer); + /// Return the env used by `writer` SERD_PURE_API SerdEnv* SERD_NONNULL @@ -1046,41 +1124,6 @@ serd_writer_set_root_uri(SerdWriter* SERD_NONNULL writer, const SerdNode* SERD_NULLABLE uri); /** - Set a namespace prefix (and emit directive if applicable). - - Note this function can be safely casted to SerdPrefixSink. -*/ -SERD_API -SerdStatus -serd_writer_set_prefix(SerdWriter* SERD_NONNULL writer, - const SerdNode* SERD_NONNULL name, - const SerdNode* SERD_NONNULL uri); - -/** - Write a statement. - - Note this function can be safely casted to SerdStatementSink. -*/ -SERD_API -SerdStatus -serd_writer_write_statement(SerdWriter* SERD_NONNULL writer, - SerdStatementFlags flags, - const SerdNode* SERD_NULLABLE graph, - const SerdNode* SERD_NONNULL subject, - const SerdNode* SERD_NONNULL predicate, - const SerdNode* SERD_NONNULL object); - -/** - Mark the end of an anonymous node's description. - - Note this function can be safely casted to SerdEndSink. -*/ -SERD_API -SerdStatus -serd_writer_end_anon(SerdWriter* SERD_NONNULL writer, - const SerdNode* SERD_NULLABLE node); - -/** Finish a write This flushes any pending output, for example terminating punctuation, so diff --git a/meson.build b/meson.build index fadc0aa6..a59ff31d 100644 --- a/meson.build +++ b/meson.build @@ -96,6 +96,7 @@ sources = [ 'src/n3.c', 'src/node.c', 'src/reader.c', + 'src/sink.c', 'src/string.c', 'src/system.c', 'src/uri.c', @@ -1091,9 +1091,7 @@ read_anon(SerdReader* reader, ReadContext ctx, bool subject, Ref* dest) return r_err(reader, SERD_ERR_BAD_SYNTAX, "`.' inside blank\n"); } read_ws_star(reader); - if (reader->end_func) { - reader->end_func(reader->handle, deref(reader, *dest)); - } + serd_sink_write_end(reader->sink, deref(reader, *dest)); *ctx.flags = old_flags; } return (eat_byte_check(reader, ']') == ']') ? SERD_SUCCESS @@ -1432,9 +1430,7 @@ read_base(SerdReader* reader, bool sparql, bool token) Ref uri = 0; TRY(st, read_IRIREF(reader, &uri)); - if (reader->base_func) { - TRY(st, reader->base_func(reader->handle, deref(reader, uri))); - } + TRY(st, serd_sink_write_base(reader->sink, deref(reader, uri))); pop_node(reader, uri); read_ws_star(reader); @@ -1472,10 +1468,8 @@ read_prefixID(SerdReader* reader, bool sparql, bool token) Ref uri = 0; TRY(st, read_IRIREF(reader, &uri)); - if (reader->prefix_func) { - st = reader->prefix_func( - reader->handle, deref(reader, name), deref(reader, uri)); - } + st = serd_sink_write_prefix( + reader->sink, deref(reader, name), deref(reader, uri)); pop_node(reader, uri); pop_node(reader, name); @@ -1483,7 +1477,6 @@ read_prefixID(SerdReader* reader, bool sparql, bool token) read_ws_star(reader); st = eat_byte_check(reader, '.') ? SERD_SUCCESS : SERD_ERR_BAD_SYNTAX; } - return st; } diff --git a/src/reader.c b/src/reader.c index d5de67d2..805d41a7 100644 --- a/src/reader.c +++ b/src/reader.c @@ -132,14 +132,12 @@ emit_statement(SerdReader* reader, ReadContext ctx, Ref o) graph = reader->default_graph; } - const SerdStatus st = !reader->statement_func - ? SERD_SUCCESS - : reader->statement_func(reader->handle, - *ctx.flags, - graph, - deref(reader, ctx.subject), - deref(reader, ctx.predicate), - deref(reader, o)); + const SerdStatus st = serd_sink_write(reader->sink, + *ctx.flags, + deref(reader, ctx.subject), + deref(reader, ctx.predicate), + deref(reader, o), + graph); *ctx.flags &= SERD_ANON_CONT | SERD_LIST_CONT; // Preserve only cont flags return st; @@ -166,26 +164,16 @@ serd_reader_read_document(SerdReader* reader) } SerdReader* -serd_reader_new(SerdSyntax syntax, - void* handle, - void (*free_handle)(void*), - SerdBaseFunc base_func, - SerdPrefixFunc prefix_func, - SerdStatementFunc statement_func, - SerdEndFunc end_func) +serd_reader_new(SerdSyntax syntax, const SerdSink* sink) { - SerdReader* me = (SerdReader*)calloc(1, sizeof(SerdReader)); - me->handle = handle; - me->free_handle = free_handle; - me->base_func = base_func; - me->prefix_func = prefix_func; - me->statement_func = statement_func; - me->end_func = end_func; - me->default_graph = NULL; - me->stack = serd_stack_new(SERD_PAGE_SIZE); - me->syntax = syntax; - me->next_id = 1; - me->strict = true; + SerdReader* me = (SerdReader*)calloc(1, sizeof(SerdReader)); + + me->sink = sink; + me->default_graph = NULL; + me->stack = serd_stack_new(SERD_PAGE_SIZE); + me->syntax = syntax; + me->next_id = 1; + me->strict = true; me->rdf_first = push_node(me, SERD_URI, NS_RDF "first", 48); me->rdf_rest = push_node(me, SERD_URI, NS_RDF "rest", 47); @@ -226,18 +214,9 @@ serd_reader_free(SerdReader* reader) #endif free(reader->stack.buf); free(reader->bprefix); - if (reader->free_handle) { - reader->free_handle(reader->handle); - } free(reader); } -void* -serd_reader_handle(const SerdReader* reader) -{ - return reader->handle; -} - void serd_reader_add_blank_prefix(SerdReader* reader, const char* prefix) { diff --git a/src/reader.h b/src/reader.h index 8df16da3..c34f3a8b 100644 --- a/src/reader.h +++ b/src/reader.h @@ -57,27 +57,22 @@ typedef struct { } ReadContext; struct SerdReaderImpl { - void* handle; - void (*free_handle)(void* ptr); - SerdBaseFunc base_func; - SerdPrefixFunc prefix_func; - SerdStatementFunc statement_func; - SerdEndFunc end_func; - SerdErrorFunc error_func; - void* error_handle; - Ref rdf_first; - Ref rdf_rest; - Ref rdf_nil; - SerdNode* default_graph; - SerdByteSource source; - SerdStack stack; - SerdSyntax syntax; - unsigned next_id; - uint8_t* buf; - char* bprefix; - size_t bprefix_len; - bool strict; ///< True iff strict parsing - bool seen_genid; + const SerdSink* sink; + SerdErrorFunc error_func; + void* error_handle; + Ref rdf_first; + Ref rdf_rest; + Ref rdf_nil; + SerdNode* default_graph; + SerdByteSource source; + SerdStack stack; + SerdSyntax syntax; + unsigned next_id; + uint8_t* buf; + char* bprefix; + size_t bprefix_len; + bool strict; ///< True iff strict parsing + bool seen_genid; #ifdef SERD_STACK_CHECK Ref* allocs; ///< Stack of push offsets size_t n_allocs; ///< Number of stack pushes diff --git a/src/serdi.c b/src/serdi.c index c54d82e6..e7b8d7ba 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -340,13 +340,7 @@ main(int argc, char** argv) output_syntax, writer_flags, env, (SerdWriteFunc)fwrite, out_fd); SerdReader* const reader = - serd_reader_new(input_syntax, - writer, - NULL, - (SerdBaseFunc)serd_writer_set_base_uri, - (SerdPrefixFunc)serd_writer_set_prefix, - (SerdStatementFunc)serd_writer_write_statement, - (SerdEndFunc)serd_writer_end_anon); + serd_reader_new(input_syntax, serd_writer_sink(writer)); serd_reader_set_strict(reader, !lax); if (quiet) { diff --git a/src/sink.c b/src/sink.c new file mode 100644 index 00000000..b62af23a --- /dev/null +++ b/src/sink.c @@ -0,0 +1,106 @@ +/* + Copyright 2011-2020 David Robillard <d@drobilla.net> + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "sink.h" + +#include "serd/serd.h" + +#include <stdlib.h> + +SerdSink* +serd_sink_new(void* handle, SerdFreeFunc free_handle) +{ + SerdSink* sink = (SerdSink*)calloc(1, sizeof(SerdSink)); + + sink->handle = handle; + sink->free_handle = free_handle; + + return sink; +} + +void +serd_sink_free(SerdSink* sink) +{ + if (sink) { + if (sink->free_handle) { + sink->free_handle(sink->handle); + } + + free(sink); + } +} + +SerdStatus +serd_sink_set_base_func(SerdSink* sink, SerdBaseFunc base_func) +{ + sink->base = base_func; + return SERD_SUCCESS; +} + +SerdStatus +serd_sink_set_prefix_func(SerdSink* sink, SerdPrefixFunc prefix_func) +{ + sink->prefix = prefix_func; + return SERD_SUCCESS; +} + +SerdStatus +serd_sink_set_statement_func(SerdSink* sink, SerdStatementFunc statement_func) +{ + sink->statement = statement_func; + return SERD_SUCCESS; +} + +SerdStatus +serd_sink_set_end_func(SerdSink* sink, SerdEndFunc end_func) +{ + sink->end = end_func; + return SERD_SUCCESS; +} + +SerdStatus +serd_sink_write_base(const SerdSink* sink, const SerdNode* uri) +{ + return sink->base ? sink->base(sink->handle, uri) : SERD_SUCCESS; +} + +SerdStatus +serd_sink_write_prefix(const SerdSink* sink, + const SerdNode* name, + const SerdNode* uri) +{ + return sink->prefix ? sink->prefix(sink->handle, name, uri) : SERD_SUCCESS; +} + +SerdStatus +serd_sink_write(const SerdSink* sink, + const SerdStatementFlags flags, + const SerdNode* subject, + const SerdNode* predicate, + const SerdNode* object, + const SerdNode* graph) +{ + return sink->statement + ? sink->statement( + sink->handle, flags, graph, subject, predicate, object) + : SERD_SUCCESS; +} + +SerdStatus +serd_sink_write_end(const SerdSink* sink, const SerdNode* node) +{ + return sink->end ? sink->end(sink->handle, node) : SERD_SUCCESS; +} diff --git a/src/sink.h b/src/sink.h new file mode 100644 index 00000000..e0f99a53 --- /dev/null +++ b/src/sink.h @@ -0,0 +1,34 @@ +/* + Copyright 2011-2020 David Robillard <d@drobilla.net> + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef SERD_SINK_H +#define SERD_SINK_H + +#include "serd/serd.h" + +/** + An interface that receives a stream of RDF data. +*/ +struct SerdSinkImpl { + void* handle; + SerdFreeFunc free_handle; + SerdBaseFunc base; + SerdPrefixFunc prefix; + SerdStatementFunc statement; + SerdEndFunc end; +}; + +#endif // SERD_SINK_H diff --git a/src/writer.c b/src/writer.c index e23058f0..2b2d5ed8 100644 --- a/src/writer.c +++ b/src/writer.c @@ -18,6 +18,7 @@ #include "env.h" #include "node.h" #include "serd_internal.h" +#include "sink.h" #include "stack.h" #include "string_utils.h" #include "uri_utils.h" @@ -92,6 +93,7 @@ static const SepRule rules[] = {{NULL, 0, 0, 0, 0}, {"\n", 1, 0, 1, 0}}; struct SerdWriterImpl { + SerdSink iface; SerdSyntax syntax; SerdWriterFlags flags; SerdEnv* env; @@ -113,6 +115,11 @@ struct SerdWriterImpl { typedef enum { WRITE_STRING, WRITE_LONG_STRING } TextContext; +static SerdStatus +serd_writer_set_prefix(SerdWriter* writer, + const SerdNode* name, + const SerdNode* uri); + static bool write_node(SerdWriter* writer, const SerdNode* node, @@ -785,7 +792,7 @@ write_list_obj(SerdWriter* writer, return false; } -SerdStatus +static SerdStatus serd_writer_write_statement(SerdWriter* writer, SerdStatementFlags flags, const SerdNode* graph, @@ -915,7 +922,7 @@ serd_writer_write_statement(SerdWriter* writer, return SERD_SUCCESS; } -SerdStatus +static SerdStatus serd_writer_end_anon(SerdWriter* writer, const SerdNode* node) { if (writer->syntax == SERD_NTRIPLES || writer->syntax == SERD_NQUADS) { @@ -976,6 +983,12 @@ serd_writer_new(SerdSyntax syntax, writer->byte_sink = serd_byte_sink_new( ssink, stream, (flags & SERD_WRITE_BULK) ? SERD_PAGE_SIZE : 1); + writer->iface.handle = writer; + writer->iface.base = (SerdBaseFunc)serd_writer_set_base_uri; + writer->iface.prefix = (SerdPrefixFunc)serd_writer_set_prefix; + writer->iface.statement = (SerdStatementFunc)serd_writer_write_statement; + writer->iface.end = (SerdEndFunc)serd_writer_end_anon; + return writer; } @@ -1083,6 +1096,12 @@ serd_writer_free(SerdWriter* writer) free(writer); } +const SerdSink* +serd_writer_sink(SerdWriter* writer) +{ + return &writer->iface; +} + SerdEnv* serd_writer_env(SerdWriter* writer) { diff --git a/test/test_free_null.c b/test/test_free_null.c index 99e0be1d..af1ae149 100644 --- a/test/test_free_null.c +++ b/test/test_free_null.c @@ -26,6 +26,7 @@ main(void) serd_free(NULL); serd_node_free(NULL); serd_env_free(NULL); + serd_sink_free(NULL); serd_reader_free(NULL); serd_writer_free(NULL); diff --git a/test/test_read_chunk.c b/test/test_read_chunk.c index 76e9b642..c4e267f3 100644 --- a/test/test_read_chunk.c +++ b/test/test_read_chunk.c @@ -79,8 +79,13 @@ on_end(void* handle, const SerdNode* node) int main(void) { - SerdReader* reader = serd_reader_new( - SERD_TURTLE, NULL, NULL, on_base, on_prefix, on_statement, on_end); + 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); + serd_sink_set_end_func(sink, on_end); + + SerdReader* reader = serd_reader_new(SERD_TURTLE, sink); assert(reader); assert(!serd_reader_start_string(reader, @@ -106,5 +111,6 @@ main(void) assert(!serd_reader_end_stream(reader)); serd_reader_free(reader); + serd_sink_free(sink); return 0; } diff --git a/test/test_reader_writer.c b/test/test_reader_writer.c index 91e95806..8f80deee 100644 --- a/test/test_reader_writer.c +++ b/test/test_reader_writer.c @@ -78,15 +78,16 @@ eof_test_error(void* stream) static void test_read_chunks(void) { - ReaderTest* const rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); - FILE* const f = tmpfile(); - static const char null = 0; - SerdReader* const reader = - serd_reader_new(SERD_TURTLE, rt, free, NULL, NULL, test_sink, NULL); + ReaderTest* const rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); + FILE* const f = tmpfile(); + static const char null = 0; + SerdSink* sink = serd_sink_new(rt, NULL); + SerdReader* reader = serd_reader_new(SERD_TURTLE, sink); assert(reader); - assert(serd_reader_handle(reader) == rt); + assert(sink); assert(f); + serd_sink_set_statement_func(sink, test_sink); SerdStatus st = serd_reader_start_stream( reader, (SerdReadFunc)fread, (SerdStreamErrorFunc)ferror, f, NULL, 1); @@ -131,18 +132,22 @@ test_read_chunks(void) assert(rt->n_statements == 2); serd_reader_free(reader); + serd_sink_free(sink); fclose(f); + free(rt); } static void test_read_string(void) { - ReaderTest* rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); - SerdReader* reader = - serd_reader_new(SERD_TURTLE, rt, free, NULL, NULL, test_sink, NULL); + ReaderTest* rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); + SerdSink* sink = serd_sink_new(rt, NULL); + SerdReader* reader = serd_reader_new(SERD_TURTLE, sink); assert(reader); - assert(serd_reader_handle(reader) == rt); + assert(sink); + + serd_sink_set_statement_func(sink, test_sink); // Test reading a string that ends exactly at the end of input (no newline) const SerdStatus st = @@ -154,6 +159,8 @@ test_read_string(void) assert(rt->n_statements == 1); serd_reader_free(reader); + serd_sink_free(sink); + free(rt); } static void @@ -172,9 +179,9 @@ test_writer(const char* const path) SerdNode* lit = serd_new_string(SERD_STATIC_STRING("hello")); - assert(serd_writer_set_base_uri(writer, lit)); - assert(serd_writer_set_prefix(writer, lit, lit)); - assert(serd_writer_end_anon(writer, NULL)); + const SerdSink* const iface = serd_writer_sink(writer); + assert(serd_sink_write_base(iface, lit)); + assert(serd_sink_write_prefix(iface, lit, lit)); assert(serd_writer_env(writer) == env); static const uint8_t buf[] = {0xEF, 0xBF, 0xBD, 0}; @@ -187,8 +194,7 @@ test_writer(const char* const path) // Write 3 invalid statements (should write nothing) const SerdNode* junk[][3] = {{s, o, o}, {o, p, o}, {s, o, p}}; for (size_t i = 0; i < sizeof(junk) / (sizeof(SerdNode*) * 3); ++i) { - assert(serd_writer_write_statement( - writer, 0, NULL, junk[i][0], junk[i][1], junk[i][2])); + assert(serd_sink_write(iface, 0, junk[i][0], junk[i][1], junk[i][2], NULL)); } static const SerdStringView empty = SERD_EMPTY_STRING(); @@ -200,8 +206,8 @@ test_writer(const char* const path) const SerdNode* good[][3] = {{s, p, o}, {s, p, t}, {s, p, l}}; for (size_t i = 0; i < sizeof(good) / (sizeof(SerdNode*) * 3); ++i) { - assert(!serd_writer_write_statement( - writer, 0, NULL, good[i][0], good[i][1], good[i][2])); + assert( + !serd_sink_write(iface, 0, good[i][0], good[i][1], good[i][2], NULL)); } // Write statements with bad UTF-8 (should be replaced) @@ -209,16 +215,15 @@ test_writer(const char* const path) const SerdStringView bad_view = {(const char*)bad_str, 4}; SerdNode* bad_lit = serd_new_string(bad_view); SerdNode* bad_uri = serd_new_uri(bad_view); - assert(!serd_writer_write_statement(writer, 0, NULL, s, p, bad_lit)); - assert(!serd_writer_write_statement(writer, 0, NULL, s, p, bad_uri)); - + assert(!serd_sink_write(iface, 0, s, p, bad_lit, 0)); + assert(!serd_sink_write(iface, 0, s, p, bad_uri, 0)); serd_node_free(bad_uri); serd_node_free(bad_lit); // Write 1 valid statement - serd_node_free(o); - o = serd_new_string(SERD_STATIC_STRING("hello")); - assert(!serd_writer_write_statement(writer, 0, NULL, s, p, o)); + SerdNode* const hello = serd_new_string(SERD_STATIC_STRING("hello")); + assert(!serd_sink_write(iface, 0, s, p, hello, 0)); + serd_node_free(hello); serd_writer_free(writer); serd_node_free(lit); @@ -252,11 +257,13 @@ test_writer(const char* const path) static void test_reader(const char* path) { - ReaderTest* rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); - SerdReader* reader = - serd_reader_new(SERD_TURTLE, rt, free, NULL, NULL, test_sink, NULL); + ReaderTest* rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); + SerdSink* const sink = serd_sink_new(rt, NULL); + SerdReader* reader = serd_reader_new(SERD_TURTLE, sink); + + assert(sink); assert(reader); - assert(serd_reader_handle(reader) == rt); + serd_sink_set_statement_func(sink, test_sink); SerdNode* g = serd_new_uri(SERD_STATIC_STRING("http://example.org/")); serd_reader_set_default_graph(reader, g); @@ -325,6 +332,8 @@ test_reader(const char* path) } serd_reader_free(reader); + serd_sink_free(sink); + free(rt); } int |