aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-06-03 19:47:07 +0200
committerDavid Robillard <d@drobilla.net>2019-12-19 20:55:19 -0500
commit0e83ea9bcdc9fd08eda66ddc64fd0464be61e2d0 (patch)
tree135ec30aec0d5ebdb506b9670d8b0c5b62e0d90b
parent0476f6737a568f8902f9a1317d6683c1d3f1f03d (diff)
downloadserd-0e83ea9bcdc9fd08eda66ddc64fd0464be61e2d0.tar.gz
serd-0e83ea9bcdc9fd08eda66ddc64fd0464be61e2d0.tar.bz2
serd-0e83ea9bcdc9fd08eda66ddc64fd0464be61e2d0.zip
Make statement sink take a statement rather than nodes
This makes the interface more extensible, towards associating more information with statements. The serd_sink_write_nodes wrapper remains so that user code does not need to allocate in order to write statement.
-rw-r--r--serd/serd.h9
-rw-r--r--src/reader.c8
-rw-r--r--src/sink.c13
-rw-r--r--src/writer.c14
-rw-r--r--tests/serd_test.c31
5 files changed, 34 insertions, 41 deletions
diff --git a/serd/serd.h b/serd/serd.h
index c76626de..281a812f 100644
--- a/serd/serd.h
+++ b/serd/serd.h
@@ -779,12 +779,9 @@ typedef SerdStatus (*SerdPrefixSink)(void* handle,
Called for every RDF statement in the serialisation.
*/
-typedef SerdStatus (*SerdStatementSink)(void* handle,
- SerdStatementFlags flags,
- const SerdNode* graph,
- const SerdNode* subject,
- const SerdNode* predicate,
- const SerdNode* object);
+typedef SerdStatus (*SerdStatementSink)(void* handle,
+ SerdStatementFlags flags,
+ const SerdStatement* statement);
/**
Sink (callback) for anonymous node end markers.
diff --git a/src/reader.c b/src/reader.c
index b20d6bc5..13fc681d 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -21,6 +21,7 @@
#include "serd/serd.h"
#include "serd_internal.h"
#include "stack.h"
+#include "statement.h"
#include "system.h"
#include "world.h"
@@ -127,9 +128,12 @@ emit_statement(SerdReader* reader, ReadContext ctx, SerdNode* o)
(subject and predicate) were already zeroed by subsequent pushes. */
serd_node_zero_pad(o);
+ const SerdStatement statement = {
+ { ctx.subject, ctx.predicate, o, graph }
+ };
+
const SerdStatus st = reader->sink->statement(
- reader->sink->handle, *ctx.flags, graph,
- ctx.subject, ctx.predicate, o);
+ reader->sink->handle, *ctx.flags, &statement);
*ctx.flags &= SERD_ANON_CONT|SERD_LIST_CONT; // Preserve only cont flags
return st;
}
diff --git a/src/sink.c b/src/sink.c
index eb31bbf6..1c5c500f 100644
--- a/src/sink.c
+++ b/src/sink.c
@@ -16,6 +16,8 @@
#include "serd/serd.h"
+#include "statement.h"
+
SerdStatus
serd_sink_write_base(const SerdSink* sink, const SerdNode* uri)
{
@@ -35,12 +37,7 @@ serd_sink_write_statement(const SerdSink* sink,
const SerdStatementFlags flags,
const SerdStatement* statement)
{
- return sink->statement(sink->handle,
- flags,
- serd_statement_get_graph(statement),
- serd_statement_get_subject(statement),
- serd_statement_get_predicate(statement),
- serd_statement_get_object(statement));
+ return sink->statement(sink->handle, flags, statement);
}
SerdStatus
@@ -51,8 +48,8 @@ serd_sink_write(const SerdSink* sink,
const SerdNode* object,
const SerdNode* graph)
{
- return sink->statement(
- sink->handle, flags, graph, subject, predicate, object);
+ const SerdStatement statement = { { subject, predicate, object, graph } };
+ return sink->statement(sink->handle, flags, &statement);
}
SerdStatus
diff --git a/src/writer.c b/src/writer.c
index 4d2e2fa4..0da06987 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -681,13 +681,15 @@ write_list_obj(SerdWriter* writer,
}
static SerdStatus
-serd_writer_write_statement(SerdWriter* writer,
- SerdStatementFlags flags,
- const SerdNode* graph,
- const SerdNode* subject,
- const SerdNode* predicate,
- const SerdNode* object)
+serd_writer_write_statement(SerdWriter* writer,
+ SerdStatementFlags flags,
+ const SerdStatement* statement)
{
+ const SerdNode* const subject = serd_statement_get_subject(statement);
+ const SerdNode* const predicate = serd_statement_get_predicate(statement);
+ const SerdNode* const object = serd_statement_get_object(statement);
+ const SerdNode* const graph = serd_statement_get_graph(statement);
+
if (!subject || !predicate || !object ||
!is_resource(subject) || !is_resource(predicate)) {
return SERD_ERR_BAD_ARG;
diff --git a/tests/serd_test.c b/tests/serd_test.c
index cba06269..bfb0af21 100644
--- a/tests/serd_test.c
+++ b/tests/serd_test.c
@@ -66,21 +66,16 @@ typedef struct {
} ReaderTest;
static SerdStatus
-test_sink(void* handle,
- SerdStatementFlags flags,
- const SerdNode* graph,
- const SerdNode* subject,
- const SerdNode* predicate,
- const SerdNode* object)
+test_sink(void* handle,
+ SerdStatementFlags flags,
+ const SerdStatement* statement)
{
(void)flags;
- (void)subject;
- (void)predicate;
- (void)object;
+ (void)statement;
ReaderTest* rt = (ReaderTest*)handle;
++rt->n_statements;
- rt->graph = graph;
+ rt->graph = serd_statement_get_graph(statement);
return SERD_SUCCESS;
}
@@ -648,9 +643,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(iface->statement(
- iface->handle, 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], 0));
}
SerdNode* urn_Type = serd_node_new_uri("urn:Type");
@@ -668,24 +662,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(!iface->statement(
- iface->handle, 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], 0));
}
// 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(bad_str);
SerdNode* bad_uri = serd_node_new_uri(bad_str);
- assert(!iface->statement(iface->handle, 0, NULL, s, p, bad_lit));
- assert(!iface->statement(iface->handle, 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_node_new_string("hello");
- assert(!iface->statement(iface->handle, 0, NULL, s, p, o));
+ assert(!serd_sink_write(iface, 0, s, p, o, 0));
serd_writer_free(writer);
serd_node_free(lit);