summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2016-07-31 03:22:58 -0400
committerDavid Robillard <d@drobilla.net>2016-07-31 03:24:29 -0400
commitb0339e5350f4b9f41f534a7f17e4bec4947fcd5f (patch)
treea0745c0dba3078c751e2d985342194619a23525e
parent34547e1405b12b0f3ec8ce82e69e05ac11495ce8 (diff)
downloadsratom-b0339e5350f4b9f41f534a7f17e4bec4947fcd5f.tar.gz
sratom-b0339e5350f4b9f41f534a7f17e4bec4947fcd5f.tar.bz2
sratom-b0339e5350f4b9f41f534a7f17e4bec4947fcd5f.zip
Add sratom_set_env() for setting prefixes
-rw-r--r--NEWS5
-rw-r--r--sratom/sratom.h13
-rw-r--r--src/sratom.c72
-rw-r--r--tests/sratom_test.c33
-rw-r--r--wscript2
5 files changed, 76 insertions, 49 deletions
diff --git a/NEWS b/NEWS
index 59f0bb3..1ff1611 100644
--- a/NEWS
+++ b/NEWS
@@ -1,12 +1,13 @@
-sratom (0.4.10) unstable;
+sratom (0.5.1) unstable;
+ * Add sratom_set_env() for setting prefixes
* Fix padding of constructed vectors (thanks Hanspeter Portner)
* Support round-trip serialisation of relative paths
* Support sequences with beat time stamps
* Fix warnings when building with ISO C++ compilers
* Upgrade to waf 1.8.14
- -- David Robillard <d@drobilla.net> Sun, 24 Jul 2016 09:50:32 -0400
+ -- David Robillard <d@drobilla.net> Sun, 31 Jul 2016 02:31:55 -0400
sratom (0.4.6) stable;
diff --git a/sratom/sratom.h b/sratom/sratom.h
index a86f67e..3e76954 100644
--- a/sratom/sratom.h
+++ b/sratom/sratom.h
@@ -1,5 +1,5 @@
/*
- Copyright 2012 David Robillard <http://drobilla.net>
+ 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
@@ -101,6 +101,17 @@ void
sratom_free(Sratom* sratom);
/**
+ Set the environment for reading or writing Turtle.
+
+ This can be used to set namespace prefixes and a base URI for
+ sratom_to_turtle() and sratom_from_turtle().
+*/
+SRATOM_API
+void
+sratom_set_env(Sratom* sratom,
+ SerdEnv* env);
+
+/**
Set the sink(s) where sratom will write its output.
This must be called before calling sratom_write().
diff --git a/src/sratom.c b/src/sratom.c
index 9a55b25..0ea7fbb 100644
--- a/src/sratom.c
+++ b/src/sratom.c
@@ -31,6 +31,9 @@
#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,
@@ -45,6 +48,7 @@ struct SratomImpl {
LV2_URID atom_beatTime;
LV2_URID midi_MidiEvent;
unsigned next_id;
+ SerdEnv* env;
SerdNode base_uri;
SerdURI base;
SerdStatementSink write_statement;
@@ -77,19 +81,16 @@ SRATOM_API
Sratom*
sratom_new(LV2_URID_Map* map)
{
- Sratom* sratom = (Sratom*)malloc(sizeof(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->next_id = 0;
- sratom->base_uri = SERD_NODE_NULL;
- sratom->base = SERD_URI_NULL;
- sratom->object_mode = SRATOM_OBJECT_MODE_BLANK;
- sratom->pretty_numbers = false;
- memset(&sratom->nodes, 0, sizeof(sratom->nodes));
- lv2_atom_forge_init(&sratom->forge, 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;
}
@@ -103,6 +104,13 @@ sratom_free(Sratom* 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,
@@ -476,30 +484,14 @@ sratom_to_turtle(Sratom* sratom,
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 = serd_env_new(&base);
- SerdChunk str = { NULL, 0 };
-
- serd_env_set_prefix_from_strings(env, USTR("midi"),
- USTR(LV2_MIDI_PREFIX));
- serd_env_set_prefix_from_strings(env, USTR("atom"),
- USTR(LV2_ATOM_URI "#"));
- serd_env_set_prefix_from_strings(env, USTR("rdf"), NS_RDF);
- serd_env_set_prefix_from_strings(env, USTR("xsd"), NS_XSD);
-
+ 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,
- (SerdStyle)(SERD_STYLE_ABBREVIATED |
- SERD_STYLE_RESOLVED |
- SERD_STYLE_CURIED),
- env, &buri, serd_chunk_sink, &str);
-
- // Write @prefix directives
- serd_env_foreach(env,
- (SerdPrefixSink)serd_writer_set_prefix,
- writer);
+ 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,
@@ -509,7 +501,9 @@ sratom_to_turtle(Sratom* sratom,
serd_writer_finish(writer);
serd_writer_free(writer);
- serd_env_free(env);
+ if (!sratom->env) {
+ serd_env_free(env);
+ }
serd_node_free(&base);
return (char*)serd_chunk_sink_finish(&str);
}
@@ -806,7 +800,7 @@ sratom_from_turtle(Sratom* sratom,
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 = serd_env_new(&base);
+ 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)) {
@@ -830,7 +824,9 @@ sratom_from_turtle(Sratom* sratom,
}
serd_reader_free(reader);
- serd_env_free(env);
+ if (!sratom->env) {
+ serd_env_free(env);
+ }
sord_free(model);
sord_world_free(world);
serd_node_free(&base);
diff --git a/tests/sratom_test.c b/tests/sratom_test.c
index a093462..c869224 100644
--- a/tests/sratom_test.c
+++ b/tests/sratom_test.c
@@ -76,7 +76,7 @@ test_fail(const char* fmt, ...)
}
static int
-test(bool top_level, bool pretty_numbers)
+test(SerdEnv* env, bool top_level, bool pretty_numbers)
{
LV2_URID_Map map = { NULL, urid_map };
LV2_URID_Unmap unmap = { NULL, urid_unmap };
@@ -84,6 +84,7 @@ test(bool top_level, bool pretty_numbers)
lv2_atom_forge_init(&forge, &map);
Sratom* sratom = sratom_new(&map);
+ sratom_set_env(sratom, env);
sratom_set_pretty_numbers(sratom, pretty_numbers);
sratom_set_object_mode(
sratom,
@@ -357,18 +358,36 @@ test(bool top_level, bool pretty_numbers)
return 0;
}
-int
-main(void)
+static int
+test_env(SerdEnv* env)
{
- if (test(false, false)) {
+ if (test(env, false, false)) {
return 1;
- } else if (test(true, false)) {
+ } else if (test(env, true, false)) {
return 1;
- } else if (test(false, true)) {
+ } else if (test(env, false, true)) {
return 1;
- } else if (test(true, true)) {
+ } else if (test(env, true, true)) {
return 1;
}
return 0;
}
+
+int
+main(void)
+{
+ // Test with no environment
+ if (test_env(NULL)) {
+ return 1;
+ }
+
+ // Test with a prefix defined
+ SerdEnv* env = serd_env_new(NULL);
+ serd_env_set_prefix_from_strings(
+ env, (const uint8_t*)"eg", (const uint8_t*)"http://example.org/");
+ test_env(env);
+ serd_env_free(env);
+
+ return 0;
+}
diff --git a/wscript b/wscript
index 4f28195..cfda033 100644
--- a/wscript
+++ b/wscript
@@ -9,7 +9,7 @@ import waflib.extras.autowaf as autowaf
# major increment <=> incompatible changes
# minor increment <=> compatible changes (additions)
# micro increment <=> no interface changes
-SRATOM_VERSION = '0.4.10'
+SRATOM_VERSION = '0.5.1'
SRATOM_MAJOR_VERSION = '0'
# Mandatory waf variables