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

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. std::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. tmpList[count] = nullptr;
  89. for (std::size_t i=0; i<count; ++i)
  90. {
  91. tmpList[i] = carla_strdup_safe(c[i]);
  92. CARLA_SAFE_ASSERT_BREAK(tmpList[i] != nullptr);
  93. }
  94. fCharList = tmpList;
  95. }
  96. void copy(const LinkedList<const char*>& list) noexcept
  97. {
  98. CARLA_SAFE_ASSERT_RETURN(fCharList == nullptr,);
  99. const std::size_t count(list.count());
  100. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  101. const char** tmpList;
  102. try {
  103. tmpList = new const char*[count+1];
  104. } CARLA_SAFE_EXCEPTION_RETURN("CharStringListPtr::copy",);
  105. tmpList[count] = nullptr;
  106. std::size_t i=0;
  107. for (LinkedList<const char*>::Itenerator it = list.begin(); it.valid(); it.next(), ++i)
  108. {
  109. tmpList[i] = carla_strdup_safe(it.getValue(nullptr));
  110. CARLA_SAFE_ASSERT_BREAK(tmpList[i] != nullptr);
  111. }
  112. CARLA_SAFE_ASSERT(i == count);
  113. fCharList = tmpList;
  114. }
  115. // -------------------------------------------------------------------
  116. private:
  117. const char* const* fCharList;
  118. };
  119. // -----------------------------------------------------------------------
  120. // CarlaStringList
  121. class CarlaStringList : public LinkedList<const char*>
  122. {
  123. public:
  124. CarlaStringList() noexcept
  125. : LinkedList<const char*>() {}
  126. CarlaStringList(const CarlaStringList& list) noexcept
  127. : LinkedList<const char*>()
  128. {
  129. for (Itenerator it = list.begin(); it.valid(); it.next())
  130. LinkedList<const char*>::append(carla_strdup_safe(it.getValue(nullptr)));
  131. }
  132. ~CarlaStringList() noexcept override
  133. {
  134. clear();
  135. }
  136. // -------------------------------------------------------------------
  137. void clear() noexcept
  138. {
  139. for (Itenerator it = begin(); it.valid(); it.next())
  140. {
  141. if (const char* const string = it.getValue(nullptr))
  142. delete[] string;
  143. }
  144. LinkedList<const char*>::clear();
  145. }
  146. // -------------------------------------------------------------------
  147. bool append(const char* const string) noexcept
  148. {
  149. CARLA_SAFE_ASSERT_RETURN(string != nullptr, false);
  150. if (const char* const stringDup = carla_strdup_safe(string))
  151. {
  152. if (LinkedList<const char*>::append(stringDup))
  153. return true;
  154. delete[] stringDup;
  155. }
  156. return false;
  157. }
  158. bool appendAt(const char* const string, const Itenerator& it) noexcept
  159. {
  160. CARLA_SAFE_ASSERT_RETURN(string != nullptr, false);
  161. if (const char* const stringDup = carla_strdup_safe(string))
  162. {
  163. if (LinkedList<const char*>::appendAt(stringDup, it))
  164. return true;
  165. delete[] stringDup;
  166. }
  167. return false;
  168. }
  169. bool insert(const char* const string) noexcept
  170. {
  171. CARLA_SAFE_ASSERT_RETURN(string != nullptr, false);
  172. if (const char* const stringDup = carla_strdup_safe(string))
  173. {
  174. if (LinkedList<const char*>::insert(stringDup))
  175. return true;
  176. delete[] stringDup;
  177. }
  178. return false;
  179. }
  180. bool insertAt(const char* const string, const Itenerator& it) noexcept
  181. {
  182. CARLA_SAFE_ASSERT_RETURN(string != nullptr, false);
  183. if (const char* const stringDup = carla_strdup_safe(string))
  184. {
  185. if (LinkedList<const char*>::insertAt(stringDup, it))
  186. return true;
  187. delete[] stringDup;
  188. }
  189. return false;
  190. }
  191. // -------------------------------------------------------------------
  192. void remove(Itenerator& it) noexcept
  193. {
  194. if (const char* const string = it.getValue(nullptr))
  195. delete[] string;
  196. LinkedList<const char*>::remove(it);
  197. }
  198. bool removeOne(const char* const string) noexcept
  199. {
  200. CARLA_SAFE_ASSERT_RETURN(string != nullptr, false);
  201. for (Itenerator it = begin(); it.valid(); it.next())
  202. {
  203. const char* const stringComp(it.getValue(nullptr));
  204. CARLA_SAFE_ASSERT_CONTINUE(stringComp != nullptr);
  205. if (std::strcmp(string, stringComp) != 0)
  206. continue;
  207. delete[] stringComp;
  208. LinkedList<const char*>::remove(it);
  209. return true;
  210. }
  211. return false;
  212. }
  213. void removeAll(const char* const string) noexcept
  214. {
  215. CARLA_SAFE_ASSERT_RETURN(string != nullptr,);
  216. for (Itenerator it = begin(); it.valid(); it.next())
  217. {
  218. const char* const stringComp(it.getValue(nullptr));
  219. CARLA_SAFE_ASSERT_CONTINUE(stringComp != nullptr);
  220. if (std::strcmp(string, stringComp) != 0)
  221. continue;
  222. delete[] stringComp;
  223. LinkedList<const char*>::remove(it);
  224. }
  225. }
  226. // -------------------------------------------------------------------
  227. CharStringListPtr toCharStringListPtr() const noexcept
  228. {
  229. return CharStringListPtr(*this);
  230. }
  231. CarlaStringList& operator=(const char* const* const charStringList) noexcept
  232. {
  233. clear();
  234. CARLA_SAFE_ASSERT_RETURN(charStringList != nullptr, *this);
  235. for (int i=0; charStringList[i] != nullptr; ++i)
  236. {
  237. if (const char* const string = carla_strdup_safe(charStringList[i]))
  238. LinkedList<const char*>::append(string);
  239. }
  240. return *this;
  241. }
  242. CarlaStringList& operator=(const CarlaStringList& list) noexcept
  243. {
  244. clear();
  245. for (Itenerator it = list.begin(); it.valid(); it.next())
  246. {
  247. if (const char* const string = carla_strdup_safe(it.getValue(nullptr)))
  248. LinkedList<const char*>::append(string);
  249. }
  250. return *this;
  251. }
  252. CARLA_PREVENT_VIRTUAL_HEAP_ALLOCATION
  253. };
  254. // -----------------------------------------------------------------------
  255. #endif // CARLA_STRING_LIST_HPP_INCLUDED