Mercurial > hg > piper-cpp
comparison ext/sord/src/syntax.c @ 226:c5cdc9e6a4bf
Add these external library files
author | Chris Cannam <cannam@all-day-breakfast.com> |
---|---|
date | Fri, 09 Jun 2017 16:41:31 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
225:025b3e2f7c17 | 226:c5cdc9e6a4bf |
---|---|
1 /* | |
2 Copyright 2011-2015 David Robillard <http://drobilla.net> | |
3 | |
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. | |
7 | |
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. | |
15 */ | |
16 | |
17 #include <assert.h> | |
18 #include <stdlib.h> | |
19 #include <string.h> | |
20 | |
21 #include "serd/serd.h" | |
22 | |
23 #include "sord_config.h" | |
24 #include "sord_internal.h" | |
25 | |
26 struct SordInserterImpl { | |
27 SordModel* model; | |
28 SerdEnv* env; | |
29 }; | |
30 | |
31 SordInserter* | |
32 sord_inserter_new(SordModel* model, | |
33 SerdEnv* env) | |
34 { | |
35 SordInserter* inserter = (SordInserter*)malloc(sizeof(SordInserter)); | |
36 inserter->model = model; | |
37 inserter->env = env; | |
38 return inserter; | |
39 } | |
40 | |
41 void | |
42 sord_inserter_free(SordInserter* inserter) | |
43 { | |
44 free(inserter); | |
45 } | |
46 | |
47 SerdStatus | |
48 sord_inserter_set_base_uri(SordInserter* inserter, | |
49 const SerdNode* uri_node) | |
50 { | |
51 return serd_env_set_base_uri(inserter->env, uri_node); | |
52 } | |
53 | |
54 SerdStatus | |
55 sord_inserter_set_prefix(SordInserter* inserter, | |
56 const SerdNode* name, | |
57 const SerdNode* uri_node) | |
58 { | |
59 return serd_env_set_prefix(inserter->env, name, uri_node); | |
60 } | |
61 | |
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) | |
71 { | |
72 SordWorld* world = sord_get_world(inserter->model); | |
73 SerdEnv* env = inserter->env; | |
74 | |
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); | |
80 | |
81 if (!s || !p || !o) { | |
82 return SERD_ERR_BAD_ARG; | |
83 } | |
84 | |
85 const SordQuad tup = { s, p, o, g }; | |
86 sord_add(inserter->model, tup); | |
87 | |
88 sord_node_free(world, o); | |
89 sord_node_free(world, p); | |
90 sord_node_free(world, s); | |
91 sord_node_free(world, g); | |
92 | |
93 return SERD_SUCCESS; | |
94 } | |
95 | |
96 SORD_API | |
97 SerdReader* | |
98 sord_new_reader(SordModel* model, | |
99 SerdEnv* env, | |
100 SerdSyntax syntax, | |
101 SordNode* graph) | |
102 { | |
103 SordInserter* inserter = sord_inserter_new(model, env); | |
104 | |
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); | |
111 | |
112 if (graph) { | |
113 serd_reader_set_default_graph(reader, sord_node_to_serd_node(graph)); | |
114 } | |
115 | |
116 return reader; | |
117 } | |
118 | |
119 static SerdStatus | |
120 write_statement(SordModel* sord, | |
121 SerdWriter* writer, | |
122 SordQuad tup, | |
123 SerdStatementFlags flags) | |
124 { | |
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); | |
133 | |
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; | |
142 }; | |
143 | |
144 // TODO: Subject abbreviation | |
145 | |
146 if (sord_node_is_inline_object(s) && !(flags & SERD_ANON_CONT)) { | |
147 return SERD_SUCCESS; | |
148 } | |
149 | |
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); | |
154 | |
155 SerdStatementFlags start_flags = flags | |
156 | ((sub_iter) ? SERD_ANON_O_BEGIN : SERD_EMPTY_O); | |
157 | |
158 st = serd_writer_write_statement( | |
159 writer, start_flags, NULL, ss, sp, so, sd, &language); | |
160 | |
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); | |
167 } | |
168 sord_iter_free(sub_iter); | |
169 serd_writer_end_anon(writer, so); | |
170 } | |
171 } else { | |
172 st = serd_writer_write_statement( | |
173 writer, flags, NULL, ss, sp, so, sd, &language); | |
174 } | |
175 | |
176 return st; | |
177 } | |
178 | |
179 bool | |
180 sord_write(SordModel* model, | |
181 SerdWriter* writer, | |
182 SordNode* graph) | |
183 { | |
184 SordQuad pat = { 0, 0, 0, graph }; | |
185 SordIter* iter = sord_find(model, pat); | |
186 return sord_write_iter(iter, writer); | |
187 } | |
188 | |
189 bool | |
190 sord_write_iter(SordIter* iter, | |
191 SerdWriter* writer) | |
192 { | |
193 if (!iter) { | |
194 return false; | |
195 } | |
196 | |
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); | |
203 } | |
204 sord_iter_free(iter); | |
205 | |
206 return !st; | |
207 } |