aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-09-30 17:31:10 +0200
committerDavid Robillard <d@drobilla.net>2020-06-21 18:12:04 +0200
commitd188719d6f585d70791a7ad00be73f780b13226f (patch)
tree7d15e33afbad8804a8da8e7c24af784f4427e26b
parent4f2640a2e4689cdda26a914c77aa2822a06da0f5 (diff)
downloadserd-d188719d6f585d70791a7ad00be73f780b13226f.tar.gz
serd-d188719d6f585d70791a7ad00be73f780b13226f.tar.bz2
serd-d188719d6f585d70791a7ad00be73f780b13226f.zip
Add serd_env_copy() and serd_env_equals()
-rw-r--r--serd/serd.h14
-rw-r--r--src/env.c36
-rw-r--r--tests/serd_test.c16
3 files changed, 66 insertions, 0 deletions
diff --git a/serd/serd.h b/serd/serd.h
index 119445fa..f0d72774 100644
--- a/serd/serd.h
+++ b/serd/serd.h
@@ -933,6 +933,20 @@ SerdEnv*
serd_env_new(const SerdNode* base_uri);
/**
+ Copy an environment.
+*/
+SERD_API
+SerdEnv*
+serd_env_copy(const SerdEnv* env);
+
+/**
+ Return true iff `a` is equal to `b`.
+*/
+SERD_API
+bool
+serd_env_equals(const SerdEnv* a, const SerdEnv* b);
+
+/**
Free `ns`.
*/
SERD_API
diff --git a/src/env.c b/src/env.c
index 1b5617fd..52b5ac53 100644
--- a/src/env.c
+++ b/src/env.c
@@ -47,6 +47,22 @@ serd_env_new(const SerdNode* base_uri)
return env;
}
+SerdEnv*
+serd_env_copy(const SerdEnv* env)
+{
+ SerdEnv* copy = (SerdEnv*)calloc(1, sizeof(struct SerdEnvImpl));
+
+ 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);
+ }
+
+ serd_env_set_base_uri(copy, serd_env_get_base_uri(env));
+ return copy;
+}
+
void
serd_env_free(SerdEnv* env)
{
@@ -59,6 +75,26 @@ serd_env_free(SerdEnv* env)
free(env);
}
+bool
+serd_env_equals(const SerdEnv* a, const SerdEnv* b)
+{
+ if (!a || !b) {
+ return !a == !b;
+ } else if (a->n_prefixes != b->n_prefixes ||
+ !serd_node_equals(a->base_uri_node, b->base_uri_node)) {
+ return false;
+ }
+
+ for (size_t i = 0; i < a->n_prefixes; ++i) {
+ if (!serd_node_equals(a->prefixes[i].name, b->prefixes[i].name) ||
+ !serd_node_equals(a->prefixes[i].uri, b->prefixes[i].uri)) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
const SerdURI*
serd_env_get_parsed_base_uri(const SerdEnv* env)
{
diff --git a/tests/serd_test.c b/tests/serd_test.c
index a50ee846..82924710 100644
--- a/tests/serd_test.c
+++ b/tests/serd_test.c
@@ -599,6 +599,21 @@ test_env(void)
SerdNode* qualified = serd_env_qualify(env, foo_u);
assert(serd_node_equals(qualified, foo_c));
+
+ SerdEnv* env_copy = serd_env_copy(env);
+ assert(serd_env_equals(env, env_copy));
+
+ SerdNode* qualified2 = serd_env_expand(env_copy, foo_u);
+ assert(serd_node_equals(qualified, foo_c));
+ serd_node_free(qualified2);
+
+ serd_env_set_prefix_from_strings(
+ env_copy, "test", "http://example.org/test");
+ assert(!serd_env_equals(env, env_copy));
+
+ serd_env_set_prefix_from_strings(env, "test2", "http://example.org/test");
+ assert(!serd_env_equals(env, env_copy));
+
serd_node_free(qualified);
serd_node_free(foo_c);
serd_node_free(foo_u);
@@ -606,6 +621,7 @@ test_env(void)
serd_node_free(b);
serd_node_free(pre);
serd_node_free(eg);
+ serd_env_free(env_copy);
serd_env_free(env);
serd_world_free(world);