diff options
Diffstat (limited to 'src/env.c')
-rw-r--r-- | src/env.c | 333 |
1 files changed, 165 insertions, 168 deletions
@@ -23,141 +23,137 @@ #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; - SerdURI base_uri; + SerdPrefix* prefixes; + size_t n_prefixes; + SerdNode base_uri_node; + SerdURI base_uri; }; SerdEnv* serd_env_new(const SerdNode* base_uri) { - SerdEnv* env = (SerdEnv*)calloc(1, sizeof(struct SerdEnvImpl)); - if (env && base_uri) { - serd_env_set_base_uri(env, base_uri); - } - return env; + SerdEnv* env = (SerdEnv*)calloc(1, sizeof(struct SerdEnvImpl)); + if (env && base_uri) { + serd_env_set_base_uri(env, base_uri); + } + + return env; } void serd_env_free(SerdEnv* env) { - if (!env) { - return; - } - - 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); + if (!env) { + return; + } + + 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); } const SerdNode* -serd_env_get_base_uri(const SerdEnv* env, - SerdURI* out) +serd_env_get_base_uri(const SerdEnv* env, SerdURI* out) { - if (out) { - *out = env->base_uri; - } - return &env->base_uri_node; + 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 SerdNode* uri) { - if (!env || (uri && uri->type != SERD_URI)) { - return SERD_ERR_BAD_ARG; - } - - if (!uri || !uri->buf) { - serd_node_free(&env->base_uri_node); - env->base_uri_node = SERD_NODE_NULL; - env->base_uri = SERD_URI_NULL; - return SERD_SUCCESS; - } - - // Resolve base URI and create a new node and URI for it - SerdURI base_uri; - SerdNode base_uri_node = serd_node_new_uri_from_node( - uri, &env->base_uri, &base_uri); - - // Replace the current base URI - serd_node_free(&env->base_uri_node); - env->base_uri_node = base_uri_node; - env->base_uri = base_uri; - - return SERD_SUCCESS; + if (!env || (uri && uri->type != SERD_URI)) { + return SERD_ERR_BAD_ARG; + } + + if (!uri || !uri->buf) { + serd_node_free(&env->base_uri_node); + env->base_uri_node = SERD_NODE_NULL; + env->base_uri = SERD_URI_NULL; + return SERD_SUCCESS; + } + + // Resolve base URI and create a new node and URI for it + SerdURI base_uri; + SerdNode base_uri_node = + serd_node_new_uri_from_node(uri, &env->base_uri, &base_uri); + + // Replace the current base URI + serd_node_free(&env->base_uri_node); + env->base_uri_node = base_uri_node; + env->base_uri = base_uri; + + return SERD_SUCCESS; } static inline SERD_PURE_FUNC SerdPrefix* -serd_env_find(const SerdEnv* env, - const uint8_t* name, - size_t name_len) +serd_env_find(const SerdEnv* env, const uint8_t* name, 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)) { - return &env->prefixes[i]; - } - } - } - return NULL; + 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)) { + return &env->prefixes[i]; + } + } + } + + return NULL; } static void -serd_env_add(SerdEnv* env, - const SerdNode* name, - const SerdNode* uri) +serd_env_add(SerdEnv* env, const SerdNode* name, const SerdNode* uri) { - SerdPrefix* const prefix = serd_env_find(env, name->buf, name->n_bytes); - 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); - } - } 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); - } + SerdPrefix* const prefix = serd_env_find(env, name->buf, name->n_bytes); + 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); + } + } 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); + } } SerdStatus -serd_env_set_prefix(SerdEnv* env, - const SerdNode* name, - const SerdNode* uri) +serd_env_set_prefix(SerdEnv* env, const SerdNode* name, const SerdNode* uri) { - if (!name->buf || uri->type != SERD_URI) { - return SERD_ERR_BAD_ARG; - } - - if (serd_uri_string_has_scheme(uri->buf)) { - // Set prefix to absolute URI - serd_env_add(env, name, uri); - } else { - // Resolve relative URI and create a new node and URI for it - SerdURI abs_uri; - SerdNode abs_uri_node = serd_node_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); - } - - return SERD_SUCCESS; + if (!name->buf || uri->type != SERD_URI) { + return SERD_ERR_BAD_ARG; + } + + if (serd_uri_string_has_scheme(uri->buf)) { + // Set prefix to absolute URI + serd_env_add(env, name, uri); + } else { + // Resolve relative URI and create a new node and URI for it + SerdURI abs_uri; + SerdNode abs_uri_node = + serd_node_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); + } + + return SERD_SUCCESS; } SerdStatus @@ -165,10 +161,10 @@ serd_env_set_prefix_from_strings(SerdEnv* env, const uint8_t* name, const uint8_t* uri) { - const SerdNode name_node = serd_node_from_string(SERD_LITERAL, name); - const SerdNode uri_node = serd_node_from_string(SERD_URI, uri); + const SerdNode name_node = serd_node_from_string(SERD_LITERAL, name); + const SerdNode uri_node = serd_node_from_string(SERD_URI, uri); - return serd_env_set_prefix(env, &name_node, &uri_node); + return serd_env_set_prefix(env, &name_node, &uri_node); } bool @@ -177,20 +173,20 @@ serd_env_qualify(const SerdEnv* env, SerdNode* prefix, SerdChunk* suffix) { - 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((const char*)uri->buf, - (const char*)prefix_uri->buf, - prefix_uri->n_bytes)) { - *prefix = env->prefixes[i].name; - suffix->buf = uri->buf + prefix_uri->n_bytes; - suffix->len = uri->n_bytes - prefix_uri->n_bytes; - return true; - } - } - } - 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((const char*)uri->buf, + (const char*)prefix_uri->buf, + prefix_uri->n_bytes)) { + *prefix = env->prefixes[i].name; + suffix->buf = uri->buf + prefix_uri->n_bytes; + suffix->len = uri->n_bytes - prefix_uri->n_bytes; + return true; + } + } + } + return false; } SerdStatus @@ -199,61 +195,62 @@ serd_env_expand(const SerdEnv* env, SerdChunk* uri_prefix, SerdChunk* uri_suffix) { - const uint8_t* const colon = (const uint8_t*)memchr( - curie->buf, ':', curie->n_bytes + 1); - if (curie->type != SERD_CURIE || !colon) { - return SERD_ERR_BAD_ARG; - } - - const size_t name_len = (size_t)(colon - curie->buf); - const SerdPrefix* const prefix = serd_env_find(env, curie->buf, name_len); - if (prefix) { - uri_prefix->buf = prefix->uri.buf; - uri_prefix->len = prefix->uri.n_bytes; - uri_suffix->buf = colon + 1; - uri_suffix->len = curie->n_bytes - name_len - 1; - return SERD_SUCCESS; - } - return SERD_ERR_BAD_CURIE; + const uint8_t* const colon = + (const uint8_t*)memchr(curie->buf, ':', curie->n_bytes + 1); + if (curie->type != SERD_CURIE || !colon) { + return SERD_ERR_BAD_ARG; + } + + const size_t name_len = (size_t)(colon - curie->buf); + const SerdPrefix* const prefix = serd_env_find(env, curie->buf, name_len); + if (prefix) { + uri_prefix->buf = prefix->uri.buf; + uri_prefix->len = prefix->uri.n_bytes; + uri_suffix->buf = colon + 1; + uri_suffix->len = curie->n_bytes - name_len - 1; + return SERD_SUCCESS; + } + return SERD_ERR_BAD_CURIE; } SerdNode -serd_env_expand_node(const SerdEnv* env, - const SerdNode* node) +serd_env_expand_node(const SerdEnv* env, const SerdNode* node) { - switch (node->type) { - case SERD_NOTHING: - case SERD_LITERAL: - break; - case SERD_URI: { - SerdURI ignored; - return serd_node_new_uri_from_node(node, &env->base_uri, &ignored); - } - case SERD_CURIE: { - SerdChunk prefix; - SerdChunk suffix; - if (serd_env_expand(env, node, &prefix, &suffix)) { - return SERD_NODE_NULL; - } - const size_t len = prefix.len + suffix.len; - uint8_t* buf = (uint8_t*)malloc(len + 1); - SerdNode ret = { buf, len, 0, 0, SERD_URI }; - snprintf((char*)buf, len + 1, "%s%s", prefix.buf, suffix.buf); - ret.n_chars = serd_strlen(buf, NULL, NULL); - return ret; - } - case SERD_BLANK: - break; - } - return SERD_NODE_NULL; + switch (node->type) { + case SERD_NOTHING: + case SERD_LITERAL: + break; + + case SERD_URI: { + SerdURI ignored; + return serd_node_new_uri_from_node(node, &env->base_uri, &ignored); + } + + case SERD_CURIE: { + SerdChunk prefix; + SerdChunk suffix; + if (serd_env_expand(env, node, &prefix, &suffix)) { + return SERD_NODE_NULL; + } + const size_t len = prefix.len + suffix.len; + uint8_t* buf = (uint8_t*)malloc(len + 1); + SerdNode ret = {buf, len, 0, 0, SERD_URI}; + snprintf((char*)buf, len + 1, "%s%s", prefix.buf, suffix.buf); + ret.n_chars = serd_strlen(buf, NULL, NULL); + return ret; + } + + case SERD_BLANK: + break; + } + + return SERD_NODE_NULL; } void -serd_env_foreach(const SerdEnv* env, - SerdPrefixSink func, - void* handle) +serd_env_foreach(const SerdEnv* env, SerdPrefixSink func, void* handle) { - for (size_t i = 0; i < env->n_prefixes; ++i) { - func(handle, &env->prefixes[i].name, &env->prefixes[i].uri); - } + for (size_t i = 0; i < env->n_prefixes; ++i) { + func(handle, &env->prefixes[i].name, &env->prefixes[i].uri); + } } |