Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

272 lines
6.9KB

  1. /*
  2. Copyright 2011-2012 David Robillard <http://drobilla.net>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. #include "serd_internal.h"
  15. #include <assert.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. typedef struct {
  19. SerdNode name;
  20. SerdNode uri;
  21. } SerdPrefix;
  22. struct SerdEnvImpl {
  23. SerdPrefix* prefixes;
  24. size_t n_prefixes;
  25. SerdNode base_uri_node;
  26. SerdURI base_uri;
  27. };
  28. SERD_API
  29. SerdEnv*
  30. serd_env_new(const SerdNode* base_uri)
  31. {
  32. SerdEnv* env = (SerdEnv*)malloc(sizeof(struct SerdEnvImpl));
  33. env->prefixes = NULL;
  34. env->n_prefixes = 0;
  35. env->base_uri_node = SERD_NODE_NULL;
  36. env->base_uri = SERD_URI_NULL;
  37. if (base_uri) {
  38. serd_env_set_base_uri(env, base_uri);
  39. }
  40. return env;
  41. }
  42. SERD_API
  43. void
  44. serd_env_free(SerdEnv* env)
  45. {
  46. for (size_t i = 0; i < env->n_prefixes; ++i) {
  47. serd_node_free(&env->prefixes[i].name);
  48. serd_node_free(&env->prefixes[i].uri);
  49. }
  50. free(env->prefixes);
  51. serd_node_free(&env->base_uri_node);
  52. free(env);
  53. }
  54. SERD_API
  55. const SerdNode*
  56. serd_env_get_base_uri(const SerdEnv* env,
  57. SerdURI* out)
  58. {
  59. if (out) {
  60. *out = env->base_uri;
  61. }
  62. return &env->base_uri_node;
  63. }
  64. SERD_API
  65. SerdStatus
  66. serd_env_set_base_uri(SerdEnv* env,
  67. const SerdNode* uri_node)
  68. {
  69. // Resolve base URI and create a new node and URI for it
  70. SerdURI base_uri;
  71. SerdNode base_uri_node = serd_node_new_uri_from_node(
  72. uri_node, &env->base_uri, &base_uri);
  73. if (base_uri_node.buf) {
  74. // Replace the current base URI
  75. serd_node_free(&env->base_uri_node);
  76. env->base_uri_node = base_uri_node;
  77. env->base_uri = base_uri;
  78. return SERD_SUCCESS;
  79. }
  80. return SERD_ERR_BAD_ARG;
  81. }
  82. static inline SerdPrefix*
  83. serd_env_find(const SerdEnv* env,
  84. const uint8_t* name,
  85. size_t name_len)
  86. {
  87. for (size_t i = 0; i < env->n_prefixes; ++i) {
  88. const SerdNode* const prefix_name = &env->prefixes[i].name;
  89. if (prefix_name->n_bytes == name_len) {
  90. if (!memcmp(prefix_name->buf, name, name_len)) {
  91. return &env->prefixes[i];
  92. }
  93. }
  94. }
  95. return NULL;
  96. }
  97. static void
  98. serd_env_add(SerdEnv* env,
  99. const SerdNode* name,
  100. const SerdNode* uri)
  101. {
  102. SerdPrefix* const prefix = serd_env_find(env, name->buf, name->n_bytes);
  103. if (prefix) {
  104. SerdNode old_prefix_uri = prefix->uri;
  105. prefix->uri = serd_node_copy(uri);
  106. serd_node_free(&old_prefix_uri);
  107. } else {
  108. env->prefixes = (SerdPrefix*)realloc(
  109. env->prefixes, (++env->n_prefixes) * sizeof(SerdPrefix));
  110. env->prefixes[env->n_prefixes - 1].name = serd_node_copy(name);
  111. env->prefixes[env->n_prefixes - 1].uri = serd_node_copy(uri);
  112. }
  113. }
  114. SERD_API
  115. SerdStatus
  116. serd_env_set_prefix(SerdEnv* env,
  117. const SerdNode* name,
  118. const SerdNode* uri_node)
  119. {
  120. if (!name->buf || uri_node->type != SERD_URI) {
  121. return SERD_ERR_BAD_ARG;
  122. } else if (serd_uri_string_has_scheme(uri_node->buf)) {
  123. // Set prefix to absolute URI
  124. serd_env_add(env, name, uri_node);
  125. } else {
  126. // Resolve relative URI and create a new node and URI for it
  127. SerdURI abs_uri;
  128. SerdNode abs_uri_node = serd_node_new_uri_from_node(
  129. uri_node, &env->base_uri, &abs_uri);
  130. // Set prefix to resolved (absolute) URI
  131. serd_env_add(env, name, &abs_uri_node);
  132. serd_node_free(&abs_uri_node);
  133. }
  134. return SERD_SUCCESS;
  135. }
  136. SERD_API
  137. SerdStatus
  138. serd_env_set_prefix_from_strings(SerdEnv* env,
  139. const uint8_t* name,
  140. const uint8_t* uri)
  141. {
  142. const SerdNode name_node = serd_node_from_string(SERD_LITERAL, name);
  143. const SerdNode uri_node = serd_node_from_string(SERD_URI, uri);
  144. return serd_env_set_prefix(env, &name_node, &uri_node);
  145. }
  146. static inline bool
  147. is_nameChar(const uint8_t c)
  148. {
  149. return is_alpha(c) || is_digit(c) || c == '_';
  150. }
  151. /**
  152. Return true iff @c buf is a valid prefixed name suffix.
  153. TODO: This is more strict than it should be.
  154. */
  155. static inline bool
  156. is_name(const uint8_t* buf, size_t len)
  157. {
  158. for (size_t i = 0; i < len; ++i) {
  159. if (!is_nameChar(buf[i])) {
  160. return false;
  161. }
  162. }
  163. return true;
  164. }
  165. SERD_API
  166. bool
  167. serd_env_qualify(const SerdEnv* env,
  168. const SerdNode* uri,
  169. SerdNode* prefix_name,
  170. SerdChunk* suffix)
  171. {
  172. for (size_t i = 0; i < env->n_prefixes; ++i) {
  173. const SerdNode* const prefix_uri = &env->prefixes[i].uri;
  174. if (uri->n_bytes >= prefix_uri->n_bytes) {
  175. if (!strncmp((const char*)uri->buf,
  176. (const char*)prefix_uri->buf,
  177. prefix_uri->n_bytes)) {
  178. *prefix_name = env->prefixes[i].name;
  179. suffix->buf = uri->buf + prefix_uri->n_bytes;
  180. suffix->len = uri->n_bytes - prefix_uri->n_bytes;
  181. if (is_name(suffix->buf, suffix->len)) {
  182. return true;
  183. }
  184. }
  185. }
  186. }
  187. return false;
  188. }
  189. SERD_API
  190. SerdStatus
  191. serd_env_expand(const SerdEnv* env,
  192. const SerdNode* qname,
  193. SerdChunk* uri_prefix,
  194. SerdChunk* uri_suffix)
  195. {
  196. const uint8_t* const colon = (const uint8_t*)memchr(
  197. qname->buf, ':', qname->n_bytes + 1);
  198. if (!colon) {
  199. return SERD_ERR_BAD_ARG; // Invalid qname
  200. }
  201. const size_t name_len = colon - qname->buf;
  202. const SerdPrefix* const prefix = serd_env_find(env, qname->buf, name_len);
  203. if (prefix) {
  204. uri_prefix->buf = prefix->uri.buf;
  205. uri_prefix->len = prefix->uri.n_bytes;
  206. uri_suffix->buf = colon + 1;
  207. uri_suffix->len = qname->n_bytes - (colon - qname->buf) - 1;
  208. return SERD_SUCCESS;
  209. }
  210. return SERD_ERR_NOT_FOUND;
  211. }
  212. SERD_API
  213. SerdNode
  214. serd_env_expand_node(const SerdEnv* env,
  215. const SerdNode* node)
  216. {
  217. switch (node->type) {
  218. case SERD_CURIE: {
  219. SerdChunk prefix;
  220. SerdChunk suffix;
  221. if (serd_env_expand(env, node, &prefix, &suffix)) {
  222. return SERD_NODE_NULL;
  223. }
  224. const size_t len = prefix.len + suffix.len; // FIXME: UTF-8?
  225. uint8_t* buf = (uint8_t*)malloc(len + 1);
  226. SerdNode ret = { buf, len, len, 0, SERD_URI };
  227. snprintf((char*)buf, ret.n_bytes + 1, "%s%s", prefix.buf, suffix.buf);
  228. return ret;
  229. }
  230. case SERD_URI: {
  231. SerdURI ignored;
  232. return serd_node_new_uri_from_node(node, &env->base_uri, &ignored);
  233. }
  234. default:
  235. return SERD_NODE_NULL;
  236. }
  237. }
  238. SERD_API
  239. void
  240. serd_env_foreach(const SerdEnv* env,
  241. SerdPrefixSink func,
  242. void* handle)
  243. {
  244. for (size_t i = 0; i < env->n_prefixes; ++i) {
  245. func(handle, &env->prefixes[i].name, &env->prefixes[i].uri);
  246. }
  247. }