summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2011-01-28 23:36:32 +0000
committerDavid Robillard <d@drobilla.net>2011-01-28 23:36:32 +0000
commit9d9cd92a474f8ad5b8c75d3bedede0960281ef48 (patch)
tree91ff2827a28ddaaf5fce3a543eb947f7aea6c692
parent04da598576390d6bf0ed73865b6b3272fdf2840b (diff)
downloadsord-9d9cd92a474f8ad5b8c75d3bedede0960281ef48.tar.gz
sord-9d9cd92a474f8ad5b8c75d3bedede0960281ef48.tar.bz2
sord-9d9cd92a474f8ad5b8c75d3bedede0960281ef48.zip
Syntax support via Serd.
Preliminary syntax-using command line program. git-svn-id: http://svn.drobilla.net/sord/trunk@9 3d64ff67-21c5-427c-a301-fe4f08042e5a
-rw-r--r--sord/sord.h15
-rw-r--r--src/sord.c7
-rw-r--r--src/sord_test.c6
-rw-r--r--src/sordi.c166
-rw-r--r--src/syntax.c176
-rw-r--r--wscript23
6 files changed, 386 insertions, 7 deletions
diff --git a/sord/sord.h b/sord/sord.h
index 49eb9d1..0820f9b 100644
--- a/sord/sord.h
+++ b/sord/sord.h
@@ -78,7 +78,7 @@ typedef enum {
/** Create a new store. */
SORD_API
Sord
-sord_new();
+sord_new(void);
/** Set a store option.
* Options are RDF properties (with the store as the subject), with the
@@ -204,6 +204,10 @@ SORD_API
const char*
sord_node_get_string(SordNode node);
+SORD_API
+const char*
+sord_node_get_string_counted(SordNode node, size_t* len);
+
/** Set an opaque user pointer on a node.
* Sord does not interpret the user pointer in any way, but it will be
* preserved on the node until the cache is cleared.
@@ -338,6 +342,15 @@ bool
sord_tuple_match(const SordTuple x, const SordTuple y);
/** @} */
+/** @name Serialisation
+ * @{
+ */
+
+SORD_API
+bool
+sord_read_file(Sord sord, const uint8_t* uri);
+
+/** @} */
/** @} */
diff --git a/src/sord.c b/src/sord.c
index a720dae..00b86ed 100644
--- a/src/sord.c
+++ b/src/sord.c
@@ -808,6 +808,13 @@ sord_node_get_string(SordNode ref)
return ref->buf;
}
+const char*
+sord_node_get_string_counted(SordNode ref, size_t* n_bytes)
+{
+ *n_bytes = ref->n_bytes;
+ return ref->buf;
+}
+
void
sord_node_set_user_data(SordNode ref, void* user_data)
{
diff --git a/src/sord_test.c b/src/sord_test.c
index 371e5c1..3df3ccc 100644
--- a/src/sord_test.c
+++ b/src/sord_test.c
@@ -266,7 +266,7 @@ main(int argc, char** argv)
sord_free(NULL); // Shouldn't crash
// Create with default options
- Sord sord = sord_new("testdb");
+ Sord sord = sord_new();
sord_set_option(sord, "http://unknown", "something", SORD_LITERAL, NULL, NULL);
sord_open(sord);
generate(sord, n_tuples, n_objects_per);
@@ -321,7 +321,7 @@ main(int argc, char** argv)
const size_t option_len = strlen(option);
for (int i = 0; i < 6; ++i) {
strncpy(option + option_len - 3, index_names[i], 3);
- sord = sord_new("testdb");
+ sord = sord_new();
sord_set_option(sord, option, "true", SORD_LITERAL, NULL, NULL);
printf("Testing Index `%s'\n", index_names[i]);
sord_open(sord);
@@ -332,7 +332,7 @@ main(int argc, char** argv)
}
free(option);
- sord = sord_new("testdb");
+ sord = sord_new();
sord_open(sord);
if (test_write(sord, n_tuples, n_objects_per))
goto fail;
diff --git a/src/sordi.c b/src/sordi.c
new file mode 100644
index 0000000..8b5a213
--- /dev/null
+++ b/src/sordi.c
@@ -0,0 +1,166 @@
+/* Sord, a lightweight RDF syntax library.
+ * Copyright 2011 David Robillard <d@drobilla.net>
+ *
+ * Sord is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Sord is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "serd/serd.h"
+#include "sord/sord.h"
+#include "sord-config.h"
+
+typedef struct {
+ SerdWriter writer;
+ SerdEnv env;
+ SerdNode base_uri_node;
+ SerdURI base_uri;
+ Sord sord;
+} State;
+
+int
+print_version()
+{
+ printf("sordi " SORD_VERSION " <http://drobilla.net/software/serd>\n");
+ printf("Copyright (C) 2011 David Robillard <http://drobilla.net>.\n"
+ "\nLicense: GNU LGPL version 3 or later "
+ "<http://gnu.org/licenses/lgpl.html>.\n"
+ "This is free software; you are free to change and redistribute it."
+ "\nThere is NO WARRANTY, to the extent permitted by law.\n");
+ return 0;
+}
+
+int
+print_usage(const char* name, bool error)
+{
+ FILE* const os = error ? stderr : stdout;
+ fprintf(os, "Usage: %s [OPTION]... INPUT [BASE_URI]\n", name);
+ fprintf(os, "Load and re-serialise RDF syntax.\n\n");
+ fprintf(os, " -h Display this help and exit\n");
+ fprintf(os, " -o SYNTAX Output syntax (`turtle' or `ntriples')\n");
+ fprintf(os, " -v Display version information and exit\n");
+ return error ? 1 : 0;
+}
+
+static size_t
+file_sink(const void* buf, size_t len, void* stream)
+{
+ FILE* file = (FILE*)stream;
+ return fwrite(buf, 1, len, file);
+}
+
+static inline SerdNode
+serd_node_from_sord_node(const SordNode n)
+{
+ size_t n_bytes = 0;
+ const char* buf = sord_node_get_string_counted(n, &n_bytes);
+ SerdNode sn = { SERD_NOTHING, n_bytes, n_bytes - 1, (const uint8_t*)buf };
+ // FIXME: UTF-8
+ switch (sord_node_get_type(n)) {
+ case SORD_URI:
+ sn.type = SERD_URI;
+ break;
+ case SORD_BLANK:
+ sn.type = SERD_BLANK_ID;
+ break;
+ case SORD_LITERAL:
+ sn.type = SERD_LITERAL;
+ break;
+ }
+ return sn;
+}
+
+int
+main(int argc, char** argv)
+{
+ if (argc < 2) {
+ return print_usage(argv[0], true);
+ }
+
+ SerdSyntax output_syntax = SERD_NTRIPLES;
+ int a = 1;
+ for (; a < argc && argv[a][0] == '-'; ++a) {
+ if (argv[a][1] == 'h') {
+ return print_usage(argv[0], false);
+ } else if (argv[a][1] == 'v') {
+ return print_version();
+ } 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]);
+ return 1;
+ }
+ } else {
+ fprintf(stderr, "unknown option `%s'\n", argv[a]);
+ return print_usage(argv[0], true);
+ }
+ }
+
+ const uint8_t* input = (const uint8_t*)argv[a++];
+
+ Sord sord = sord_new();
+ sord_open(sord);
+
+ bool success = sord_read_file(sord, input);
+
+ printf("loaded %u statements\n", sord_num_nodes(sord));
+
+ SerdURI base_uri;
+ if (!serd_uri_parse(input, &base_uri)) {
+ fprintf(stderr, "bad input URI `%s'\n", input);
+ return 1;
+ }
+
+ SerdEnv env = serd_env_new();
+ SerdWriter writer = serd_writer_new(SERD_TURTLE, SERD_STYLE_ABBREVIATED,
+ env, &base_uri, file_sink, stdout);
+
+ // Query
+ SordTuple pat = { 0, 0, 0, 0 };
+ SordIter iter = sord_find(sord, pat);
+ for (; !sord_iter_is_end(iter); sord_iter_increment(iter)) {
+ SordTuple tup;
+ sord_iter_get(iter, tup);
+ SordNode s, p, o;
+ sord_tuple_load(sord, tup, &s, &p, &o);
+ /*printf("%s %s %s .\n",
+ sord_node_get_string(s),
+ sord_node_get_string(p),
+ sord_node_get_string(o));*/
+
+ SerdNode ss = serd_node_from_sord_node(s);
+ SerdNode sp = serd_node_from_sord_node(p);
+ SerdNode so = serd_node_from_sord_node(o);
+ serd_writer_write_statement(
+ writer, NULL, &ss, &sp, &so, NULL, NULL);
+ }
+
+ serd_writer_finish(writer);
+ serd_writer_free(writer);
+
+ serd_env_free(env);
+
+ sord_free(sord);
+
+ return success ? 0 : 1;
+}
diff --git a/src/syntax.c b/src/syntax.c
new file mode 100644
index 0000000..4a58ac9
--- /dev/null
+++ b/src/syntax.c
@@ -0,0 +1,176 @@
+/* Sord, a lightweight RDF model library.
+ * Copyright 2010-2011 David Robillard <d@drobilla.net>
+ *
+ * Sord is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Sord is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "serd/serd.h"
+
+#include "sord-config.h"
+#include "sord/sord.h"
+
+typedef struct {
+ SerdReader reader;
+ SerdEnv env;
+ SerdNode base_uri_node;
+ SerdURI base_uri;
+ Sord sord;
+} ReadState;
+
+static uint8_t*
+copy_string(const uint8_t* str, size_t* n_bytes)
+{
+ const size_t len = strlen((const char*)str);
+ uint8_t* const ret = malloc(len + 1);
+ memcpy(ret, str, len + 1);
+ *n_bytes = len + 1;
+ return ret;
+}
+
+static bool
+event_base(void* handle,
+ const SerdNode* uri_node)
+{
+ ReadState* const state = (ReadState*)handle;
+ SerdNode base_uri_node = *uri_node;
+ SerdURI base_uri;
+ if (!serd_uri_parse(uri_node->buf, &base_uri)) {
+ return false;
+ }
+
+ SerdURI abs_base_uri;
+ if (!serd_uri_resolve(&base_uri, &state->base_uri, &abs_base_uri)) {
+ fprintf(stderr, "error: failed to resolve new base URI\n");
+ return false;
+ }
+ base_uri_node = serd_node_new_uri(&abs_base_uri, &base_uri);
+
+ serd_node_free(&state->base_uri_node);
+ state->base_uri_node = base_uri_node;
+ state->base_uri = base_uri;
+ return true;
+}
+
+static bool
+event_prefix(void* handle,
+ const SerdNode* name,
+ const SerdNode* uri_node)
+{
+ ReadState* const state = (ReadState*)handle;
+ if (!serd_uri_string_has_scheme(uri_node->buf)) {
+ SerdURI uri;
+ if (!serd_uri_parse(uri_node->buf, &uri)) {
+ return false;
+ }
+ SerdURI abs_uri;
+ if (!serd_uri_resolve(&uri, &state->base_uri, &abs_uri)) {
+ return false;
+ }
+ SerdURI base_uri;
+ SerdNode base_uri_node = serd_node_new_uri(&abs_uri, &base_uri);
+ serd_env_add(state->env, name, &base_uri_node);
+ serd_node_free(&base_uri_node);
+ } else {
+ serd_env_add(state->env, name, uri_node);
+ }
+ return true;
+}
+
+static inline SordID
+sord_node_from_serd_node(ReadState* state, const SerdNode* sn)
+{
+ return sord_get_uri_counted(state->sord, true, (const char*)sn->buf, sn->n_chars);
+}
+
+static bool
+event_statement(void* handle,
+ const SerdNode* graph,
+ const SerdNode* subject,
+ const SerdNode* predicate,
+ const SerdNode* object,
+ const SerdNode* object_datatype,
+ const SerdNode* object_lang)
+{
+ ReadState* const state = (ReadState*)handle;
+
+ SordTuple tup;
+ tup[0] = sord_node_from_serd_node(state, subject);
+ tup[1] = sord_node_from_serd_node(state, predicate);
+ tup[2] = sord_node_from_serd_node(state, object);
+ tup[3] = (graph && graph->buf)
+ ? sord_node_from_serd_node(state, graph)
+ : NULL;
+
+ sord_add(state->sord, tup);
+
+ return true;
+}
+
+SORD_API
+bool
+sord_read_file(Sord sord, const uint8_t* input)
+{
+ const uint8_t* filename = NULL;
+ if (serd_uri_string_has_scheme(input)) {
+ // INPUT is an absolute URI, ensure it a file and chop scheme
+ if (strncmp((const char*)input, "file:", 5)) {
+ fprintf(stderr, "unsupported URI scheme `%s'\n", input);
+ return 1;
+ } else if (!strncmp((const char*)input, "file://", 7)) {
+ filename = input + 7;
+ } else {
+ filename = input + 5;
+ }
+ } else {
+ filename = input;
+ }
+
+ FILE* in_fd = fopen((const char*)input, "r");
+ if (!in_fd) {
+ fprintf(stderr, "failed to open file %s\n", input);
+ return 1;
+ }
+
+ size_t base_uri_n_bytes = 0;
+ uint8_t* base_uri_str = copy_string(input, &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);
+ }
+
+ SerdEnv env = serd_env_new();
+
+ const SerdNode base_uri_node = { SERD_URI,
+ base_uri_n_bytes,
+ base_uri_n_bytes - 1, // FIXME: UTF-8
+ base_uri_str };
+
+ ReadState state = { NULL, env, base_uri_node, base_uri, sord };
+
+ state.reader = serd_reader_new(
+ SERD_TURTLE, &state,
+ event_base, event_prefix, event_statement, NULL);
+
+ const bool success = serd_reader_read_file(state.reader, in_fd, input);
+
+ serd_reader_free(state.reader);
+ serd_env_free(state.env);
+ serd_node_free(&state.base_uri_node);
+ fclose(in_fd);
+
+ return success;
+}
diff --git a/wscript b/wscript
index 852cd30..7480e5d 100644
--- a/wscript
+++ b/wscript
@@ -35,9 +35,13 @@ def configure(conf):
conf.env.append_value('CCFLAGS', '-std=c99')
autowaf.check_pkg(conf, 'glib-2.0', uselib_store='GLIB',
- atleast_version='2.0.0', mandatory=True)
+ atleast_version='2.0.0', mandatory=True)
+
+ autowaf.check_pkg(conf, 'serd', uselib_store='SERD',
+ atleast_version='0.1.0', mandatory=False)
conf.env['BUILD_TESTS'] = Options.options.build_tests
+ conf.env['BUILD_UTILS'] = conf.env['HAVE_SERD'] != 0
dump = Options.options.dump.split(',')
all = 'all' in dump
@@ -51,6 +55,7 @@ def configure(conf):
conf.define('SORD_VERSION', SORD_VERSION)
conf.write_config_header('sord-config.h')
+ autowaf.display_msg(conf, "Utilities", str(conf.env['BUILD_UTILS']))
autowaf.display_msg(conf, "Unit tests", str(conf.env['BUILD_TESTS']))
autowaf.display_msg(conf, "Debug dumping", dump)
print
@@ -64,7 +69,7 @@ def build(bld):
# Library
obj = bld(features = 'c cshlib')
- obj.source = 'src/sord.c'
+ obj.source = 'src/sord.c src/syntax.c'
obj.includes = ['.', './src']
obj.name = 'libsord'
obj.target = 'sord'
@@ -77,7 +82,7 @@ def build(bld):
if bld.env['BUILD_TESTS']:
# Static library (for unit test code coverage)
obj = bld(features = 'c cstlib')
- obj.source = 'src/sord.c'
+ obj.source = 'src/sord.c src/syntax.c'
obj.includes = ['.', './src']
obj.name = 'libsord_static'
obj.target = 'sord_static'
@@ -97,6 +102,18 @@ def build(bld):
obj.cflags = [ '-fprofile-arcs', '-ftest-coverage' ]
autowaf.use_lib(bld, obj, 'GLIB')
+ # Unit test programa
+ if bld.env['BUILD_UTILS']:
+ obj = bld(features = 'c cprogram')
+ obj.source = 'src/sordi.c'
+ obj.includes = ['.', './src']
+ obj.use = 'libsord_static'
+ obj.linkflags = '-lgcov'
+ obj.target = 'sordi_static'
+ obj.install_path = ''
+ obj.cflags = [ '-fprofile-arcs', '-ftest-coverage' ]
+ autowaf.use_lib(bld, obj, 'SERD')
+
# Documentation
autowaf.build_dox(bld, 'SORD', SORD_VERSION, top, out)