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.

427 lines
11KB

  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() noexcept
  125. : LinkedList<const char*>() {}
  126. CarlaStringList(const CarlaStringList& list) noexcept
  127. : LinkedList<const char*>()
  128. {
  129. for (Itenerator it = list.begin2(); 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 = begin2(); 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. const char* getAt(const std::size_t index) const noexcept
  193. {
  194. CARLA_SAFE_ASSERT_RETURN(fCount > 0 && index < fCount, nullptr);
  195. std::size_t i = 0;
  196. ListHead* entry = fQueue.next;
  197. for (; i++ != index; entry = entry->next) {}
  198. const Data* const data(list_entry_const(entry, Data, siblings));
  199. CARLA_SAFE_ASSERT_RETURN(data != nullptr, nullptr);
  200. return data->value;
  201. }
  202. const char* getFirst() const noexcept
  203. {
  204. CARLA_SAFE_ASSERT_RETURN(fCount > 0, nullptr);
  205. const Data* const data(list_entry_const(fQueue.next, Data, siblings));
  206. CARLA_SAFE_ASSERT_RETURN(data != nullptr, nullptr);
  207. return data->value;
  208. }
  209. const char* getLast() const noexcept
  210. {
  211. CARLA_SAFE_ASSERT_RETURN(fCount > 0, nullptr);
  212. const Data* const data(list_entry_const(fQueue.prev, Data, siblings));
  213. CARLA_SAFE_ASSERT_RETURN(data != nullptr, nullptr);
  214. return data->value;
  215. }
  216. // -------------------------------------------------------------------
  217. const char* getAndRemoveFirst() noexcept
  218. {
  219. CARLA_SAFE_ASSERT_RETURN(fCount > 0, nullptr);
  220. const Data* const data = list_entry_const(fQueue.next, Data, siblings);
  221. CARLA_SAFE_ASSERT_RETURN(data != nullptr, nullptr);
  222. const char* const ret = data->value;
  223. Itenerator it = begin2();
  224. LinkedList<const char*>::remove(it);
  225. return ret;
  226. }
  227. // -------------------------------------------------------------------
  228. bool contains(const char* const string) noexcept
  229. {
  230. CARLA_SAFE_ASSERT_RETURN(string != nullptr, false);
  231. if (fCount == 0)
  232. return false;
  233. for (Itenerator it = begin2(); it.valid(); it.next())
  234. {
  235. const char* const stringComp(it.getValue(nullptr));
  236. CARLA_SAFE_ASSERT_CONTINUE(stringComp != nullptr);
  237. if (std::strcmp(string, stringComp) == 0)
  238. return true;
  239. }
  240. return false;
  241. }
  242. const char* containsAndReturnString(const char* const string) noexcept
  243. {
  244. CARLA_SAFE_ASSERT_RETURN(string != nullptr, nullptr);
  245. if (fCount == 0)
  246. return nullptr;
  247. for (Itenerator it = begin2(); it.valid(); it.next())
  248. {
  249. const char* const stringComp(it.getValue(nullptr));
  250. CARLA_SAFE_ASSERT_CONTINUE(stringComp != nullptr);
  251. if (std::strcmp(string, stringComp) == 0)
  252. return stringComp;
  253. }
  254. return nullptr;
  255. }
  256. // -------------------------------------------------------------------
  257. void remove(Itenerator& it) noexcept
  258. {
  259. if (const char* const string = it.getValue(nullptr))
  260. delete[] string;
  261. LinkedList<const char*>::remove(it);
  262. }
  263. bool removeOne(const char* const string) noexcept
  264. {
  265. CARLA_SAFE_ASSERT_RETURN(string != nullptr, false);
  266. for (Itenerator it = begin2(); it.valid(); it.next())
  267. {
  268. const char* const stringComp(it.getValue(nullptr));
  269. CARLA_SAFE_ASSERT_CONTINUE(stringComp != nullptr);
  270. if (std::strcmp(string, stringComp) != 0)
  271. continue;
  272. delete[] stringComp;
  273. LinkedList<const char*>::remove(it);
  274. return true;
  275. }
  276. return false;
  277. }
  278. void removeAll(const char* const string) noexcept
  279. {
  280. CARLA_SAFE_ASSERT_RETURN(string != nullptr,);
  281. for (Itenerator it = begin2(); it.valid(); it.next())
  282. {
  283. const char* const stringComp(it.getValue(nullptr));
  284. CARLA_SAFE_ASSERT_CONTINUE(stringComp != nullptr);
  285. if (std::strcmp(string, stringComp) != 0)
  286. continue;
  287. delete[] stringComp;
  288. LinkedList<const char*>::remove(it);
  289. }
  290. }
  291. // -------------------------------------------------------------------
  292. CharStringListPtr toCharStringListPtr() const noexcept
  293. {
  294. return CharStringListPtr(*this);
  295. }
  296. CarlaStringList& operator=(const char* const* const charStringList) noexcept
  297. {
  298. clear();
  299. CARLA_SAFE_ASSERT_RETURN(charStringList != nullptr, *this);
  300. for (int i=0; charStringList[i] != nullptr; ++i)
  301. {
  302. if (const char* const string = carla_strdup_safe(charStringList[i]))
  303. LinkedList<const char*>::append(string);
  304. }
  305. return *this;
  306. }
  307. CarlaStringList& operator=(const CarlaStringList& list) noexcept
  308. {
  309. clear();
  310. for (Itenerator it = list.begin2(); it.valid(); it.next())
  311. {
  312. if (const char* const string = carla_strdup_safe(it.getValue(nullptr)))
  313. LinkedList<const char*>::append(string);
  314. }
  315. return *this;
  316. }
  317. CARLA_PREVENT_VIRTUAL_HEAP_ALLOCATION
  318. };
  319. // -----------------------------------------------------------------------
  320. #endif // CARLA_STRING_LIST_HPP_INCLUDED