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.

ysfx_parse_menu.cpp 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2021 Jean Pierre Cimalando
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // SPDX-License-Identifier: Apache-2.0
  16. //
  17. #include "ysfx_parse_menu.hpp"
  18. #include "ysfx_utils.hpp"
  19. #include <vector>
  20. #include <cstring>
  21. #include <cassert>
  22. static void ysfx_menu_insn_clear(ysfx_menu_insn_t *insn)
  23. {
  24. delete[] insn->name;
  25. }
  26. static bool ysfx_do_create_menu(std::vector<ysfx_menu_insn_t> &insns, const char **str, uint32_t *startid, uint32_t menudepth)
  27. {
  28. if (menudepth >= 8)
  29. return false;
  30. ///
  31. auto shrink_insns_to = [&insns](size_t count) {
  32. assert(insns.size() >= count);
  33. while (insns.size() > count) {
  34. ysfx_menu_insn_clear(&insns.back());
  35. insns.pop_back();
  36. }
  37. };
  38. ///
  39. size_t insn_count_at_start = insns.size();
  40. ///
  41. size_t pos = 0;
  42. uint32_t id = *startid;
  43. const char *p = *str;
  44. const char *sep = strchr(p, '|');
  45. while (sep || *p) {
  46. size_t len = sep ? (size_t)(sep - p) : strlen(p);
  47. std::string buf(p, len);
  48. p += len;
  49. if (sep)
  50. sep = strchr(++p, '|');
  51. const char *q = buf.c_str();
  52. bool subm = false;
  53. size_t insn_count_at_subm = 0;
  54. bool done = false;
  55. uint32_t item_flags = 0;
  56. while (*q && strchr(">#!<", *q)) {
  57. if (*q == '>' && !subm) {
  58. insn_count_at_subm = insns.size();
  59. insns.emplace_back();
  60. insns.back().opcode = ysfx_menu_sub;
  61. subm = ysfx_do_create_menu(insns, &p, &id, menudepth + 1);
  62. insns.emplace_back();
  63. insns.back().opcode = ysfx_menu_endsub;
  64. sep = strchr(p, '|');
  65. }
  66. if (*q == '#')
  67. item_flags |= ysfx_menu_item_disabled;
  68. if (*q == '!')
  69. item_flags |= ysfx_menu_item_checked;
  70. if (*q == '<')
  71. done = true;
  72. ++q;
  73. }
  74. if (*q) {
  75. if (subm) {
  76. for (ysfx_menu_insn_t *insn : {&insns[insn_count_at_subm], &insns.back()}) {
  77. insn->name = ysfx::strdup_using_new(q);
  78. insn->item_flags = item_flags;
  79. }
  80. }
  81. else {
  82. ysfx_menu_insn_t &insn = (insns.emplace_back(), insns.back());
  83. insn.opcode = ysfx_menu_item;
  84. insn.id = id++;
  85. insn.name = ysfx::strdup_using_new(q);
  86. insn.item_flags = item_flags;
  87. }
  88. }
  89. else {
  90. if (subm)
  91. shrink_insns_to(insn_count_at_subm);
  92. if (!done) {
  93. ysfx_menu_insn_t &insn = (insns.emplace_back(), insns.back());
  94. insn.opcode = ysfx_menu_separator;
  95. }
  96. }
  97. ++pos;
  98. if (done)
  99. break;
  100. }
  101. *str = p;
  102. *startid = id;
  103. ///
  104. if (!pos) {
  105. shrink_insns_to(insn_count_at_start);
  106. return false;
  107. }
  108. return true;
  109. }
  110. ysfx_menu_t *ysfx_parse_menu(const char *text)
  111. {
  112. std::vector<ysfx_menu_insn_t> insns;
  113. insns.reserve(256);
  114. ///
  115. auto cleanup = ysfx::defer([&insns]() {
  116. for (ysfx_menu_insn_t &insn : insns)
  117. ysfx_menu_insn_clear(&insn);
  118. });
  119. ///
  120. const char *textpos = text;
  121. uint32_t id = 1;
  122. ysfx_do_create_menu(insns, &textpos, &id, 0);
  123. ///
  124. ysfx_menu_u menu{new ysfx_menu_t};
  125. menu->insn_count = (uint32_t)insns.size();
  126. menu->insns = new ysfx_menu_insn_t[menu->insn_count];
  127. memcpy(menu->insns, insns.data(), menu->insn_count * sizeof(ysfx_menu_insn_t));
  128. insns.clear();
  129. return menu.release();
  130. }
  131. void ysfx_menu_free(ysfx_menu_t *menu)
  132. {
  133. if (!menu)
  134. return;
  135. for (uint32_t i = 0; i < menu->insn_count; ++i)
  136. ysfx_menu_insn_clear(&menu->insns[i]);
  137. delete[] menu->insns;
  138. delete menu;
  139. }