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.

uri_table.h 1.9KB

12 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. /**
  15. @file uri_table.h A toy URI map/unmap implementation.
  16. This file contains function definitions and must only be included once.
  17. */
  18. #ifndef URI_TABLE_H
  19. #define URI_TABLE_H
  20. typedef struct {
  21. char** uris;
  22. size_t n_uris;
  23. } URITable;
  24. static void
  25. uri_table_init(URITable* table)
  26. {
  27. table->uris = NULL;
  28. table->n_uris = 0;
  29. }
  30. static void
  31. uri_table_destroy(URITable* table)
  32. {
  33. free(table->uris);
  34. }
  35. static LV2_URID
  36. uri_table_map(LV2_URID_Map_Handle handle,
  37. const char* uri)
  38. {
  39. URITable* table = (URITable*)handle;
  40. for (size_t i = 0; i < table->n_uris; ++i) {
  41. if (!strcmp(table->uris[i], uri)) {
  42. return i + 1;
  43. }
  44. }
  45. const size_t len = strlen(uri);
  46. table->uris = (char**)realloc(table->uris, ++table->n_uris * sizeof(char*));
  47. table->uris[table->n_uris - 1] = malloc(len + 1);
  48. memcpy(table->uris[table->n_uris - 1], uri, len + 1);
  49. return table->n_uris;
  50. }
  51. static const char*
  52. uri_table_unmap(LV2_URID_Map_Handle handle,
  53. LV2_URID urid)
  54. {
  55. URITable* table = (URITable*)handle;
  56. if (urid > 0 && urid <= table->n_uris) {
  57. return table->uris[urid - 1];
  58. }
  59. return NULL;
  60. }
  61. #endif /* URI_TABLE_H */