diff options
author | David Robillard <d@drobilla.net> | 2019-03-09 17:44:36 +0100 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2019-12-12 19:42:09 +0100 |
commit | 3c0ed6b66b6e99c68fc01c86b5796a55d74a5977 (patch) | |
tree | 7be3252c13ef123b956424169c1bfbf55359564a /src/TurtleWriter.cpp | |
parent | e32f32a360f2bf8f017ea347b6d1e568c0beaf68 (diff) | |
download | ingen-3c0ed6b66b6e99c68fc01c86b5796a55d74a5977.tar.gz ingen-3c0ed6b66b6e99c68fc01c86b5796a55d74a5977.tar.bz2 ingen-3c0ed6b66b6e99c68fc01c86b5796a55d74a5977.zip |
WIP: Port to serd1serd1
Diffstat (limited to 'src/TurtleWriter.cpp')
-rw-r--r-- | src/TurtleWriter.cpp | 93 |
1 files changed, 32 insertions, 61 deletions
diff --git a/src/TurtleWriter.cpp b/src/TurtleWriter.cpp index 1deb2e13..e8a38561 100644 --- a/src/TurtleWriter.cpp +++ b/src/TurtleWriter.cpp @@ -19,72 +19,43 @@ #include "ingen/URIMap.hpp" #include "lv2/atom/atom.h" -#define USTR(s) ((const uint8_t*)(s)) - namespace ingen { -static size_t -c_text_sink(const void* buf, size_t len, void* stream) -{ - auto* writer = static_cast<TurtleWriter*>(stream); - return writer->text_sink(buf, len); -} - -static SerdStatus -write_prefix(void* handle, const SerdNode* name, const SerdNode* uri) -{ - serd_writer_set_prefix((SerdWriter*)handle, name, uri); - return SERD_SUCCESS; -} - -TurtleWriter::TurtleWriter(URIMap& map, URIs& uris, URI uri) - : AtomWriter(map, uris, *this) +TurtleWriter::TurtleWriter(serd::World& world, + URIMap& map, + URIs& uris, + const URI& uri) + : AtomWriter(world, map, uris, *this) , _map(map) - , _sratom(sratom_new(&map.urid_map_feature()->urid_map)) - , _base(SERD_NODE_NULL) - , _base_uri(SERD_URI_NULL) - , _uri(std::move(uri)) + , _streamer(world, + map.urid_map_feature()->urid_map, + map.urid_unmap_feature()->urid_unmap) + , _base(serd::make_uri("ingen:/")) // Make relative URIs like bundle paths + , _env(_base) + , _writer(world, + serd::Syntax::Turtle, + {}, + _env, + [&](const char* str, size_t len) { return text_sink(str, len); }) + , _uri(uri) , _wrote_prefixes(false) { - // Use <ingen:/> as base URI, so relative URIs are like bundle paths - _base = serd_node_from_string(SERD_URI, (const uint8_t*)"ingen:/"); - serd_uri_parse(_base.buf, &_base_uri); - - // Set up serialisation environment - _env = serd_env_new(&_base); - serd_env_set_prefix_from_strings(_env, USTR("atom"), USTR("http://lv2plug.in/ns/ext/atom#")); - serd_env_set_prefix_from_strings(_env, USTR("doap"), USTR("http://usefulinc.com/ns/doap#")); - serd_env_set_prefix_from_strings(_env, USTR("ingen"), USTR(INGEN_NS)); - serd_env_set_prefix_from_strings(_env, USTR("lv2"), USTR("http://lv2plug.in/ns/lv2core#")); - serd_env_set_prefix_from_strings(_env, USTR("midi"), USTR("http://lv2plug.in/ns/ext/midi#")); - serd_env_set_prefix_from_strings(_env, USTR("owl"), USTR("http://www.w3.org/2002/07/owl#")); - serd_env_set_prefix_from_strings(_env, USTR("patch"), USTR("http://lv2plug.in/ns/ext/patch#")); - serd_env_set_prefix_from_strings(_env, USTR("rdf"), USTR("http://www.w3.org/1999/02/22-rdf-syntax-ns#")); - serd_env_set_prefix_from_strings(_env, USTR("rdfs"), USTR("http://www.w3.org/2000/01/rdf-schema#")); - serd_env_set_prefix_from_strings(_env, USTR("xsd"), USTR("http://www.w3.org/2001/XMLSchema#")); - - // Make a Turtle writer that writes to text_sink - _writer = serd_writer_new( - SERD_TURTLE, - (SerdStyle)(SERD_STYLE_RESOLVED|SERD_STYLE_ABBREVIATED|SERD_STYLE_CURIED), - _env, - &_base_uri, - c_text_sink, - this); - - // Configure sratom to write directly to the writer (and thus text_sink) - sratom_set_sink(_sratom, - (const char*)_base.buf, - (SerdStatementSink)serd_writer_write_statement, - (SerdEndSink)serd_writer_end_anon, - _writer); + // Set namespace prefixes + _env.set_prefix("atom", "http://lv2plug.in/ns/ext/atom#"); + _env.set_prefix("doap", "http://usefulinc.com/ns/doap#"); + _env.set_prefix("ingen", INGEN_NS); + _env.set_prefix("lv2", "http://lv2plug.in/ns/lv2core#"); + _env.set_prefix("midi", "http://lv2plug.in/ns/ext/midi#"); + _env.set_prefix("owl", "http://www.w3.org/2002/07/owl#"); + _env.set_prefix("patch", "http://lv2plug.in/ns/ext/patch#"); + _env.set_prefix("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); + _env.set_prefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#"); + _env.set_prefix("xsd", "http://www.w3.org/2001/XMLSchema#"); } TurtleWriter::~TurtleWriter() { - sratom_free(_sratom); - serd_writer_free(_writer); - serd_env_free(_env); + _writer.finish(); } bool @@ -92,13 +63,13 @@ TurtleWriter::write(const LV2_Atom* msg, int32_t) { if (!_wrote_prefixes) { // Write namespace prefixes once to reduce traffic - serd_env_foreach(_env, write_prefix, _writer); + _env.write_prefixes(_writer.sink()); + _writer.finish(); _wrote_prefixes = true; } - sratom_write(_sratom, &_map.urid_unmap_feature()->urid_unmap, 0, - nullptr, nullptr, msg->type, msg->size, LV2_ATOM_BODY_CONST(msg)); - serd_writer_finish(_writer); + _streamer.write(_writer.sink(), *msg); + _writer.finish(); return true; } |