summaryrefslogtreecommitdiffstats
path: root/test/test_utils.h
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-04-14 13:34:25 -0400
committerDavid Robillard <d@drobilla.net>2021-04-14 13:34:25 -0400
commitc37e9cccb6a8dfee42bb50ec2646ecc41f353ae2 (patch)
tree10f9bc42d45ae779e799afcb8b3211240b12841c /test/test_utils.h
parent95b2b6e8db55a0a06aaee32a0e0d36ce9b087cb3 (diff)
downloadsratom-c37e9cccb6a8dfee42bb50ec2646ecc41f353ae2.tar.gz
sratom-c37e9cccb6a8dfee42bb50ec2646ecc41f353ae2.tar.bz2
sratom-c37e9cccb6a8dfee42bb50ec2646ecc41f353ae2.zip
Add missing file
Diffstat (limited to 'test/test_utils.h')
-rw-r--r--test/test_utils.h64
1 files changed, 64 insertions, 0 deletions
diff --git a/test/test_utils.h b/test/test_utils.h
new file mode 100644
index 0000000..7081433
--- /dev/null
+++ b/test/test_utils.h
@@ -0,0 +1,64 @@
+/*
+ Copyright 2019-2021 David Robillard <d@drobilla.net>
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted, provided that the above
+ copyright notice and this permission notice appear in all copies.
+
+ THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#include "lv2/urid/urid.h"
+
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+/// Simple O(n) URI map
+typedef struct {
+ char** uris;
+ uint32_t n_uris;
+} Uris;
+
+static char*
+copy_string(const char* str)
+{
+ const size_t len = strlen(str);
+ char* dup = (char*)malloc(len + 1);
+ memcpy(dup, str, len + 1);
+ return dup;
+}
+
+static LV2_URID
+urid_map(LV2_URID_Map_Handle handle, const char* uri)
+{
+ Uris* const uris = (Uris*)handle;
+
+ for (uint32_t i = 0; i < uris->n_uris; ++i) {
+ if (!strcmp(uris->uris[i], uri)) {
+ return i + 1;
+ }
+ }
+
+ uris->uris = (char**)realloc(uris->uris, ++uris->n_uris * sizeof(char*));
+ uris->uris[uris->n_uris - 1] = copy_string(uri);
+ return uris->n_uris;
+}
+
+static const char*
+urid_unmap(LV2_URID_Unmap_Handle handle, LV2_URID urid)
+{
+ Uris* const uris = (Uris*)handle;
+
+ if (urid > 0 && urid <= uris->n_uris) {
+ return uris->uris[urid - 1];
+ }
+
+ return NULL;
+}