aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
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)
{