diff options
Diffstat (limited to 'src/env.c')
-rw-r--r-- | src/env.c | 72 |
1 files changed, 36 insertions, 36 deletions
@@ -199,10 +199,16 @@ serd_env_set_prefix(SerdEnv* const env, return SERD_ERR_BAD_ARG; } - // Resolve relative URI and create a new node and URI for it - SerdNode* const abs_uri = serd_new_resolved_uri(uri, env->base_uri); + // Resolve potentially relative URI reference to an absolute URI + const SerdURIView uri_view = serd_parse_uri(uri.buf); + const SerdURIView abs_uri_view = serd_resolve_uri(uri_view, env->base_uri); + assert(abs_uri_view.scheme.len); - // Set prefix to resolved (absolute) URI + // Serialise absolute URI to a new node + SerdNode* const abs_uri = serd_new_parsed_uri(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); return SERD_SUCCESS; @@ -248,22 +254,15 @@ serd_env_expand_in_place(const SerdEnv* const env, const size_t name_len = (size_t)(colon - str); const SerdPrefix* const prefix = serd_env_find(env, str, name_len); - if (prefix) { - uri_prefix->buf = serd_node_string(prefix->uri); - uri_prefix->len = prefix->uri ? prefix->uri->length : 0; - uri_suffix->buf = colon + 1; - uri_suffix->len = curie.len - name_len - 1; - return SERD_SUCCESS; + if (!prefix || !prefix->uri) { + return SERD_ERR_BAD_CURIE; } - return SERD_ERR_BAD_CURIE; -} -static SerdNode* -expand_uri(const SerdEnv* env, const SerdNode* node) -{ - assert(serd_node_type(node) == SERD_URI); - - return serd_new_resolved_uri(serd_node_string_view(node), env->base_uri); + uri_prefix->buf = serd_node_string(prefix->uri); + uri_prefix->len = prefix->uri ? prefix->uri->length : 0; + uri_suffix->buf = colon + 1; + uri_suffix->len = curie.len - name_len - 1; + return SERD_SUCCESS; } SerdNode* @@ -273,39 +272,40 @@ serd_env_expand_curie(const SerdEnv* const env, const SerdStringView curie) return NULL; } - SerdStringView prefix; - SerdStringView suffix; - if (serd_env_expand_in_place(env, curie, &prefix, &suffix)) { + SerdStringView prefix = SERD_EMPTY_STRING(); + SerdStringView suffix = SERD_EMPTY_STRING(); + SerdStatus st = serd_env_expand_in_place(env, curie, &prefix, &suffix); + if (st || !prefix.buf || !suffix.buf) { return NULL; } - const size_t len = prefix.len + suffix.len; - SerdNode* ret = serd_node_malloc(len, 0, SERD_URI); - char* buf = serd_node_buffer(ret); + const size_t len = prefix.len + suffix.len; + SerdNode* const ret = serd_node_malloc(len, 0u, SERD_URI); + if (!ret) { + return NULL; + } + + char* const string = serd_node_buffer(ret); + assert(string); + assert(prefix.buf); + + memcpy(string, prefix.buf, prefix.len); + memcpy(string + prefix.len, suffix.buf, suffix.len); - snprintf(buf, len + 1, "%s%s", prefix.buf, suffix.buf); - ret->length = len; return ret; } SerdNode* serd_env_expand_node(const SerdEnv* const env, const SerdNode* const node) { - if (!env || !node) { + if (!env || !node || node->type != SERD_URI) { return NULL; } - switch (node->type) { - case SERD_LITERAL: - break; - case SERD_URI: - return expand_uri(env, node); - case SERD_BLANK: - case SERD_VARIABLE: - break; - } + const SerdURIView uri = serd_node_uri_view(node); + const SerdURIView abs_uri = serd_resolve_uri(uri, env->base_uri); - return NULL; + return abs_uri.scheme.len ? serd_new_parsed_uri(abs_uri) : NULL; } void |