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.

CarlaStringList.hpp 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Carla String List
  3. * Copyright (C) 2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_STRING_LIST_HPP_INCLUDED
  18. #define CARLA_STRING_LIST_HPP_INCLUDED
  19. #include "LinkedList.hpp"
  20. // -----------------------------------------------------------------------
  21. // Helper class to manage the lifetime of a "char**" object
  22. class CharStringListPtr
  23. {
  24. public:
  25. CharStringListPtr() noexcept
  26. : fCharList(nullptr) {}
  27. CharStringListPtr(const char* const* const c) noexcept
  28. : fCharList(c) {}
  29. CharStringListPtr(const CharStringListPtr& ptr) noexcept
  30. : fCharList(nullptr)
  31. {
  32. copy(ptr.fCharList);
  33. }
  34. CharStringListPtr(const LinkedList<const char*>& list) noexcept
  35. : fCharList(nullptr)
  36. {
  37. copy(list);
  38. }
  39. ~CharStringListPtr() noexcept
  40. {
  41. clear();
  42. }
  43. // -------------------------------------------------------------------
  44. operator const char* const*() const noexcept
  45. {
  46. return fCharList;
  47. }
  48. CharStringListPtr& operator=(const char* const* const c) noexcept
  49. {
  50. clear();
  51. fCharList = c;
  52. return *this;
  53. }
  54. CharStringListPtr& operator=(const CharStringListPtr& ptr) noexcept
  55. {
  56. clear();
  57. copy(ptr.fCharList);
  58. return *this;
  59. }
  60. CharStringListPtr& operator=(const LinkedList<const char*>& list) noexcept
  61. {
  62. clear();
  63. copy(list);
  64. return *this;
  65. }
  66. // -------------------------------------------------------------------
  67. protected:
  68. void clear() noexcept
  69. {
  70. if (fCharList == nullptr)
  71. return;
  72. for (int i=0; fCharList[i] != nullptr; ++i)
  73. delete[] fCharList[i];
  74. delete[] fCharList;
  75. fCharList = nullptr;
  76. }
  77. void copy(const char* const* const c) noexcept
  78. {
  79. CARLA_SAFE_ASSERT_RETURN(c != nullptr,);
  80. CARLA_SAFE_ASSERT_RETURN(fCharList == nullptr,);
  81. size_t count = 0;
  82. for (; c[count] != nullptr; ++count) {}
  83. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  84. const char** tmpList;
  85. try {
  86. tmpList = new const char*[count+1];
  87. } CARLA_SAFE_EXCEPTION_RETURN("CharStringListPtr::copy",);
  88. for (size_t i=0; i<count; ++i)
  89. {
  90. try {
  91. tmpList[i] = carla_strdup(c[i]);
  92. }
  93. catch(...) {
  94. tmpList[i] = nullptr;
  95. break;
  96. }
  97. }
  98. tmpList[count] = nullptr;
  99. fCharList = tmpList;
  100. }
  101. void copy(const LinkedList<const char*>& list) noexcept
  102. {
  103. CARLA_SAFE_ASSERT_RETURN(fCharList == nullptr,);
  104. const size_t count(list.count());
  105. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  106. const char** tmpList;
  107. try {
  108. tmpList = new const char*[count+1];
  109. } CARLA_SAFE_EXCEPTION_RETURN("CharStringListPtr::copy",);
  110. size_t i=0;
  111. for (LinkedList<const char*>::Itenerator it = list.begin(); it.valid(); it.next(), ++i)
  112. {
  113. tmpList[i] = carla_strdup_safe(it.getValue());
  114. CARLA_SAFE_ASSERT_BREAK(tmpList[i] != nullptr);
  115. }
  116. tmpList[count] = nullptr;
  117. fCharList = tmpList;
  118. }
  119. // -------------------------------------------------------------------
  120. private:
  121. const char* const* fCharList;
  122. };
  123. // -----------------------------------------------------------------------
  124. // CarlaStringList
  125. class CarlaStringList : public LinkedList<const char*>
  126. {
  127. public:
  128. CarlaStringList() noexcept
  129. : LinkedList<const char*>() {}
  130. CarlaStringList(const CarlaStringList& list) noexcept
  131. : LinkedList<const char*>()
  132. {
  133. for (Itenerator it = list.begin(); it.valid(); it.next())
  134. LinkedList<const char*>::append(carla_strdup_safe(it.getValue()));
  135. }
  136. ~CarlaStringList() noexcept override
  137. {
  138. clear();
  139. }
  140. // -------------------------------------------------------------------
  141. void clear() noexcept
  142. {
  143. for (Itenerator it = begin(); it.valid(); it.next())
  144. {
  145. if (const char* const string = it.getValue())
  146. delete[] string;
  147. }
  148. LinkedList<const char*>::clear();
  149. }
  150. // -------------------------------------------------------------------
  151. bool append(const char* const string) noexcept
  152. {
  153. CARLA_SAFE_ASSERT_RETURN(string != nullptr, false);
  154. if (const char* const stringDup = carla_strdup_safe(string))
  155. {
  156. if (LinkedList<const char*>::append(stringDup))
  157. return true;
  158. delete[] stringDup;
  159. }
  160. return false;
  161. }
  162. bool appendAt(const char* const string, const Itenerator& it) noexcept
  163. {
  164. CARLA_SAFE_ASSERT_RETURN(string != nullptr, false);
  165. if (const char* const stringDup = carla_strdup_safe(string))
  166. {
  167. if (LinkedList<const char*>::appendAt(stringDup, it))
  168. return true;
  169. delete[] stringDup;
  170. }
  171. return false;
  172. }
  173. bool insert(const char* const string) noexcept
  174. {
  175. CARLA_SAFE_ASSERT_RETURN(string != nullptr, false);
  176. if (const char* const stringDup = carla_strdup_safe(string))
  177. {
  178. if (LinkedList<const char*>::insert(stringDup))
  179. return true;
  180. delete[] stringDup;
  181. }
  182. return false;
  183. }
  184. bool insertAt(const char* const string, const Itenerator& it) noexcept
  185. {
  186. CARLA_SAFE_ASSERT_RETURN(string != nullptr, false);
  187. if (const char* const stringDup = carla_strdup_safe(string))
  188. {
  189. if (LinkedList<const char*>::insertAt(stringDup, it))
  190. return true;
  191. delete[] stringDup;
  192. }
  193. return false;
  194. }
  195. // -------------------------------------------------------------------
  196. void remove(Itenerator& it) noexcept
  197. {
  198. if (const char* const string = it.getValue())
  199. delete[] string;
  200. LinkedList<const char*>::remove(it);
  201. }
  202. bool removeOne(const char* const string) noexcept
  203. {
  204. CARLA_SAFE_ASSERT_RETURN(string != nullptr, false);
  205. for (Itenerator it = begin(); it.valid(); it.next())
  206. {
  207. const char* const stringComp(it.getValue());
  208. CARLA_SAFE_ASSERT_CONTINUE(stringComp != nullptr);
  209. if (std::strcmp(string, stringComp) != 0)
  210. continue;
  211. delete[] stringComp;
  212. LinkedList<const char*>::remove(it);
  213. return true;
  214. }
  215. return false;
  216. }
  217. void removeAll(const char* const string) noexcept
  218. {
  219. CARLA_SAFE_ASSERT_RETURN(string != nullptr,);
  220. for (Itenerator it = begin(); it.valid(); it.next())
  221. {
  222. const char* const stringComp(it.getValue());
  223. CARLA_SAFE_ASSERT_CONTINUE(stringComp != nullptr);
  224. if (std::strcmp(string, stringComp) != 0)
  225. continue;
  226. delete[] stringComp;
  227. LinkedList<const char*>::remove(it);
  228. }
  229. }
  230. // -------------------------------------------------------------------
  231. CharStringListPtr toCharStringListPtr() const noexcept
  232. {
  233. return CharStringListPtr(*this);
  234. }
  235. CarlaStringList& operator=(const char* const* const charStringList) noexcept
  236. {
  237. clear();
  238. CARLA_SAFE_ASSERT_RETURN(charStringList != nullptr, *this);
  239. for (int i=0; charStringList[i] != nullptr; ++i)
  240. {
  241. if (const char* const string = carla_strdup_safe(charStringList[i]))
  242. LinkedList<const char*>::append(string);
  243. }
  244. return *this;
  245. }
  246. CarlaStringList& operator=(const CarlaStringList& list) noexcept
  247. {
  248. clear();
  249. for (Itenerator it = list.begin(); it.valid(); it.next())
  250. {
  251. if (const char* const string = carla_strdup_safe(it.getValue()))
  252. LinkedList<const char*>::append(string);
  253. }
  254. return *this;
  255. }
  256. CARLA_PREVENT_VIRTUAL_HEAP_ALLOCATION
  257. };
  258. // -----------------------------------------------------------------------
  259. #endif // CARLA_STRING_LIST_HPP_INCLUDED