aboutsummaryrefslogtreecommitdiffstats
path: root/src/env.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/env.c')
-rw-r--r--src/env.c125
1 files changed, 68 insertions, 57 deletions
diff --git a/src/env.c b/src/env.c
index d11a5bab..516eaf32 100644
--- a/src/env.c
+++ b/src/env.c
@@ -3,20 +3,24 @@
#include "serd/env.h"
+#include "node.h"
+
+#include "serd/node.h"
+
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
- SerdNode name;
- SerdNode uri;
+ SerdNode* name;
+ SerdNode* uri;
} SerdPrefix;
struct SerdEnvImpl {
SerdPrefix* prefixes;
size_t n_prefixes;
- SerdNode base_uri_node;
+ SerdNode* base_uri_node;
SerdURIView base_uri;
};
@@ -24,7 +28,7 @@ SerdEnv*
serd_env_new(const SerdNode* const base_uri)
{
SerdEnv* env = (SerdEnv*)calloc(1, sizeof(struct SerdEnvImpl));
- if (env && base_uri && base_uri->type != SERD_NOTHING) {
+ if (env && base_uri) {
if (serd_env_set_base_uri(env, base_uri)) {
free(env);
return NULL;
@@ -42,12 +46,11 @@ serd_env_free(SerdEnv* const 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);
+ serd_node_free(env->prefixes[i].name);
+ serd_node_free(env->prefixes[i].uri);
}
-
free(env->prefixes);
- serd_node_free(&env->base_uri_node);
+ serd_node_free(env->base_uri_node);
free(env);
}
@@ -58,7 +61,7 @@ serd_env_base_uri(const SerdEnv* const env, SerdURIView* const out)
*out = env->base_uri;
}
- return &env->base_uri_node;
+ return env->base_uri_node;
}
SerdStatus
@@ -68,20 +71,20 @@ serd_env_set_base_uri(SerdEnv* const env, const SerdNode* const uri)
return SERD_BAD_ARG;
}
- if (!uri || !uri->buf) {
- serd_node_free(&env->base_uri_node);
- env->base_uri_node = SERD_NODE_NULL;
+ if (!uri) {
+ serd_node_free(env->base_uri_node);
+ env->base_uri_node = NULL;
env->base_uri = SERD_URI_NULL;
return SERD_SUCCESS;
}
// Resolve base URI and create a new node and URI for it
SerdURIView base_uri;
- SerdNode base_uri_node =
+ SerdNode* base_uri_node =
serd_new_uri_from_node(uri, &env->base_uri, &base_uri);
// Replace the current base URI
- serd_node_free(&env->base_uri_node);
+ serd_node_free(env->base_uri_node);
env->base_uri_node = base_uri_node;
env->base_uri = base_uri;
@@ -94,14 +97,13 @@ serd_env_find(const SerdEnv* const env,
const size_t name_len)
{
for (size_t i = 0; i < env->n_prefixes; ++i) {
- const SerdNode* const prefix_name = &env->prefixes[i].name;
- if (prefix_name->n_bytes == name_len) {
- if (!memcmp(prefix_name->buf, name, name_len)) {
+ const SerdNode* const prefix_name = env->prefixes[i].name;
+ if (prefix_name->length == name_len) {
+ if (!memcmp(serd_node_string(prefix_name), name, name_len)) {
return &env->prefixes[i];
}
}
}
-
return NULL;
}
@@ -110,12 +112,13 @@ serd_env_add(SerdEnv* const env,
const SerdNode* const name,
const SerdNode* const uri)
{
- SerdPrefix* const prefix = serd_env_find(env, name->buf, name->n_bytes);
+ const char* name_str = serd_node_string(name);
+ SerdPrefix* const prefix = serd_env_find(env, name_str, name->length);
if (prefix) {
- if (!serd_node_equals(&prefix->uri, uri)) {
- SerdNode old_prefix_uri = prefix->uri;
- prefix->uri = serd_node_copy(uri);
- serd_node_free(&old_prefix_uri);
+ if (!serd_node_equals(prefix->uri, uri)) {
+ SerdNode* old_prefix_uri = prefix->uri;
+ prefix->uri = serd_node_copy(uri);
+ serd_node_free(old_prefix_uri);
}
} else {
SerdPrefix* const new_prefixes = (SerdPrefix*)realloc(
@@ -133,22 +136,22 @@ serd_env_set_prefix(SerdEnv* const env,
const SerdNode* const name,
const SerdNode* const uri)
{
- if (!name->buf || uri->type != SERD_URI) {
+ if (!name || uri->type != SERD_URI) {
return SERD_BAD_ARG;
}
- if (serd_uri_string_has_scheme(uri->buf)) {
+ if (serd_uri_string_has_scheme(serd_node_string(uri))) {
// Set prefix to absolute URI
serd_env_add(env, name, uri);
} else {
// Resolve relative URI and create a new node and URI for it
SerdURIView abs_uri;
- SerdNode abs_uri_node =
+ SerdNode* abs_uri_node =
serd_new_uri_from_node(uri, &env->base_uri, &abs_uri);
// Set prefix to resolved (absolute) URI
- serd_env_add(env, name, &abs_uri_node);
- serd_node_free(&abs_uri_node);
+ serd_env_add(env, name, abs_uri_node);
+ serd_node_free(abs_uri_node);
}
return SERD_SUCCESS;
@@ -159,29 +162,36 @@ serd_env_set_prefix_from_strings(SerdEnv* const env,
const char* const name,
const char* const uri)
{
- const SerdNode name_node = serd_node_from_string(SERD_LITERAL, name);
- const SerdNode uri_node = serd_node_from_string(SERD_URI, uri);
+ SerdNode* name_node = serd_new_string(SERD_LITERAL, name);
+ SerdNode* uri_node = serd_new_string(SERD_URI, uri);
+
+ const SerdStatus st = serd_env_set_prefix(env, name_node, uri_node);
- return serd_env_set_prefix(env, &name_node, &uri_node);
+ serd_node_free(name_node);
+ serd_node_free(uri_node);
+ return st;
}
bool
-serd_env_qualify(const SerdEnv* const env,
- const SerdNode* const uri,
- SerdNode* const prefix,
- SerdStringView* const suffix)
+serd_env_qualify(const SerdEnv* const env,
+ const SerdNode* const uri,
+ const SerdNode** const prefix,
+ SerdStringView* const suffix)
{
if (!env) {
return false;
}
for (size_t i = 0; i < env->n_prefixes; ++i) {
- const SerdNode* const prefix_uri = &env->prefixes[i].uri;
- if (uri->n_bytes >= prefix_uri->n_bytes) {
- if (!strncmp(uri->buf, prefix_uri->buf, prefix_uri->n_bytes)) {
+ const SerdNode* const prefix_uri = env->prefixes[i].uri;
+ if (uri->length >= prefix_uri->length) {
+ const char* prefix_str = serd_node_string(prefix_uri);
+ const char* uri_str = serd_node_string(uri);
+
+ if (!strncmp(uri_str, prefix_str, prefix_uri->length)) {
*prefix = env->prefixes[i].name;
- suffix->data = uri->buf + prefix_uri->n_bytes;
- suffix->length = uri->n_bytes - prefix_uri->n_bytes;
+ suffix->data = uri_str + prefix_uri->length;
+ suffix->length = uri->length - prefix_uri->length;
return true;
}
}
@@ -195,37 +205,36 @@ serd_env_expand(const SerdEnv* const env,
SerdStringView* const uri_prefix,
SerdStringView* const uri_suffix)
{
- if (!env) {
+ if (!env || !curie) {
return SERD_BAD_CURIE;
}
- const char* const colon =
- (const char*)memchr(curie->buf, ':', curie->n_bytes + 1);
+ const char* const str = serd_node_string(curie);
+ const char* const colon = (const char*)memchr(str, ':', curie->length + 1);
if (curie->type != SERD_CURIE || !colon) {
return SERD_BAD_ARG;
}
- const size_t name_len = (size_t)(colon - curie->buf);
- const SerdPrefix* const prefix = serd_env_find(env, curie->buf, name_len);
+ const size_t name_len = (size_t)(colon - str);
+ const SerdPrefix* const prefix = serd_env_find(env, str, name_len);
if (prefix) {
- uri_prefix->data = prefix->uri.buf;
- uri_prefix->length = prefix->uri.n_bytes;
+ uri_prefix->data = serd_node_string(prefix->uri);
+ uri_prefix->length = prefix->uri ? prefix->uri->length : 0;
uri_suffix->data = colon + 1;
- uri_suffix->length = curie->n_bytes - name_len - 1;
+ uri_suffix->length = curie->length - name_len - 1;
return SERD_SUCCESS;
}
return SERD_BAD_CURIE;
}
-SerdNode
+SerdNode*
serd_env_expand_node(const SerdEnv* const env, const SerdNode* const node)
{
if (!env) {
- return SERD_NODE_NULL;
+ return NULL;
}
switch (node->type) {
- case SERD_NOTHING:
case SERD_LITERAL:
break;
case SERD_URI: {
@@ -236,18 +245,20 @@ serd_env_expand_node(const SerdEnv* const env, const SerdNode* const node)
SerdStringView prefix;
SerdStringView suffix;
if (serd_env_expand(env, node, &prefix, &suffix)) {
- return SERD_NODE_NULL;
+ return NULL;
}
const size_t len = prefix.length + suffix.length;
- char* buf = (char*)malloc(len + 1);
- SerdNode ret = {buf, len, 0, SERD_URI};
- snprintf(buf, ret.n_bytes + 1, "%s%s", prefix.data, suffix.data);
+ SerdNode* ret = serd_node_malloc(len, 0, SERD_URI);
+ char* buf = serd_node_buffer(ret);
+
+ snprintf(buf, len + 1, "%s%s", prefix.data, suffix.data);
+ ret->length = len;
return ret;
}
case SERD_BLANK:
break;
}
- return SERD_NODE_NULL;
+ return NULL;
}
void
@@ -256,6 +267,6 @@ serd_env_foreach(const SerdEnv* const env,
void* const handle)
{
for (size_t i = 0; i < env->n_prefixes; ++i) {
- func(handle, &env->prefixes[i].name, &env->prefixes[i].uri);
+ func(handle, env->prefixes[i].name, env->prefixes[i].uri);
}
}