Browse Source

Remove leftover *.orig files

tags/1.9.8
falkTX 7 years ago
parent
commit
88c8018d8b
6 changed files with 0 additions and 6968 deletions
  1. +0
    -1841
      source/modules/lilv/lilv-0.24.0/lilv/lilv.h.orig
  2. +0
    -1132
      source/modules/lilv/lilv-0.24.0/src/plugin.c.orig
  3. +0
    -1329
      source/modules/lilv/lilv-0.24.0/src/state.c.orig
  4. +0
    -637
      source/modules/lilv/lilv-0.24.0/src/util.c.orig
  5. +0
    -1194
      source/modules/lilv/lilv-0.24.0/src/world.c.orig
  6. +0
    -835
      source/modules/lilv/sratom-0.6.0/src/sratom.c.orig

+ 0
- 1841
source/modules/lilv/lilv-0.24.0/lilv/lilv.h.orig
File diff suppressed because it is too large
View File


+ 0
- 1132
source/modules/lilv/lilv-0.24.0/src/plugin.c.orig
File diff suppressed because it is too large
View File


+ 0
- 1329
source/modules/lilv/lilv-0.24.0/src/state.c.orig
File diff suppressed because it is too large
View File


+ 0
- 637
source/modules/lilv/lilv-0.24.0/src/util.c.orig View File

@@ -1,637 +0,0 @@
/*
Copyright 2007-2016 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.
*/

#define _POSIX_C_SOURCE 200809L /* for fileno */
#define _BSD_SOURCE 1 /* for realpath, symlink */
#define _DEFAULT_SOURCE 1 /* for realpath, symlink */

#ifdef __APPLE__
# define _DARWIN_C_SOURCE 1 /* for flock */
#endif

#include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>

#ifdef _WIN32
#ifndef _WIN32_WINNT
# define _WIN32_WINNT 0x0600 /* for CreateSymbolicLink */
#endif
# include <windows.h>
# include <direct.h>
# include <io.h>
# define F_OK 0
# define mkdir(path, flags) _mkdir(path)
/** Implement 'CreateSymbolicLink()' for MSVC 8 or earlier */
BOOLEAN WINAPI
CreateSymbolicLink(LPCTSTR linkpath, LPCTSTR targetpath, DWORD flags)
{
typedef BOOLEAN (WINAPI* PFUNC)(LPCTSTR, LPCTSTR, DWORD);

PFUNC pfn = (PFUNC)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")),
"CreateSymbolicLinkA");
return pfn ? pfn(linkpath, targetpath, flags) : 0;
}
#else
# include <dirent.h>
# include <limits.h>
# include <unistd.h>
#endif

#include <sys/stat.h>
#include <sys/types.h>

#include "lilv_internal.h"

#if defined(HAVE_FLOCK) && defined(HAVE_FILENO)
# include <sys/file.h>
#endif

#ifndef PAGE_SIZE
# define PAGE_SIZE 4096
#endif

void
lilv_free(void* ptr)
{
free(ptr);
}

char*
lilv_strjoin(const char* first, ...)
{
size_t len = strlen(first);
char* result = (char*)malloc(len + 1);

memcpy(result, first, len);

va_list args;
va_start(args, first);
while (1) {
const char* const s = va_arg(args, const char *);
if (s == NULL)
break;

const size_t this_len = strlen(s);
char* new_result = (char*)realloc(result, len + this_len + 1);
if (!new_result) {
free(result);
return NULL;
}

result = new_result;
memcpy(result + len, s, this_len);
len += this_len;
}
va_end(args);

result[len] = '\0';

return result;
}

char*
lilv_strdup(const char* str)
{
if (!str) {
return NULL;
}

const size_t len = strlen(str);
char* copy = (char*)malloc(len + 1);
memcpy(copy, str, len + 1);
return copy;
}

const char*
lilv_uri_to_path(const char* uri)
{
return (const char*)serd_uri_to_path((const uint8_t*)uri);
}

char*
lilv_file_uri_parse(const char* uri, char** hostname)
{
return (char*)serd_file_uri_parse((const uint8_t*)uri, (uint8_t**)hostname);
}

/** Return the current LANG converted to Turtle (i.e. RFC3066) style.
* For example, if LANG is set to "en_CA.utf-8", this returns "en-ca".
*/
char*
lilv_get_lang(void)
{
const char* const env_lang = getenv("LANG");
if (!env_lang || !strcmp(env_lang, "")
|| !strcmp(env_lang, "C") || !strcmp(env_lang, "POSIX")) {
return NULL;
}

const size_t env_lang_len = strlen(env_lang);
char* const lang = (char*)malloc(env_lang_len + 1);
for (size_t i = 0; i < env_lang_len + 1; ++i) {
if (env_lang[i] == '_') {
lang[i] = '-'; // Convert _ to -
} else if (env_lang[i] >= 'A' && env_lang[i] <= 'Z') {
lang[i] = env_lang[i] + ('a' - 'A'); // Convert to lowercase
} else if (env_lang[i] >= 'a' && env_lang[i] <= 'z') {
lang[i] = env_lang[i]; // Lowercase letter, copy verbatim
} else if (env_lang[i] >= '0' && env_lang[i] <= '9') {
lang[i] = env_lang[i]; // Digit, copy verbatim
} else if (env_lang[i] == '\0' || env_lang[i] == '.') {
// End, or start of suffix (e.g. en_CA.utf-8), finished
lang[i] = '\0';
break;
} else {
LILV_ERRORF("Illegal LANG `%s' ignored\n", env_lang);
free(lang);
return NULL;
}
}

return lang;
}

/** Append suffix to dst, update dst_len, and return the realloc'd result. */
static char*
strappend(char* dst, size_t* dst_len, const char* suffix, size_t suffix_len)
{
dst = (char*)realloc(dst, *dst_len + suffix_len + 1);
memcpy(dst + *dst_len, suffix, suffix_len);
dst[(*dst_len += suffix_len)] = '\0';
return dst;
}

/** Append the value of the environment variable var to dst. */
static char*
append_var(char* dst, size_t* dst_len, const char* var)
{
// Get value from environment
const char* val = getenv(var);
if (val) { // Value found, append it
return strappend(dst, dst_len, val, strlen(val));
} else { // No value found, append variable reference as-is
return strappend(strappend(dst, dst_len, "$", 1),
dst_len, var, strlen(var));
}
}

