summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sord/sord.h62
-rw-r--r--sord/sordmm.hpp51
-rw-r--r--src/sordi.c67
-rw-r--r--src/syntax.c204
-rw-r--r--wscript4
5 files changed, 149 insertions, 239 deletions
diff --git a/sord/sord.h b/sord/sord.h
index 62bef78..5796a6a 100644
--- a/sord/sord.h
+++ b/sord/sord.h
@@ -265,10 +265,20 @@ bool
sord_node_equals(const SordNode* a,
const SordNode* b);
+/**
+ Return a SordNode as a SerdNode.
+
+ The returned node is shared and must not be freed or modified.
+*/
SORD_API
const SerdNode*
sord_node_to_serd_node(const SordNode* node);
+/**
+ Create a new SordNode from a SerdNode.
+
+ The returned node must be freed using sord_node_free.
+*/
SORD_API
SordNode*
sord_node_from_serd_node(SordWorld* world,
@@ -426,41 +436,14 @@ sord_quad_match(const SordQuad x, const SordQuad y);
*/
/**
- Read a file into a model.
-
- The @c base_uri may be NULL, in which case @c uri will be used.
-*/
-SORD_API
-bool
-sord_read_file(SordModel* model,
- SerdEnv* env,
- const uint8_t* uri,
- const uint8_t* base_uri,
- SordNode* graph,
- const uint8_t* blank_prefix);
-
-/**
- Read a file handle into a model.
-*/
-SORD_API
-bool
-sord_read_file_handle(SordModel* model,
- SerdEnv* env,
- FILE* fd,
- const uint8_t* name,
- const uint8_t* base_uri,
- SordNode* graph,
- const uint8_t* blank_prefix);
-
-/**
- Read a string into a model.
+ Return a reader that will read into @c model.
*/
SORD_API
-bool
-sord_read_string(SordModel* model,
- SerdEnv* env,
- const uint8_t* str,
- const uint8_t* base_uri);
+SerdReader*
+sord_new_reader(SordModel* model,
+ SerdEnv* env,
+ SerdSyntax syntax,
+ SordNode* graph);
/**
Write a model to a file.
@@ -469,24 +452,16 @@ SORD_API
bool
sord_write_file(SordModel* model,
SerdEnv* env,
+ SerdSyntax syntax,
const uint8_t* uri,
SordNode* graph,
const uint8_t* blank_prefix);
/**
- Write a model to a file handle.
+ Write a model to a writer.
*/
SORD_API
bool
-sord_write_file_handle(SordModel* model,
- SerdEnv* env,
- FILE* fd,
- const uint8_t* base_uri,
- SordNode* graph,
- const uint8_t* blank_prefix);
-
-SORD_API
-bool
sord_write_writer(SordModel* model,
SerdWriter* writer,
SordNode* graph);
@@ -500,6 +475,7 @@ SORD_API
uint8_t*
sord_write_string(SordModel* model,
SerdEnv* env,
+ SerdSyntax syntax,
const uint8_t* base_uri);
/**
diff --git a/sord/sordmm.hpp b/sord/sordmm.hpp
index 6f9391b..75e4a21 100644
--- a/sord/sordmm.hpp
+++ b/sord/sordmm.hpp
@@ -63,7 +63,7 @@ protected:
/** Collection of RDF namespaces with prefixes. */
class Namespaces : public Wrapper<SerdEnv*> {
public:
- Namespaces() : Wrapper<SerdEnv*>(serd_env_new()) {}
+ Namespaces() : Wrapper<SerdEnv*>(serd_env_new(NULL)) {}
static inline SerdNode string_to_node(SerdType type, const std::string& s) {
SerdNode ret = {
@@ -391,19 +391,19 @@ public:
inline const Node& base_uri() const { return _base; }
inline void load_file(SerdEnv* env,
+ SerdSyntax syntax,
const std::string& uri,
const std::string& base_uri="");
inline void load_string(SerdEnv* env,
+ SerdSyntax syntax,
const char* str,
size_t len,
- const std::string& base_uri,
- const std::string lang="turtle");
+ const std::string& base_uri);
- inline void write_to_file_handle(FILE* fd, const char* lang);
- inline void write_to_file(const std::string& uri, const char* lang);
+ inline void write_to_file(const std::string& uri, SerdSyntax syntax);
- inline std::string write_to_string(const char* lang);
+ inline std::string write_to_string(SerdSyntax syntax);
inline void add_statement(const Node& subject,
const Node& predicate,
@@ -436,15 +436,14 @@ Model::Model(World& world, const std::string& base_uri)
inline void
Model::load_string(SerdEnv* env,
+ SerdSyntax syntax,
const char* str,
size_t len,
- const std::string& base_uri,
- const std::string lang)
+ const std::string& base_uri)
{
- sord_read_string(_c_obj,
- env,
- (const uint8_t*)str,
- (const uint8_t*)base_uri.c_str());
+ SerdReader* reader = sord_new_reader(_c_obj, env, syntax, NULL);
+ serd_reader_read_string(reader, (const uint8_t*)str);
+ serd_reader_free(reader);
}
inline Model::~Model()
@@ -454,41 +453,37 @@ inline Model::~Model()
inline void
Model::load_file(SerdEnv* env,
+ SerdSyntax syntax,
const std::string& data_uri,
const std::string& base_uri)
{
- // FIXME: blank prefix
- sord_read_file(_c_obj, env, (const uint8_t*)data_uri.c_str(),
- (base_uri == "") ? NULL : (const uint8_t*)base_uri.c_str(),
- NULL, (const uint8_t*)"b");
-}
+ if (data_uri.substr(0, 5) != "file:") {
+ return;
+ }
-inline void
-Model::write_to_file_handle(FILE* fd, const char* lang)
-{
- sord_write_file_handle(_c_obj,
- _world.prefixes().c_obj(),
- fd,
- _base.to_u_string(),
- NULL,
- NULL);
+ // FIXME: blank prefix parameter?
+ SerdReader* reader = sord_new_reader(_c_obj, env, syntax, NULL);
+ serd_reader_read_file(reader, (const uint8_t*)(data_uri.c_str() + 5));
+ serd_reader_free(reader);
}
inline void
-Model::write_to_file(const std::string& uri, const char* lang)
+Model::write_to_file(const std::string& uri, SerdSyntax syntax)
{
sord_write_file(_c_obj,
_world.prefixes().c_obj(),
+ syntax,
(const uint8_t*)uri.c_str(),
NULL,
NULL);
}
inline std::string
-Model::write_to_string(const char* lang)
+Model::write_to_string(SerdSyntax syntax)
{
uint8_t* const c_str = sord_write_string(_c_obj,
_world.prefixes().c_obj(),
+ syntax,
base_uri().to_u_string());
std::string ret((const char*)c_str);
free(c_str);
diff --git a/src/sordi.c b/src/sordi.c
index 3430ef4..8843162 100644
--- a/src/sordi.c
+++ b/src/sordi.c
@@ -62,6 +62,20 @@ file_sink(const void* buf, size_t len, void* stream)
return fwrite(buf, 1, len, file);
}
+bool
+set_syntax(SerdSyntax* syntax, const char* name)
+{
+ if (!strcmp(name, "turtle")) {
+ *syntax = SERD_TURTLE;
+ } else if (!strcmp(name, "ntriples")) {
+ *syntax = SERD_NTRIPLES;
+ } else {
+ fprintf(stderr, "Unknown input format `%s'\n", name);
+ return false;
+ }
+ return true;
+}
+
int
main(int argc, char** argv)
{
@@ -70,6 +84,7 @@ main(int argc, char** argv)
}
FILE* in_fd = NULL;
+ SerdSyntax input_syntax = SERD_TURTLE;
SerdSyntax output_syntax = SERD_NTRIPLES;
bool from_file = true;
const uint8_t* in_name = NULL;
@@ -88,17 +103,20 @@ main(int argc, char** argv)
from_file = false;
++a;
break;
+ } else if (argv[a][1] == 'i') {
+ if (++a == argc) {
+ fprintf(stderr, "Missing value for -i\n");
+ return 1;
+ }
+ if (!set_syntax(&input_syntax, argv[a])) {
+ return 1;
+ }
} else if (argv[a][1] == 'o') {
if (++a == argc) {
fprintf(stderr, "Missing value for -o\n");
return 1;
}
- if (!strcmp(argv[a], "turtle")) {
- output_syntax = SERD_TURTLE;
- } else if (!strcmp(argv[a], "ntriples")) {
- output_syntax = SERD_NTRIPLES;
- } else {
- fprintf(stderr, "Unknown output format `%s'\n", argv[a]);
+ if (!set_syntax(&output_syntax, argv[a])) {
return 1;
}
} else {
@@ -136,43 +154,40 @@ main(int argc, char** argv)
}
const uint8_t* base_uri_str = NULL;
- SerdURI base_uri;
if (a < argc) { // Base URI given on command line
- const uint8_t* const in_base_uri = (const uint8_t*)argv[a];
- if (serd_uri_parse((const uint8_t*)in_base_uri, &base_uri)) {
- fprintf(stderr, "Invalid base URI <%s>\n", argv[2]);
- return 1;
- }
- base_uri_str = in_base_uri;
+ base_uri_str = (const uint8_t*)argv[a];
} else if (from_file) { // Use input file URI
base_uri_str = input;
} else {
base_uri_str = (const uint8_t*)"";
}
- if (serd_uri_parse(base_uri_str, &base_uri)) {
+ SerdURI base_uri = SERD_URI_NULL;
+ SerdNode base_uri_node = serd_node_new_uri_from_string(
+ base_uri_str, &base_uri, &base_uri);
+
+ if (!base_uri_node.buf) {
fprintf(stderr, "Invalid base URI <%s>\n", base_uri_str);
return 1;
}
-
- SordWorld* world = sord_world_new();
- SordModel* sord = sord_new(world, SORD_SPO|SORD_OPS, false);
- SerdEnv* env = serd_env_new();
+
+ SordWorld* world = sord_world_new();
+ SordModel* sord = sord_new(world, SORD_SPO|SORD_OPS, false);
+ SerdEnv* env = serd_env_new(&base_uri_node);
+ SerdReader* reader = sord_new_reader(sord, env, input_syntax, NULL);
bool success = false;
if (from_file) {
- success = sord_read_file_handle(sord, env, in_fd, in_name,
- base_uri_str, NULL, NULL);
+ success = !serd_reader_read_file_handle(reader, in_fd, in_name);
} else {
- success = sord_read_string(sord, env, input, base_uri_str);
+ success = !serd_reader_read_string(reader, input);
}
+ serd_reader_free(reader);
+
fprintf(stderr, "Loaded %zu statements\n", sord_num_quads(sord));
- SerdEnv* write_env = serd_env_new();
- SerdNode base_uri_node = serd_node_from_string(SERD_URI, base_uri_str);
- serd_env_set_base_uri(write_env, &base_uri_node);
- serd_env_get_base_uri(write_env, &base_uri);
+ SerdEnv* write_env = serd_env_new(&base_uri_node);
SerdStyle output_style = SERD_STYLE_RESOLVED;
if (output_syntax == SERD_NTRIPLES) {
@@ -199,8 +214,10 @@ main(int argc, char** argv)
serd_env_free(env);
serd_env_free(write_env);
+ serd_node_free(&base_uri_node);
sord_free(sord);
+ sord_world_free(world);
return success ? 0 : 1;
}
diff --git a/src/syntax.c b/src/syntax.c
index d515b47..3519b17 100644
--- a/src/syntax.c
+++ b/src/syntax.c
@@ -26,11 +26,10 @@
#include "sord_internal.h"
typedef struct {
- SerdReader* reader;
- SerdEnv* env;
- SordNode* graph_uri_node;
- SordWorld* world;
- SordModel* sord;
+ SerdEnv* env;
+ SordNode* graph_uri_node;
+ SordWorld* world;
+ SordModel* sord;
} ReadState;
static uint8_t*
@@ -77,8 +76,8 @@ event_statement(void* handle,
subject, NULL, NULL);
SordNode* p = sord_node_from_serd_node(state->world, state->env,
predicate, NULL, NULL);
- SordNode* o = sord_node_from_serd_node(state->world, state->env,
- object, object_datatype, object_lang);
+ SordNode* o = sord_node_from_serd_node(
+ state->world, state->env, object, object_datatype, object_lang);
SordNode* g = NULL;
if (state->graph_uri_node) {
@@ -128,113 +127,82 @@ sord_file_uri_to_path(const uint8_t* uri)
}
SORD_API
-bool
-sord_read_file(SordModel* model,
- SerdEnv* env,
- const uint8_t* uri,
- const uint8_t* base_uri,
- SordNode* graph,
- const uint8_t* blank_prefix)
+SerdReader*
+sord_new_reader(SordModel* model,
+ SerdEnv* env,
+ SerdSyntax syntax,
+ SordNode* graph)
{
- if (!base_uri) {
- base_uri = uri;
- }
-
- const uint8_t* const path = sord_file_uri_to_path(uri);
- if (!path) {
- fprintf(stderr, "Unable to read non-file URI <%s>\n", uri);
- return false;
- }
-
- FILE* const fd = fopen((const char*)path, "r");
- if (!fd) {
- fprintf(stderr, "Failed to open file %s\n", path);
- return false;
- }
+ ReadState* state = malloc(sizeof(ReadState));
+ state->env = env;
+ state->graph_uri_node = graph;
+ state->world = sord_get_world(model);
+ state->sord = model;
+
+ SerdReader* reader = serd_reader_new(
+ syntax, state, free,
+ event_base, event_prefix, event_statement, NULL);
- const bool ret = sord_read_file_handle(
- model, env, fd, path, base_uri, graph, blank_prefix);
- fclose(fd);
- return ret;
+ return reader;
}
-SORD_API
-bool
-sord_read_file_handle(SordModel* model,
- SerdEnv* env,
- FILE* fd,
- const uint8_t* name,
- const uint8_t* base_uri_str_in,
- SordNode* graph,
- const uint8_t* blank_prefix)
+static SerdWriter*
+make_writer(SerdEnv* env,
+ SerdSyntax syntax,
+ const uint8_t* base_uri_str_in,
+ SerdSink sink,
+ void* stream)
{
size_t base_uri_n_bytes = 0;
uint8_t* base_uri_str = copy_string(base_uri_str_in, &base_uri_n_bytes);
-
SerdURI base_uri;
if (serd_uri_parse(base_uri_str, &base_uri)) {
fprintf(stderr, "Invalid base URI <%s>\n", base_uri_str);
}
- SerdNode base_uri_node = serd_node_from_string(SERD_URI, base_uri_str);
- serd_env_set_base_uri(env, &base_uri_node);
-
- ReadState state = { NULL, env, graph,
- sord_get_world(model), model };
+ SerdWriter* writer = serd_writer_new(
+ syntax,
+ SERD_STYLE_ABBREVIATED|SERD_STYLE_CURIED,
+ env,
+ &base_uri,
+ sink,
+ stream);
- state.reader = serd_reader_new(
- SERD_TURTLE, &state,
- event_base, event_prefix, event_statement, NULL);
-
- if (blank_prefix) {
- serd_reader_set_blank_prefix(state.reader, blank_prefix);
- }
-
- const SerdStatus ret = serd_reader_read_file(state.reader, fd, name);
-
- serd_reader_free(state.reader);
- free(base_uri_str);
+ serd_env_foreach(env,
+ (SerdPrefixSink)serd_writer_set_prefix,
+ writer);
- return (ret == SERD_SUCCESS);
+ return writer;
}
-SORD_API
-bool
-sord_read_string(SordModel* model,
- SerdEnv* env,
- const uint8_t* str,
- const uint8_t* base_uri_str_in)
+static size_t
+file_sink(const void* buf, size_t len, void* stream)
{
- size_t base_uri_n_bytes = 0;
- uint8_t* base_uri_str = copy_string(base_uri_str_in, &base_uri_n_bytes);
-
- SerdURI base_uri;
- if (serd_uri_parse(base_uri_str, &base_uri)) {
- fprintf(stderr, "Invalid base URI <%s>\n", base_uri_str);
- }
-
- SerdNode base_uri_node = serd_node_from_string(SERD_URI, base_uri_str);
- serd_env_set_base_uri(env, &base_uri_node);
-
- ReadState state = { NULL, env, NULL,
- sord_get_world(model), model };
-
- state.reader = serd_reader_new(
- SERD_TURTLE, &state,
- event_base, event_prefix, event_statement, NULL);
-
- const SerdStatus status = serd_reader_read_string(state.reader, str);
-
- serd_reader_free(state.reader);
- free(base_uri_str);
+ FILE* file = (FILE*)stream;
+ return fwrite(buf, 1, len, file);
+}
- return (status == SERD_SUCCESS);
+static bool
+sord_write_file_handle(SordModel* model,
+ SerdEnv* env,
+ SerdSyntax syntax,
+ FILE* fd,
+ const uint8_t* base_uri_str_in,
+ SordNode* graph,
+ const uint8_t* blank_prefix)
+{
+ SerdWriter* writer = make_writer(env, syntax, base_uri_str_in,
+ file_sink, fd);
+ sord_write_writer(model, writer, graph);
+ serd_writer_free(writer);
+ return true;
}
SORD_API
bool
sord_write_file(SordModel* model,
SerdEnv* env,
+ SerdSyntax syntax,
const uint8_t* uri,
SordNode* graph,
const uint8_t* blank_prefix)
@@ -250,18 +218,12 @@ sord_write_file(SordModel* model,
return false;
}
- const bool ret = sord_write_file_handle(model, env, fd, uri, graph, blank_prefix);
+ const bool ret = sord_write_file_handle(model, env, syntax, fd,
+ uri, graph, blank_prefix);
fclose(fd);
return ret;
}
-static size_t
-file_sink(const void* buf, size_t len, void* stream)
-{
- FILE* file = (FILE*)stream;
- return fwrite(buf, 1, len, file);
-}
-
static void
write_statement(SordModel* sord, SerdWriter* writer, SordQuad tup,
const SordNode* anon_subject)
@@ -329,48 +291,6 @@ sord_write_writer(SordModel* model,
return true;
}
-static SerdWriter*
-make_writer(SerdEnv* env,
- const uint8_t* base_uri_str_in,
- SerdSink sink,
- void* stream)
-{
- size_t base_uri_n_bytes = 0;
- uint8_t* base_uri_str = copy_string(base_uri_str_in, &base_uri_n_bytes);
- SerdURI base_uri;
- if (serd_uri_parse(base_uri_str, &base_uri)) {
- fprintf(stderr, "Invalid base URI <%s>\n", base_uri_str);
- }
-
- SerdWriter* writer = serd_writer_new(SERD_TURTLE,
- SERD_STYLE_ABBREVIATED|SERD_STYLE_CURIED,
- env,
- &base_uri,
- sink,
- stream);
-
- serd_env_foreach(env,
- (SerdPrefixSink)serd_writer_set_prefix,
- writer);
-
- return writer;
-}
-
-SORD_API
-bool
-sord_write_file_handle(SordModel* model,
- SerdEnv* env,
- FILE* fd,
- const uint8_t* base_uri_str_in,
- SordNode* graph,
- const uint8_t* blank_prefix)
-{
- SerdWriter* writer = make_writer(env, base_uri_str_in, file_sink, fd);
- sord_write_writer(model, writer, graph);
- serd_writer_free(writer);
- return true;
-}
-
struct SerdBuffer {
uint8_t* buf;
size_t len;
@@ -390,10 +310,12 @@ SORD_API
uint8_t*
sord_write_string(SordModel* model,
SerdEnv* env,
+ SerdSyntax syntax,
const uint8_t* base_uri)
{
struct SerdBuffer buf = { NULL, 0 };
- SerdWriter* writer = make_writer(env, base_uri, string_sink, &buf);
+
+ SerdWriter* writer = make_writer(env, syntax, base_uri, string_sink, &buf);
sord_write_writer(model, writer, NULL);
serd_writer_free(writer);
string_sink("", 1, &buf);
diff --git a/wscript b/wscript
index c95e5da..9bb8701 100644
--- a/wscript
+++ b/wscript
@@ -6,7 +6,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)
-SORD_VERSION = '0.2.0'
+SORD_VERSION = '0.3.0'
SORD_MAJOR_VERSION = '0'
# Library version (UNIX style major, minor, micro)
@@ -43,7 +43,7 @@ def configure(conf):
atleast_version='2.0.0', mandatory=True)
autowaf.check_pkg(conf, 'serd-0', uselib_store='SERD',
- atleast_version='0.2.0', mandatory=True)
+ atleast_version='0.3.0', mandatory=True)
conf.env['BUILD_TESTS'] = Options.options.build_tests
conf.env['BUILD_UTILS'] = True