aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/env.c104
-rw-r--r--test/test_env.c8
2 files changed, 63 insertions, 49 deletions
diff --git a/src/env.c b/src/env.c
index 434d1a3b..66698c12 100644
--- a/src/env.c
+++ b/src/env.c
@@ -27,21 +27,25 @@
#include <string.h>
typedef struct {
- SerdNode* name;
- SerdNode* uri;
+ const SerdNode* name;
+ const SerdNode* uri;
} SerdPrefix;
struct SerdEnvImpl {
- SerdPrefix* prefixes;
- size_t n_prefixes;
- SerdNode* base_uri_node;
- SerdURIView base_uri;
+ SerdNodes* nodes;
+ SerdPrefix* prefixes;
+ size_t n_prefixes;
+ const SerdNode* base_uri_node;
+ SerdURIView base_uri;
};
SerdEnv*
serd_env_new(const SerdStringView base_uri)
{
SerdEnv* env = (SerdEnv*)calloc(1, sizeof(struct SerdEnvImpl));
+
+ env->nodes = serd_nodes_new();
+
if (env && base_uri.len) {
serd_env_set_base_uri(env, base_uri);
}
@@ -58,11 +62,16 @@ serd_env_copy(const SerdEnv* const env)
SerdEnv* copy = (SerdEnv*)calloc(1, sizeof(struct SerdEnvImpl));
if (copy) {
+ copy->nodes = serd_nodes_new();
copy->n_prefixes = env->n_prefixes;
+
copy->prefixes = (SerdPrefix*)malloc(copy->n_prefixes * sizeof(SerdPrefix));
for (size_t i = 0; i < copy->n_prefixes; ++i) {
- copy->prefixes[i].name = serd_node_copy(env->prefixes[i].name);
- copy->prefixes[i].uri = serd_node_copy(env->prefixes[i].uri);
+ copy->prefixes[i].name =
+ serd_nodes_intern(copy->nodes, env->prefixes[i].name);
+
+ copy->prefixes[i].uri =
+ serd_nodes_intern(copy->nodes, env->prefixes[i].uri);
}
const SerdNode* const base = serd_env_base_uri(env);
@@ -77,17 +86,11 @@ serd_env_copy(const SerdEnv* const env)
void
serd_env_free(SerdEnv* const env)
{
- if (!env) {
- return;
+ if (env) {
+ free(env->prefixes);
+ serd_nodes_free(env->nodes);
+ free(env);
}
-
- for (size_t i = 0; i < env->n_prefixes; ++i) {
- serd_node_free(env->prefixes[i].name);
- serd_node_free(env->prefixes[i].uri);
- }
- free(env->prefixes);
- serd_node_free(env->base_uri_node);
- free(env);
}
bool
@@ -128,23 +131,23 @@ SerdStatus
serd_env_set_base_uri(SerdEnv* const env, const SerdStringView uri)
{
if (!uri.len) {
- serd_node_free(env->base_uri_node);
+ serd_nodes_deref(env->nodes, env->base_uri_node);
env->base_uri_node = NULL;
env->base_uri = SERD_URI_NULL;
return SERD_SUCCESS;
}
- SerdNode* const old_base_uri = env->base_uri_node;
+ const SerdNode* const old_base_uri = env->base_uri_node;
// Resolve the new base against the current base in case it is relative
const SerdURIView new_base_uri =
serd_resolve_uri(serd_parse_uri(uri.buf), env->base_uri);
// Replace the current base URI
- env->base_uri_node = serd_new_parsed_uri(new_base_uri);
+ env->base_uri_node = serd_nodes_parsed_uri(env->nodes, new_base_uri);
env->base_uri = serd_node_uri_view(env->base_uri_node);
- serd_node_free(old_base_uri);
+ serd_nodes_deref(env->nodes, old_base_uri);
return SERD_SUCCESS;
}
@@ -166,21 +169,24 @@ serd_env_find(const SerdEnv* const env,
}
static void
-serd_env_add(SerdEnv* const env,
- const SerdStringView name,
- const SerdStringView uri)
+serd_env_add(SerdEnv* const env,
+ const SerdStringView name,
+ const SerdNode* const uri)
{
SerdPrefix* const prefix = serd_env_find(env, name.buf, name.len);
if (prefix) {
- if (strcmp(serd_node_string(prefix->uri), uri.buf)) {
- serd_node_free(prefix->uri);
- prefix->uri = serd_new_uri(uri);
+ if (!!strcmp(serd_node_string(prefix->uri), serd_node_string(uri))) {
+ serd_nodes_deref(env->nodes, prefix->uri);
+ prefix->uri = uri;
}
} else {
env->prefixes = (SerdPrefix*)realloc(
env->prefixes, (++env->n_prefixes) * sizeof(SerdPrefix));
- env->prefixes[env->n_prefixes - 1].name = serd_new_string(name);
- env->prefixes[env->n_prefixes - 1].uri = serd_new_uri(uri);
+
+ env->prefixes[env->n_prefixes - 1].name =
+ serd_nodes_string(env->nodes, name);
+
+ env->prefixes[env->n_prefixes - 1].uri = uri;
}
}
@@ -191,7 +197,7 @@ serd_env_set_prefix(SerdEnv* const env,
{
if (serd_uri_string_has_scheme(uri.buf)) {
// Set prefix to absolute URI
- serd_env_add(env, name, uri);
+ serd_env_add(env, name, serd_nodes_uri(env->nodes, uri));
return SERD_SUCCESS;
}
@@ -205,12 +211,13 @@ serd_env_set_prefix(SerdEnv* const env,
assert(abs_uri_view.scheme.len);
// Serialise absolute URI to a new node
- SerdNode* const abs_uri = serd_new_parsed_uri(abs_uri_view);
+ const SerdNode* const abs_uri =
+ serd_nodes_parsed_uri(env->nodes, abs_uri_view);
+
assert(serd_uri_string_has_scheme(serd_node_string(abs_uri)));
// Set prefix to resolved absolute URI
- serd_env_add(env, name, serd_node_string_view(abs_uri));
- serd_node_free(abs_uri);
+ serd_env_add(env, name, abs_uri);
return SERD_SUCCESS;
}
@@ -280,23 +287,19 @@ serd_env_expand_curie(const SerdEnv* const env, const SerdStringView curie)
}
const size_t len = prefix.len + suffix.len;
+ SerdNode* ret = serd_node_malloc(sizeof(SerdNode) + len + 1);
+ if (ret) {
+ ret->length = len;
+ ret->flags = 0u;
+ ret->type = SERD_URI;
- const size_t real_length = serd_node_pad_length(len);
- const size_t node_size = sizeof(SerdNode) + real_length;
- SerdNode* node = serd_node_malloc(node_size);
-
- if (node) {
- node->length = len;
- node->flags = 0u;
- node->type = SERD_URI;
-
- char* const string = (char*)(node + 1u);
+ char* const string = serd_node_buffer(ret);
assert(prefix.buf);
memcpy(string, prefix.buf, prefix.len);
memcpy(string + prefix.len, suffix.buf, suffix.len);
}
- return node;
+ return ret;
}
SerdNode*
@@ -308,8 +311,17 @@ serd_env_expand_node(const SerdEnv* const env, const SerdNode* const node)
const SerdURIView uri = serd_node_uri_view(node);
const SerdURIView abs_uri = serd_resolve_uri(uri, env->base_uri);
+ if (!abs_uri.scheme.len) {
+ return NULL;
+ }
+
+ const SerdWriteResult r = serd_node_construct_uri(0u, NULL, abs_uri);
+ SerdNode* const expanded = serd_node_try_malloc(r);
+ if (expanded) {
+ serd_node_construct_uri(r.count, expanded, abs_uri);
+ }
- return abs_uri.scheme.len ? serd_new_parsed_uri(abs_uri) : NULL;
+ return expanded;
}
void
diff --git a/test/test_env.c b/test/test_env.c
index 9ab13ef2..58455167 100644
--- a/test/test_env.c
+++ b/test/test_env.c
@@ -164,15 +164,17 @@ test_expand_bad_uri_datatype(void)
{
static const SerdStringView type = SERD_STRING("Type");
- SerdNode* const typed =
- serd_new_literal(SERD_STRING("data"), SERD_HAS_DATATYPE, type);
+ SerdNodes* nodes = serd_nodes_new();
+
+ const SerdNode* const typed =
+ serd_nodes_literal(nodes, SERD_STRING("data"), SERD_HAS_DATATYPE, type);
SerdEnv* const env = serd_env_new(SERD_EMPTY_STRING());
assert(!serd_env_expand_node(env, typed));
serd_env_free(env);
- serd_node_free(typed);
+ serd_nodes_free(nodes);
}
static void