diff options
Diffstat (limited to 'src/inserter.c')
-rw-r--r-- | src/inserter.c | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/src/inserter.c b/src/inserter.c new file mode 100644 index 00000000..d7059c57 --- /dev/null +++ b/src/inserter.c @@ -0,0 +1,93 @@ +/* + Copyright 2011-2018 David Robillard <http://drobilla.net> + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "inserter.h" + +#include <stdlib.h> + +static SerdStatus +serd_inserter_on_base(SerdInserter* inserter, const SerdNode* uri) +{ + return serd_env_set_base_uri(inserter->env, uri); +} + +static SerdStatus +serd_inserter_on_prefix(SerdInserter* inserter, + const SerdNode* name, + const SerdNode* uri) +{ + return serd_env_set_prefix(inserter->env, name, uri); +} + +static SerdStatus +serd_inserter_on_statement(SerdInserter* inserter, + SerdStatementFlags flags, + const SerdNode* graph, + const SerdNode* subject, + const SerdNode* predicate, + const SerdNode* object) +{ + if (!inserter || !subject || !predicate || !object) { + return SERD_ERR_BAD_ARG; + } + + SerdNode* g = graph ? serd_env_expand_node(inserter->env, graph) : NULL; + SerdNode* s = serd_env_expand_node(inserter->env, subject); + SerdNode* p = serd_env_expand_node(inserter->env, predicate); + SerdNode* o = serd_env_expand_node(inserter->env, object); + + const SerdStatus st = serd_model_add(inserter->model, + s ? s : subject, + p ? p : predicate, + o ? o : object, + g ? g : inserter->default_graph); + + serd_node_free(g); + serd_node_free(s); + serd_node_free(p); + serd_node_free(o); + + return st > SERD_FAILURE ? st : SERD_SUCCESS; +} + +SerdInserter* +serd_inserter_new(SerdModel* model, SerdEnv* env, const SerdNode* default_graph) +{ + SerdInserter* inserter = (SerdInserter*)calloc(1, sizeof(SerdInserter)); + inserter->model = model; + inserter->env = env; + inserter->default_graph = serd_node_copy(default_graph); + inserter->iface.handle = inserter; + inserter->iface.base = (SerdBaseSink)serd_inserter_on_base; + inserter->iface.prefix = (SerdPrefixSink)serd_inserter_on_prefix; + inserter->iface.statement = (SerdStatementSink)serd_inserter_on_statement; + return inserter; +} + +void +serd_inserter_free(SerdInserter* inserter) +{ + if (inserter) { + serd_node_free(inserter->default_graph); + free(inserter); + } +} + +const SerdSinkInterface* +serd_inserter_get_sink_interface(SerdInserter* inserter) +{ + return &inserter->iface; +} |