summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/sordi.c67
-rw-r--r--src/syntax.c204
2 files changed, 105 insertions, 166 deletions
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);