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.

query.c 4.1KB

12 years ago
9 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. Copyright 2007-2015 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 <limits.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "lilv_internal.h"
  18. typedef enum {
  19. LILV_LANG_MATCH_NONE, ///< Language does not match at all
  20. LILV_LANG_MATCH_PARTIAL, ///< Partial (language, but not country) match
  21. LILV_LANG_MATCH_EXACT ///< Exact (language and country) match
  22. } LilvLangMatch;
  23. static LilvLangMatch
  24. lilv_lang_matches(const char* a, const char* b)
  25. {
  26. if (!strcmp(a, b)) {
  27. return LILV_LANG_MATCH_EXACT;
  28. }
  29. const char* a_dash = strchr(a, '-');
  30. const size_t a_lang_len = a_dash ? (size_t)(a_dash - a) : strlen(a);
  31. const char* b_dash = strchr(b, '-');
  32. const size_t b_lang_len = b_dash ? (size_t)(b_dash - b) : strlen(b);
  33. if (a_lang_len == b_lang_len && !strncmp(a, b, a_lang_len)) {
  34. return LILV_LANG_MATCH_PARTIAL;
  35. }
  36. return LILV_LANG_MATCH_NONE;
  37. }
  38. static LilvNodes*
  39. lilv_nodes_from_stream_objects_i18n(LilvWorld* world,
  40. SordIter* stream,
  41. SordQuadIndex field)
  42. {
  43. LilvNodes* values = lilv_nodes_new();
  44. const SordNode* nolang = NULL; // Untranslated value
  45. const SordNode* partial = NULL; // Partial language match
  46. char* syslang = lilv_get_lang();
  47. FOREACH_MATCH(stream) {
  48. const SordNode* value = sord_iter_get_node(stream, field);
  49. if (sord_node_get_type(value) == SORD_LITERAL) {
  50. const char* lang = sord_node_get_language(value);
  51. LilvLangMatch lm = LILV_LANG_MATCH_NONE;
  52. if (lang) {
  53. lm = (syslang)
  54. ? lilv_lang_matches(lang, syslang)
  55. : LILV_LANG_MATCH_PARTIAL;
  56. } else {
  57. nolang = value;
  58. if (!syslang) {
  59. lm = LILV_LANG_MATCH_EXACT;
  60. }
  61. }
  62. if (lm == LILV_LANG_MATCH_EXACT) {
  63. // Exact language match, add to results
  64. zix_tree_insert((ZixTree*)values,
  65. lilv_node_new_from_node(world, value),
  66. NULL);
  67. } else if (lm == LILV_LANG_MATCH_PARTIAL) {
  68. // Partial language match, save in case we find no exact
  69. partial = value;
  70. }
  71. } else {
  72. zix_tree_insert((ZixTree*)values,
  73. lilv_node_new_from_node(world, value),
  74. NULL);
  75. }
  76. }
  77. sord_iter_free(stream);
  78. free(syslang);
  79. if (lilv_nodes_size(values) > 0) {
  80. return values;
  81. }
  82. const SordNode* best = nolang;
  83. if (syslang && partial) {
  84. // Partial language match for system language
  85. best = partial;
  86. } else if (!best) {
  87. // No languages matches at all, and no untranslated value
  88. // Use any value, if possible
  89. best = partial;
  90. }
  91. if (best) {
  92. zix_tree_insert(
  93. (ZixTree*)values, lilv_node_new_from_node(world, best), NULL);
  94. } else {
  95. // No matches whatsoever
  96. lilv_nodes_free(values);
  97. values = NULL;
  98. }
  99. return values;
  100. }
  101. LilvNodes*
  102. lilv_nodes_from_stream_objects(LilvWorld* world,
  103. SordIter* stream,
  104. SordQuadIndex field)
  105. {
  106. if (sord_iter_end(stream)) {
  107. sord_iter_free(stream);
  108. return NULL;
  109. } else if (world->opt.filter_language) {
  110. return lilv_nodes_from_stream_objects_i18n(world, stream, field);
  111. } else {
  112. LilvNodes* values = lilv_nodes_new();
  113. FOREACH_MATCH(stream) {
  114. const SordNode* value = sord_iter_get_node(stream, field);
  115. LilvNode* node = lilv_node_new_from_node(world, value);
  116. if (node) {
  117. zix_tree_insert((ZixTree*)values, node, NULL);
  118. }
  119. }
  120. sord_iter_free(stream);
  121. return values;
  122. }
  123. }