diff options
author | David Robillard <d@drobilla.net> | 2018-05-10 19:29:22 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2018-11-25 15:58:15 +0100 |
commit | f2a19949d0da60465e2d68b5e66a52e0d970ebd1 (patch) | |
tree | 0e72fcb7205e52f82e3821b2da9e5335716a5115 /src | |
parent | 6cf2ecfab4b3cb150b154bdc1130aca022a8f89c (diff) | |
download | serd-f2a19949d0da60465e2d68b5e66a52e0d970ebd1.tar.gz serd-f2a19949d0da60465e2d68b5e66a52e0d970ebd1.tar.bz2 serd-f2a19949d0da60465e2d68b5e66a52e0d970ebd1.zip |
Add SerdSink interface and hide individual function implementations
Diffstat (limited to 'src')
-rw-r--r-- | src/n3.c | 16 | ||||
-rw-r--r-- | src/reader.c | 43 | ||||
-rw-r--r-- | src/reader.h | 43 | ||||
-rw-r--r-- | src/serdi.c | 8 | ||||
-rw-r--r-- | src/writer.c | 23 |
5 files changed, 62 insertions, 71 deletions
@@ -957,8 +957,8 @@ 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_sink) { - reader->end_sink(reader->handle, deref(reader, *dest)); + if (reader->sink->end) { + reader->sink->end(reader->sink->handle, deref(reader, *dest)); } *ctx.flags = old_flags; } @@ -1252,8 +1252,8 @@ read_base(SerdReader* reader, bool sparql, bool token) Ref uri; read_ws_star(reader); TRY_RET(uri = read_IRIREF(reader)); - if (reader->base_sink) { - reader->base_sink(reader->handle, deref(reader, uri)); + if (reader->sink->base) { + reader->sink->base(reader->sink->handle, deref(reader, uri)); } pop_node(reader, uri); @@ -1292,10 +1292,10 @@ read_prefixID(SerdReader* reader, bool sparql, bool token) return false; } - if (reader->prefix_sink) { - ret = !reader->prefix_sink(reader->handle, - deref(reader, name), - deref(reader, uri)); + if (reader->sink->prefix) { + ret = !reader->sink->prefix(reader->sink->handle, + deref(reader, name), + deref(reader, uri)); } pop_node(reader, uri); pop_node(reader, name); diff --git a/src/reader.c b/src/reader.c index 236ebdd2..8bcbb3d4 100644 --- a/src/reader.c +++ b/src/reader.c @@ -121,9 +121,9 @@ emit_statement(SerdReader* reader, ReadContext ctx, Ref o, Ref d, Ref l) if (!graph && reader->default_graph) { graph = reader->default_graph; } - bool ret = !reader->statement_sink || - !reader->statement_sink( - reader->handle, *ctx.flags, graph, + bool ret = !reader->sink->statement || + !reader->sink->statement( + reader->sink->handle, *ctx.flags, graph, deref(reader, ctx.subject), deref(reader, ctx.predicate), deref(reader, o)); *ctx.flags &= SERD_ANON_CONT|SERD_LIST_CONT; // Preserve only cont flags @@ -155,26 +155,16 @@ serd_reader_read_document(SerdReader* reader) } SerdReader* -serd_reader_new(SerdSyntax syntax, - void* handle, - void (*free_handle)(void*), - SerdBaseSink base_sink, - SerdPrefixSink prefix_sink, - SerdStatementSink statement_sink, - SerdEndSink end_sink) +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_sink = base_sink; - me->prefix_sink = prefix_sink; - me->statement_sink = statement_sink; - me->end_sink = end_sink; - 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); @@ -211,18 +201,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_get_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 842ba8b8..668c4b40 100644 --- a/src/reader.h +++ b/src/reader.h @@ -46,31 +46,26 @@ typedef struct { } ReadContext; struct SerdReaderImpl { - void* handle; - void (*free_handle)(void* ptr); - SerdBaseSink base_sink; - SerdPrefixSink prefix_sink; - SerdStatementSink statement_sink; - SerdEndSink end_sink; - SerdErrorSink error_sink; - void* error_handle; - Ref rdf_first; - Ref rdf_rest; - Ref rdf_nil; - SerdNode* default_graph; - SerdByteSource source; - SerdStack stack; - SerdSyntax syntax; - unsigned next_id; - SerdStatus status; - uint8_t* buf; - char* bprefix; - size_t bprefix_len; - bool strict; ///< True iff strict parsing - bool seen_genid; + const SerdSink* sink; + SerdErrorSink error_sink; + void* error_handle; + Ref rdf_first; + Ref rdf_rest; + Ref rdf_nil; + SerdNode* default_graph; + SerdByteSource source; + SerdStack stack; + SerdSyntax syntax; + unsigned next_id; + SerdStatus status; + 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 + Ref* allocs; ///< Stack of push offsets + size_t n_allocs; ///< Number of stack pushes #endif }; diff --git a/src/serdi.c b/src/serdi.c index d70a52cc..d3537457 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -279,12 +279,8 @@ main(int argc, char** argv) (SerdWriteFunc)fwrite, out_fd); - SerdReader* reader = serd_reader_new( - input_syntax, writer, NULL, - (SerdBaseSink)serd_writer_set_base_uri, - (SerdPrefixSink)serd_writer_set_prefix, - (SerdStatementSink)serd_writer_write_statement, - (SerdEndSink)serd_writer_end_anon); + SerdReader* reader = + serd_reader_new(input_syntax, serd_writer_get_sink(writer)); serd_reader_set_strict(reader, !lax); if (quiet) { diff --git a/src/writer.c b/src/writer.c index 11aa5b53..e817c023 100644 --- a/src/writer.c +++ b/src/writer.c @@ -89,6 +89,7 @@ static const SepRule rules[] = { }; struct SerdWriterImpl { + SerdSink iface; SerdSyntax syntax; SerdStyle style; SerdEnv* env; @@ -114,6 +115,11 @@ typedef enum { 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, @@ -692,7 +698,7 @@ write_list_obj(SerdWriter* writer, return false; } -SerdStatus +static SerdStatus serd_writer_write_statement(SerdWriter* writer, SerdStatementFlags flags, const SerdNode* graph, @@ -825,7 +831,7 @@ serd_writer_write_statement(SerdWriter* writer, return SERD_SUCCESS; } -SerdStatus +static SerdStatus serd_writer_end_anon(SerdWriter* writer, const SerdNode* node) { @@ -888,6 +894,13 @@ serd_writer_new(SerdSyntax syntax, writer->empty = true; writer->byte_sink = serd_byte_sink_new( ssink, stream, (style & SERD_STYLE_BULK) ? SERD_PAGE_SIZE : 1); + + writer->iface.handle = writer; + 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; + writer->iface.end = (SerdEndSink)serd_writer_end_anon; + return writer; } @@ -986,6 +999,12 @@ serd_writer_free(SerdWriter* writer) free(writer); } +const SerdSink* +serd_writer_get_sink(SerdWriter* writer) +{ + return &writer->iface; +} + SerdEnv* serd_writer_get_env(SerdWriter* writer) { |