/** Expand variables (e.g. POSIX ~ or $FOO, Windows %FOO%) in `path`. */
char*
lilv_expand(const char* path)
{
#ifdef _WIN32
char* out = (char*)malloc(MAX_PATH);
ExpandEnvironmentStrings(path, out, MAX_PATH);
#else
char* out = NULL;
size_t len = 0;

const char* start = path; // Start of current chunk to copy
for (const char* s = path; *s;) {
if (*s == '$') {
// Hit $ (variable reference, e.g. $VAR_NAME)
for (const char* t = s + 1; ; ++t) {
if (!*t || (!isupper(*t) && !isdigit(*t) && *t != '_')) {
// Append preceding chunk
out = strappend(out, &len, start, s - start);

// Append variable value (or $VAR_NAME if not found)
char* var = (char*)calloc(t - s, 1);
memcpy(var, s + 1, t - s - 1);
out = append_var(out, &len, var);
free(var);

// Continue after variable reference
start = s = t;
break;
}
}
} else if (*s == '~' && (*(s + 1) == '/' || !*(s + 1))) {
// Hit ~ before slash or end of string (home directory reference)
out = strappend(out, &len, start, s - start);
out = append_var(out, &len, "HOME");
start = ++s;
} else {
++s;
}
}

if (*start) {
out = strappend(out, &len, start, strlen(start));
}
#endif

return out;
}

static bool
lilv_is_dir_sep(const char c)
{
return c == '/' || c == LILV_DIR_SEP[0];
}

char*
lilv_dirname(const char* path)
{
const char* s = path + strlen(path) - 1; // Last character
for (; s > path && lilv_is_dir_sep(*s); --s) {} // Last non-slash
for (; s > path && !lilv_is_dir_sep(*s); --s) {} // Last internal slash
for (; s > path && lilv_is_dir_sep(*s); --s) {} // Skip duplicates

if (s == path) { // Hit beginning
return lilv_is_dir_sep(*s) ? lilv_strdup("/") : lilv_strdup(".");
} else { // Pointing to the last character of the result (inclusive)
char* dirname = (char*)malloc(s - path + 2);
memcpy(dirname, path, s - path + 1);
dirname[s - path + 1] = '\0';
return dirname;
}
}

bool
lilv_path_exists(const char* path, void* ignored)
{
return !access(path, F_OK);
}

char*
lilv_find_free_path(const char* in_path,
bool (*exists)(const char*, void*), void* user_data)
{
const size_t in_path_len = strlen(in_path);
char* path = (char*)malloc(in_path_len + 7);
memcpy(path, in_path, in_path_len + 1);

for (int i = 2; i < 1000000; ++i) {
if (!exists(path, user_data)) {
return path;
}
snprintf(path, in_path_len + 7, "%s.%u", in_path, i);
}

return NULL;
}

int
lilv_copy_file(const char* src, const char* dst)
{
FILE* in = fopen(src, "r");
if (!in) {
return errno;
}

FILE* out = fopen(dst, "w");
if (!out) {
fclose(in);
return errno;
}

char* page = (char*)malloc(PAGE_SIZE);
size_t n_read = 0;
int st = 0;
while ((n_read = fread(page, 1, PAGE_SIZE, in)) > 0) {
if (fwrite(page, 1, n_read, out) != n_read) {
st = errno;
break;
}
}

if (!st && (ferror(in) || ferror(out))) {
st = EBADF;
}

free(page);
fclose(in);
fclose(out);

return st;
}

bool
lilv_path_is_absolute(const char* path)
{
if (lilv_is_dir_sep(path[0])) {
return true;
}

#ifdef _WIN32
if (isalpha(path[0]) && path[1] == ':' && lilv_is_dir_sep(path[2])) {
return true;
}
#endif

return false;
}

char*
lilv_path_absolute(const char* path)
{
if (lilv_path_is_absolute(path)) {
return lilv_strdup(path);
} else {
char* cwd = getcwd(NULL, 0);
char* abs_path = lilv_path_join(cwd, path);
free(cwd);
return abs_path;
}
}

char*
lilv_path_join(const char* a, const char* b)
{
if (!a) {
return lilv_strdup(b);
}

const size_t a_len = strlen(a);
const size_t b_len = b ? strlen(b) : 0;
const size_t pre_len = a_len - (lilv_is_dir_sep(a[a_len - 1]) ? 1 : 0);
char* path = (char*)calloc(1, a_len + b_len + 2);
memcpy(path, a, pre_len);
path[pre_len] = '/';
if (b) {
memcpy(path + pre_len + 1,
b + (lilv_is_dir_sep(b[0]) ? 1 : 0),
lilv_is_dir_sep(b[0]) ? b_len - 1 : b_len);
}
return path;
}

typedef struct {
char* pattern;
time_t time;
char* latest;
} Latest;

static void
update_latest(const char* path, const char* name, void* data)
{
Latest* latest = (Latest*)data;
char* entry_path = lilv_path_join(path, name);
unsigned num;
if (sscanf(entry_path, latest->pattern, &num) == 1) {
struct stat st;
if (!stat(entry_path, &st)) {
if (st.st_mtime >= latest->time) {
free(latest->latest);
latest->latest = entry_path;
}
} else {
LILV_ERRORF("stat(%s) (%s)\n", path, strerror(errno));
}
}
if (entry_path != latest->latest) {
free(entry_path);
}
}

/** Return the latest copy of the file at `path` that is newer. */
char*
lilv_get_latest_copy(const char* path, const char* copy_path)
{
char* copy_dir = lilv_dirname(copy_path);
Latest latest = { lilv_strjoin(copy_path, ".%u", NULL), 0, NULL };

struct stat st;
if (!stat(path, &st)) {
latest.time = st.st_mtime;
} else {
LILV_ERRORF("stat(%s) (%s)\n", path, strerror(errno));
}

lilv_dir_for_each(copy_dir, &latest, update_latest);

free(latest.pattern);
free(copy_dir);
return latest.latest;
}

char*
lilv_realpath(const char* path)
{
#if defined(_WIN32)
char* out = (char*)malloc(MAX_PATH);
GetFullPathName(path, MAX_PATH, out, NULL);
return out;
#elif _POSIX_VERSION >= 200809L
char* real_path = realpath(path, NULL);
return real_path ? real_path : lilv_strdup(path);
#else
// OSX <= 10.5, if anyone cares. I sure don't.
char* out = (char*)malloc(PATH_MAX);
char* real_path = realpath(path, out);
if (!real_path) {
free(out);
return lilv_strdup(path);
} else {
return real_path;
}
#endif
}

