diff options
Diffstat (limited to 'src/node_syntax.c')
-rw-r--r-- | src/node_syntax.c | 51 |
1 files changed, 35 insertions, 16 deletions
diff --git a/src/node_syntax.c b/src/node_syntax.c index 5c579b84..261d4b03 100644 --- a/src/node_syntax.c +++ b/src/node_syntax.c @@ -14,6 +14,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include "env.h" #include "writer.h" #include "serd/serd.h" @@ -35,7 +36,8 @@ on_node_string_event(void* const handle, const SerdEvent* const event) } static SerdNode* -serd_node_from_syntax_in(const char* const str, +serd_node_from_syntax_in(SerdWorld* const world, + const char* const str, const SerdSyntax syntax, SerdEnv* const env) { @@ -50,9 +52,9 @@ serd_node_from_syntax_in(const char* const str, 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); + SerdNode* object = NULL; + SerdSink* const sink = + serd_sink_new(world, &object, on_node_string_event, NULL); SerdReader* const reader = serd_reader_new(world, @@ -73,7 +75,6 @@ serd_node_from_syntax_in(const char* const str, serd_close_input(&in); serd_reader_free(reader); serd_sink_free(sink); - serd_world_free(world); free(doc); return object; @@ -87,22 +88,34 @@ serd_node_from_syntax(const char* const str, assert(str); if (env) { - return serd_node_from_syntax_in(str, syntax, env); + return serd_node_from_syntax_in(serd_env_world(env), 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); + SerdWorld* const temp_world = serd_world_new(); + if (!temp_world) { + return NULL; + } + + SerdEnv* const temp_env = serd_env_new(temp_world, SERD_EMPTY_STRING()); + if (!temp_env) { + serd_world_free(temp_world); + return NULL; + } + + SerdNode* const node = + serd_node_from_syntax_in(temp_world, str, syntax, temp_env); serd_env_free(temp_env); + serd_world_free(temp_world); return node; } static char* -serd_node_to_syntax_in(const SerdNode* const node, +serd_node_to_syntax_in(SerdWorld* const world, + const SerdNode* const node, const SerdSyntax syntax, const SerdEnv* const env) { - SerdWorld* const world = serd_world_new(); SerdBuffer buffer = {NULL, 0}; SerdOutputStream out = serd_open_output_buffer(&buffer); SerdWriter* const writer = serd_writer_new(world, syntax, 0, env, &out, 1); @@ -118,7 +131,6 @@ serd_node_to_syntax_in(const SerdNode* const node, serd_writer_free(writer); serd_close_output(&out); - serd_world_free(world); return result; } @@ -131,12 +143,19 @@ serd_node_to_syntax(const SerdNode* const node, assert(node); if (env) { - return serd_node_to_syntax_in(node, syntax, env); + return serd_node_to_syntax_in(serd_env_world(env), 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); + SerdWorld* const temp_world = serd_world_new(); + SerdEnv* const temp_env = serd_env_new(temp_world, SERD_EMPTY_STRING()); + if (temp_env) { + char* const string = + serd_node_to_syntax_in(temp_world, node, syntax, temp_env); - serd_env_free(temp_env); - return string; + serd_env_free(temp_env); + serd_world_free(temp_world); + return string; + } + + return NULL; } |