aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2020-06-21 18:19:48 +0200
committerDavid Robillard <d@drobilla.net>2022-01-14 19:37:51 -0500
commit4ac6d94d9a8826e09b81c6505408bed44d11e567 (patch)
treec14d3545d56c1e10be516db1838c1811bc83bcc9 /src
parent4436f80225c441a878c4baf856d21dee0394c8dc (diff)
downloadserd-4ac6d94d9a8826e09b81c6505408bed44d11e567.tar.gz
serd-4ac6d94d9a8826e09b81c6505408bed44d11e567.tar.bz2
serd-4ac6d94d9a8826e09b81c6505408bed44d11e567.zip
Add serd_node_from_syntax() and serd_node_to_syntax()
Diffstat (limited to 'src')
-rw-r--r--src/node_syntax.c122
-rw-r--r--src/writer.c8
-rw-r--r--src/writer.h20
3 files changed, 150 insertions, 0 deletions
diff --git a/src/node_syntax.c b/src/node_syntax.c
new file mode 100644
index 00000000..626648f6
--- /dev/null
+++ b/src/node_syntax.c
@@ -0,0 +1,122 @@
+/*
+ Copyright 2011-2020 David Robillard <d@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 "writer.h"
+
+#include "serd/serd.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static SerdStatus
+on_node_string_event(void* const handle, const SerdEvent* const event)
+{
+ if (event->type == SERD_STATEMENT) {
+ *(SerdNode**)handle =
+ serd_node_copy(serd_statement_object(event->statement.statement));
+ }
+
+ return SERD_SUCCESS;
+}
+
+static SerdNode*
+serd_node_from_syntax_in(const char* const str,
+ const SerdSyntax syntax,
+ SerdEnv* const env)
+{
+ static const char* const prelude =
+ "_:s <http://www.w3.org/2000/01/rdf-schema#object>";
+
+ const size_t str_len = strlen(str);
+ const size_t doc_len = strlen(prelude) + str_len + 4;
+ char* const doc = (char*)calloc(doc_len + 1, 1);
+
+ snprintf(doc, doc_len + 1, "%s %s .", prelude, str);
+
+ SerdNode* object = NULL;
+ SerdWorld* const world = serd_world_new();
+ SerdSink* const sink = serd_sink_new(&object, on_node_string_event, NULL);
+
+ SerdByteSource* const source = serd_byte_source_new_string(doc, NULL);
+ SerdReader* const reader = serd_reader_new(
+ world, syntax, SERD_READ_EXACT_BLANKS, env, sink, 1024 + doc_len);
+
+ serd_reader_start(reader, source);
+ serd_reader_read_document(reader);
+ serd_reader_finish(reader);
+ serd_reader_free(reader);
+ serd_byte_source_free(source);
+ serd_sink_free(sink);
+ serd_world_free(world);
+ free(doc);
+
+ return object;
+}
+
+SerdNode*
+serd_node_from_syntax(const char* const str,
+ const SerdSyntax syntax,
+ SerdEnv* const env)
+{
+ if (env) {
+ return serd_node_from_syntax_in(str, syntax, env);
+ }
+
+ SerdEnv* const temp_env = serd_env_new(SERD_EMPTY_STRING());
+ SerdNode* const node = serd_node_from_syntax_in(str, syntax, temp_env);
+
+ serd_env_free(temp_env);
+ return node;
+}
+
+static char*
+serd_node_to_syntax_in(const SerdNode* const node,
+ const SerdSyntax syntax,
+ const SerdEnv* const env)
+{
+ SerdWorld* const world = serd_world_new();
+ SerdBuffer buffer = {NULL, 0};
+ SerdByteSink* const out = serd_byte_sink_new_buffer(&buffer);
+ SerdWriter* const writer = serd_writer_new(world, syntax, 0, env, out);
+
+ char* result = NULL;
+ if (!serd_writer_write_node(writer, node) && !serd_writer_finish(writer)) {
+ result = serd_buffer_sink_finish(&buffer);
+ }
+
+ serd_writer_free(writer);
+ serd_byte_sink_free(out);
+ serd_world_free(world);
+
+ return result;
+}
+
+char*
+serd_node_to_syntax(const SerdNode* const node,
+ const SerdSyntax syntax,
+ const SerdEnv* const env)
+{
+ if (env) {
+ return serd_node_to_syntax_in(node, syntax, env);
+ }
+
+ SerdEnv* const temp_env = serd_env_new(SERD_EMPTY_STRING());
+ char* const string = serd_node_to_syntax_in(node, syntax, temp_env);
+
+ serd_env_free(temp_env);
+ return string;
+}
diff --git a/src/writer.c b/src/writer.c
index bfce4f9e..c6a91d78 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -14,6 +14,8 @@
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+#include "writer.h"
+
#include "byte_sink.h"
#include "env.h"
#include "node.h"
@@ -1259,6 +1261,12 @@ serd_writer_on_event(SerdWriter* writer, const SerdEvent* event)
}
SerdStatus
+serd_writer_write_node(SerdWriter* writer, const SerdNode* node)
+{
+ return write_node(writer, node, SERD_OBJECT, 0);
+}
+
+SerdStatus
serd_writer_finish(SerdWriter* writer)
{
SerdStatus st = SERD_SUCCESS;
diff --git a/src/writer.h b/src/writer.h
new file mode 100644
index 00000000..c80fe49c
--- /dev/null
+++ b/src/writer.h
@@ -0,0 +1,20 @@
+/*
+ Copyright 2019-2020 David Robillard <d@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 "serd/serd.h"
+
+SerdStatus
+serd_writer_write_node(SerdWriter* writer, const SerdNode* node);