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.

449 lines
12KB

  1. /*
  2. * Carla String List
  3. * Copyright (C) 2014-2019 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.begin2(); 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(bool allocateElements = true) noexcept
  125. : LinkedList<const char*>(),
  126. fAllocateElements(allocateElements) {}
  127. CarlaStringList(const CarlaStringList& list) noexcept
  128. : LinkedList<const char*>(),
  129. fAllocateElements(list.fAllocateElements)
  130. {
  131. for (Itenerator it = list.begin2(); it.valid(); it.next())
  132. LinkedList<const char*>::append(carla_strdup_safe(it.getValue(nullptr)));
  133. }
  134. ~CarlaStringList() noexcept override
  135. {
  136. clear();
  137. }
  138. // -------------------------------------------------------------------
  139. void clear() noexcept
  140. {
  141. if (fAllocateElements)
  142. {
  143. for (Itenerator it = begin2(); it.valid(); it.next())
  144. {
  145. if (const char* const string = it.getValue(nullptr))
  146. delete[] string;
  147. }
  148. }
  149. LinkedList<const char*>::clear();
  150. }
  151. // -------------------------------------------------------------------
  152. bool append(const char* const string) noexcept
  153. {
  154. CARLA_SAFE_ASSERT_RETURN(string != nullptr, false);
  155. if (const char* const stringDup = fAllocateElements ? carla_strdup_safe(string) : string)
  156. {
  157. if (LinkedList<const char*>::append(stringDup))
  158. return true;
  159. delete[] stringDup;
  160. }
  161. return false;
  162. }
  163. bool appendUnique(const char* const string) noexcept
  164. {
  165. CARLA_SAFE_ASSERT_RETURN(string != nullptr, false);
  166. if (contains(string))
  167. return false;
  168. return append(string);
  169. }
  170. bool appendAt(const char* const string, const Itenerator& it) noexcept
  171. {
  172. CARLA_SAFE_ASSERT_RETURN(string != nullptr, false);
  173. if (const char* const stringDup = fAllocateElements ? carla_strdup_safe(string) : string)
  174. {
  175. if (LinkedList<const char*>::appendAt(stringDup, it))
  176. return true;
  177. delete[] stringDup;
  178. }
  179. return false;
  180. }
  181. bool insert(const char* const string) noexcept
  182. {
  183. CARLA_SAFE_ASSERT_RETURN(string != nullptr, false);
  184. if (const char* const stringDup = fAllocateElements ? carla_strdup_safe(string) : string)
  185. {
  186. if (LinkedList<const char*>::insert(stringDup))
  187. return true;
  188. delete[] stringDup;
  189. }
  190. return false;
  191. }
  192. bool insertAt(const char* const string, const Itenerator& it) noexcept
  193. {
  194. CARLA_SAFE_ASSERT_RETURN(string != nullptr, false);
  195. if (const char* const stringDup = fAllocateElements ? carla_strdup_safe(string) : string)
  196. {
  197. if (LinkedList<const char*>::insertAt(stringDup, it))
  198. return true;
  199. delete[] stringDup;
  200. }
  201. return false;
  202. }
  203. // -------------------------------------------------------------------
  204. const char* getAt(const std::size_t index) const noexcept
  205. {
  206. CARLA_SAFE_ASSERT_RETURN(fCount > 0 && index < fCount, nullptr);
  207. std::size_t i = 0;
  208. ListHead* entry = fQueue.next;
  209. for (; i++ != index; entry = entry->next) {}
  210. const Data* const data(list_entry_const(entry, Data, siblings));
  211. CARLA_SAFE_ASSERT_RETURN(data != nullptr, nullptr);
  212. return data->value;
  213. }
  214. const char* getFirst() const noexcept
  215. {
  216. CARLA_SAFE_ASSERT_RETURN(fCount > 0, nullptr);
  217. const Data* const data(list_entry_const(fQueue.next, Data, siblings));
  218. CARLA_SAFE_ASSERT_RETURN(data != nullptr, nullptr);
  219. return data->value;
  220. }
  221. const char* getLast() const noexcept
  222. {
  223. CARLA_SAFE_ASSERT_RETURN(fCount > 0, nullptr);
  224. const Data* const data(list_entry_const(fQueue.prev, Data, siblings));
  225. CARLA_SAFE_ASSERT_RETURN(data != nullptr, nullptr);
  226. return data->value;
  227. }
  228. // -------------------------------------------------------------------
  229. const char* getAndRemoveFirst() noexcept
  230. {
  231. CARLA_SAFE_ASSERT_RETURN(fCount > 0, nullptr);
  232. const Data* const data = list_entry_const(fQueue.next, Data, siblings);
  233. CARLA_SAFE_ASSERT_RETURN(data != nullptr, nullptr);
  234. const char* const ret = data->value;
  235. Itenerator it = begin2();
  236. LinkedList<const char*>::remove(it);
  237. return ret;
  238. }
  239. // -------------------------------------------------------------------
  240. bool contains(const char* const string) noexcept
  241. {
  242. CARLA_SAFE_ASSERT_RETURN(string != nullptr, false);
  243. if (fCount == 0)
  244. return false;
  245. for (Itenerator it = begin2(); it.valid(); it.next())
  246. {
  247. const char* const stringComp(it.getValue(nullptr));
  248. CARLA_SAFE_ASSERT_CONTINUE(stringComp != nullptr);
  249. if (std::strcmp(string, stringComp) == 0)
  250. return true;
  251. }
  252. return false;
  253. }
  254. const char* containsAndReturnString(const char* const string) noexcept
  255. {
  256. CARLA_SAFE_ASSERT_RETURN(string != nullptr, nullptr);
  257. if (fCount == 0)
  258. return nullptr;
  259. for (Itenerator it = begin2(); it.valid(); it.next())
  260. {
  261. const char* const stringComp(it.getValue(nullptr));
  262. CARLA_SAFE_ASSERT_CONTINUE(stringComp != nullptr);
  263. if (std::strcmp(string, stringComp) == 0)
  264. return stringComp;
  265. }
  266. return nullptr;
  267. }
  268. // -------------------------------------------------------------------
  269. void remove(Itenerator& it) noexcept
  270. {
  271. if (const char* const string = it.getValue(nullptr))
  272. delete[] string;
  273. LinkedList<const char*>::remove(it);
  274. }
  275. bool removeOne(const char* const string) noexcept
  276. {
  277. CARLA_SAFE_ASSERT_RETURN(string != nullptr, false);
  278. for (Itenerator it = begin2(); it.valid(); it.next())
  279. {
  280. const char* const stringComp(it.getValue(nullptr));
  281. CARLA_SAFE_ASSERT_CONTINUE(stringComp != nullptr);
  282. if (std::strcmp(string, stringComp) != 0)
  283. continue;
  284. delete[] stringComp;
  285. LinkedList<const char*>::remove(it);
  286. return true;
  287. }
  288. return false;
  289. }
  290. void removeAll(const char* const string) noexcept
  291. {
  292. CARLA_SAFE_ASSERT_RETURN(string != nullptr,);
  293. for (Itenerator it = begin2(); it.valid(); it.next())
  294. {
  295. const char* const stringComp(it.getValue(nullptr));
  296. CARLA_SAFE_ASSERT_CONTINUE(stringComp != nullptr);
  297. if (std::strcmp(string, stringComp) != 0)
  298. continue;
  299. delete[] stringComp;
  300. LinkedList<const char*>::remove(it);
  301. }
  302. }
  303. // -------------------------------------------------------------------
  304. CharStringListPtr toCharStringListPtr() const noexcept
  305. {
  306. return CharStringListPtr(*this);
  307. }
  308. CarlaStringList& operator=(const char* const* const charStringList) noexcept
  309. {
  310. CARLA_SAFE_ASSERT_RETURN(! fAllocateElements, *this);
  311. clear();
  312. CARLA_SAFE_ASSERT_RETURN(charStringList != nullptr, *this);
  313. for (int i=0; charStringList[i] != nullptr; ++i)
  314. {
  315. if (const char* const string = carla_strdup_safe(charStringList[i]))
  316. LinkedList<const char*>::append(string);
  317. }
  318. return *this;
  319. }
  320. CarlaStringList& operator=(const CarlaStringList& list) noexcept
  321. {
  322. CARLA_SAFE_ASSERT_RETURN(! fAllocateElements, *this);
  323. clear();
  324. for (Itenerator it = list.begin2(); it.valid(); it.next())
  325. {
  326. if (const char* const string = carla_strdup_safe(it.getValue(nullptr)))
  327. LinkedList<const char*>::append(string);
  328. }
  329. return *this;
  330. }
  331. private:
  332. const bool fAllocateElements;
  333. CARLA_PREVENT_VIRTUAL_HEAP_ALLOCATION
  334. };
  335. // -----------------------------------------------------------------------
  336. #endif // CARLA_STRING_LIST_HPP_INCLUDED