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 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 "CarlaString.hpp"
  20. #include "LinkedList.hpp"
  21. // -----------------------------------------------------------------------
  22. // Helper class to manage the lifetime of a "char**" object
  23. class CharStringListPtr
  24. {
  25. public:
  26. CharStringListPtr() noexcept
  27. : fCharList(nullptr) {}
  28. CharStringListPtr(const char* const* const c) noexcept
  29. : fCharList(c) {}
  30. CharStringListPtr(const CharStringListPtr& ptr) noexcept
  31. : fCharList(nullptr)
  32. {
  33. copy(ptr.fCharList);
  34. }
  35. CharStringListPtr(const LinkedList<CarlaString>& list) noexcept
  36. : fCharList(nullptr)
  37. {
  38. copy(list);
  39. }
  40. ~CharStringListPtr() noexcept
  41. {
  42. clear();
  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<CarlaString>& list) noexcept
  61. {
  62. clear();
  63. copy(list);
  64. return *this;
  65. }
  66. protected:
  67. void clear() noexcept
  68. {
  69. if (fCharList == nullptr)
  70. return;
  71. for (int i=0; fCharList[i] != nullptr; ++i)
  72. delete[] fCharList[i];
  73. delete[] fCharList;
  74. fCharList = nullptr;
  75. }
  76. void copy(const char* const* const c) noexcept
  77. {
  78. CARLA_SAFE_ASSERT_RETURN(c != nullptr,);
  79. CARLA_SAFE_ASSERT_RETURN(fCharList == nullptr,);
  80. size_t count = 0;
  81. for (; c[count] != nullptr; ++count) {}
  82. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  83. const char** tmpList;
  84. try {
  85. tmpList = new const char*[count+1];
  86. } CARLA_SAFE_EXCEPTION_RETURN("CharStringListPtr::copy",);
  87. for (size_t i=0; i<count; ++i)
  88. {
  89. try {
  90. tmpList[i] = carla_strdup(c[i]);
  91. }
  92. catch(...) {
  93. tmpList[i] = nullptr;
  94. break;
  95. }
  96. }
  97. tmpList[count] = nullptr;
  98. fCharList = tmpList;
  99. }
  100. void copy(const LinkedList<CarlaString>& list) noexcept
  101. {
  102. CARLA_SAFE_ASSERT_RETURN(fCharList == nullptr,);
  103. const size_t count(list.count());
  104. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  105. const char** tmpList;
  106. try {
  107. tmpList = new const char*[count+1];
  108. } CARLA_SAFE_EXCEPTION_RETURN("CharStringListPtr::copy",);
  109. size_t i=0;
  110. for (LinkedList<CarlaString>::Itenerator it = list.begin(); it.valid(); it.next(), ++i)
  111. {
  112. const CarlaString& string(it.getValue());
  113. try {
  114. tmpList[i] = string.dup();
  115. }
  116. catch(...) {
  117. tmpList[i] = nullptr;
  118. break;
  119. }
  120. }
  121. tmpList[count] = nullptr;
  122. fCharList = tmpList;
  123. }
  124. private:
  125. const char* const* fCharList;
  126. };
  127. // -----------------------------------------------------------------------
  128. // CarlaStringList
  129. class CarlaStringList : public LinkedList<CarlaString>
  130. {
  131. public:
  132. CarlaStringList() noexcept
  133. : LinkedList<CarlaString>(true) {}
  134. #if 0
  135. CarlaStringList(const CarlaStringList& list) noexcept
  136. : LinkedList<CarlaString>(true)
  137. {
  138. for (Itenerator it = list.begin(); it.valid(); it.next())
  139. LinkedList<CarlaString>::append(it.getValue());
  140. }
  141. #endif
  142. ~CarlaStringList() noexcept
  143. {
  144. clear();
  145. }
  146. bool append(const char* const strBuf) noexcept
  147. {
  148. const CarlaString string(strBuf);
  149. return LinkedList<CarlaString>::append(string);
  150. }
  151. bool appendAt(const char* const strBuf, const Itenerator& it) noexcept
  152. {
  153. const CarlaString string(strBuf);
  154. return LinkedList<CarlaString>::appendAt(string, it);
  155. }
  156. bool insert(const char* const strBuf) noexcept
  157. {
  158. const CarlaString string(strBuf);
  159. return LinkedList<CarlaString>::insert(string);
  160. }
  161. bool insertAt(const char* const strBuf, const Itenerator& it) noexcept
  162. {
  163. const CarlaString string(strBuf);
  164. return LinkedList<CarlaString>::insertAt(string, it);
  165. }
  166. CharStringListPtr toCharStringListPtr() const noexcept
  167. {
  168. return CharStringListPtr(*this);
  169. }
  170. CarlaStringList& operator=(const char* const* const charStringList) noexcept
  171. {
  172. clear();
  173. for (int i=0; charStringList[i] != nullptr; ++i)
  174. append(charStringList[i]);
  175. return *this;
  176. }
  177. CarlaStringList& operator=(const CarlaStringList& list) noexcept
  178. {
  179. clear();
  180. for (Itenerator it = list.begin(); it.valid(); it.next())
  181. LinkedList<CarlaString>::append(it.getValue());
  182. return *this;
  183. }
  184. private:
  185. LinkedList<CarlaString> fList;
  186. };
  187. // -----------------------------------------------------------------------
  188. #endif // CARLA_STRING_LIST_HPP_INCLUDED