aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--serd/serd.h47
-rw-r--r--src/namespaces.c8
-rw-r--r--src/serdi.c31
-rw-r--r--src/write.c70
4 files changed, 121 insertions, 35 deletions
diff --git a/serd/serd.h b/serd/serd.h
index d59e3002..1dd49c65 100644
--- a/serd/serd.h
+++ b/serd/serd.h
@@ -55,6 +55,7 @@ extern "C" {
typedef struct SerdNamespacesImpl* SerdNamespaces; ///< Set of namespaces
typedef struct SerdReaderImpl* SerdReader; ///< RDF reader
+typedef struct SerdWriterImpl* SerdWriter; ///< RDF writer
/** RDF syntax */
typedef enum {
@@ -182,10 +183,10 @@ serd_namespaces_add(SerdNamespaces ns,
/** Expand @a qname. */
SERD_API
bool
-serd_namespaces_expand(SerdNamespaces ns,
- const SerdString* qname,
- SerdChunk* uri_prefix,
- SerdChunk* uri_suffix);
+serd_namespaces_expand(const SerdNamespaces ns,
+ const SerdString* qname,
+ SerdChunk* uri_prefix,
+ SerdChunk* uri_suffix);
/** @} */
/** @name Reader
@@ -241,16 +242,38 @@ serd_reader_free(SerdReader reader);
* @{
*/
-/** Write a node to @a file. */
+/** Create a new RDF writer. */
+SERD_API
+SerdWriter
+serd_writer_new(SerdSyntax syntax,
+ SerdNamespaces ns,
+ FILE* file,
+ const SerdURI* base_uri);
+
+/** Free @a writer. */
+SERD_API
+void
+serd_writer_free(SerdWriter writer);
+
+/** Set the base URI of writer. */
+SERD_API
+void
+serd_writer_set_base_uri(SerdWriter writer,
+ const SerdURI* uri);
+
+/** Write a statement. */
SERD_API
bool
-serd_write_node(FILE* file,
- const SerdURI* base_uri,
- SerdNamespaces ns,
- SerdNodeType type,
- const SerdString* str,
- const SerdString* datatype,
- const SerdString* lang);
+serd_writer_write_statement(SerdWriter writer,
+ const SerdString* graph,
+ const SerdString* subject,
+ SerdNodeType subject_type,
+ const SerdString* predicate,
+ SerdNodeType predicate_type,
+ const SerdString* object,
+ SerdNodeType object_type,
+ const SerdString* object_datatype,
+ const SerdString* object_lang);
/** @} */
diff --git a/src/namespaces.c b/src/namespaces.c
index 6c400a3c..409c92b9 100644
--- a/src/namespaces.c
+++ b/src/namespaces.c
@@ -93,10 +93,10 @@ serd_namespaces_add(SerdNamespaces ns,
SERD_API
bool
-serd_namespaces_expand(SerdNamespaces ns,
- const SerdString* qname,
- SerdChunk* uri_prefix,
- SerdChunk* uri_suffix)
+serd_namespaces_expand(const SerdNamespaces ns,
+ const SerdString* qname,
+ SerdChunk* uri_prefix,
+ SerdChunk* uri_suffix)
{
const uint8_t* colon = memchr((const char*)qname->buf, ':', qname->n_bytes);
if (!colon) {
diff --git a/src/serdi.c b/src/serdi.c
index 13fdb2af..abac1010 100644
--- a/src/serdi.c
+++ b/src/serdi.c
@@ -22,7 +22,7 @@
#include "serd/serd.h"
typedef struct {
- FILE* out_fd;
+ SerdWriter writer;
SerdNamespaces ns;
SerdString* base_uri_str;
SerdURI base_uri;
@@ -61,8 +61,9 @@ event_base(void* handle,
// Replace the old base URI
serd_string_free(state->base_uri_str);
- state->base_uri_str = base_uri_str;
- state->base_uri = base_uri;
+ state->base_uri_str = base_uri_str;
+ state->base_uri = base_uri;
+ serd_writer_set_base_uri(state->writer, &base_uri);
return true;
}
@@ -105,17 +106,12 @@ event_statement(void* handle,
const SerdString* object_lang)
{
State* const state = (State*)handle;
- FILE* const fd = state->out_fd;
- serd_write_node(fd, &state->base_uri, state->ns,
- subject_type, subject, NULL, NULL);
- fwrite(" ", 1, 1, fd);
- serd_write_node(fd, &state->base_uri, state->ns,
- predicate_type, predicate, NULL, NULL);
- fwrite(" ", 1, 1, fd);
- serd_write_node(fd, &state->base_uri, state->ns,
- object_type, object, object_datatype, object_lang);
- fwrite(" .\n", 1, 3, fd);
- return true;
+ return serd_writer_write_statement(
+ state->writer,
+ graph,
+ subject, subject_type,
+ predicate, predicate_type,
+ object, object_type, object_datatype, object_lang);
}
int
@@ -146,7 +142,11 @@ main(int argc, char** argv)
return 1;
}
- State state = { out_fd, serd_namespaces_new(), serd_string_new(base_uri_str), base_uri };
+ SerdNamespaces ns = serd_namespaces_new();
+ State state = { serd_writer_new(SERD_NTRIPLES, ns, out_fd, &base_uri),
+ ns,
+ serd_string_new(base_uri_str),
+ base_uri };
SerdReader reader = serd_reader_new(
SERD_TURTLE, &state, event_base, event_prefix, event_statement);
@@ -155,6 +155,7 @@ main(int argc, char** argv)
serd_reader_free(reader);
fclose(in_fd);
serd_namespaces_free(state.ns);
+ serd_writer_free(state.writer);
serd_string_free(state.base_uri_str);
if (success) {
diff --git a/src/write.c b/src/write.c
index 633655d8..893062ef 100644
--- a/src/write.c
+++ b/src/write.c
@@ -20,6 +20,13 @@
#include "serd/serd.h"
+struct SerdWriterImpl {
+ SerdSyntax syntax;
+ SerdNamespaces ns;
+ FILE* fd;
+ SerdURI base_uri;
+};
+
static size_t
file_sink(const uint8_t* buf, size_t len, void* stream)
{
@@ -107,14 +114,16 @@ serd_write_ascii(const uint8_t* utf8, size_t n_bytes, FILE* out_fd, const uint8_
SERD_API
bool
-serd_write_node(FILE* fd,
- const SerdURI* base_uri,
- SerdNamespaces ns,
+serd_write_node(SerdWriter writer,
SerdNodeType type,
const SerdString* str,
const SerdString* datatype,
const SerdString* lang)
{
+ FILE* const fd = writer->fd;
+ const SerdURI* base_uri = &writer->base_uri;
+ SerdNamespaces ns = writer->ns;
+
SerdChunk uri_prefix;
SerdChunk uri_suffix;
switch (type) {
@@ -161,10 +170,63 @@ serd_write_node(FILE* fd,
fwrite("\"", 1, 1, fd);
} else if (datatype) {
fwrite("^^", 1, 2, fd);
- serd_write_node(fd, base_uri, ns, URI, datatype, NULL, NULL);
+ serd_write_node(writer, URI, datatype, NULL, NULL);
}
break;
}
return true;
}
+SERD_API
+bool
+serd_writer_write_statement(SerdWriter writer,
+ const SerdString* graph,
+ const SerdString* subject,
+ SerdNodeType subject_type,
+ const SerdString* predicate,
+ SerdNodeType predicate_type,
+ const SerdString* object,
+ SerdNodeType object_type,
+ const SerdString* object_datatype,
+ const SerdString* object_lang)
+{
+ FILE* const fd = writer->fd;
+ serd_write_node(writer, subject_type, subject, NULL, NULL);
+ fwrite(" ", 1, 1, fd);
+ serd_write_node(writer, predicate_type, predicate, NULL, NULL);
+ fwrite(" ", 1, 1, fd);
+ serd_write_node(writer, object_type, object, object_datatype, object_lang);
+ fwrite(" .\n", 1, 3, fd);
+ return true;
+}
+
+SERD_API
+SerdWriter
+serd_writer_new(SerdSyntax syntax,
+ SerdNamespaces ns,
+ FILE* file,
+ const SerdURI* base_uri)
+{
+ SerdWriter writer = malloc(sizeof(struct SerdWriterImpl));
+ writer->syntax = syntax;
+ writer->ns = ns;
+ writer->fd = file;
+ writer->base_uri = *base_uri;
+ return writer;
+}
+
+SERD_API
+void
+serd_writer_set_base_uri(SerdWriter writer,
+ const SerdURI* uri)
+{
+ writer->base_uri = *uri;
+}
+
+SERD_API
+void
+serd_writer_free(SerdWriter writer)
+{
+ SerdWriter const me = (SerdWriter)writer;
+ free(me);
+}