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.

479 lines
11KB

  1. /*
  2. * Carla common utils
  3. * Copyright (C) 2011-2013 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 GPL.txt file
  16. */
  17. #ifndef __CARLA_STRING_HPP__
  18. #define __CARLA_STRING_HPP__
  19. #include "CarlaBase64Utils.hpp"
  20. #include "CarlaJuceUtils.hpp"
  21. // -------------------------------------------------
  22. // CarlaString class
  23. class CarlaString
  24. {
  25. public:
  26. // ---------------------------------------------
  27. // constructors (no explicit conversions allowed)
  28. explicit CarlaString()
  29. {
  30. _init();
  31. _dup(nullptr);
  32. }
  33. explicit CarlaString(char* const strBuf)
  34. {
  35. _init();
  36. _dup(strBuf);
  37. }
  38. explicit CarlaString(const char* const strBuf)
  39. {
  40. _init();
  41. _dup(strBuf);
  42. }
  43. explicit CarlaString(const int value)
  44. {
  45. const size_t strBufSize = std::abs(value/10) + 3;
  46. char strBuf[strBufSize];
  47. std::snprintf(strBuf, strBufSize, "%d", value);
  48. _init();
  49. _dup(strBuf, strBufSize);
  50. }
  51. explicit CarlaString(const unsigned int value, const bool hexadecimal = false)
  52. {
  53. const size_t strBufSize = value/10 + 2 + (hexadecimal ? 2 : 0);
  54. char strBuf[strBufSize];
  55. std::snprintf(strBuf, strBufSize, hexadecimal ? "0x%x" : "%u", value);
  56. _init();
  57. _dup(strBuf, strBufSize);
  58. }
  59. explicit CarlaString(const long int value)
  60. {
  61. const size_t strBufSize = std::abs(value/10) + 3;
  62. char strBuf[strBufSize];
  63. std::snprintf(strBuf, strBufSize, "%ld", value);
  64. _init();
  65. _dup(strBuf, strBufSize);
  66. }
  67. explicit CarlaString(const unsigned long int value, const bool hexadecimal = false)
  68. {
  69. const size_t strBufSize = value/10 + 2 + (hexadecimal ? 2 : 0);
  70. char strBuf[strBufSize];
  71. std::snprintf(strBuf, strBufSize, hexadecimal ? "0x%lx" : "%lu", value);
  72. _init();
  73. _dup(strBuf, strBufSize);
  74. }
  75. explicit CarlaString(const float value)
  76. {
  77. char strBuf[0xff];
  78. std::snprintf(strBuf, 0xff, "%f", value);
  79. _init();
  80. _dup(strBuf);
  81. }
  82. explicit CarlaString(const double value)
  83. {
  84. char strBuf[0xff];
  85. std::snprintf(strBuf, 0xff, "%g", value);
  86. _init();
  87. _dup(strBuf);
  88. }
  89. // ---------------------------------------------
  90. // non-explicit constructor
  91. CarlaString(const CarlaString& str)
  92. {
  93. _init();
  94. _dup(str.buffer);
  95. }
  96. // ---------------------------------------------
  97. // deconstructor
  98. ~CarlaString()
  99. {
  100. CARLA_ASSERT(buffer);
  101. delete[] buffer;
  102. }
  103. // ---------------------------------------------
  104. // public methods
  105. size_t length() const
  106. {
  107. return bufferLen;
  108. }
  109. bool isEmpty() const
  110. {
  111. return (bufferLen == 0);
  112. }
  113. bool isNotEmpty() const
  114. {
  115. return (bufferLen != 0);
  116. }
  117. #if __USE_GNU
  118. bool contains(const char* const strBuf, const bool ignoreCase = false) const
  119. {
  120. if (strBuf == nullptr)
  121. return false;
  122. if (ignoreCase)
  123. return (strcasestr(buffer, strBuf) != nullptr);
  124. else
  125. return (std::strstr(buffer, strBuf) != nullptr);
  126. }
  127. bool contains(const CarlaString& str, const bool ignoreCase = false) const
  128. {
  129. return contains(str.buffer, ignoreCase);
  130. }
  131. #else
  132. bool contains(const char* const strBuf) const
  133. {
  134. if (strBuf == nullptr)
  135. return false;
  136. return (std::strstr(buffer, strBuf) != nullptr);
  137. }
  138. bool contains(const CarlaString& str) const
  139. {
  140. return contains(str.buffer);
  141. }
  142. #endif
  143. bool isDigit(const size_t pos) const
  144. {
  145. if (pos >= bufferLen)
  146. return false;
  147. return (buffer[pos] >= '0' && buffer[pos] <= '9');
  148. }
  149. void clear()
  150. {
  151. truncate(0);
  152. }
  153. void replace(const char before, const char after)
  154. {
  155. if (after == '\0')
  156. return;
  157. for (size_t i=0; i < bufferLen; i++)
  158. {
  159. if (buffer[i] == before)
  160. buffer[i] = after;
  161. else if (buffer[i] == '\0')
  162. break;
  163. }
  164. }
  165. void truncate(const size_t n)
  166. {
  167. if (n >= bufferLen)
  168. return;
  169. for (size_t i=n; i < bufferLen; i++)
  170. buffer[i] = '\0';
  171. bufferLen = n;
  172. }
  173. void toBasic()
  174. {
  175. for (size_t i=0; i < bufferLen; i++)
  176. {
  177. if (buffer[i] >= '0' && buffer[i] <= '9')
  178. continue;
  179. if (buffer[i] >= 'A' && buffer[i] <= 'Z')
  180. continue;
  181. if (buffer[i] >= 'a' && buffer[i] <= 'z')
  182. continue;
  183. if (buffer[i] == '_')
  184. continue;
  185. buffer[i] = '_';
  186. }
  187. }
  188. void toLower()
  189. {
  190. static const char charDiff = 'a' - 'A';
  191. for (size_t i=0; i < bufferLen; i++)
  192. {
  193. if (buffer[i] >= 'A' && buffer[i] <= 'Z')
  194. buffer[i] += charDiff;
  195. }
  196. }
  197. void toUpper()
  198. {
  199. static const char charDiff = 'a' - 'A';
  200. for (size_t i=0; i < bufferLen; i++)
  201. {
  202. if (buffer[i] >= 'a' && buffer[i] <= 'z')
  203. buffer[i] -= charDiff;
  204. }
  205. }
  206. void toBase64()
  207. {
  208. importBinaryAsBase64((const uint8_t*)buffer, bufferLen);
  209. }
  210. void fromBase64()
  211. {
  212. uint8_t buffer2[carla_base64_decoded_max_len(buffer)];
  213. if (unsigned int len = carla_base64_decode(buffer, buffer2))
  214. _dup((char*)buffer2, len);
  215. else
  216. clear();
  217. }
  218. void importBinaryAsBase64(const uint8_t* const raw, const size_t rawLen)
  219. {
  220. const size_t rawBufferSize = carla_base64_encoded_len(rawLen) + 1;
  221. char rawBuffer[rawBufferSize];
  222. carla_base64_encode(raw, rawLen, rawBuffer);
  223. _dup(rawBuffer, rawBufferSize-1);
  224. }
  225. template<typename T>
  226. void importBinaryAsBase64(const T* const t)
  227. {
  228. const size_t tSize = sizeof(T);
  229. importBinaryAsBase64((const uint8_t*)t, tSize);
  230. }
  231. size_t exportAsBase64Binary(uint8_t** const rawPtr)
  232. {
  233. uint8_t binaryBuffer[carla_base64_decoded_max_len(buffer)];
  234. if (unsigned int len = carla_base64_decode(buffer, binaryBuffer))
  235. {
  236. uint8_t* const binaryBufferHeap = new uint8_t[len];
  237. std::memcpy(binaryBufferHeap, binaryBuffer, len);
  238. *rawPtr = binaryBufferHeap;
  239. return len;
  240. }
  241. return 0;
  242. }
  243. template<typename T>
  244. T* exportAsBase64Binary()
  245. {
  246. uint8_t binaryBuffer[carla_base64_decoded_max_len(buffer)];
  247. if (unsigned int len = carla_base64_decode(buffer, binaryBuffer))
  248. {
  249. CARLA_ASSERT_INT2(len == sizeof(T), len, sizeof(T));
  250. T* const t = new T();
  251. std::memcpy(t, binaryBuffer, len);
  252. return t;
  253. }
  254. return nullptr;
  255. }
  256. // ---------------------------------------------
  257. // public operators
  258. operator const char*() const
  259. {
  260. return buffer;
  261. }
  262. char& operator[](const size_t pos)
  263. {
  264. return buffer[pos];
  265. }
  266. bool operator==(const char* const strBuf) const
  267. {
  268. return (strBuf != nullptr && std::strcmp(buffer, strBuf) == 0);
  269. }
  270. bool operator==(const CarlaString& str) const
  271. {
  272. return operator==(str.buffer);
  273. }
  274. bool operator!=(const char* const strBuf) const
  275. {
  276. return !operator==(strBuf);
  277. }
  278. bool operator!=(const CarlaString& str) const
  279. {
  280. return !operator==(str.buffer);
  281. }
  282. CarlaString& operator=(const char* const strBuf)
  283. {
  284. _dup(strBuf);
  285. return *this;
  286. }
  287. CarlaString& operator=(const CarlaString& str)
  288. {
  289. return operator=(str.buffer);
  290. }
  291. CarlaString& operator+=(const char* const strBuf)
  292. {
  293. const size_t newBufSize = bufferLen + ((strBuf != nullptr) ? std::strlen(strBuf) : 0) + 1;
  294. char newBuf[newBufSize];
  295. std::strcpy(newBuf, buffer);
  296. std::strcat(newBuf, strBuf);
  297. _dup(newBuf, newBufSize-1);
  298. return *this;
  299. }
  300. CarlaString& operator+=(const CarlaString& str)
  301. {
  302. return operator+=(str.buffer);
  303. }
  304. CarlaString operator+(const char* const strBuf)
  305. {
  306. const size_t newBufSize = bufferLen + ((strBuf != nullptr) ? std::strlen(strBuf) : 0) + 1;
  307. char newBuf[newBufSize];
  308. std::strcpy(newBuf, buffer);
  309. std::strcat(newBuf, strBuf);
  310. return CarlaString(newBuf);
  311. }
  312. CarlaString operator+(const CarlaString& str)
  313. {
  314. return operator+(str.buffer);
  315. }
  316. // ---------------------------------------------
  317. private:
  318. char* buffer;
  319. size_t bufferLen;
  320. bool firstInit;
  321. void _init()
  322. {
  323. buffer = nullptr;
  324. bufferLen = 0;
  325. firstInit = true;
  326. }
  327. // allocate string strBuf if not null
  328. // size > 0 only if strBuf is valid
  329. void _dup(const char* const strBuf, const size_t size = 0)
  330. {
  331. if (strBuf != nullptr)
  332. {
  333. // don't recreate string if contents match
  334. if (firstInit || std::strcmp(buffer, strBuf) != 0)
  335. {
  336. if (! firstInit)
  337. {
  338. CARLA_ASSERT(buffer);
  339. delete[] buffer;
  340. }
  341. bufferLen = (size > 0) ? size : std::strlen(strBuf);
  342. buffer = new char[bufferLen+1];
  343. std::strcpy(buffer, strBuf);
  344. buffer[bufferLen] = '\0';
  345. firstInit = false;
  346. }
  347. }
  348. else
  349. {
  350. CARLA_ASSERT(size == 0);
  351. // don't recreate null string
  352. if (firstInit || bufferLen != 0)
  353. {
  354. if (! firstInit)
  355. {
  356. CARLA_ASSERT(buffer);
  357. delete[] buffer;
  358. }
  359. bufferLen = 0;
  360. buffer = new char[1];
  361. buffer[0] = '\0';
  362. firstInit = false;
  363. }
  364. }
  365. }
  366. CARLA_LEAK_DETECTOR(CarlaString)
  367. CARLA_PREVENT_HEAP_ALLOCATION
  368. };
  369. static inline
  370. CarlaString operator+(const char* const strBufBefore, const CarlaString& strAfter)
  371. {
  372. const char* const strBufAfter = (const char*)strAfter;
  373. const size_t newBufSize = ((strBufBefore != nullptr) ? std::strlen(strBufBefore) : 0) + std::strlen(strBufAfter) + 1;
  374. char newBuf[newBufSize];
  375. std::strcpy(newBuf, strBufBefore);
  376. std::strcat(newBuf, strBufAfter);
  377. return CarlaString(newBuf);
  378. }
  379. // -------------------------------------------------
  380. #endif // __CARLA_UTILS_HPP__