int
lilv_symlink(const char* oldpath, const char* newpath)
{
int ret = 0;
if (strcmp(oldpath, newpath)) {
#ifdef _WIN32
ret = !CreateSymbolicLink(newpath, oldpath, 0);
if (ret) {
ret = !CreateHardLink(newpath, oldpath, 0);
}
#else
ret = symlink(oldpath, newpath);
#endif
}
if (ret) {
LILV_ERRORF("Failed to link %s => %s (%s)\n",
newpath, oldpath, strerror(errno));
}
return ret;
}

char*
lilv_path_relative_to(const char* path, const char* base)
{
const size_t path_len = strlen(path);
const size_t base_len = strlen(base);
const size_t min_len = (path_len < base_len) ? path_len : base_len;

// Find the last separator common to both paths
size_t last_shared_sep = 0;
for (size_t i = 0; i < min_len && path[i] == base[i]; ++i) {
if (lilv_is_dir_sep(path[i])) {
last_shared_sep = i;
}
}

if (last_shared_sep == 0) {
// No common components, return path
return lilv_strdup(path);
}

// Find the number of up references ("..") required
size_t up = 0;
for (size_t i = last_shared_sep + 1; i < base_len; ++i) {
if (lilv_is_dir_sep(base[i])) {
++up;
}
}

// Write up references
const size_t suffix_len = path_len - last_shared_sep;
char* rel = (char*)calloc(1, suffix_len + (up * 3) + 1);
for (size_t i = 0; i < up; ++i) {
memcpy(rel + (i * 3), "../", 3);
}

// Write suffix
memcpy(rel + (up * 3), path + last_shared_sep + 1, suffix_len);
return rel;
}

bool
lilv_path_is_child(const char* path, const char* dir)
{
if (path && dir) {
const size_t path_len = strlen(path);
const size_t dir_len = strlen(dir);
return dir && path_len >= dir_len && !strncmp(path, dir, dir_len);
}
return false;
}

int
lilv_flock(FILE* file, bool lock)
{
#if defined(HAVE_FLOCK) && defined(HAVE_FILENO)
return flock(fileno(file), lock ? LOCK_EX : LOCK_UN);
#else
return 0;
#endif
}

void
lilv_dir_for_each(const char* path,
void* data,
void (*f)(const char* path, const char* name, void* data))
{
#ifdef _WIN32
char* pat = lilv_path_join(path, "*");
WIN32_FIND_DATA fd;
HANDLE fh = FindFirstFile(pat, &fd);
if (fh != INVALID_HANDLE_VALUE) {
do {
f(path, fd.cFileName, data);
} while (FindNextFile(fh, &fd));
}
free(pat);
#else
DIR* dir = opendir(path);
if (dir) {
long name_max = pathconf(path, _PC_NAME_MAX);
if (name_max == -1) {
name_max = 255; // Limit not defined, or error
}

const size_t len = offsetof(struct dirent, d_name) + name_max + 1;
struct dirent* entry = (struct dirent*)malloc(len);
struct dirent* result;
while (!readdir_r(dir, entry, &result) && result) {
f(path, entry->d_name, data);
}
free(entry);
closedir(dir);
}
#endif
}

int
lilv_mkdir_p(const char* dir_path)
{
char* path = lilv_strdup(dir_path);
const size_t path_len = strlen(path);
for (size_t i = 1; i <= path_len; ++i) {
if (path[i] == LILV_DIR_SEP[0] || path[i] == '\0') {
path[i] = '\0';
if (mkdir(path, 0755) && errno != EEXIST) {
free(path);
return errno;
}
path[i] = LILV_DIR_SEP[0];
}
}

free(path);
return 0;
}

static off_t
lilv_file_size(const char* path)
{
struct stat buf;
if (stat(path, &buf)) {
LILV_ERRORF("stat(%s) (%s)\n", path, strerror(errno));
return 0;
}
return buf.st_size;
}

bool
lilv_file_equals(const char* a_path, const char* b_path)
{
if (!strcmp(a_path, b_path)) {
return true; // Paths match
}

bool match = false;
FILE* a_file = NULL;
FILE* b_file = NULL;
char* const a_real = lilv_realpath(a_path);
char* const b_real = lilv_realpath(b_path);
if (!strcmp(a_real, b_real)) {
match = true; // Real paths match
} else if (lilv_file_size(a_path) != lilv_file_size(b_path)) {
match = false; // Sizes differ
} else if (!(a_file = fopen(a_real, "rb")) ||
!(b_file = fopen(b_real, "rb"))) {
match = false; // Missing file matches nothing
} else {
// TODO: Improve performance by reading chunks
match = true;
while (!feof(a_file) && !feof(b_file)) {
if (fgetc(a_file) != fgetc(b_file)) {
match = false;
break;
}
}
}

if (a_file) {
fclose(a_file);
}
if (b_file) {
fclose(b_file);
}
free(a_real);
free(b_real);
return match;
}

+ 0
- 1194
source/modules/lilv/lilv-0.24.0/src/world.c.orig
File diff suppressed because it is too large
View File


+ 0
- 835
source/modules/lilv/sratom-0.6.0/src/sratom.c.orig View File

