diff options
Diffstat (limited to 'src/env.c')
-rw-r--r-- | src/env.c | 82 |
1 files changed, 31 insertions, 51 deletions
@@ -14,6 +14,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include "env.h" + #include "node.h" #include "serd/serd.h" @@ -36,10 +38,10 @@ struct SerdEnvImpl { }; SerdEnv* -serd_env_new(const SerdNode* base_uri) +serd_env_new(const SerdStringView base_uri) { SerdEnv* env = (SerdEnv*)calloc(1, sizeof(struct SerdEnvImpl)); - if (env && base_uri) { + if (env && base_uri.len) { serd_env_set_base_uri(env, base_uri); } @@ -62,40 +64,39 @@ serd_env_free(SerdEnv* env) free(env); } +SerdURIView +serd_env_base_uri_view(const SerdEnv* env) +{ + return env->base_uri; +} + const SerdNode* -serd_env_base_uri(const SerdEnv* env, SerdURIView* out) +serd_env_base_uri(const SerdEnv* env) { - if (out) { - *out = env->base_uri; - } return env->base_uri_node; } SerdStatus -serd_env_set_base_uri(SerdEnv* env, const SerdNode* uri) +serd_env_set_base_uri(SerdEnv* env, const SerdStringView uri) { - if (!env || (uri && uri->type != SERD_URI)) { - return SERD_ERR_BAD_ARG; - } - - if (!uri) { + if (!uri.len) { serd_node_free(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; + // Resolve the new base against the current base in case it is relative const SerdURIView new_base_uri = - serd_resolve_uri(serd_parse_uri(serd_node_string(uri)), env->base_uri); - - SerdNode* const new_base_node = serd_new_parsed_uri(new_base_uri); + serd_resolve_uri(serd_parse_uri(uri.buf), env->base_uri); // Replace the current base URI - serd_node_free(env->base_uri_node); - env->base_uri_node = new_base_node; + env->base_uri_node = serd_new_parsed_uri(new_base_uri); env->base_uri = serd_node_uri_view(env->base_uri_node); + serd_node_free(old_base_uri); return SERD_SUCCESS; } @@ -115,65 +116,44 @@ serd_env_find(const SerdEnv* env, const char* name, size_t name_len) } static void -serd_env_add(SerdEnv* env, const SerdNode* name, const SerdNode* uri) +serd_env_add(SerdEnv* env, const SerdStringView name, const SerdStringView uri) { - const char* name_str = serd_node_string(name); - SerdPrefix* const prefix = serd_env_find(env, name_str, name->n_bytes); + SerdPrefix* const prefix = serd_env_find(env, name.buf, name.len); 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 (strcmp(serd_node_string(prefix->uri), uri.buf)) { + serd_node_free(prefix->uri); + prefix->uri = serd_new_uri(uri); } } else { env->prefixes = (SerdPrefix*)realloc( env->prefixes, (++env->n_prefixes) * sizeof(SerdPrefix)); - env->prefixes[env->n_prefixes - 1].name = serd_node_copy(name); - env->prefixes[env->n_prefixes - 1].uri = serd_node_copy(uri); + env->prefixes[env->n_prefixes - 1].name = serd_new_string(name); + env->prefixes[env->n_prefixes - 1].uri = serd_new_uri(uri); } } SerdStatus -serd_env_set_prefix(SerdEnv* env, const SerdNode* name, const SerdNode* uri) +serd_env_set_prefix(SerdEnv* env, + const SerdStringView name, + const SerdStringView uri) { - if (!name || uri->type != SERD_URI) { - return SERD_ERR_BAD_ARG; - } - - if (serd_uri_string_has_scheme(serd_node_string(uri))) { + if (serd_uri_string_has_scheme(uri.buf)) { // Set prefix to absolute URI serd_env_add(env, name, uri); } else if (!env->base_uri_node) { return SERD_ERR_BAD_ARG; } else { // Resolve relative URI and create a new node and URI for it - SerdNode* const abs_uri = - serd_new_resolved_uri(serd_node_string_view(uri), env->base_uri); + SerdNode* const abs_uri = serd_new_resolved_uri(uri, env->base_uri); // Set prefix to resolved (absolute) URI - serd_env_add(env, name, abs_uri); - + serd_env_add(env, name, serd_node_string_view(abs_uri)); serd_node_free(abs_uri); } return SERD_SUCCESS; } -SerdStatus -serd_env_set_prefix_from_strings(SerdEnv* env, - const char* name, - const char* 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); - - serd_node_free(name_node); - serd_node_free(uri_node); - return st; -} - bool serd_env_qualify(const SerdEnv* env, const SerdNode* uri, |