| 1 |
// Copyright 2012-2026 David Robillard <[email protected]> |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include <sratom/sratom.h> |
| 5 |
|
| 6 |
#include <lv2/atom/atom.h> |
| 7 |
#include <lv2/atom/forge.h> |
| 8 |
#include <lv2/atom/util.h> |
| 9 |
#include <lv2/midi/midi.h> |
| 10 |
#include <lv2/urid/urid.h> |
| 11 |
#include <serd/serd.h> |
| 12 |
#include <sord/sord.h> |
| 13 |
|
| 14 |
#include <assert.h> |
| 15 |
#include <ctype.h> |
| 16 |
#include <stdio.h> |
| 17 |
#include <stdlib.h> |
| 18 |
#include <string.h> |
| 19 |
|
| 20 |
#define NS_RDF (const uint8_t*)"http://www.w3.org/1999/02/22-rdf-syntax-ns#" |
| 21 |
#define NS_XSD (const uint8_t*)"http://www.w3.org/2001/XMLSchema#" |
| 22 |
|
| 23 |
#define USTR(str) ((const uint8_t*)(str)) |
| 24 |
|
| 25 |
static const SerdStyle style = |
| 26 |
(SerdStyle)(SERD_STYLE_ABBREVIATED | SERD_STYLE_RESOLVED | SERD_STYLE_CURIED); |
| 27 |
|
| 28 |
typedef enum { MODE_SUBJECT, MODE_BODY, MODE_SEQUENCE } ReadMode; |
| 29 |
|
| 30 |
typedef struct { |
| 31 |
Sratom* sratom; |
| 32 |
const SerdNode* subject; |
| 33 |
const SerdNode* predicate; |
| 34 |
SerdStatementFlags flags; |
| 35 |
uint8_t idbuf[12]; |
| 36 |
uint8_t nodebuf[12]; |
| 37 |
SerdNode id; |
| 38 |
SerdNode node; |
| 39 |
} WriteContext; |
| 40 |
|
| 41 |
struct SratomImpl { |
| 42 |
LV2_URID_Map* map; |
| 43 |
LV2_Atom_Forge forge; |
| 44 |
SerdEnv* env; |
| 45 |
SerdNode base_uri; |
| 46 |
SerdURI base; |
| 47 |
SerdStatementSink write_statement; |
| 48 |
SerdEndSink end_anon; |
| 49 |
void* handle; |
| 50 |
LV2_URID atom_Event; |
| 51 |
LV2_URID atom_frameTime; |
| 52 |
LV2_URID atom_beatTime; |
| 53 |
LV2_URID midi_MidiEvent; |
| 54 |
unsigned next_id; |
| 55 |
SratomObjectMode object_mode; |
| 56 |
uint32_t seq_unit; |
| 57 |
struct { |
| 58 |
SordNode* atom_childType; |
| 59 |
SordNode* atom_frameTime; |
| 60 |
SordNode* atom_beatTime; |
| 61 |
SordNode* rdf_first; |
| 62 |
SordNode* rdf_rest; |
| 63 |
SordNode* rdf_type; |
| 64 |
SordNode* rdf_value; |
| 65 |
SordNode* xsd_base64Binary; |
| 66 |
} nodes; |
| 67 |
|
| 68 |
bool pretty_numbers; |
| 69 |
}; |
| 70 |
|
| 71 |
static void |
| 72 |
read_node(Sratom* sratom, |
| 73 |
LV2_Atom_Forge* forge, |
| 74 |
SordWorld* world, |
| 75 |
SordModel* model, |
| 76 |
const SordNode* node, |
| 77 |
ReadMode mode); |
| 78 |
|
| 79 |
Sratom* |
| 80 |
sratom_new(LV2_URID_Map* map) |
| 81 |
{ |
| 82 |
Sratom* sratom = (Sratom*)calloc(1, sizeof(Sratom)); |
| 83 |
if (sratom) { |
| 84 |
sratom->map = map; |
| 85 |
sratom->atom_Event = map->map(map->handle, LV2_ATOM__Event); |
| 86 |
sratom->atom_frameTime = map->map(map->handle, LV2_ATOM__frameTime); |
| 87 |
sratom->atom_beatTime = map->map(map->handle, LV2_ATOM__beatTime); |
| 88 |
sratom->midi_MidiEvent = map->map(map->handle, LV2_MIDI__MidiEvent); |
| 89 |
sratom->object_mode = SRATOM_OBJECT_MODE_BLANK; |
| 90 |
lv2_atom_forge_init(&sratom->forge, map); |
| 91 |
} |
| 92 |
return sratom; |
| 93 |
} |
| 94 |
|
| 95 |
void |
| 96 |
sratom_free(Sratom* sratom) |
| 97 |
{ |
| 98 |
if (sratom) { |
| 99 |
serd_node_free(&sratom->base_uri); |
| 100 |
free(sratom); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
void |
| 105 |
sratom_set_env(Sratom* sratom, SerdEnv* env) |
| 106 |
{ |
| 107 |
sratom->env = env; |
| 108 |
} |
| 109 |
|
| 110 |
void |
| 111 |
sratom_set_sink(Sratom* sratom, |
| 112 |
const char* base_uri, |
| 113 |
SerdStatementSink sink, |
| 114 |
SerdEndSink end_sink, |
| 115 |
void* handle) |
| 116 |
{ |
| 117 |
if (base_uri) { |
| 118 |
serd_node_free(&sratom->base_uri); |
| 119 |
sratom->base_uri = |
| 120 |
serd_node_new_uri_from_string(USTR(base_uri), NULL, &sratom->base); |
| 121 |
} |
| 122 |
sratom->write_statement = sink; |
| 123 |
sratom->end_anon = end_sink; |
| 124 |
sratom->handle = handle; |
| 125 |
} |
| 126 |
|
| 127 |
void |
| 128 |
sratom_set_pretty_numbers(Sratom* sratom, bool pretty_numbers) |
| 129 |
{ |
| 130 |
sratom->pretty_numbers = pretty_numbers; |
| 131 |
} |
| 132 |
|
| 133 |
void |
| 134 |
sratom_set_object_mode(Sratom* sratom, SratomObjectMode object_mode) |
| 135 |
{ |
| 136 |
sratom->object_mode = object_mode; |
| 137 |
} |
| 138 |
|
| 139 |
static void |
| 140 |
gensym(SerdNode* out, char c, unsigned num) |
| 141 |
{ |
| 142 |
out->n_bytes = out->n_chars = snprintf((char*)out->buf, 12, "%c%u", c, num); |
| 143 |
} |
| 144 |
|
| 145 |
static SerdStatus |
| 146 |
list_append(Sratom* sratom, |
| 147 |
LV2_URID_Unmap* unmap, |
| 148 |
unsigned* flags, |
| 149 |
SerdNode* s, |
| 150 |
SerdNode* p, |
| 151 |
SerdNode* node, |
| 152 |
uint32_t size, |
| 153 |
uint32_t type, |
| 154 |
const void* body) |
| 155 |
{ |
| 156 |
int st = 0; |
| 157 |
|
| 158 |
// Generate a list node |
| 159 |
gensym(node, 'l', sratom->next_id); |
| 160 |
if ((st = sratom->write_statement( |
| 161 |
sratom->handle, *flags, NULL, s, p, node, NULL, NULL))) { |
| 162 |
return (SerdStatus)st; |
| 163 |
} |
| 164 |
|
| 165 |
// _:node rdf:first value |
| 166 |
*flags = SERD_LIST_CONT; |
| 167 |
*p = serd_node_from_string(SERD_URI, NS_RDF "first"); |
| 168 |
st = sratom_write(sratom, unmap, *flags, node, p, type, size, body); |
| 169 |
|
| 170 |
// Set subject to node and predicate to rdf:rest for next time |
| 171 |
gensym(node, 'l', ++sratom->next_id); |
| 172 |
*s = *node; |
| 173 |
*p = serd_node_from_string(SERD_URI, NS_RDF "rest"); |
| 174 |
return (SerdStatus)st; |
| 175 |
} |
| 176 |
|
| 177 |
static SerdStatus |
| 178 |
list_end(SerdStatementSink sink, |
| 179 |
void* handle, |
| 180 |
const unsigned flags, |
| 181 |
SerdNode* s, |
| 182 |
SerdNode* p) |
| 183 |
{ |
| 184 |
// _:node rdf:rest rdf:nil |
| 185 |
const SerdNode nil = serd_node_from_string(SERD_URI, NS_RDF "nil"); |
| 186 |
return sink(handle, flags, NULL, s, p, &nil, NULL, NULL); |
| 187 |
} |
| 188 |
|
| 189 |
static SerdStatus |
| 190 |
start_object(Sratom* sratom, |
| 191 |
uint32_t* flags, |
| 192 |
const SerdNode* subject, |
| 193 |
const SerdNode* predicate, |
| 194 |
const SerdNode* node, |
| 195 |
const char* type) |
| 196 |
{ |
| 197 |
SerdStatus st = SERD_SUCCESS; |
| 198 |
|
| 199 |
if (subject && predicate) { |
| 200 |
st = sratom->write_statement(sratom->handle, |
| 201 |
*flags | SERD_ANON_O_BEGIN, |
| 202 |
NULL, |
| 203 |
subject, |
| 204 |
predicate, |
| 205 |
node, |
| 206 |
NULL, |
| 207 |
NULL); |
| 208 |
|
| 209 |
// Start abbreviating object properties |
| 210 |
*flags |= SERD_ANON_CONT; |
| 211 |
|
| 212 |
// Object is in a list, stop list abbreviating if necessary |
| 213 |
*flags &= ~(uint32_t)SERD_LIST_CONT; |
| 214 |
} |
| 215 |
|
| 216 |
if (!st && type) { |
| 217 |
SerdNode p = serd_node_from_string(SERD_URI, NS_RDF "type"); |
| 218 |
SerdNode o = serd_node_from_string(SERD_URI, USTR(type)); |
| 219 |
|
| 220 |
st = sratom->write_statement( |
| 221 |
sratom->handle, *flags, NULL, node, &p, &o, NULL, NULL); |
| 222 |
} |
| 223 |
|
| 224 |
return st; |
| 225 |
} |
| 226 |
|
| 227 |
static bool |
| 228 |
path_is_absolute(const char* path) |
| 229 |
{ |
| 230 |
return (path[0] == '/' || (isalpha(path[0]) && path[1] == ':' && |
| 231 |
(path[2] == '/' || path[2] == '\\'))); |
| 232 |
} |
| 233 |
|
| 234 |
static SerdNode |
| 235 |
number_type(const Sratom* sratom, const uint8_t* type) |
| 236 |
{ |
| 237 |
if (sratom->pretty_numbers && |
| 238 |
(!strcmp((const char*)type, (const char*)NS_XSD "int") || |
| 239 |
!strcmp((const char*)type, (const char*)NS_XSD "long"))) { |
| 240 |
return serd_node_from_string(SERD_URI, NS_XSD "integer"); |
| 241 |
} |
| 242 |
|
| 243 |
if (sratom->pretty_numbers && |
| 244 |
(!strcmp((const char*)type, (const char*)NS_XSD "float") || |
| 245 |
!strcmp((const char*)type, (const char*)NS_XSD "double"))) { |
| 246 |
return serd_node_from_string(SERD_URI, NS_XSD "decimal"); |
| 247 |
} |
| 248 |
|
| 249 |
return serd_node_from_string(SERD_URI, type); |
| 250 |
} |
| 251 |
|
| 252 |
static SerdStatus |
| 253 |
write_node(const WriteContext* const ctx, |
| 254 |
const SerdNode object, |
| 255 |
const SerdNode datatype, |
| 256 |
const SerdNode language) |
| 257 |
{ |
| 258 |
const SerdNode def_s = serd_node_from_string(SERD_BLANK, USTR("atom")); |
| 259 |
const SerdNode def_p = serd_node_from_string(SERD_URI, USTR(NS_RDF "value")); |
| 260 |
return ctx->sratom->write_statement(ctx->sratom->handle, |
| 261 |
ctx->flags, |
| 262 |
NULL, |
| 263 |
ctx->subject ? ctx->subject : &def_s, |
| 264 |
ctx->predicate ? ctx->predicate : &def_p, |
| 265 |
&object, |
| 266 |
&datatype, |
| 267 |
&language); |
| 268 |
} |
| 269 |
|
| 270 |
static SerdStatus |
| 271 |
write_free_node(const WriteContext* const ctx, |
| 272 |
SerdNode object, |
| 273 |
const SerdNode datatype, |
| 274 |
const SerdNode language) |
| 275 |
{ |
| 276 |
const SerdStatus st = write_node(ctx, object, datatype, language); |
| 277 |
serd_node_free(&object); |
| 278 |
return st; |
| 279 |
} |
| 280 |
|
| 281 |
static const char* |
| 282 |
unmap_uri(LV2_URID_Unmap* const unmap, const LV2_URID urid) |
| 283 |
{ |
| 284 |
return unmap->unmap(unmap->handle, urid); |
| 285 |
} |
| 286 |
|
| 287 |
static SerdStatus |
| 288 |
write_literal(const WriteContext* const ctx, |
| 289 |
LV2_URID_Unmap* const unmap, |
| 290 |
const LV2_Atom_Literal_Body* const lit) |
| 291 |
{ |
| 292 |
const uint8_t* const str = USTR(lit + 1); |
| 293 |
|
| 294 |
const SerdNode object = serd_node_from_string(SERD_LITERAL, str); |
| 295 |
if (lit->datatype) { |
| 296 |
return write_node( |
| 297 |
ctx, |
| 298 |
object, |
| 299 |
serd_node_from_string(SERD_URI, USTR(unmap_uri(unmap, lit->datatype))), |
| 300 |
SERD_NODE_NULL); |
| 301 |
} |
| 302 |
|
| 303 |
if (lit->lang) { |
| 304 |
const char* const lang = unmap_uri(unmap, lit->lang); |
| 305 |
const char* const prefix = "http://lexvo.org/id/iso639-3/"; |
| 306 |
const size_t prefix_len = strlen(prefix); |
| 307 |
if (!lang || !!strncmp(lang, prefix, prefix_len)) { |
| 308 |
fprintf(stderr, "Unknown language URID %u\n", lit->lang); |
| 309 |
return SERD_ERR_BAD_ARG; |
| 310 |
} |
| 311 |
|
| 312 |
return write_node( |
| 313 |
ctx, |
| 314 |
object, |
| 315 |
SERD_NODE_NULL, |
| 316 |
serd_node_from_string(SERD_LITERAL, USTR(lang + prefix_len))); |
| 317 |
} |
| 318 |
|
| 319 |
return write_node(ctx, object, SERD_NODE_NULL, SERD_NODE_NULL); |
| 320 |
} |
| 321 |
|
| 322 |
static SerdStatus |
| 323 |
write_path(const WriteContext* const ctx, const uint8_t* const str) |
| 324 |
{ |
| 325 |
SerdNode object = SERD_NODE_NULL; |
| 326 |
SerdNode datatype = SERD_NODE_NULL; |
| 327 |
if (path_is_absolute((const char*)str)) { |
| 328 |
object = serd_node_new_file_uri(str, NULL, NULL, true); |
| 329 |
} else if (!ctx->sratom->base_uri.buf || |
| 330 |
!!strncmp((const char*)ctx->sratom->base_uri.buf, "file://", 7)) { |
| 331 |
fprintf(stderr, "warning: Relative path but base is not a file URI.\n"); |
| 332 |
fprintf(stderr, "warning: Writing ambiguous atom:Path literal.\n"); |
| 333 |
SerdNode string = serd_node_from_string(SERD_LITERAL, str); |
| 334 |
object = serd_node_copy(&string); |
| 335 |
datatype = serd_node_from_string(SERD_URI, USTR(LV2_ATOM__Path)); |
| 336 |
} else { |
| 337 |
SerdNode rel = serd_node_new_file_uri(str, NULL, NULL, true); |
| 338 |
object = serd_node_new_uri_from_node(&rel, &ctx->sratom->base, NULL); |
| 339 |
serd_node_free(&rel); |
| 340 |
} |
| 341 |
|
| 342 |
return write_free_node(ctx, object, datatype, SERD_NODE_NULL); |
| 343 |
} |
| 344 |
|
| 345 |
static SerdStatus |
| 346 |
write_midi_event(const WriteContext* const ctx, |
| 347 |
const uint32_t size, |
| 348 |
const void* const body) |
| 349 |
{ |
| 350 |
static const char hex_chars[] = "0123456789ABCDEF"; |
| 351 |
|
| 352 |
const size_t len = (size_t)size * 2U; |
| 353 |
char* const str = (char*)calloc(len + 1, 1); |
| 354 |
for (size_t i = 0U; i < size; ++i) { |
| 355 |
const uint8_t byte = ((const uint8_t*)body)[i]; |
| 356 |
str[2U * i] = hex_chars[byte >> 4U]; |
| 357 |
str[(2U * i) + 1U] = hex_chars[byte & 0x0FU]; |
| 358 |
} |
| 359 |
|
| 360 |
const SerdStatus st = |
| 361 |
write_node(ctx, |
| 362 |
serd_node_from_string(SERD_LITERAL, USTR(str)), |
| 363 |
serd_node_from_string(SERD_URI, USTR(LV2_MIDI__MidiEvent)), |
| 364 |
SERD_NODE_NULL); |
| 365 |
free(str); |
| 366 |
return st; |
| 367 |
} |
| 368 |
|
| 369 |
static SerdStatus |
| 370 |
write_event(WriteContext* const ctx, |
| 371 |
LV2_URID_Unmap* const unmap, |
| 372 |
const LV2_Atom_Event* const ev) |
| 373 |
{ |
| 374 |
gensym(&ctx->id, 'e', ctx->sratom->next_id++); |
| 375 |
|
| 376 |
SerdStatus st = start_object( |
| 377 |
ctx->sratom, &ctx->flags, ctx->subject, ctx->predicate, &ctx->id, NULL); |
| 378 |
if (st) { |
| 379 |
return st; |
| 380 |
} |
| 381 |
|
| 382 |
SerdNode time = SERD_NODE_NULL; |
| 383 |
SerdNode p = SERD_NODE_NULL; |
| 384 |
SerdNode datatype = SERD_NODE_NULL; |
| 385 |
SerdNode language = SERD_NODE_NULL; |
| 386 |
if (ctx->sratom->seq_unit == ctx->sratom->atom_beatTime) { |
| 387 |
time = serd_node_new_decimal(ev->time.beats, 16); |
| 388 |
p = serd_node_from_string(SERD_URI, USTR(LV2_ATOM__beatTime)); |
| 389 |
datatype = number_type(ctx->sratom, NS_XSD "double"); |
| 390 |
} else { |
| 391 |
time = serd_node_new_integer(ev->time.frames); |
| 392 |
p = serd_node_from_string(SERD_URI, USTR(LV2_ATOM__frameTime)); |
| 393 |
datatype = number_type(ctx->sratom, NS_XSD "long"); |
| 394 |
} |
| 395 |
|
| 396 |
st = ctx->sratom->write_statement(ctx->sratom->handle, |
| 397 |
SERD_ANON_CONT, |
| 398 |
NULL, |
| 399 |
&ctx->id, |
| 400 |
&p, |
| 401 |
&time, |
| 402 |
&datatype, |
| 403 |
&language); |
| 404 |
serd_node_free(&time); |
| 405 |
if (st) { |
| 406 |
return st; |
| 407 |
} |
| 408 |
|
| 409 |
p = serd_node_from_string(SERD_URI, NS_RDF "value"); |
| 410 |
st = (SerdStatus)sratom_write(ctx->sratom, |
| 411 |
unmap, |
| 412 |
SERD_ANON_CONT, |
| 413 |
&ctx->id, |
| 414 |
&p, |
| 415 |
ev->body.type, |
| 416 |
ev->body.size, |
| 417 |
LV2_ATOM_BODY(&ev->body)); |
| 418 |
|
| 419 |
return st ? st |
| 420 |
: ctx->sratom->end_anon |
| 421 |
? ctx->sratom->end_anon(ctx->sratom->handle, &ctx->id) |
| 422 |
: SERD_SUCCESS; |
| 423 |
} |
| 424 |
|
| 425 |
static SerdStatus |
| 426 |
write_tuple(WriteContext* const ctx, |
| 427 |
LV2_URID_Unmap* const unmap, |
| 428 |
const char* const type_uri, |
| 429 |
const uint32_t size, |
| 430 |
const void* body) |
| 431 |
{ |
| 432 |
SerdStatus st = SERD_SUCCESS; |
| 433 |
|
| 434 |
gensym(&ctx->id, 't', ctx->sratom->next_id++); |
| 435 |
|
| 436 |
if ((st = start_object(ctx->sratom, |
| 437 |
&ctx->flags, |
| 438 |
ctx->subject, |
| 439 |
ctx->predicate, |
| 440 |
&ctx->id, |
| 441 |
type_uri))) { |
| 442 |
return st; |
| 443 |
} |
| 444 |
|
| 445 |
SerdNode p = serd_node_from_string(SERD_URI, NS_RDF "value"); |
| 446 |
ctx->flags |= SERD_LIST_O_BEGIN; |
| 447 |
for (const LV2_Atom* i = (const LV2_Atom*)body; |
| 448 |
!lv2_atom_tuple_is_end(body, size, i); |
| 449 |
i = lv2_atom_tuple_next(i)) { |
| 450 |
if ((st = list_append(ctx->sratom, |
| 451 |
unmap, |
| 452 |
&ctx->flags, |
| 453 |
&ctx->id, |
| 454 |
&p, |
| 455 |
&ctx->node, |
| 456 |
i->size, |
| 457 |
i->type, |
| 458 |
LV2_ATOM_BODY(i)))) { |
| 459 |
return st; |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
st = list_end(ctx->sratom->write_statement, |
| 464 |
ctx->sratom->handle, |
| 465 |
ctx->flags, |
| 466 |
&ctx->id, |
| 467 |
&p); |
| 468 |
|
| 469 |
return st ? st |
| 470 |
: ctx->sratom->end_anon |
| 471 |
? ctx->sratom->end_anon(ctx->sratom->handle, &ctx->id) |
| 472 |
: SERD_SUCCESS; |
| 473 |
} |
| 474 |
|
| 475 |
static SerdStatus |
| 476 |
write_vector(WriteContext* const ctx, |
| 477 |
LV2_URID_Unmap* const unmap, |
| 478 |
const char* const type_uri, |
| 479 |
const uint32_t size, |
| 480 |
const void* body) |
| 481 |
{ |
| 482 |
SerdStatus st = SERD_SUCCESS; |
| 483 |
|
| 484 |
const LV2_Atom_Vector_Body* vec = (const LV2_Atom_Vector_Body*)body; |
| 485 |
if (!vec->child_size) { |
| 486 |
return SERD_ERR_BAD_ARG; |
| 487 |
} |
| 488 |
|
| 489 |
gensym(&ctx->id, 'v', ctx->sratom->next_id++); |
| 490 |
if ((st = start_object(ctx->sratom, |
| 491 |
&ctx->flags, |
| 492 |
ctx->subject, |
| 493 |
ctx->predicate, |
| 494 |
&ctx->id, |
| 495 |
type_uri))) { |
| 496 |
return st; |
| 497 |
} |
| 498 |
|
| 499 |
SerdNode p = serd_node_from_string(SERD_URI, USTR(LV2_ATOM__childType)); |
| 500 |
SerdNode child_type = |
| 501 |
serd_node_from_string(SERD_URI, USTR(unmap_uri(unmap, vec->child_type))); |
| 502 |
|
| 503 |
if ((st = ctx->sratom->write_statement(ctx->sratom->handle, |
| 504 |
ctx->flags, |
| 505 |
NULL, |
| 506 |
&ctx->id, |
| 507 |
&p, |
| 508 |
&child_type, |
| 509 |
NULL, |
| 510 |
NULL))) { |
| 511 |
return st; |
| 512 |
} |
| 513 |
|
| 514 |
p = serd_node_from_string(SERD_URI, NS_RDF "value"); |
| 515 |
ctx->flags |= SERD_LIST_O_BEGIN; |
| 516 |
for (const char* i = (const char*)(vec + 1); i < (const char*)vec + size; |
| 517 |
i += vec->child_size) { |
| 518 |
if ((st = list_append(ctx->sratom, |
| 519 |
unmap, |
| 520 |
&ctx->flags, |
| 521 |
&ctx->id, |
| 522 |
&p, |
| 523 |
&ctx->node, |
| 524 |
vec->child_size, |
| 525 |
vec->child_type, |
| 526 |
i))) { |
| 527 |
return st; |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
st = list_end(ctx->sratom->write_statement, |
| 532 |
ctx->sratom->handle, |
| 533 |
ctx->flags, |
| 534 |
&ctx->id, |
| 535 |
&p); |
| 536 |
|
| 537 |
return st ? st |
| 538 |
: ctx->sratom->end_anon |
| 539 |
? ctx->sratom->end_anon(ctx->sratom->handle, &ctx->id) |
| 540 |
: SERD_SUCCESS; |
| 541 |
} |
| 542 |
|
| 543 |
static SerdStatus |
| 544 |
write_atom_object(WriteContext* const ctx, |
| 545 |
LV2_URID_Unmap* const unmap, |
| 546 |
const uint32_t type_urid, |
| 547 |
const uint32_t size, |
| 548 |
const void* body) |
| 549 |
{ |
| 550 |
int st = SERD_SUCCESS; |
| 551 |
|
| 552 |
const LV2_Atom_Object_Body* const obj = (const LV2_Atom_Object_Body*)body; |
| 553 |
const char* const otype = unmap_uri(unmap, obj->otype); |
| 554 |
|
| 555 |
if (lv2_atom_forge_is_blank(&ctx->sratom->forge, type_urid, obj)) { |
| 556 |
gensym(&ctx->id, 'b', ctx->sratom->next_id++); |
| 557 |
st = start_object( |
| 558 |
ctx->sratom, &ctx->flags, ctx->subject, ctx->predicate, &ctx->id, otype); |
| 559 |
} else { |
| 560 |
ctx->id = serd_node_from_string(SERD_URI, USTR(unmap_uri(unmap, obj->id))); |
| 561 |
ctx->flags = 0U; |
| 562 |
st = start_object(ctx->sratom, &ctx->flags, NULL, NULL, &ctx->id, otype); |
| 563 |
} |
| 564 |
|
| 565 |
for (const LV2_Atom_Property_Body* p = lv2_atom_object_begin(obj); |
| 566 |
!st && !lv2_atom_object_is_end(obj, size, p); |
| 567 |
p = lv2_atom_object_next(p)) { |
| 568 |
const char* const key = unmap_uri(unmap, p->key); |
| 569 |
SerdNode pred = serd_node_from_string(SERD_URI, USTR(key)); |
| 570 |
|
| 571 |
st = sratom_write(ctx->sratom, |
| 572 |
unmap, |
| 573 |
ctx->flags, |
| 574 |
&ctx->id, |
| 575 |
&pred, |
| 576 |
p->value.type, |
| 577 |
p->value.size, |
| 578 |
LV2_ATOM_BODY(&p->value)); |
| 579 |
} |
| 580 |
|
| 581 |
return st ? (SerdStatus)st |
| 582 |
: (ctx->sratom->end_anon && (ctx->flags & SERD_ANON_CONT)) |
| 583 |
? ctx->sratom->end_anon(ctx->sratom->handle, &ctx->id) |
| 584 |
: SERD_SUCCESS; |
| 585 |
} |
| 586 |
|
| 587 |
static SerdStatus |
| 588 |
write_sequence(WriteContext* const ctx, |
| 589 |
LV2_URID_Unmap* const unmap, |
| 590 |
const char* const type_uri, |
| 591 |
const uint32_t size, |
| 592 |
const void* const body) |
| 593 |
{ |
| 594 |
SerdStatus st = SERD_SUCCESS; |
| 595 |
|
| 596 |
const LV2_Atom_Sequence_Body* seq = (const LV2_Atom_Sequence_Body*)body; |
| 597 |
gensym(&ctx->id, 'v', ctx->sratom->next_id++); |
| 598 |
if ((st = start_object(ctx->sratom, |
| 599 |
&ctx->flags, |
| 600 |
ctx->subject, |
| 601 |
ctx->predicate, |
| 602 |
&ctx->id, |
| 603 |
type_uri))) { |
| 604 |
return st; |
| 605 |
} |
| 606 |
|
| 607 |
SerdNode p = serd_node_from_string(SERD_URI, NS_RDF "value"); |
| 608 |
ctx->flags |= SERD_LIST_O_BEGIN; |
| 609 |
for (const LV2_Atom_Event* ev = lv2_atom_sequence_begin(seq); |
| 610 |
!lv2_atom_sequence_is_end(seq, size, ev); |
| 611 |
ev = lv2_atom_sequence_next(ev)) { |
| 612 |
ctx->sratom->seq_unit = seq->unit; |
| 613 |
if ((st = list_append(ctx->sratom, |
| 614 |
unmap, |
| 615 |
&ctx->flags, |
| 616 |
&ctx->id, |
| 617 |
&p, |
| 618 |
&ctx->node, |
| 619 |
sizeof(LV2_Atom_Event) + ev->body.size, |
| 620 |
ctx->sratom->atom_Event, |
| 621 |
ev))) { |
| 622 |
return st; |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
st = list_end(ctx->sratom->write_statement, |
| 627 |
ctx->sratom->handle, |
| 628 |
ctx->flags, |
| 629 |
&ctx->id, |
| 630 |
&p); |
| 631 |
|
| 632 |
return st ? st |
| 633 |
: (ctx->sratom->end_anon && ctx->subject && ctx->predicate) |
| 634 |
? ctx->sratom->end_anon(ctx->sratom->handle, &ctx->id) |
| 635 |
: SERD_SUCCESS; |
| 636 |
} |
| 637 |
|
| 638 |
static SerdStatus |
| 639 |
write_value_object(WriteContext* const ctx, |
| 640 |
const char* const type_uri, |
| 641 |
const uint32_t size, |
| 642 |
const void* const body) |
| 643 |
{ |
| 644 |
SerdStatus st = SERD_SUCCESS; |
| 645 |
|
| 646 |
gensym(&ctx->id, 'b', ctx->sratom->next_id++); |
| 647 |
if ((st = start_object(ctx->sratom, |
| 648 |
&ctx->flags, |
| 649 |
ctx->subject, |
| 650 |
ctx->predicate, |
| 651 |
&ctx->id, |
| 652 |
type_uri))) { |
| 653 |
return st; |
| 654 |
} |
| 655 |
|
| 656 |
SerdNode p = serd_node_from_string(SERD_URI, NS_RDF "value"); |
| 657 |
SerdNode o = serd_node_new_blob(body, size, true); |
| 658 |
SerdNode datatype = serd_node_from_string(SERD_URI, NS_XSD "base64Binary"); |
| 659 |
|
| 660 |
st = ctx->sratom->write_statement( |
| 661 |
ctx->sratom->handle, ctx->flags, NULL, &ctx->id, &p, &o, &datatype, NULL); |
| 662 |
|
| 663 |
if (!st && ctx->sratom->end_anon && ctx->subject && ctx->predicate) { |
| 664 |
st = ctx->sratom->end_anon(ctx->sratom->handle, &ctx->id); |
| 665 |
} |
| 666 |
|
| 667 |
serd_node_free(&o); |
| 668 |
return st; |
| 669 |
} |
| 670 |
|
| 671 |
int |
| 672 |
sratom_write(Sratom* sratom, |
| 673 |
LV2_URID_Unmap* unmap, |
| 674 |
uint32_t flags, |
| 675 |
const SerdNode* subject, |
| 676 |
const SerdNode* predicate, |
| 677 |
uint32_t type_urid, |
| 678 |
uint32_t size, |
| 679 |
const void* body) |
| 680 |
{ |
| 681 |
WriteContext ctx = { |
| 682 |
sratom, |
| 683 |
subject, |
| 684 |
predicate, |
| 685 |
flags, |
| 686 |
{'b', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '\0'}, |
| 687 |
{'b', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '\0'}, |
| 688 |
SERD_NODE_NULL, |
| 689 |
SERD_NODE_NULL}; |
| 690 |
|
| 691 |
ctx.id = serd_node_from_string(SERD_BLANK, ctx.idbuf); |
| 692 |
ctx.node = serd_node_from_string(SERD_BLANK, ctx.nodebuf); |
| 693 |
|
| 694 |
const char* const type = unmap_uri(unmap, type_urid); |
| 695 |
if (type_urid == 0 && size == 0) { |
| 696 |
return write_node(&ctx, |
| 697 |
serd_node_from_string(SERD_URI, USTR(NS_RDF "nil")), |
| 698 |
SERD_NODE_NULL, |
| 699 |
SERD_NODE_NULL); |
| 700 |
} |
| 701 |
|
| 702 |
if (type_urid == sratom->forge.String) { |
| 703 |
return write_node(&ctx, |
| 704 |
serd_node_from_string(SERD_LITERAL, USTR(body)), |
| 705 |
SERD_NODE_NULL, |
| 706 |
SERD_NODE_NULL); |
| 707 |
} |
| 708 |
|
| 709 |
if (type_urid == sratom->forge.Chunk) { |
| 710 |
return write_free_node( |
| 711 |
&ctx, |
| 712 |
serd_node_new_blob(body, size, true), |
| 713 |
serd_node_from_string(SERD_URI, NS_XSD "base64Binary"), |
| 714 |
SERD_NODE_NULL); |
| 715 |
} |
| 716 |
|
| 717 |
if (type_urid == sratom->forge.Literal) { |
| 718 |
return write_literal(&ctx, unmap, (const LV2_Atom_Literal_Body*)body); |
| 719 |
} |
| 720 |
|
| 721 |
if (type_urid == sratom->forge.URID) { |
| 722 |
return write_node( |
| 723 |
&ctx, |
| 724 |
serd_node_from_string(SERD_URI, |
| 725 |
USTR(unmap_uri(unmap, *(const uint32_t*)body))), |
| 726 |
SERD_NODE_NULL, |
| 727 |
SERD_NODE_NULL); |
| 728 |
} |
| 729 |
|
| 730 |
if (type_urid == sratom->forge.Path) { |
| 731 |
return write_path(&ctx, USTR(body)); |
| 732 |
} |
| 733 |
|
| 734 |
if (type_urid == sratom->forge.URI) { |
| 735 |
return write_node(&ctx, |
| 736 |
serd_node_from_string(SERD_URI, USTR(body)), |
| 737 |
SERD_NODE_NULL, |
| 738 |
SERD_NODE_NULL); |
| 739 |
} |
| 740 |
|
| 741 |
if (type_urid == sratom->forge.Int) { |
| 742 |
return write_free_node(&ctx, |
| 743 |
serd_node_new_integer(*(const int32_t*)body), |
| 744 |
number_type(sratom, NS_XSD "int"), |
| 745 |
SERD_NODE_NULL); |
| 746 |
} |
| 747 |
|
| 748 |
if (type_urid == sratom->forge.Long) { |
| 749 |
return write_free_node(&ctx, |
| 750 |
serd_node_new_integer(*(const int64_t*)body), |
| 751 |
number_type(sratom, NS_XSD "long"), |
| 752 |
SERD_NODE_NULL); |
| 753 |
} |
| 754 |
|
| 755 |
if (type_urid == sratom->forge.Float) { |
| 756 |
return write_free_node(&ctx, |
| 757 |
serd_node_new_decimal(*(const float*)body, 8), |
| 758 |
number_type(sratom, NS_XSD "float"), |
| 759 |
SERD_NODE_NULL); |
| 760 |
} |
| 761 |
|
| 762 |
if (type_urid == sratom->forge.Double) { |
| 763 |
return write_free_node(&ctx, |
| 764 |
serd_node_new_decimal(*(const double*)body, 16), |
| 765 |
number_type(sratom, NS_XSD "double"), |
| 766 |
SERD_NODE_NULL); |
| 767 |
} |
| 768 |
|
| 769 |
if (type_urid == sratom->forge.Bool) { |
| 770 |
return write_node( |
| 771 |
&ctx, |
| 772 |
serd_node_from_string(SERD_LITERAL, |
| 773 |
USTR(*(const int32_t*)body ? "true" : "false")), |
| 774 |
serd_node_from_string(SERD_URI, NS_XSD "boolean"), |
| 775 |
SERD_NODE_NULL); |
| 776 |
} |
| 777 |
|
| 778 |
if (type_urid == sratom->midi_MidiEvent) { |
| 779 |
return write_midi_event(&ctx, size, body); |
| 780 |
} |
| 781 |
|
| 782 |
if (type_urid == sratom->atom_Event) { |
| 783 |
return write_event(&ctx, unmap, (const LV2_Atom_Event*)body); |
| 784 |
} |
| 785 |
|
| 786 |
if (type_urid == sratom->forge.Tuple) { |
| 787 |
return write_tuple(&ctx, unmap, type, size, body); |
| 788 |
} |
| 789 |
|
| 790 |
if (type_urid == sratom->forge.Vector) { |
| 791 |
return write_vector(&ctx, unmap, type, size, body); |
| 792 |
} |
| 793 |
|
| 794 |
if (lv2_atom_forge_is_object_type(&sratom->forge, type_urid)) { |
| 795 |
return write_atom_object(&ctx, unmap, type_urid, size, body); |
| 796 |
} |
| 797 |
|
| 798 |
if (type_urid == sratom->forge.Sequence) { |
| 799 |
return write_sequence(&ctx, unmap, type, size, body); |
| 800 |
} |
| 801 |
|
| 802 |
return write_value_object(&ctx, type, size, body); |
| 803 |
} |
| 804 |
|
| 805 |
char* |
| 806 |
sratom_to_turtle(Sratom* sratom, |
| 807 |
LV2_URID_Unmap* unmap, |
| 808 |
const char* base_uri, |
| 809 |
const SerdNode* subject, |
| 810 |
const SerdNode* predicate, |
| 811 |
uint32_t type, |
| 812 |
uint32_t size, |
| 813 |
const void* body) |
| 814 |
{ |
| 815 |
SerdURI buri = SERD_URI_NULL; |
| 816 |
SerdNode base = |
| 817 |
serd_node_new_uri_from_string(USTR(base_uri), &sratom->base, &buri); |
| 818 |
SerdEnv* env = sratom->env ? sratom->env : serd_env_new(NULL); |
| 819 |
SerdChunk str = {NULL, 0}; |
| 820 |
SerdWriter* writer = |
| 821 |
serd_writer_new(SERD_TURTLE, style, env, &buri, serd_chunk_sink, &str); |
| 822 |
|
| 823 |
serd_env_set_base_uri(env, &base); |
| 824 |
sratom_set_sink(sratom, |
| 825 |
base_uri, |
| 826 |
(SerdStatementSink)serd_writer_write_statement, |
| 827 |
(SerdEndSink)serd_writer_end_anon, |
| 828 |
writer); |
| 829 |
|
| 830 |
const int st = sratom_write( |
| 831 |
sratom, unmap, SERD_EMPTY_S, subject, predicate, type, size, body); |
| 832 |
|
| 833 |
serd_writer_finish(writer); |
| 834 |
|
| 835 |
serd_writer_free(writer); |
| 836 |
if (!sratom->env) { |
| 837 |
serd_env_free(env); |
| 838 |
} |
| 839 |
|
| 840 |
serd_node_free(&base); |
| 841 |
if (st) { |
| 842 |
free((void*)str.buf); |
| 843 |
return NULL; |
| 844 |
} |
| 845 |
|
| 846 |
return (char*)serd_chunk_sink_finish(&str); |
| 847 |
} |
| 848 |
|
| 849 |
static void |
| 850 |
read_list_value(Sratom* sratom, |
| 851 |
LV2_Atom_Forge* forge, |
| 852 |
SordWorld* world, |
| 853 |
SordModel* model, |
| 854 |
const SordNode* node, |
| 855 |
ReadMode mode) |
| 856 |
{ |
| 857 |
SordNode* fst = sord_get(model, node, sratom->nodes.rdf_first, NULL, NULL); |
| 858 |
SordNode* rst = sord_get(model, node, sratom->nodes.rdf_rest, NULL, NULL); |
| 859 |
if (fst && rst) { |
| 860 |
read_node(sratom, forge, world, model, fst, mode); |
| 861 |
read_list_value(sratom, forge, world, model, rst, mode); |
| 862 |
} |
| 863 |
|
| 864 |
sord_node_free(world, rst); |
| 865 |
sord_node_free(world, fst); |
| 866 |
} |
| 867 |
|
| 868 |
static void |
| 869 |
read_resource(Sratom* sratom, |
| 870 |
LV2_Atom_Forge* forge, |
| 871 |
SordWorld* world, |
| 872 |
SordModel* model, |
| 873 |
const SordNode* node, |
| 874 |
LV2_URID otype) |
| 875 |
{ |
| 876 |
LV2_URID_Map* map = sratom->map; |
| 877 |
SordQuad q = {node, NULL, NULL, NULL}; |
| 878 |
SordIter* i = sord_find(model, q); |
| 879 |
SordQuad match; |
| 880 |
for (; !sord_iter_end(i); sord_iter_next(i)) { |
| 881 |
sord_iter_get(i, match); |
| 882 |
const SordNode* p = match[SORD_PREDICATE]; |
| 883 |
const SordNode* o = match[SORD_OBJECT]; |
| 884 |
const char* p_uri = (const char*)sord_node_get_string(p); |
| 885 |
uint32_t p_urid = map->map(map->handle, p_uri); |
| 886 |
if (!(sord_node_equals(p, sratom->nodes.rdf_type) && |
| 887 |
sord_node_get_type(o) == SORD_URI && |
| 888 |
map->map(map->handle, (const char*)sord_node_get_string(o)) == |
| 889 |
otype)) { |
| 890 |
lv2_atom_forge_key(forge, p_urid); |
| 891 |
read_node(sratom, forge, world, model, o, MODE_BODY); |
| 892 |
} |
| 893 |
} |
| 894 |
sord_iter_free(i); |
| 895 |
} |
| 896 |
|
| 897 |
static uint32_t |
| 898 |
atom_size(const Sratom* sratom, uint32_t type_urid) |
| 899 |
{ |
| 900 |
if (type_urid == sratom->forge.Int || type_urid == sratom->forge.Bool) { |
| 901 |
return sizeof(int32_t); |
| 902 |
} |
| 903 |
|
| 904 |
if (type_urid == sratom->forge.Long) { |
| 905 |
return sizeof(int64_t); |
| 906 |
} |
| 907 |
|
| 908 |
if (type_urid == sratom->forge.Float) { |
| 909 |
return sizeof(float); |
| 910 |
} |
| 911 |
|
| 912 |
if (type_urid == sratom->forge.Double) { |
| 913 |
return sizeof(double); |
| 914 |
} |
| 915 |
|
| 916 |
if (type_urid == sratom->forge.URID) { |
| 917 |
return sizeof(uint32_t); |
| 918 |
} |
| 919 |
|
| 920 |
return 0; |
| 921 |
} |
| 922 |
|
| 923 |
static inline uint8_t |
| 924 |
hex_digit_value(const uint8_t c) |
| 925 |
{ |
| 926 |
return (uint8_t)((c > '9') ? ((c & ~0x20) - 'A' + 10) : (c - '0')); |
| 927 |
} |
| 928 |
|
| 929 |
static void |
| 930 |
read_literal(Sratom* sratom, LV2_Atom_Forge* forge, const SordNode* node) |
| 931 |
{ |
| 932 |
assert(sord_node_get_type(node) == SORD_LITERAL); |
| 933 |
|
| 934 |
size_t len = 0; |
| 935 |
const char* str = (const char*)sord_node_get_string_counted(node, &len); |
| 936 |
const SordNode* datatype = sord_node_get_datatype(node); |
| 937 |
const char* language = sord_node_get_language(node); |
| 938 |
if (datatype) { |
| 939 |
const char* type_uri = (const char*)sord_node_get_string(datatype); |
| 940 |
if (!strcmp(type_uri, (const char*)NS_XSD "int") || |
| 941 |
!strcmp(type_uri, (const char*)NS_XSD "integer")) { |
| 942 |
lv2_atom_forge_int(forge, strtol(str, NULL, 10)); |
| 943 |
} else if (!strcmp(type_uri, (const char*)NS_XSD "long")) { |
| 944 |
lv2_atom_forge_long(forge, strtol(str, NULL, 10)); |
| 945 |
} else if (!strcmp(type_uri, (const char*)NS_XSD "float") || |
| 946 |
!strcmp(type_uri, (const char*)NS_XSD "decimal")) { |
| 947 |
lv2_atom_forge_float(forge, (float)serd_strtod(str, NULL)); |
| 948 |
} else if (!strcmp(type_uri, (const char*)NS_XSD "double")) { |
| 949 |
lv2_atom_forge_double(forge, serd_strtod(str, NULL)); |
| 950 |
} else if (!strcmp(type_uri, (const char*)NS_XSD "boolean")) { |
| 951 |
lv2_atom_forge_bool(forge, !strcmp(str, "true")); |
| 952 |
} else if (!strcmp(type_uri, (const char*)NS_XSD "base64Binary")) { |
| 953 |
size_t size = 0; |
| 954 |
void* body = serd_base64_decode(USTR(str), len, &size); |
| 955 |
lv2_atom_forge_atom(forge, size, forge->Chunk); |
| 956 |
lv2_atom_forge_write(forge, body, size); |
| 957 |
free(body); |
| 958 |
} else if (!strcmp(type_uri, LV2_ATOM__Path)) { |
| 959 |
lv2_atom_forge_path(forge, str, len); |
| 960 |
} else if (!strcmp(type_uri, LV2_MIDI__MidiEvent)) { |
| 961 |
lv2_atom_forge_atom(forge, len / 2, sratom->midi_MidiEvent); |
| 962 |
for (const char* s = str; s < str + len; s += 2) { |
| 963 |
const uint8_t hi = hex_digit_value(s[0]); |
| 964 |
const uint8_t lo = hex_digit_value(s[1]); |
| 965 |
const uint8_t c = (uint8_t)(((unsigned)hi << 4U) | lo); |
| 966 |
lv2_atom_forge_raw(forge, &c, 1); |
| 967 |
} |
| 968 |
lv2_atom_forge_pad(forge, len / 2); |
| 969 |
} else { |
| 970 |
lv2_atom_forge_literal( |
| 971 |
forge, str, len, sratom->map->map(sratom->map->handle, type_uri), 0); |
| 972 |
} |
| 973 |
} else if (language) { |
| 974 |
static const char* const prefix = "http://lexvo.org/id/iso639-3/"; |
| 975 |
const size_t prefix_len = strlen(prefix); |
| 976 |
const size_t language_len = strlen(language); |
| 977 |
const size_t lang_uri_len = prefix_len + language_len; |
| 978 |
char* lang_uri = (char*)calloc(lang_uri_len + 1, 1); |
| 979 |
|
| 980 |
memcpy(lang_uri, prefix, prefix_len + 1); |
| 981 |
memcpy(lang_uri + prefix_len, language, language_len + 1); |
| 982 |
|
| 983 |
lv2_atom_forge_literal( |
| 984 |
forge, str, len, 0, sratom->map->map(sratom->map->handle, lang_uri)); |
| 985 |
free(lang_uri); |
| 986 |
} else { |
| 987 |
lv2_atom_forge_string(forge, str, len); |
| 988 |
} |
| 989 |
} |
| 990 |
|
| 991 |
static void |
| 992 |
read_object(Sratom* sratom, |
| 993 |
LV2_Atom_Forge* forge, |
| 994 |
SordWorld* world, |
| 995 |
SordModel* model, |
| 996 |
const SordNode* node, |
| 997 |
ReadMode mode) |
| 998 |
{ |
| 999 |
LV2_URID_Map* map = sratom->map; |
| 1000 |
size_t len = 0; |
| 1001 |
const char* str = (const char*)sord_node_get_string_counted(node, &len); |
| 1002 |
|
| 1003 |
SordNode* type = sord_get(model, node, sratom->nodes.rdf_type, NULL, NULL); |
| 1004 |
SordNode* value = sord_get(model, node, sratom->nodes.rdf_value, NULL, NULL); |
| 1005 |
|
| 1006 |
const uint8_t* type_uri = NULL; |
| 1007 |
uint32_t type_urid = 0; |
| 1008 |
if (type) { |
| 1009 |
type_uri = sord_node_get_string(type); |
| 1010 |
type_urid = map->map(map->handle, (const char*)type_uri); |
| 1011 |
} |
| 1012 |
|
| 1013 |
LV2_Atom_Forge_Frame frame = {0, 0}; |
| 1014 |
if (mode == MODE_SEQUENCE) { |
| 1015 |
SordNode* time = |
| 1016 |
sord_get(model, node, sratom->nodes.atom_beatTime, NULL, NULL); |
| 1017 |
uint32_t seq_unit = 0U; |
| 1018 |
if (time) { |
| 1019 |
const char* time_str = (const char*)sord_node_get_string(time); |
| 1020 |
lv2_atom_forge_beat_time(forge, serd_strtod(time_str, NULL)); |
| 1021 |
seq_unit = sratom->atom_beatTime; |
| 1022 |
} else { |
| 1023 |
time = sord_get(model, node, sratom->nodes.atom_frameTime, NULL, NULL); |
| 1024 |
const char* time_str = |
| 1025 |
time ? (const char*)sord_node_get_string(time) : ""; |
| 1026 |
lv2_atom_forge_frame_time(forge, serd_strtod(time_str, NULL)); |
| 1027 |
seq_unit = sratom->atom_frameTime; |
| 1028 |
} |
| 1029 |
read_node(sratom, forge, world, model, value, MODE_BODY); |
| 1030 |
sord_node_free(world, time); |
| 1031 |
sratom->seq_unit = seq_unit; |
| 1032 |
} else if (type_urid == sratom->forge.Tuple) { |
| 1033 |
lv2_atom_forge_tuple(forge, &frame); |
| 1034 |
read_list_value(sratom, forge, world, model, value, MODE_BODY); |
| 1035 |
} else if (type_urid == sratom->forge.Sequence) { |
| 1036 |
const LV2_Atom_Forge_Ref ref = |
| 1037 |
lv2_atom_forge_sequence_head(forge, &frame, 0); |
| 1038 |
sratom->seq_unit = 0; |
| 1039 |
read_list_value(sratom, forge, world, model, value, MODE_SEQUENCE); |
| 1040 |
|
| 1041 |
LV2_Atom_Sequence* seq = |
| 1042 |
(LV2_Atom_Sequence*)lv2_atom_forge_deref(forge, ref); |
| 1043 |
seq->body.unit = |
| 1044 |
(sratom->seq_unit == sratom->atom_frameTime) ? 0 : sratom->seq_unit; |
| 1045 |
} else if (type_urid == sratom->forge.Vector) { |
| 1046 |
SordNode* child_type_node = |
| 1047 |
sord_get(model, node, sratom->nodes.atom_childType, NULL, NULL); |
| 1048 |
if (child_type_node) { |
| 1049 |
uint32_t child_type = map->map( |
| 1050 |
map->handle, (const char*)sord_node_get_string(child_type_node)); |
| 1051 |
uint32_t child_size = atom_size(sratom, child_type); |
| 1052 |
if (child_size > 0) { |
| 1053 |
LV2_Atom_Forge_Ref ref = |
| 1054 |
lv2_atom_forge_vector_head(forge, &frame, child_size, child_type); |
| 1055 |
read_list_value(sratom, forge, world, model, value, MODE_BODY); |
| 1056 |
lv2_atom_forge_pop(forge, &frame); |
| 1057 |
frame.ref = 0; |
| 1058 |
lv2_atom_forge_pad(forge, lv2_atom_forge_deref(forge, ref)->size); |
| 1059 |
} |
| 1060 |
} |
| 1061 |
sord_node_free(world, child_type_node); |
| 1062 |
} else if (value && sord_node_equals(sord_node_get_datatype(value), |
| 1063 |
sratom->nodes.xsd_base64Binary)) { |
| 1064 |
size_t vlen = 0; |
| 1065 |
const uint8_t* vstr = sord_node_get_string_counted(value, &vlen); |
| 1066 |
size_t size = 0; |
| 1067 |
void* body = serd_base64_decode(vstr, vlen, &size); |
| 1068 |
lv2_atom_forge_atom(forge, size, type_urid); |
| 1069 |
lv2_atom_forge_write(forge, body, size); |
| 1070 |
free(body); |
| 1071 |
} else if (sord_node_get_type(node) == SORD_URI) { |
| 1072 |
lv2_atom_forge_object(forge, &frame, map->map(map->handle, str), type_urid); |
| 1073 |
read_resource(sratom, forge, world, model, node, type_urid); |
| 1074 |
} else { |
| 1075 |
lv2_atom_forge_object(forge, &frame, 0, type_urid); |
| 1076 |
read_resource(sratom, forge, world, model, node, type_urid); |
| 1077 |
} |
| 1078 |
|
| 1079 |
if (frame.ref) { |
| 1080 |
lv2_atom_forge_pop(forge, &frame); |
| 1081 |
} |
| 1082 |
sord_node_free(world, value); |
| 1083 |
sord_node_free(world, type); |
| 1084 |
} |
| 1085 |
|
| 1086 |
static void |
| 1087 |
read_node(Sratom* sratom, |
| 1088 |
LV2_Atom_Forge* forge, |
| 1089 |
SordWorld* world, |
| 1090 |
SordModel* model, |
| 1091 |
const SordNode* node, |
| 1092 |
ReadMode mode) |
| 1093 |
{ |
| 1094 |
LV2_URID_Map* map = sratom->map; |
| 1095 |
size_t len = 0; |
| 1096 |
const char* str = (const char*)sord_node_get_string_counted(node, &len); |
| 1097 |
if (sord_node_get_type(node) == SORD_LITERAL) { |
| 1098 |
read_literal(sratom, forge, node); |
| 1099 |
} else if (sord_node_get_type(node) == SORD_URI && |
| 1100 |
!(sratom->object_mode == SRATOM_OBJECT_MODE_BLANK_SUBJECT && |
| 1101 |
mode == MODE_SUBJECT)) { |
| 1102 |
if (!strcmp(str, (const char*)NS_RDF "nil")) { |
| 1103 |
lv2_atom_forge_atom(forge, 0, 0); |
| 1104 |
} else if (!strncmp(str, "file://", 7)) { |
| 1105 |
SerdURI uri; |
| 1106 |
serd_uri_parse(USTR(str), &uri); |
| 1107 |
|
| 1108 |
SerdNode rel = |
| 1109 |
serd_node_new_relative_uri(&uri, &sratom->base, &sratom->base, NULL); |
| 1110 |
|
| 1111 |
uint8_t* const path = serd_file_uri_parse(USTR(rel.buf), NULL); |
| 1112 |
|
| 1113 |
if (path) { |
| 1114 |
lv2_atom_forge_path( |
| 1115 |
forge, (const char*)path, strlen((const char*)path)); |
| 1116 |
serd_free(path); |
| 1117 |
} else { |
| 1118 |
// FIXME: Report errors (required API change) |
| 1119 |
lv2_atom_forge_atom(forge, 0, 0); |
| 1120 |
} |
| 1121 |
serd_node_free(&rel); |
| 1122 |
} else { |
| 1123 |
lv2_atom_forge_urid(forge, map->map(map->handle, str)); |
| 1124 |
} |
| 1125 |
} else { |
| 1126 |
read_object(sratom, forge, world, model, node, mode); |
| 1127 |
} |
| 1128 |
} |
| 1129 |
|
| 1130 |
void |
| 1131 |
sratom_read(Sratom* sratom, |
| 1132 |
LV2_Atom_Forge* forge, |
| 1133 |
SordWorld* world, |
| 1134 |
SordModel* model, |
| 1135 |
const SordNode* node) |
| 1136 |
{ |
| 1137 |
sratom->nodes.atom_childType = sord_new_uri(world, USTR(LV2_ATOM__childType)); |
| 1138 |
sratom->nodes.atom_frameTime = sord_new_uri(world, USTR(LV2_ATOM__frameTime)); |
| 1139 |
sratom->nodes.atom_beatTime = sord_new_uri(world, USTR(LV2_ATOM__beatTime)); |
| 1140 |
sratom->nodes.rdf_first = sord_new_uri(world, NS_RDF "first"); |
| 1141 |
sratom->nodes.rdf_rest = sord_new_uri(world, NS_RDF "rest"); |
| 1142 |
sratom->nodes.rdf_type = sord_new_uri(world, NS_RDF "type"); |
| 1143 |
sratom->nodes.rdf_value = sord_new_uri(world, NS_RDF "value"); |
| 1144 |
sratom->nodes.xsd_base64Binary = sord_new_uri(world, NS_XSD "base64Binary"); |
| 1145 |
|
| 1146 |
sratom->next_id = 1; |
| 1147 |
read_node(sratom, forge, world, model, node, MODE_SUBJECT); |
| 1148 |
|
| 1149 |
sord_node_free(world, sratom->nodes.xsd_base64Binary); |
| 1150 |
sord_node_free(world, sratom->nodes.rdf_value); |
| 1151 |
sord_node_free(world, sratom->nodes.rdf_type); |
| 1152 |
sord_node_free(world, sratom->nodes.rdf_rest); |
| 1153 |
sord_node_free(world, sratom->nodes.rdf_first); |
| 1154 |
sord_node_free(world, sratom->nodes.atom_frameTime); |
| 1155 |
sord_node_free(world, sratom->nodes.atom_beatTime); |
| 1156 |
sord_node_free(world, sratom->nodes.atom_childType); |
| 1157 |
|
| 1158 |
sratom->nodes.xsd_base64Binary = NULL; |
| 1159 |
sratom->nodes.rdf_value = NULL; |
| 1160 |
sratom->nodes.rdf_type = NULL; |
| 1161 |
sratom->nodes.rdf_rest = NULL; |
| 1162 |
sratom->nodes.rdf_first = NULL; |
| 1163 |
sratom->nodes.atom_beatTime = NULL; |
| 1164 |
sratom->nodes.atom_frameTime = NULL; |
| 1165 |
sratom->nodes.atom_childType = NULL; |
| 1166 |
} |
| 1167 |
|
| 1168 |
LV2_Atom_Forge_Ref |
| 1169 |
sratom_forge_sink(LV2_Atom_Forge_Sink_Handle handle, |
| 1170 |
const void* buf, |
| 1171 |
uint32_t size) |
| 1172 |
{ |
| 1173 |
SerdChunk* chunk = (SerdChunk*)handle; |
| 1174 |
const LV2_Atom_Forge_Ref ref = chunk->len + 1; |
| 1175 |
serd_chunk_sink(buf, size, chunk); |
| 1176 |
return ref; |
| 1177 |
} |
| 1178 |
|
| 1179 |
LV2_Atom* |
| 1180 |
sratom_forge_deref(LV2_Atom_Forge_Sink_Handle handle, LV2_Atom_Forge_Ref ref) |
| 1181 |
{ |
| 1182 |
const SerdChunk* chunk = (const SerdChunk*)handle; |
| 1183 |
return (LV2_Atom*)(chunk->buf + ref - 1); |
| 1184 |
} |
| 1185 |
|
| 1186 |
LV2_Atom* |
| 1187 |
sratom_from_turtle(Sratom* sratom, |
| 1188 |
const char* base_uri, |
| 1189 |
const SerdNode* subject, |
| 1190 |
const SerdNode* predicate, |
| 1191 |
const char* str) |
| 1192 |
{ |
| 1193 |
SerdChunk out = {NULL, 0}; |
| 1194 |
SerdNode base = |
| 1195 |
serd_node_new_uri_from_string(USTR(base_uri), &sratom->base, NULL); |
| 1196 |
SordWorld* world = sord_world_new(); |
| 1197 |
SordModel* model = sord_new(world, SORD_SPO, false); |
| 1198 |
SerdEnv* env = sratom->env ? sratom->env : serd_env_new(&base); |
| 1199 |
SerdReader* reader = sord_new_reader(model, env, SERD_TURTLE, NULL); |
| 1200 |
|
| 1201 |
if (!serd_reader_read_string(reader, USTR(str))) { |
| 1202 |
const SordNode* s = sord_node_from_serd_node(world, env, subject, 0, 0); |
| 1203 |
lv2_atom_forge_set_sink( |
| 1204 |
&sratom->forge, sratom_forge_sink, sratom_forge_deref, &out); |
| 1205 |
if (subject && predicate) { |
| 1206 |
const SordNode* p = sord_node_from_serd_node(world, env, predicate, 0, 0); |
| 1207 |
SordNode* o = sord_get(model, s, p, NULL, NULL); |
| 1208 |
if (o) { |
| 1209 |
sratom_read(sratom, &sratom->forge, world, model, o); |
| 1210 |
sord_node_free(world, o); |
| 1211 |
} else { |
| 1212 |
fprintf(stderr, "Failed to find node\n"); |
| 1213 |
} |
| 1214 |
} else { |
| 1215 |
sratom_read(sratom, &sratom->forge, world, model, s); |
| 1216 |
} |
| 1217 |
} else { |
| 1218 |
fprintf(stderr, "Failed to read Turtle\n"); |
| 1219 |
} |
| 1220 |
|
| 1221 |
serd_reader_free(reader); |
| 1222 |
if (!sratom->env) { |
| 1223 |
serd_env_free(env); |
| 1224 |
} |
| 1225 |
|
| 1226 |
sord_free(model); |
| 1227 |
sord_world_free(world); |
| 1228 |
serd_node_free(&base); |
| 1229 |
|
| 1230 |
return (LV2_Atom*)out.buf; |
| 1231 |
} |