aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2012-03-18 15:41:58 +0000
committerDavid Robillard <d@drobilla.net>2012-03-18 15:41:58 +0000
commit91ae836711ba3df5f44407dadcadb0bd9ac53621 (patch)
treef01047babb9d09230aa8a774e20bbe7cff505c3e
parent90830a30aa7e16b4ebb3a249f1456da174b1eca3 (diff)
downloadserd-91ae836711ba3df5f44407dadcadb0bd9ac53621.tar.gz
serd-91ae836711ba3df5f44407dadcadb0bd9ac53621.tar.bz2
serd-91ae836711ba3df5f44407dadcadb0bd9ac53621.zip
Add serd_reader_set_default_graph() for reading a file as a named graph.
git-svn-id: http://svn.drobilla.net/serd/trunk@338 490d8e77-9747-427b-9fa3-0b8f29cee8a0
-rw-r--r--ChangeLog1
-rw-r--r--serd/serd.h12
-rw-r--r--src/reader.c18
-rw-r--r--tests/serd_test.c47
-rw-r--r--wscript2
5 files changed, 61 insertions, 19 deletions
diff --git a/ChangeLog b/ChangeLog
index d4140211..637b71f2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -39,6 +39,7 @@ serd (UNRELEASED) unstable; urgency=low
* Add serd_writer_set_root_uri() and corresponding -r option to serdi to
enable writing URIs with up references (../).
* Resolve dot segments in serd_uri_resolve() instead of at write time.
+ * Add serd_reader_set_default_graph() for reading a file as a named graph.
-- David Robillard <d@drobilla.net> (UNRELEASED)
diff --git a/serd/serd.h b/serd/serd.h
index 40e47499..807664dd 100644
--- a/serd/serd.h
+++ b/serd/serd.h
@@ -685,6 +685,18 @@ serd_reader_add_blank_prefix(SerdReader* reader,
const uint8_t* prefix);
/**
+ Set the URI of the default graph.
+
+ If this is set, the reader will emit quads with the graph set to the given
+ node for any statements that are not in a named graph (which is currently
+ all of them since Serd currently does not support any graph syntaxes).
+*/
+SERD_API
+void
+serd_reader_set_default_graph(SerdReader* reader,
+ const SerdNode* graph);
+
+/**
Read a file at a given @c uri.
*/
SERD_API
diff --git a/src/reader.c b/src/reader.c
index 7e9bb3d4..23ef9fef 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -67,6 +67,7 @@ struct SerdReaderImpl {
Ref rdf_first;
Ref rdf_rest;
Ref rdf_nil;
+ SerdNode default_graph;
FILE* fd;
SerdStack stack;
SerdSyntax syntax;
@@ -236,9 +237,13 @@ pop_node(SerdReader* reader, Ref ref)
static inline bool
emit_statement(SerdReader* reader, ReadContext ctx, Ref o, Ref d, Ref l)
{
+ SerdNode* graph = deref(reader, ctx.graph);
+ if (!graph && reader->default_graph.buf) {
+ graph = &reader->default_graph;
+ }
bool ret = !reader->statement_sink ||
!reader->statement_sink(
- reader->handle, *ctx.flags, deref(reader, ctx.graph),
+ reader->handle, *ctx.flags, graph,
deref(reader, ctx.subject), deref(reader, ctx.predicate),
deref(reader, o), deref(reader, d), deref(reader, l));
*ctx.flags &= SERD_ANON_CONT|SERD_LIST_CONT; // Preserve only cont flags
@@ -1354,6 +1359,7 @@ serd_reader_new(SerdSyntax syntax,
me->prefix_sink = prefix_sink;
me->statement_sink = statement_sink;
me->end_sink = end_sink;
+ me->default_graph = SERD_NODE_NULL;
me->fd = 0;
me->stack = serd_stack_new(SERD_PAGE_SIZE);
me->syntax = syntax;
@@ -1384,6 +1390,7 @@ serd_reader_free(SerdReader* reader)
pop_node(reader, reader->rdf_nil);
pop_node(reader, reader->rdf_rest);
pop_node(reader, reader->rdf_first);
+ serd_node_free(&reader->default_graph);
#ifdef SERD_STACK_CHECK
free(reader->allocs);
@@ -1417,6 +1424,15 @@ serd_reader_add_blank_prefix(SerdReader* reader,
memcpy(reader->bprefix, prefix, reader->bprefix_len + 1);
}
}
+
+SERD_API
+void
+serd_reader_set_default_graph(SerdReader* reader,
+ const SerdNode* graph)
+{
+ serd_node_free(&reader->default_graph);
+ reader->default_graph = serd_node_copy(graph);
+}
SERD_API
SerdStatus
diff --git a/tests/serd_test.c b/tests/serd_test.c
index 3a4584ed..73276204 100644
--- a/tests/serd_test.c
+++ b/tests/serd_test.c
@@ -59,17 +59,24 @@ count_prefixes(void* handle, const SerdNode* name, const SerdNode* uri)
return SERD_SUCCESS;
}
+typedef struct {
+ int n_statements;
+ const SerdNode* graph;
+} ReaderTest;
+
static SerdStatus
-count_statements(void* handle,
- SerdStatementFlags flags,
- const SerdNode* graph,
- const SerdNode* subject,
- const SerdNode* predicate,
- const SerdNode* object,
- const SerdNode* object_datatype,
- const SerdNode* object_lang)
+test_sink(void* handle,
+ SerdStatementFlags flags,
+ const SerdNode* graph,
+ const SerdNode* subject,
+ const SerdNode* predicate,
+ const SerdNode* object,
+ const SerdNode* object_datatype,
+ const SerdNode* object_lang)
{
- ++*(int*)handle;
+ ReaderTest* rt = (ReaderTest*)handle;
+ ++rt->n_statements;
+ rt->graph = graph;
return SERD_SUCCESS;
}
@@ -459,9 +466,6 @@ main()
return failure("Failed to open file %s\n", path);
}
- int* n_statements = (int*)malloc(sizeof(int));
- *n_statements = 0;
-
SerdWriter* writer = serd_writer_new(
SERD_TURTLE, (SerdStyle)0, env, NULL, serd_file_sink, fd);
if (!writer) {
@@ -567,16 +571,22 @@ main()
// Rewind and test reader
fseek(fd, 0, SEEK_SET);
+ ReaderTest* rt = (ReaderTest*)malloc(sizeof(ReaderTest));
+ rt->n_statements = 0;
+ rt->graph = NULL;
+
SerdReader* reader = serd_reader_new(
- SERD_TURTLE, n_statements, free,
- NULL, NULL, count_statements, NULL);
+ SERD_TURTLE, rt, free,
+ NULL, NULL, test_sink, NULL);
if (!reader) {
return failure("Failed to create reader\n");
}
- if (serd_reader_get_handle(reader) != n_statements) {
+ if (serd_reader_get_handle(reader) != rt) {
return failure("Corrupt reader handle\n");
}
+ SerdNode g = serd_node_from_string(SERD_URI, USTR("http://example.org/"));
+ serd_reader_set_default_graph(reader, &g);
serd_reader_add_blank_prefix(reader, USTR("tmp"));
serd_reader_add_blank_prefix(reader, NULL);
@@ -591,8 +601,11 @@ main()
return failure("Error reading file (%s)\n", serd_strerror(st));
}
- if (*n_statements != 12) {
- return failure("Bad statement count %d\n", *n_statements);
+ if (rt->n_statements != 12) {
+ return failure("Bad statement count %d\n", rt->n_statements);
+ } else if (!rt->graph || !rt->graph->buf ||
+ strcmp((const char*)rt->graph->buf, "http://example.org/")) {
+ return failure("Bad graph %p\n", rt->graph);
}
if (!serd_reader_read_string(reader, USTR("This isn't Turtle at all."))) {
diff --git a/wscript b/wscript
index e5683523..1630b4bd 100644
--- a/wscript
+++ b/wscript
@@ -9,7 +9,7 @@ from waflib.extras import autowaf as autowaf
import waflib.Logs as Logs, waflib.Options as Options
# Version of this package (even if built as a child)
-SERD_VERSION = '0.13.0'
+SERD_VERSION = '0.14.0'
SERD_MAJOR_VERSION = '0'
# Library version (UNIX style major, minor, micro)