aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2011-05-06 16:48:51 +0000
committerDavid Robillard <d@drobilla.net>2011-05-06 16:48:51 +0000
commit00433beb65e513283dbd1b05d4232fa2c8e0de69 (patch)
treebd677dea97b2e00d9e996dcbc6dc162960b7dd07
parent246aa2aca72b5580ceae29487c80a2dee567bde2 (diff)
downloadserd-00433beb65e513283dbd1b05d4232fa2c8e0de69.tar.gz
serd-00433beb65e513283dbd1b05d4232fa2c8e0de69.tar.bz2
serd-00433beb65e513283dbd1b05d4232fa2c8e0de69.zip
Make writer methods directly compatible with reader callbacks.
Connect writer directly to reader in serdi. git-svn-id: http://svn.drobilla.net/serd/trunk@164 490d8e77-9747-427b-9fa3-0b8f29cee8a0
-rw-r--r--serd/serd.h12
-rw-r--r--src/serdi.c54
-rw-r--r--src/writer.c53
3 files changed, 45 insertions, 74 deletions
diff --git a/serd/serd.h b/serd/serd.h
index de090cf4..d1f19cd3 100644
--- a/serd/serd.h
+++ b/serd/serd.h
@@ -559,14 +559,18 @@ serd_writer_free(SerdWriter* writer);
/**
Set the current output base URI (and emit directive if applicable).
+
+ Note this function can be safely casted to SerdBaseSink.
*/
SERD_API
SerdStatus
-serd_writer_set_base_uri(SerdWriter* writer,
- const SerdURI* uri);
+serd_writer_set_base_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
@@ -576,6 +580,8 @@ serd_writer_set_prefix(SerdWriter* writer,
/**
Write a statement.
+
+ Note this function can be safely casted to SerdStatementSink.
*/
SERD_API
SerdStatus
@@ -589,6 +595,8 @@ serd_writer_write_statement(SerdWriter* writer,
/**
Mark the end of an anonymous node's description.
+
+ Note this function can be safely casted to SerdEndSink.
*/
SERD_API
SerdStatus
diff --git a/src/serdi.c b/src/serdi.c
index bc46bd6b..61ea497f 100644
--- a/src/serdi.c
+++ b/src/serdi.c
@@ -26,53 +26,6 @@ typedef struct {
SerdWriter* writer;
} State;
-static SerdStatus
-event_base(void* handle,
- const SerdNode* uri_node)
-{
- State* const state = (State*)handle;
- if (!serd_env_set_base_uri(state->env, uri_node)) {
- SerdURI base_uri;
- serd_env_get_base_uri(state->env, &base_uri);
- serd_writer_set_base_uri(state->writer, &base_uri);
- return SERD_SUCCESS;
- }
- return SERD_ERR_UNKNOWN;
-}
-
-static SerdStatus
-event_prefix(void* handle,
- const SerdNode* name,
- const SerdNode* uri_node)
-{
- State* const state = (State*)handle;
- serd_env_set_prefix(state->env, name, uri_node);
- serd_writer_set_prefix(state->writer, name, uri_node);
- return SERD_SUCCESS;
-}
-
-static SerdStatus
-event_statement(void* handle,
- const SerdNode* graph,
- const SerdNode* subject,
- const SerdNode* predicate,
- const SerdNode* object,
- const SerdNode* object_datatype,
- const SerdNode* object_lang)
-{
- return serd_writer_write_statement(
- ((State*)handle)->writer,
- graph, subject, predicate, object, object_datatype, object_lang);
-}
-
-static SerdStatus
-event_end(void* handle,
- const SerdNode* node)
-{
- State* const state = (State*)handle;
- return serd_writer_end_anon(state->writer, node);
-}
-
int
print_version()
{
@@ -216,8 +169,11 @@ main(int argc, char** argv)
State state = { env, writer };
SerdReader* reader = serd_reader_new(
- SERD_TURTLE, &state,
- event_base, event_prefix, event_statement, event_end);
+ SERD_TURTLE, state.writer,
+ (SerdBaseSink)serd_writer_set_base_uri,
+ (SerdPrefixSink)serd_writer_set_prefix,
+ (SerdStatementSink)serd_writer_write_statement,
+ (SerdEndSink)serd_writer_end_anon);
const SerdStatus status = (from_file)
? serd_reader_read_file(reader, in_fd, input)
diff --git a/src/writer.c b/src/writer.c
index 122cde01..4e4ee2eb 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -426,21 +426,25 @@ serd_writer_new(SerdSyntax syntax,
SERD_API
SerdStatus
-serd_writer_set_base_uri(SerdWriter* writer,
- const SerdURI* uri)
+serd_writer_set_base_uri(SerdWriter* writer,
+ const SerdNode* uri)
{
- writer->base_uri = *uri;
- if (writer->syntax != SERD_NTRIPLES) {
- if (writer->context.graph.buf || writer->context.subject.buf) {
- writer->sink(" .\n\n", 4, writer->stream);
- reset_context(writer);
+ if (!serd_env_set_base_uri(writer->env, uri)) {
+ serd_env_get_base_uri(writer->env, &writer->base_uri);
+
+ if (writer->syntax != SERD_NTRIPLES) {
+ if (writer->context.graph.buf || writer->context.subject.buf) {
+ writer->sink(" .\n\n", 4, writer->stream);
+ reset_context(writer);
+ }
+ writer->sink("@base <", 7, writer->stream);
+ writer->sink(uri->buf, uri->n_bytes - 1, writer->stream);
+ writer->sink("> .\n", 4, writer->stream);
}
- writer->sink("@base <", 7, writer->stream);
- serd_uri_serialise(uri, writer->sink, writer->stream);
- writer->sink("> .\n", 4, writer->stream);
+ reset_context(writer);
+ return SERD_SUCCESS;
}
- reset_context(writer);
- return SERD_SUCCESS;
+ return SERD_ERR_UNKNOWN;
}
SERD_API
@@ -449,19 +453,22 @@ serd_writer_set_prefix(SerdWriter* writer,
const SerdNode* name,
const SerdNode* uri)
{
- if (writer->syntax != SERD_NTRIPLES) {
- if (writer->context.graph.buf || writer->context.subject.buf) {
- writer->sink(" .\n\n", 4, writer->stream);
- reset_context(writer);
+ if (!serd_env_set_prefix(writer->env, name, uri)) {
+ if (writer->syntax != SERD_NTRIPLES) {
+ if (writer->context.graph.buf || writer->context.subject.buf) {
+ writer->sink(" .\n\n", 4, writer->stream);
+ reset_context(writer);
+ }
+ writer->sink("@prefix ", 8, writer->stream);
+ writer->sink(name->buf, name->n_bytes - 1, writer->stream);
+ writer->sink(": <", 3, writer->stream);
+ write_text(writer, WRITE_URI, uri->buf, uri->n_bytes - 1, '>');
+ writer->sink("> .\n", 4, writer->stream);
}
- writer->sink("@prefix ", 8, writer->stream);
- writer->sink(name->buf, name->n_bytes - 1, writer->stream);
- writer->sink(": <", 3, writer->stream);
- write_text(writer, WRITE_URI, uri->buf, uri->n_bytes - 1, '>');
- writer->sink("> .\n", 4, writer->stream);
+ reset_context(writer);
+ return SERD_SUCCESS;
}
- reset_context(writer);
- return SERD_SUCCESS;
+ return SERD_ERR_UNKNOWN;
}
SERD_API