Cleanup
[carla.git] / source / modules / lilv / sord-0.16.0 / src / syntax.c
blobf35e6f9ba610ad86db4a95c7f7f2b3b1a75d634f
1 /*
2 Copyright 2011-2015 David Robillard <http://drobilla.net>
4 Permission to use, copy, modify, and/or distribute this software for any
5 purpose with or without fee is hereby granted, provided that the above
6 copyright notice and this permission notice appear in all copies.
8 THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <assert.h>
18 #include <stdlib.h>
19 #include <string.h>
21 #include "serd/serd.h"
23 #include "sord_config.h"
24 #include "sord_internal.h"
26 struct SordInserterImpl {
27 SordModel* model;
28 SerdEnv* env;
31 SordInserter*
32 sord_inserter_new(SordModel* model,
33 SerdEnv* env)
35 SordInserter* inserter = (SordInserter*)malloc(sizeof(SordInserter));
36 inserter->model = model;
37 inserter->env = env;
38 return inserter;
41 void
42 sord_inserter_free(SordInserter* inserter)
44 free(inserter);
47 SerdStatus
48 sord_inserter_set_base_uri(SordInserter* inserter,
49 const SerdNode* uri_node)
51 return serd_env_set_base_uri(inserter->env, uri_node);
54 SerdStatus
55 sord_inserter_set_prefix(SordInserter* inserter,
56 const SerdNode* name,
57 const SerdNode* uri_node)
59 return serd_env_set_prefix(inserter->env, name, uri_node);
62 SerdStatus
63 sord_inserter_write_statement(SordInserter* inserter,
64 SerdStatementFlags flags,
65 const SerdNode* graph,
66 const SerdNode* subject,
67 const SerdNode* predicate,
68 const SerdNode* object,
69 const SerdNode* object_datatype,
70 const SerdNode* object_lang)
72 SordWorld* world = sord_get_world(inserter->model);
73 SerdEnv* env = inserter->env;
75 SordNode* g = sord_node_from_serd_node(world, env, graph, NULL, NULL);
76 SordNode* s = sord_node_from_serd_node(world, env, subject, NULL, NULL);
77 SordNode* p = sord_node_from_serd_node(world, env, predicate, NULL, NULL);
78 SordNode* o = sord_node_from_serd_node(world, env, object,
79 object_datatype, object_lang);
81 if (!s || !p || !o) {
82 return SERD_ERR_BAD_ARG;
85 const SordQuad tup = { s, p, o, g };
86 sord_add(inserter->model, tup);
88 sord_node_free(world, o);
89 sord_node_free(world, p);
90 sord_node_free(world, s);
91 sord_node_free(world, g);
93 return SERD_SUCCESS;
96 SORD_API
97 SerdReader*
98 sord_new_reader(SordModel* model,
99 SerdEnv* env,
100 SerdSyntax syntax,
101 SordNode* graph)
103 SordInserter* inserter = sord_inserter_new(model, env);
105 SerdReader* reader = serd_reader_new(
106 syntax, inserter, (void (*)(void*))sord_inserter_free,
107 (SerdBaseSink)sord_inserter_set_base_uri,
108 (SerdPrefixSink)sord_inserter_set_prefix,
109 (SerdStatementSink)sord_inserter_write_statement,
110 NULL);
112 if (graph) {
113 serd_reader_set_default_graph(reader, sord_node_to_serd_node(graph));
116 return reader;
119 static SerdStatus
120 write_statement(SordModel* sord,
121 SerdWriter* writer,
122 SordQuad tup,
123 SerdStatementFlags flags)
125 const SordNode* s = tup[SORD_SUBJECT];
126 const SordNode* p = tup[SORD_PREDICATE];
127 const SordNode* o = tup[SORD_OBJECT];
128 const SordNode* d = sord_node_get_datatype(o);
129 const SerdNode* ss = sord_node_to_serd_node(s);
130 const SerdNode* sp = sord_node_to_serd_node(p);
131 const SerdNode* so = sord_node_to_serd_node(o);
132 const SerdNode* sd = sord_node_to_serd_node(d);
134 const char* lang_str = sord_node_get_language(o);
135 size_t lang_len = lang_str ? strlen(lang_str) : 0;
136 SerdNode language = SERD_NODE_NULL;
137 if (lang_str) {
138 language.type = SERD_LITERAL;
139 language.n_bytes = lang_len;
140 language.n_chars = lang_len;
141 language.buf = (const uint8_t*)lang_str;
144 // TODO: Subject abbreviation
146 if (sord_node_is_inline_object(s) && !(flags & SERD_ANON_CONT)) {
147 return SERD_SUCCESS;
150 SerdStatus st = SERD_SUCCESS;
151 if (sord_node_is_inline_object(o)) {
152 SordQuad sub_pat = { o, 0, 0, 0 };
153 SordIter* sub_iter = sord_find(sord, sub_pat);
155 SerdStatementFlags start_flags = flags
156 | ((sub_iter) ? SERD_ANON_O_BEGIN : SERD_EMPTY_O);
158 st = serd_writer_write_statement(
159 writer, start_flags, NULL, ss, sp, so, sd, &language);
161 if (!st && sub_iter) {
162 flags |= SERD_ANON_CONT;
163 for (; !st && !sord_iter_end(sub_iter); sord_iter_next(sub_iter)) {
164 SordQuad sub_tup;
165 sord_iter_get(sub_iter, sub_tup);
166 st = write_statement(sord, writer, sub_tup, flags);
168 sord_iter_free(sub_iter);
169 serd_writer_end_anon(writer, so);
171 } else {
172 st = serd_writer_write_statement(
173 writer, flags, NULL, ss, sp, so, sd, &language);
176 return st;
179 bool
180 sord_write(SordModel* model,
181 SerdWriter* writer,
182 SordNode* graph)
184 SordQuad pat = { 0, 0, 0, graph };
185 SordIter* iter = sord_find(model, pat);
186 return sord_write_iter(iter, writer);
189 bool
190 sord_write_iter(SordIter* iter,
191 SerdWriter* writer)
193 if (!iter) {
194 return false;
197 SordModel* model = (SordModel*)sord_iter_get_model(iter);
198 SerdStatus st = SERD_SUCCESS;
199 for (; !st && !sord_iter_end(iter); sord_iter_next(iter)) {
200 SordQuad tup;
201 sord_iter_get(iter, tup);
202 st = write_statement(model, writer, tup, 0);
204 sord_iter_free(iter);
206 return !st;