@@ -1,835 +0,0 @@
/*
Copyright 2012-2016 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 <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "lv2/lv2plug.in/ns/ext/atom/forge.h"
#include "lv2/lv2plug.in/ns/ext/atom/util.h"
#include "lv2/lv2plug.in/ns/ext/midi/midi.h"

#include "sratom/sratom.h"

#define NS_RDF (const uint8_t*)"http://www.w3.org/1999/02/22-rdf-syntax-ns#"
#define NS_XSD (const uint8_t*)"http://www.w3.org/2001/XMLSchema#"

#define USTR(str) ((const uint8_t*)(str))

static const SerdStyle style = (SerdStyle)(
SERD_STYLE_ABBREVIATED|SERD_STYLE_RESOLVED|SERD_STYLE_CURIED);

typedef enum {
MODE_SUBJECT,
MODE_BODY,
MODE_SEQUENCE
} ReadMode;

struct SratomImpl {
LV2_URID_Map* map;
LV2_Atom_Forge forge;
LV2_URID atom_Event;
LV2_URID atom_frameTime;
LV2_URID atom_beatTime;
LV2_URID midi_MidiEvent;
unsigned next_id;
SerdEnv* env;
SerdNode base_uri;
SerdURI base;
SerdStatementSink write_statement;
SerdEndSink end_anon;
void* handle;
SratomObjectMode object_mode;
bool pretty_numbers;
uint32_t seq_unit;
struct {
SordNode* atom_childType;
SordNode* atom_frameTime;
SordNode* atom_beatTime;
SordNode* rdf_first;
SordNode* rdf_rest;
SordNode* rdf_type;
SordNode* rdf_value;
SordNode* xsd_base64Binary;
} nodes;
};

static void
read_node(Sratom* sratom,
LV2_Atom_Forge* forge,
SordWorld* world,
SordModel* model,
const SordNode* node,
ReadMode mode);

SRATOM_API
Sratom*
sratom_new(LV2_URID_Map* map)
{
Sratom* sratom = (Sratom*)calloc(1, sizeof(Sratom));
if (sratom) {
sratom->map = map;
sratom->atom_Event = map->map(map->handle, LV2_ATOM__Event);
sratom->atom_frameTime = map->map(map->handle, LV2_ATOM__frameTime);
sratom->atom_beatTime = map->map(map->handle, LV2_ATOM__beatTime);
sratom->midi_MidiEvent = map->map(map->handle, LV2_MIDI__MidiEvent);
sratom->object_mode = SRATOM_OBJECT_MODE_BLANK;
lv2_atom_forge_init(&sratom->forge, map);
}
return sratom;
}

SRATOM_API
void
sratom_free(Sratom* sratom)
{
serd_node_free(&sratom->base_uri);
free(sratom);
}

SRATOM_API
void
sratom_set_env(Sratom* sratom, SerdEnv* env)
{
sratom->env = env;
}

SRATOM_API
void
sratom_set_sink(Sratom* sratom,
const char* base_uri,
SerdStatementSink write_statement,
SerdEndSink end_anon,
void* handle)
{
if (base_uri) {
serd_node_free(&sratom->base_uri);
sratom->base_uri = serd_node_new_uri_from_string(
USTR(base_uri), NULL, NULL);
serd_uri_parse(sratom->base_uri.buf, &sratom->base);
}
sratom->write_statement = write_statement;
sratom->end_anon = end_anon;
sratom->handle = handle;
}

SRATOM_API
void
sratom_set_pretty_numbers(Sratom* sratom,
bool pretty_numbers)
{
sratom->pretty_numbers = pretty_numbers;
}

SRATOM_API
void
sratom_set_object_mode(Sratom* sratom,
SratomObjectMode object_mode)
{
sratom->object_mode = object_mode;
}

static void
gensym(SerdNode* out, char c, unsigned num)
{
out->n_bytes = out->n_chars = snprintf(
(char*)out->buf, 10, "%c%u", c, num);
}

static void
list_append(Sratom* sratom,
LV2_URID_Unmap* unmap,
unsigned* flags,
SerdNode* s,
SerdNode* p,
SerdNode* node,
uint32_t size,
uint32_t type,
const void* body)
{
// Generate a list node
gensym(node, 'l', sratom->next_id);
sratom->write_statement(sratom->handle, *flags, NULL,
s, p, node, NULL, NULL);

// _:node rdf:first value
*flags = SERD_LIST_CONT;
*p = serd_node_from_string(SERD_URI, NS_RDF "first");
sratom_write(sratom, unmap, *flags, node, p, type, size, body);

// Set subject to node and predicate to rdf:rest for next time
gensym(node, 'l', ++sratom->next_id);
*s = *node;
*p = serd_node_from_string(SERD_URI, NS_RDF "rest");
}

static void
list_end(SerdStatementSink sink,
void* handle,
unsigned* flags,
SerdNode* s,
SerdNode* p)
{
// _:node rdf:rest rdf:nil
const SerdNode nil = serd_node_from_string(SERD_URI, NS_RDF "nil");
sink(handle, *flags, NULL, s, p, &nil, NULL, NULL);
}

static void
start_object(Sratom* sratom,
uint32_t* flags,
const SerdNode* subject,
const SerdNode* predicate,
const SerdNode* node,
const char* type)
{
if (subject && predicate) {
sratom->write_statement(sratom->handle, *flags|SERD_ANON_O_BEGIN, NULL,
subject, predicate, node, NULL, NULL);
// Start abbreviating object properties
*flags |= SERD_ANON_CONT;

// Object is in a list, stop list abbreviating if necessary
*flags &= ~SERD_LIST_CONT;
}
if (type) {
SerdNode p = serd_node_from_string(SERD_URI, NS_RDF "type");
SerdNode o = serd_node_from_string(SERD_URI, USTR(type));
sratom->write_statement(sratom->handle, *flags, NULL,
node, &p, &o, NULL, NULL);
}
}

static bool
path_is_absolute(const char* path)
{
return (path[0] == '/'
|| (isalpha(path[0]) && path[1] == ':'
&& (path[2] == '/' || path[2] == '\\')));
}

static SerdNode
number_type(const Sratom* sratom, const uint8_t* type)
{
if (sratom->pretty_numbers &&
(!strcmp((const char*)type, (const char*)NS_XSD "int") ||
!strcmp((const char*)type, (const char*)NS_XSD "long"))) {
return serd_node_from_string(SERD_URI, NS_XSD "integer");
} else if (sratom->pretty_numbers &&
(!strcmp((const char*)type, (const char*)NS_XSD "float") ||
!strcmp((const char*)type, (const char*)NS_XSD "double"))) {
return serd_node_from_string(SERD_URI, NS_XSD "decimal");
} else {
return serd_node_from_string(SERD_URI, (const uint8_t*)type);
}
}

SRATOM_API
int
sratom_write(Sratom* sratom,
LV2_URID_Unmap* unmap,
uint32_t flags,
const SerdNode* subject,
const SerdNode* predicate,
uint32_t type_urid,
uint32_t size,
const void* body)
{
const char* const type = unmap->unmap(unmap->handle, type_urid);
uint8_t idbuf[12] = "b0000000000";
SerdNode id = serd_node_from_string(SERD_BLANK, idbuf);
uint8_t nodebuf[12] = "b0000000000";
SerdNode node = serd_node_from_string(SERD_BLANK, nodebuf);
SerdNode object = SERD_NODE_NULL;
SerdNode datatype = SERD_NODE_NULL;
SerdNode language = SERD_NODE_NULL;
bool new_node = false;
if (type_urid == 0 && size == 0) {
object = serd_node_from_string(SERD_URI, USTR(NS_RDF "nil"));
} else if (type_urid == sratom->forge.String) {
object = serd_node_from_string(SERD_LITERAL, (const uint8_t*)body);
} else if (type_urid == sratom->forge.Chunk) {
datatype = serd_node_from_string(SERD_URI, NS_XSD "base64Binary");
object = serd_node_new_blob(body, size, true);
new_node = true;
} else if (type_urid == sratom->forge.Literal) {
const LV2_Atom_Literal_Body* lit = (const LV2_Atom_Literal_Body*)body;
const uint8_t* str = USTR(lit + 1);
object = serd_node_from_string(SERD_LITERAL, str);
if (lit->datatype) {
datatype = serd_node_from_string(
SERD_URI, USTR(unmap->unmap(unmap->handle, lit->datatype)));
} else if (lit->lang) {
const char* lang = unmap->unmap(unmap->handle, lit->lang);
const char* prefix = "http://lexvo.org/id/iso639-3/";
const size_t prefix_len = strlen(prefix);
if (lang && !strncmp(lang, prefix, prefix_len)) {
language = serd_node_from_string(
SERD_LITERAL, USTR(lang + prefix_len));
} else {
fprintf(stderr, "Unknown language URID %d\n", lit->lang);
}
}
} else if (type_urid == sratom->forge.URID) {
const uint32_t urid = *(const uint32_t*)body;
const uint8_t* str = USTR(unmap->unmap(unmap->handle, urid));
object = serd_node_from_string(SERD_URI, str);
} else if (type_urid == sratom->forge.Path) {
const uint8_t* str = USTR(body);
if (path_is_absolute((const char*)str)) {
new_node = true;
object = serd_node_new_file_uri(str, NULL, NULL, true);
} else {
if (!sratom->base_uri.buf ||
strncmp((const char*)sratom->base_uri.buf, "file://", 7)) {
fprintf(stderr, "warning: Relative path but base is not a file URI.\n");
fprintf(stderr, "warning: Writing ambiguous atom:Path literal.\n");
object = serd_node_from_string(SERD_LITERAL, str);
datatype = serd_node_from_string(SERD_URI, USTR(LV2_ATOM__Path));
} else {
new_node = true;
SerdNode rel = serd_node_new_file_uri(str, NULL, NULL, true);
object = serd_node_new_uri_from_node(&rel, &sratom->base, NULL);
serd_node_free(&rel);
}
}
} else if (type_urid == sratom->forge.URI) {
const uint8_t* str = USTR(body);
object = serd_node_from_string(SERD_URI, str);
} else if (type_urid == sratom->forge.Int) {
new_node = true;
object = serd_node_new_integer(*(const int32_t*)body);
datatype = number_type(sratom, NS_XSD "int");
} else if (type_urid == sratom->forge.Long) {
new_node = true;
object = serd_node_new_integer(*(const int64_t*)body);
datatype = number_type(sratom, NS_XSD "long");
} else if (type_urid == sratom->forge.Float) {
new_node = true;
object = serd_node_new_decimal(*(const float*)body, 8);
datatype = number_type(sratom, NS_XSD "float");
} else if (type_urid == sratom->forge.Double) {
new_node = true;
object = serd_node_new_decimal(*(const double*)body, 16);
datatype = number_type(sratom, NS_XSD "double");
} else if (type_urid == sratom->forge.Bool) {
const int32_t val = *(const int32_t*)body;
datatype = serd_node_from_string(SERD_URI, NS_XSD "boolean");
object = serd_node_from_string(SERD_LITERAL,
USTR(val ? "true" : "false"));
} else if (type_urid == sratom->midi_MidiEvent) {
new_node = true;
datatype = serd_node_from_string(SERD_URI, USTR(LV2_MIDI__MidiEvent));
uint8_t* str = (uint8_t*)calloc(size * 2 + 1, 1);
for (uint32_t i = 0; i < size; ++i) {
snprintf((char*)str + (2 * i), size * 2 + 1, "%02X",
(unsigned)(uint8_t)*((const uint8_t*)body + i));
}
object = serd_node_from_string(SERD_LITERAL, USTR(str));
} else if (type_urid == sratom->atom_Event) {
const LV2_Atom_Event* ev = (const LV2_Atom_Event*)body;
gensym(&id, 'e', sratom->next_id++);
start_object(sratom, &flags, subject, predicate, &id, NULL);
SerdNode time;
SerdNode p;
if (sratom->seq_unit == sratom->atom_beatTime) {
time = serd_node_new_decimal(ev->time.beats, 16);
p = serd_node_from_string(SERD_URI, USTR(LV2_ATOM__beatTime));
datatype = number_type(sratom, NS_XSD "double");
} else {
time = serd_node_new_integer(ev->time.frames);
p = serd_node_from_string(SERD_URI, USTR(LV2_ATOM__frameTime));
datatype = number_type(sratom, NS_XSD "long");
}
sratom->write_statement(sratom->handle, SERD_ANON_CONT, NULL,
&id, &p, &time, &datatype, &language);
serd_node_free(&time);

p = serd_node_from_string(SERD_URI, NS_RDF "value");
sratom_write(sratom, unmap, SERD_ANON_CONT, &id, &p,
ev->body.type, ev->body.size, LV2_ATOM_BODY(&ev->body));
if (sratom->end_anon) {
sratom->end_anon(sratom->handle, &id);
}
} else if (type_urid == sratom->forge.Tuple) {
gensym(&id, 't', sratom->next_id++);
start_object(sratom, &flags, subject, predicate, &id, type);
SerdNode p = serd_node_from_string(SERD_URI, NS_RDF "value");
flags |= SERD_LIST_O_BEGIN;
LV2_ATOM_TUPLE_BODY_FOREACH(body, size, i) {
list_append(sratom, unmap, &flags, &id, &p, &node,
i->size, i->type, LV2_ATOM_BODY(i));
}
list_end(sratom->write_statement, sratom->handle, &flags, &id, &p);
if (sratom->end_anon) {
sratom->end_anon(sratom->handle, &id);
}
} else if (type_urid == sratom->forge.Vector) {
const LV2_Atom_Vector_Body* vec = (const LV2_Atom_Vector_Body*)body;
gensym(&id, 'v', sratom->next_id++);
start_object(sratom, &flags, subject, predicate, &id, type);
SerdNode p = serd_node_from_string(SERD_URI, (const uint8_t*)LV2_ATOM__childType);
SerdNode child_type = serd_node_from_string(
SERD_URI, (const uint8_t*)unmap->unmap(unmap->handle, vec->child_type));
sratom->write_statement(sratom->handle, flags, NULL, &id, &p, &child_type, NULL, NULL);
p = serd_node_from_string(SERD_URI, NS_RDF "value");
flags |= SERD_LIST_O_BEGIN;
for (const char* i = (const char*)(vec + 1);
i < (const char*)vec + size;
i += vec->child_size) {
list_append(sratom, unmap, &flags, &id, &p, &node,
vec->child_size, vec->child_type, i);
}
list_end(sratom->write_statement, sratom->handle, &flags, &id, &p);
if (sratom->end_anon) {
sratom->end_anon(sratom->handle, &id);
}
} else if (lv2_atom_forge_is_object_type(&sratom->forge, type_urid)) {
const LV2_Atom_Object_Body* obj = (const LV2_Atom_Object_Body*)body;
const char* otype = unmap->unmap(unmap->handle,
obj->otype);

if (lv2_atom_forge_is_blank(&sratom->forge, type_urid, obj)) {
gensym(&id, 'b', sratom->next_id++);
start_object(sratom, &flags, subject, predicate, &id, otype);
} else {
id = serd_node_from_string(
SERD_URI, (const uint8_t*)unmap->unmap(unmap->handle, obj->id));
flags = 0;
start_object(sratom, &flags, NULL, NULL, &id, otype);
}
LV2_ATOM_OBJECT_BODY_FOREACH(obj, size, prop) {
const char* const key = unmap->unmap(unmap->handle, prop->key);
SerdNode pred = serd_node_from_string(SERD_URI, USTR(key));
sratom_write(sratom, unmap, flags, &id, &pred,
prop->value.type, prop->value.size,
LV2_ATOM_BODY(&prop->value));
}
if (sratom->end_anon && (flags & SERD_ANON_CONT)) {
sratom->end_anon(sratom->handle, &id);
}
} else if (type_urid == sratom->forge.Sequence) {
const LV2_Atom_Sequence_Body* seq = (const LV2_Atom_Sequence_Body*)body;
gensym(&id, 'v', sratom->next_id++);
start_object(sratom, &flags, subject, predicate, &id, type);
SerdNode p = serd_node_from_string(SERD_URI, NS_RDF "value");
flags |= SERD_LIST_O_BEGIN;
LV2_ATOM_SEQUENCE_BODY_FOREACH(seq, size, ev) {
sratom->seq_unit = seq->unit;
list_append(sratom, unmap, &flags, &id, &p, &node,
sizeof(LV2_Atom_Event) + ev->body.size,
sratom->atom_Event,
ev);
}
list_end(sratom->write_statement, sratom->handle, &flags, &id, &p);
if (sratom->end_anon && subject && predicate) {
sratom->end_anon(sratom->handle, &id);
}
} else {
gensym(&id, 'b', sratom->next_id++);
start_object(sratom, &flags, subject, predicate, &id, type);
SerdNode p = serd_node_from_string(SERD_URI, NS_RDF "value");
SerdNode o = serd_node_new_blob(body, size, true);
datatype = serd_node_from_string(SERD_URI, NS_XSD "base64Binary");
sratom->write_statement(sratom->handle, flags, NULL, &id, &p, &o, &datatype, NULL);
if (sratom->end_anon && subject && predicate) {
sratom->end_anon(sratom->handle, &id);
}
serd_node_free(&o);
}

if (object.buf) {
SerdNode def_s = serd_node_from_string(SERD_BLANK, USTR("atom"));
SerdNode def_p = serd_node_from_string(SERD_URI, USTR(NS_RDF "value"));
if (!subject) {
subject = &def_s;
}
if (!predicate) {
predicate = &def_p;
}
sratom->write_statement(sratom->handle, flags, NULL,
subject, predicate, &object, &datatype, &language);
}

if (new_node) {
serd_node_free(&object);
}

return 0;
}

SRATOM_API
char*
sratom_to_turtle(Sratom* sratom,
LV2_URID_Unmap* unmap,
const char* base_uri,
const SerdNode* subject,
const SerdNode* predicate,
uint32_t type,
uint32_t size,
const void* body)
{
SerdURI buri = SERD_URI_NULL;
SerdNode base = serd_node_new_uri_from_string(USTR(base_uri), &sratom->base, &buri);
SerdEnv* env = sratom->env ? sratom->env : serd_env_new(NULL);
SerdChunk str = { NULL, 0 };
SerdWriter* writer = serd_writer_new(
SERD_TURTLE, style, env, &buri, serd_chunk_sink, &str);

serd_env_set_base_uri(env, &base);
sratom_set_sink(sratom, base_uri,
(SerdStatementSink)serd_writer_write_statement,
(SerdEndSink)serd_writer_end_anon,
writer);
sratom_write(sratom, unmap, SERD_EMPTY_S,
subject, predicate, type, size, body);
serd_writer_finish(writer);

serd_writer_free(writer);
if (!sratom->env) {
serd_env_free(env);
}
serd_node_free(&base);
return (char*)serd_chunk_sink_finish(&str);
}

static void
read_list_value(Sratom* sratom,
LV2_Atom_Forge* forge,
SordWorld* world,
SordModel* model,
const SordNode* node,
ReadMode mode)
{
SordNode* fst = sord_get(model, node, sratom->nodes.rdf_first, NULL, NULL);
SordNode* rst = sord_get(model, node, sratom->nodes.rdf_rest, NULL, NULL);
if (fst && rst) {
read_node(sratom, forge, world, model, fst, mode);
read_list_value(sratom, forge, world, model, rst, mode);
}
sord_node_free(world, rst);
sord_node_free(world, fst);
}

static void
read_resource(Sratom* sratom,
LV2_Atom_Forge* forge,
SordWorld* world,
SordModel* model,
const SordNode* node,
LV2_URID otype)
{
LV2_URID_Map* map = sratom->map;
SordQuad q = { node, NULL, NULL, NULL };
SordIter* i = sord_find(model, q);
SordQuad match;
for (; !sord_iter_end(i); sord_iter_next(i)) {
sord_iter_get(i, match);
const SordNode* p = match[SORD_PREDICATE];
const SordNode* o = match[SORD_OBJECT];
const char* p_uri = (const char*)sord_node_get_string(p);
uint32_t p_urid = map->map(map->handle, p_uri);
if (!(sord_node_equals(p, sratom->nodes.rdf_type) &&
sord_node_get_type(o) == SORD_URI &&
map->map(map->handle, (const char*)sord_node_get_string(o)) == otype)) {
lv2_atom_forge_key(forge, p_urid);
read_node(sratom, forge, world, model, o, MODE_BODY);
}
}
sord_iter_free(i);
}

static uint32_t
atom_size(Sratom* sratom, uint32_t type_urid)
{
if (type_urid == sratom->forge.Int) {
return sizeof(int32_t);
} else if (type_urid == sratom->forge.Long) {
return sizeof(int64_t);
} else if (type_urid == sratom->forge.Float) {
return sizeof(float);
} else if (type_urid == sratom->forge.Double) {
return sizeof(double);
} else if (type_urid == sratom->forge.Bool) {
return sizeof(int32_t);
} else if (type_urid == sratom->forge.URID) {
return sizeof(uint32_t);
}
return 0;
}

static void
read_node(Sratom* sratom,
LV2_Atom_Forge* forge,
SordWorld* world,
SordModel* model,
const SordNode* node,
ReadMode mode)
{
LV2_URID_Map* map = sratom->map;
size_t len = 0;
const char* str = (const char*)sord_node_get_string_counted(node, &len);
if (sord_node_get_type(node) == SORD_LITERAL) {
SordNode* datatype = sord_node_get_datatype(node);
const char* language = sord_node_get_language(node);
if (datatype) {
const char* type_uri = (const char*)sord_node_get_string(datatype);
if (!strcmp(type_uri, (const char*)NS_XSD "int") ||
!strcmp(type_uri, (const char*)NS_XSD "integer")) {
lv2_atom_forge_int(forge, strtol(str, NULL, 10));
} else if (!strcmp(type_uri, (const char*)NS_XSD "long")) {
lv2_atom_forge_long(forge, strtol(str, NULL, 10));
} else if (!strcmp(type_uri, (const char*)NS_XSD "float") ||
!strcmp(type_uri, (const char*)NS_XSD "decimal")) {
lv2_atom_forge_float(forge, serd_strtod(str, NULL));
} else if (!strcmp(type_uri, (const char*)NS_XSD "double")) {
lv2_atom_forge_double(forge, serd_strtod(str, NULL));
} else if (!strcmp(type_uri, (const char*)NS_XSD "boolean")) {
lv2_atom_forge_bool(forge, !strcmp(str, "true"));
} else if (!strcmp(type_uri, (const char*)NS_XSD "base64Binary")) {
size_t size = 0;
void* body = serd_base64_decode(USTR(str), len, &size);
lv2_atom_forge_atom(forge, size, forge->Chunk);
lv2_atom_forge_write(forge, body, size);
free(body);
} else if (!strcmp(type_uri, LV2_ATOM__Path)) {
lv2_atom_forge_path(forge, str, len);
} else if (!strcmp(type_uri, LV2_MIDI__MidiEvent)) {
lv2_atom_forge_atom(forge, len / 2, sratom->midi_MidiEvent);
for (const char* s = str; s < str + len; s += 2) {
unsigned num;
sscanf(s, "%2X", &num);
const uint8_t c = num;
lv2_atom_forge_raw(forge, &c, 1);
}
lv2_atom_forge_pad(forge, len / 2);
} else {
lv2_atom_forge_literal(
forge, str, len,
sratom->map->map(sratom->map->handle, type_uri),
0);
}
} else if (language) {
const char* prefix = "http://lexvo.org/id/iso639-3/";
const size_t lang_len = strlen(prefix) + strlen(language);
char* lang_uri = (char*)calloc(lang_len + 1, 1);
snprintf(lang_uri, lang_len + 1, "%s%s", prefix, language);
lv2_atom_forge_literal(
forge, str, len, 0,
sratom->map->map(sratom->map->handle, lang_uri));
free(lang_uri);
} else {
lv2_atom_forge_string(forge, str, len);
}
} else if (sord_node_get_type(node) == SORD_URI &&
!(sratom->object_mode == SRATOM_OBJECT_MODE_BLANK_SUBJECT
&& mode == MODE_SUBJECT)) {
if (!strcmp(str, (const char*)NS_RDF "nil")) {
lv2_atom_forge_atom(forge, 0, 0);
} else if (!strncmp(str, "file://", 7)) {
SerdURI uri;
serd_uri_parse((const uint8_t*)str, &uri);

SerdNode rel = serd_node_new_relative_uri(&uri, &sratom->base, NULL, NULL);
uint8_t* path = serd_file_uri_parse(rel.buf, NULL);
lv2_atom_forge_path(forge, (const char*)path, strlen((const char*)path));
free(path);
serd_node_free(&rel);
} else {
lv2_atom_forge_urid(forge, map->map(map->handle, str));
}
} else {
SordNode* type = sord_get(
model, node, sratom->nodes.rdf_type, NULL, NULL);
SordNode* value = sord_get(
model, node, sratom->nodes.rdf_value, NULL, NULL);

const uint8_t* type_uri = NULL;
uint32_t type_urid = 0;
if (type) {
type_uri = sord_node_get_string(type);
type_urid = map->map(map->handle, (const char*)type_uri);
}

LV2_Atom_Forge_Frame frame = { 0, 0 };
if (mode == MODE_SEQUENCE) {
SordNode* time = sord_get(
model, node, sratom->nodes.atom_beatTime, NULL, NULL);
uint32_t seq_unit;
if (time) {
const char* time_str = (const char*)sord_node_get_string(time);
lv2_atom_forge_beat_time(forge, serd_strtod(time_str, NULL));
seq_unit = sratom->atom_beatTime;
} else {
time = sord_get(model, node, sratom->nodes.atom_frameTime, NULL, NULL);
const char* time_str = time
? (const char*)sord_node_get_string(time)
: "";
lv2_atom_forge_frame_time(forge, serd_strtod(time_str, NULL));
seq_unit = sratom->atom_frameTime;
}
read_node(sratom, forge, world, model, value, MODE_BODY);
sord_node_free(world, time);
sratom->seq_unit = seq_unit;
} else if (type_urid == sratom->forge.Tuple) {
lv2_atom_forge_tuple(forge, &frame);
read_list_value(sratom, forge, world, model, value, MODE_BODY);
} else if (type_urid == sratom->forge.Sequence) {
const LV2_Atom_Forge_Ref ref = lv2_atom_forge_sequence_head(forge, &frame, 0);
sratom->seq_unit = 0;
read_list_value(sratom, forge, world, model, value, MODE_SEQUENCE);

LV2_Atom_Sequence* seq = (LV2_Atom_Sequence*)lv2_atom_forge_deref(forge, ref);
seq->body.unit = (sratom->seq_unit == sratom->atom_frameTime) ? 0 : sratom->seq_unit;
} else if (type_urid == sratom->forge.Vector) {
SordNode* child_type_node = sord_get(
model, node, sratom->nodes.atom_childType, NULL, NULL);
uint32_t child_type = map->map(
map->handle, (const char*)sord_node_get_string(child_type_node));
uint32_t child_size = atom_size(sratom, child_type);
if (child_size > 0) {
LV2_Atom_Forge_Ref ref = lv2_atom_forge_vector_head(
forge, &frame, child_size, child_type);
read_list_value(sratom, forge, world, model, value, MODE_BODY);
lv2_atom_forge_pop(forge, &frame);
frame.ref = 0;
lv2_atom_forge_pad(forge, lv2_atom_forge_deref(forge, ref)->size);
}
sord_node_free(world, child_type_node);
} else if (value && sord_node_equals(sord_node_get_datatype(value),
sratom->nodes.xsd_base64Binary)) {
size_t vlen = 0;
const uint8_t* vstr = sord_node_get_string_counted(value, &vlen);
size_t size = 0;
void* body = serd_base64_decode(vstr, vlen, &size);
lv2_atom_forge_atom(forge, size, type_urid);
lv2_atom_forge_write(forge, body, size);
free(body);
} else if (sord_node_get_type(node) == SORD_URI) {
lv2_atom_forge_object(
forge, &frame, map->map(map->handle, str), type_urid);
read_resource(sratom, forge, world, model, node, type_urid);
} else {
lv2_atom_forge_object(forge, &frame, 0, type_urid);
read_resource(sratom, forge, world, model, node, type_urid);
}

if (frame.ref) {
lv2_atom_forge_pop(forge, &frame);
}
sord_node_free(world, value);
sord_node_free(world, type);
}
}

SRATOM_API
void
sratom_read(Sratom* sratom,
LV2_Atom_Forge* forge,
SordWorld* world,
SordModel* model,
const SordNode* node)
{
sratom->nodes.atom_childType = sord_new_uri(world, USTR(LV2_ATOM__childType));
sratom->nodes.atom_frameTime = sord_new_uri(world, USTR(LV2_ATOM__frameTime));
sratom->nodes.atom_beatTime = sord_new_uri(world, USTR(LV2_ATOM__beatTime));
sratom->nodes.rdf_first = sord_new_uri(world, NS_RDF "first");
sratom->nodes.rdf_rest = sord_new_uri(world, NS_RDF "rest");
sratom->nodes.rdf_type = sord_new_uri(world, NS_RDF "type");
sratom->nodes.rdf_value = sord_new_uri(world, NS_RDF "value");
sratom->nodes.xsd_base64Binary = sord_new_uri(world, NS_XSD "base64Binary");

sratom->next_id = 1;
read_node(sratom, forge, world, model, node, MODE_SUBJECT);

sord_node_free(world, sratom->nodes.xsd_base64Binary);
sord_node_free(world, sratom->nodes.rdf_value);
sord_node_free(world, sratom->nodes.rdf_type);
sord_node_free(world, sratom->nodes.rdf_rest);
sord_node_free(world, sratom->nodes.rdf_first);
sord_node_free(world, sratom->nodes.atom_frameTime);
sord_node_free(world, sratom->nodes.atom_beatTime);
sord_node_free(world, sratom->nodes.atom_childType);
memset(&sratom->nodes, 0, sizeof(sratom->nodes));
}

SRATOM_API
LV2_Atom_Forge_Ref
sratom_forge_sink(LV2_Atom_Forge_Sink_Handle handle,
const void* buf,
uint32_t size)
{
SerdChunk* chunk = (SerdChunk*)handle;
const LV2_Atom_Forge_Ref ref = chunk->len + 1;
serd_chunk_sink(buf, size, chunk);
return ref;
}

SRATOM_API
LV2_Atom*
sratom_forge_deref(LV2_Atom_Forge_Sink_Handle handle, LV2_Atom_Forge_Ref ref)
{
SerdChunk* chunk = (SerdChunk*)handle;
return (LV2_Atom*)(chunk->buf + ref - 1);
}

SRATOM_API
LV2_Atom*
sratom_from_turtle(Sratom* sratom,
const char* base_uri,
const SerdNode* subject,
const SerdNode* predicate,
const char* str)
{
SerdChunk out = { NULL, 0 };
SerdNode base = serd_node_new_uri_from_string(USTR(base_uri), &sratom->base, NULL);
SordWorld* world = sord_world_new();
SordModel* model = sord_new(world, SORD_SPO, false);
SerdEnv* env = sratom->env ? sratom->env : serd_env_new(&base);
SerdReader* reader = sord_new_reader(model, env, SERD_TURTLE, NULL);

if (!serd_reader_read_string(reader, (const uint8_t*)str)) {
SordNode* s = sord_node_from_serd_node(world, env, subject, 0, 0);
lv2_atom_forge_set_sink(
&sratom->forge, sratom_forge_sink, sratom_forge_deref, &out);
if (subject && predicate) {
SordNode* p = sord_node_from_serd_node(world, env, predicate, 0, 0);
SordNode* o = sord_get(model, s, p, NULL, NULL);
if (o) {
sratom_read(sratom, &sratom->forge, world, model, o);
sord_node_free(world, o);
} else {
fprintf(stderr, "Failed to find node\n");
}
} else {
sratom_read(sratom, &sratom->forge, world, model, s);
}
} else {
fprintf(stderr, "Failed to read Turtle\n");
}

serd_reader_free(reader);
if (!sratom->env) {
serd_env_free(env);
}
sord_free(model);
sord_world_free(world);
serd_node_free(&base);

return (LV2_Atom*)out.buf;
}

Loading…
Cancel
Save