aboutsummaryrefslogtreecommitdiffstats
path: root/src/node_syntax.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-09-10 13:20:47 -0400
committerDavid Robillard <d@drobilla.net>2022-01-28 21:57:07 -0500
commiteb804125430e3445e85c423b28e1c41346772ed0 (patch)
tree532f6995ace537170fbbfde2d0d8226d1a48279b /src/node_syntax.c
parentcbf01be4126cbc0f6d80364a7e0b6ad777a7d8ae (diff)
downloadserd-eb804125430e3445e85c423b28e1c41346772ed0.tar.gz
serd-eb804125430e3445e85c423b28e1c41346772ed0.tar.bz2
serd-eb804125430e3445e85c423b28e1c41346772ed0.zip
Make environments and sinks part of the world
Although functions/components that require minimal pre-existing state are nice, these allocate memory and could potentially share resources. So, give them a pointer to a world which can be used to configure such things. In particular, this is working towards supporting custom allocators everywhere.
Diffstat (limited to 'src/node_syntax.c')
-rw-r--r--src/node_syntax.c51
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;
}