aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2023-12-02 17:11:23 -0500
committerDavid Robillard <d@drobilla.net>2023-12-02 18:49:08 -0500
commitb13ad41a4d65b577b4db67660a9edf3056bdf7af (patch)
treee89535f6098a1b860f38ec8a7a1b698b30a72545 /src
parent0db18e483f11ac2f9518d96e137d217040ed1339 (diff)
downloadserd-b13ad41a4d65b577b4db67660a9edf3056bdf7af.tar.gz
serd-b13ad41a4d65b577b4db67660a9edf3056bdf7af.tar.bz2
serd-b13ad41a4d65b577b4db67660a9edf3056bdf7af.zip
Use SerdNodes instead of manual memory management in tests
Diffstat (limited to 'src')
-rw-r--r--src/env.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/env.c b/src/env.c
index 8d63c9c9..e90b68a4 100644
--- a/src/env.c
+++ b/src/env.c
@@ -10,7 +10,9 @@
#include "serd/node.h"
#include "serd/nodes.h"
#include "serd/write_result.h"
+#include "zix/allocator.h"
#include "zix/attributes.h"
+#include "zix/filesystem.h"
#include <assert.h>
#include <stdbool.h>
@@ -165,6 +167,46 @@ serd_env_set_base_uri(SerdEnv* const env, const SerdStringView uri)
return SERD_SUCCESS;
}
+SerdStatus
+serd_env_set_base_path(SerdEnv* const env, const SerdStringView path)
+{
+ assert(env);
+
+ if (!path.data || !path.length) {
+ return serd_env_set_base_uri(env, serd_empty_string());
+ }
+
+ char* const real_path = zix_canonical_path(NULL, path.data);
+ if (!real_path) {
+ return SERD_BAD_ARG;
+ }
+
+ const size_t real_path_len = strlen(real_path);
+ SerdNode* base_node = NULL;
+ const char path_last = path.data[path.length - 1];
+ if (path_last == '/' || path_last == '\\') {
+ char* const base_path =
+ (char*)serd_acalloc(env->allocator, real_path_len + 2, 1);
+
+ memcpy(base_path, real_path, real_path_len + 1);
+ base_path[real_path_len] = path_last;
+
+ base_node = serd_node_new(
+ NULL, serd_a_file_uri(serd_string(base_path), serd_empty_string()));
+
+ serd_afree(env->allocator, base_path);
+ } else {
+ base_node = serd_node_new(
+ NULL, serd_a_file_uri(serd_string(real_path), serd_empty_string()));
+ }
+
+ serd_env_set_base_uri(env, serd_node_string_view(base_node));
+ serd_node_free(NULL, base_node);
+ zix_free(NULL, real_path);
+
+ return SERD_SUCCESS;
+}
+
SerdStringView
serd_env_find_prefix(const SerdEnv* const env, const SerdStringView name)
{