From 51f917f8a5179a0e16b32a7f478b4245ea7f2505 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 21 Jun 2020 18:19:48 +0200 Subject: WIP: Add serd_node_from_syntax() and serd_node_to_syntax() --- src/node_syntax.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/writer.c | 8 +++++ src/writer.h | 21 ++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 src/node_syntax.c create mode 100644 src/writer.h (limited to 'src') diff --git a/src/node_syntax.c b/src/node_syntax.c new file mode 100644 index 00000000..10483188 --- /dev/null +++ b/src/node_syntax.c @@ -0,0 +1,97 @@ +/* + Copyright 2011-2020 David Robillard + + 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 +#include +#include + +static SerdStatus +on_node_string_event(void* handle, const SerdEvent* event) +{ + if (event->type == SERD_STATEMENT) { + *(SerdNode**)handle = serd_node_copy( + serd_statement_object(event->statement.statement)); + } + + return SERD_SUCCESS; +} + +SerdNode* +serd_node_from_syntax(const char* str, const SerdSyntax syntax) +{ + static const char* const prelude = + "_:s "; + + const size_t str_len = strlen(str); + const size_t doc_len = strlen(prelude) + str_len + 3; + char* const doc = (char*)calloc(doc_len + 1, 1); + + snprintf(doc, doc_len + 1, "%s %s .", prelude, str); + + SerdNode* object = NULL; + SerdWorld* world = serd_world_new(); + SerdSink* sink = serd_sink_new(&object, NULL); + + SerdByteSource* byte_source = serd_byte_source_new_string(doc, NULL); + + SerdReader* reader = serd_reader_new( + world, syntax, SERD_READ_EXACT_BLANKS, sink, 1024 + doc_len); + + serd_world_set_log_func(world, serd_quiet_error_func, NULL); + serd_sink_set_event_func(sink, on_node_string_event); + serd_reader_start(reader, byte_source); + serd_reader_read_document(reader); + serd_reader_finish(reader); + serd_byte_source_free(byte_source); + serd_reader_free(reader); + serd_sink_free(sink); + serd_world_free(world); + free(doc); + + return object; +} + +char* +serd_node_to_syntax(const SerdNode* node, const SerdSyntax syntax) +{ + SerdWorld* world = serd_world_new(); + SerdBuffer buffer = {NULL, 0}; + SerdByteSink* bsink = serd_byte_sink_new_buffer(&buffer); + SerdWriter* writer = serd_writer_new(world, syntax, 0, NULL, bsink); + + serd_world_set_log_func(world, serd_quiet_error_func, NULL); + + SerdStatus st = serd_writer_write_node(writer, node); + if (!st) { + st = serd_writer_finish(writer); + } + + serd_buffer_sink_finish(&buffer); + serd_writer_free(writer); + serd_byte_sink_free(bsink); + serd_world_free(world); + + if (st) { + serd_free(buffer.buf); + return NULL; + } + + return (char*)buffer.buf; +} diff --git a/src/writer.c b/src/writer.c index ed3be4e7..7acf7922 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" @@ -1048,6 +1050,12 @@ serd_writer_on_event(SerdWriter* writer, const SerdEvent* event) return SERD_SUCCESS; } +SerdStatus +serd_writer_write_node(SerdWriter* writer, const SerdNode* node) +{ + return write_node(writer, node, SERD_OBJECT, 0); +} + SerdStatus serd_writer_finish(SerdWriter* writer) { diff --git a/src/writer.h b/src/writer.h new file mode 100644 index 00000000..e86fc775 --- /dev/null +++ b/src/writer.h @@ -0,0 +1,21 @@ +/* + Copyright 2019-2020 David Robillard + + 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); + -- cgit v1.2.1