aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--serd/serd.h68
-rw-r--r--src/n3.c16
-rw-r--r--src/reader.c43
-rw-r--r--src/reader.h43
-rw-r--r--src/serdi.c8
-rw-r--r--src/writer.c23
-rw-r--r--tests/serd_test.c44
7 files changed, 101 insertions, 144 deletions
diff --git a/serd/serd.h b/serd/serd.h
index fbe67682..51248442 100644
--- a/serd/serd.h
+++ b/serd/serd.h
@@ -704,6 +704,17 @@ typedef SerdStatus (*SerdEndSink)(void* handle,
const SerdNode* node);
/**
+ An interface that receives a stream of RDF data.
+*/
+typedef struct SerdSink {
+ void* handle;
+ SerdBaseSink base;
+ SerdPrefixSink prefix;
+ SerdStatementSink statement;
+ SerdEndSink end;
+} SerdSink;
+
+/**
@}
@name Environment
@{
@@ -810,13 +821,7 @@ serd_env_foreach(const SerdEnv* env,
*/
SERD_API
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);
/**
Enable or disable strict parsing.
@@ -842,13 +847,6 @@ serd_reader_set_error_sink(SerdReader* reader,
void* error_handle);
/**
- Return the `handle` passed to serd_reader_new().
-*/
-SERD_API
-void*
-serd_reader_get_handle(const SerdReader* reader);
-
-/**
Set a prefix to be added to all blank node identifiers.
This is useful when multiple files are to be parsed into the same output
@@ -974,6 +972,13 @@ void
serd_writer_free(SerdWriter* writer);
/**
+ Return a sink interface that emits statements via `writer`.
+*/
+SERD_API
+const SerdSink*
+serd_writer_get_sink(SerdWriter* writer);
+
+/**
Return the env used by `writer`.
*/
SERD_API
@@ -1048,41 +1053,6 @@ serd_writer_set_root_uri(SerdWriter* writer,
const SerdNode* 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* writer,
- const SerdNode* name,
- const SerdNode* uri);
-
-/**
- Write a statement.
-
- Note this function can be safely casted to SerdStatementSink.
-*/
-SERD_API
-SerdStatus
-serd_writer_write_statement(SerdWriter* writer,
- SerdStatementFlags flags,
- const SerdNode* graph,
- const SerdNode* subject,
- const SerdNode* predicate,
- const SerdNode* 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* writer,
- const SerdNode* node);
-
-/**
Finish a write.
*/
SERD_API
diff --git a/src/n3.c b/src/n3.c
index 2547fc93..714f6878 100644
--- a/src/n3.c
+++ b/src/n3.c
@@ -965,8 +965,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;
}
@@ -1259,8 +1259,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);
@@ -1299,10 +1299,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 bdf46772..e9bc68fa 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -124,9 +124,9 @@ emit_statement(SerdReader* reader, ReadContext ctx, Ref o)
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
@@ -156,26 +156,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);
@@ -212,18 +202,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 45e3350d..405af546 100644
--- a/src/reader.h
+++ b/src/reader.h
@@ -52,31 +52,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 29c78b7f..6656ccd2 100644
--- a/src/serdi.c
+++ b/src/serdi.c
@@ -294,12 +294,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 4eb2b848..5c457566 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,
@@ -689,7 +695,7 @@ write_list_obj(SerdWriter* writer,
return false;
}
-SerdStatus
+static SerdStatus
serd_writer_write_statement(SerdWriter* writer,
SerdStatementFlags flags,
const SerdNode* graph,
@@ -822,7 +828,7 @@ serd_writer_write_statement(SerdWriter* writer,
return SERD_SUCCESS;
}
-SerdStatus
+static SerdStatus
serd_writer_end_anon(SerdWriter* writer,
const SerdNode* node)
{
@@ -885,6 +891,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;
}
@@ -983,6 +996,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)
{
diff --git a/tests/serd_test.c b/tests/serd_test.c
index 103936f1..5998588c 100644
--- a/tests/serd_test.c
+++ b/tests/serd_test.c
@@ -108,14 +108,13 @@ test_file_uri(const char* hostname,
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 = {rt, NULL, NULL, test_sink, NULL};
+ SerdReader* reader = serd_reader_new(SERD_TURTLE, &sink);
assert(reader);
- assert(serd_reader_get_handle(reader) == rt);
assert(f);
SerdStatus st = serd_reader_start_stream(reader,
@@ -171,6 +170,7 @@ test_read_chunks(void)
serd_reader_free(reader);
fclose(f);
+ free(rt);
}
static void
@@ -539,9 +539,10 @@ test_writer(const char* const path)
SerdNode* lit = serd_node_new_string(SERD_LITERAL, "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* iface = serd_writer_get_sink(writer);
+ assert(iface->base(iface->handle, lit));
+ assert(iface->prefix(iface->handle, lit, lit));
+ assert(iface->end(iface->handle, NULL));
assert(serd_writer_get_env(writer) == env);
uint8_t buf[] = { 0xEF, 0xBF, 0xBD, 0 };
@@ -561,8 +562,8 @@ test_writer(const char* const path)
{ s, p, NULL },
{ NULL, NULL, NULL } };
for (unsigned i = 0; i < sizeof(junk) / (sizeof(SerdNode*) * 5); ++i) {
- assert(serd_writer_write_statement(
- writer, 0, NULL,
+ assert(iface->statement(
+ iface->handle, 0, NULL,
junk[i][0], junk[i][1], junk[i][2]));
}
@@ -579,27 +580,23 @@ test_writer(const char* const path)
{ s, p, o },
{ s, p, o } };
for (unsigned i = 0; i < sizeof(good) / (sizeof(SerdNode*) * 5); ++i) {
- assert(!serd_writer_write_statement(
- writer, 0, NULL,
- good[i][0], good[i][1], good[i][2]));
+ assert(!iface->statement(
+ iface->handle, 0, NULL, good[i][0], good[i][1], good[i][2]));
}
// Write statements with bad UTF-8 (should be replaced)
const char bad_str[] = { (char)0xFF, (char)0x90, 'h', 'i', 0 };
SerdNode* bad_lit = serd_node_new_string(SERD_LITERAL, bad_str);
SerdNode* bad_uri = serd_node_new_string(SERD_URI, bad_str);
- 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(!iface->statement(iface->handle, 0, NULL, s, p, bad_lit));
+ assert(!iface->statement(iface->handle, 0, NULL, s, p, bad_uri));
serd_node_free(bad_lit);
serd_node_free(bad_uri);
// Write 1 valid statement
serd_node_free(o);
o = serd_node_new_string(SERD_LITERAL, "hello");
- assert(!serd_writer_write_statement(writer, 0, NULL,
- s, p, o));
+ assert(!iface->statement(iface->handle, 0, NULL, s, p, o));
serd_writer_free(writer);
serd_node_free(lit);
@@ -631,11 +628,9 @@ 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);
+ SerdSink sink = { rt, NULL, NULL, test_sink, NULL };
+ SerdReader* reader = serd_reader_new(SERD_TURTLE, &sink);
assert(reader);
- assert(serd_reader_get_handle(reader) == rt);
SerdNode* g = serd_node_new_string(SERD_URI, "http://example.org/");
serd_reader_set_default_graph(reader, g);
@@ -656,6 +651,7 @@ test_reader(const char* path)
assert(serd_reader_read_string(reader, "This isn't Turtle at all."));
serd_reader_free(reader);
+ free(rt);
}
int