diff options
Diffstat (limited to 'src/serdi.c')
-rw-r--r-- | src/serdi.c | 47 |
1 files changed, 41 insertions, 6 deletions
diff --git a/src/serdi.c b/src/serdi.c index 6efce26e..1f12812b 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -94,6 +94,7 @@ print_usage(const char* name, bool error) fprintf(os, " -h Display this help and exit.\n"); fprintf(os, " -i SYNTAX Input syntax: turtle/ntriples/trig/nquads.\n"); fprintf(os, " -l Lax (non-strict) parsing.\n"); + fprintf(os, " -m Build and serialise a model (no streaming).\n"); fprintf(os, " -o SYNTAX Output syntax: turtle/ntriples/nquads.\n"); fprintf(os, " -p PREFIX Add PREFIX to blank node IDs.\n"); fprintf(os, " -q Suppress all output except data.\n"); @@ -132,6 +133,7 @@ main(int argc, char** argv) bool bulk_write = false; bool full_uris = false; bool lax = false; + bool use_model = false; bool quiet = false; const char* add_prefix = NULL; const char* chop_prefix = NULL; @@ -153,6 +155,8 @@ main(int argc, char** argv) return print_usage(argv[0], false); } else if (argv[a][1] == 'l') { lax = true; + } else if (argv[a][1] == 'm') { + use_model = true; } else if (argv[a][1] == 'q') { quiet = true; } else if (argv[a][1] == 'v') { @@ -205,11 +209,11 @@ main(int argc, char** argv) input_syntax = SERD_TRIG; } + const bool input_has_graphs = + input_syntax == SERD_NQUADS || input_syntax == SERD_TRIG; + if (!output_syntax) { - output_syntax = ( - (input_syntax == SERD_TURTLE || input_syntax == SERD_NTRIPLES) - ? SERD_NTRIPLES - : SERD_NQUADS); + output_syntax = input_has_graphs ? SERD_NQUADS : SERD_NTRIPLES; } SerdNode* base = NULL; @@ -247,9 +251,19 @@ main(int argc, char** argv) world, output_syntax, (SerdStyle)output_style, env, serd_file_sink, out_fd); - SerdReader* reader = serd_reader_new( - world, input_syntax, serd_writer_get_sink_interface(writer)); + SerdReader* reader = NULL; + SerdModel* model = NULL; + SerdInserter* inserter = NULL; + const SerdSinkInterface* sink = NULL; + if (use_model) { + model = serd_model_new(world, SERD_SPO, input_has_graphs); + inserter = serd_inserter_new(model, env, NULL); + sink = serd_inserter_get_sink_interface(inserter); + } else { + sink = serd_writer_get_sink_interface(writer); + } + reader = serd_reader_new(world, input_syntax, sink); serd_reader_set_strict(reader, !lax); if (quiet) { serd_world_set_error_sink(world, quiet_error_sink, NULL); @@ -285,6 +299,27 @@ main(int argc, char** argv) serd_reader_end_stream(reader); + if (status <= SERD_FAILURE && use_model) { + const SerdSinkInterface* wsink = serd_writer_get_sink_interface(writer); + serd_env_foreach(env, wsink->prefix, writer); + + SerdIter* iter = serd_model_begin(model); + for (; !serd_iter_end(iter); serd_iter_next(iter)) { + const SerdStatement* s = serd_iter_get(iter); + if ((status = wsink->statement(wsink->handle, + 0, + serd_statement_get_graph(s), + serd_statement_get_subject(s), + serd_statement_get_predicate(s), + serd_statement_get_object(s)))) { + break; + } + } + serd_iter_free(iter); + } + + serd_inserter_free(inserter); + serd_model_free(model); serd_reader_free(reader); serd_writer_finish(writer); serd_writer_free(writer); |