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.

516 lines
12KB

  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 = static_cast<size_t>(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 = static_cast<size_t>(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 != nullptr);
  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. size_t find(const char c)
  154. {
  155. for (size_t i=0; i < bufferLen; i++)
  156. {
  157. if (buffer[i] == c)
  158. return i;
  159. }
  160. return 0;
  161. }
  162. size_t rfind(const char c)
  163. {
  164. size_t pos = 0;
  165. for (size_t i=0; i < bufferLen; i++)
  166. {
  167. if (buffer[i] == c)
  168. pos = i;
  169. }
  170. return pos;
  171. }
  172. void replace(const char before, const char after)
  173. {
  174. if (after == '\0')
  175. return;
  176. for (size_t i=0; i < bufferLen; i++)
  177. {
  178. if (buffer[i] == before)
  179. buffer[i] = after;
  180. else if (buffer[i] == '\0')
  181. break;
  182. }
  183. }
  184. void truncate(const size_t n)
  185. {
  186. if (n >= bufferLen)
  187. return;
  188. for (size_t i=n; i < bufferLen; i++)
  189. buffer[i] = '\0';
  190. bufferLen = n;
  191. }
  192. void toBasic()
  193. {
  194. for (size_t i=0; i < bufferLen; i++)
  195. {
  196. if (buffer[i] >= '0' && buffer[i] <= '9')
  197. continue;
  198. if (buffer[i] >= 'A' && buffer[i] <= 'Z')
  199. continue;
  200. if (buffer[i] >= 'a' && buffer[i] <= 'z')
  201. continue;
  202. if (buffer[i] == '_')
  203. continue;
  204. buffer[i] = '_';
  205. }
  206. }
  207. void toLower()
  208. {
  209. static const char charDiff = 'a' - 'A';
  210. for (size_t i=0; i < bufferLen; i++)
  211. {
  212. if (buffer[i] >= 'A' && buffer[i] <= 'Z')
  213. buffer[i] += charDiff;
  214. }
  215. }
  216. void toUpper()
  217. {
  218. static const char charDiff = 'a' - 'A';
  219. for (size_t i=0; i < bufferLen; i++)
  220. {
  221. if (buffer[i] >= 'a' && buffer[i] <= 'z')
  222. buffer[i] -= charDiff;
  223. }
  224. }
  225. void toBase64()
  226. {
  227. importBinaryAsBase64((const uint8_t*)buffer, bufferLen);
  228. }
  229. void fromBase64()
  230. {
  231. uint8_t buffer2[carla_base64_decoded_max_len(buffer)];
  232. if (unsigned int len = carla_base64_decode(buffer, buffer2))
  233. _dup((char*)buffer2, len);
  234. else
  235. clear();
  236. }
  237. void importBinaryAsBase64(const uint8_t* const raw, const size_t rawLen)
  238. {
  239. const size_t rawBufferSize = carla_base64_encoded_len(rawLen) + 1;
  240. char rawBuffer[rawBufferSize];
  241. carla_base64_encode(raw, rawLen, rawBuffer);
  242. _dup(rawBuffer, rawBufferSize-1);
  243. }
  244. template<typename T>
  245. void importBinaryAsBase64(const T* const t)
  246. {
  247. const size_t tSize = sizeof(T);
  248. importBinaryAsBase64((const uint8_t*)t, tSize);
  249. }
  250. size_t exportAsBase64Binary(uint8_t** const rawPtr)
  251. {
  252. uint8_t binaryBuffer[carla_base64_decoded_max_len(buffer)];
  253. if (unsigned int len = carla_base64_decode(buffer, binaryBuffer))
  254. {
  255. uint8_t* const binaryBufferHeap = new uint8_t[len];
  256. carla_copy<uint8_t>(binaryBufferHeap, binaryBuffer, len);
  257. *rawPtr = binaryBufferHeap;
  258. return len;
  259. }
  260. return 0;
  261. }
  262. template<typename T>
  263. T* exportAsBase64Binary()
  264. {
  265. uint8_t binaryBuffer[carla_base64_decoded_max_len(buffer)];
  266. if (unsigned int len = carla_base64_decode(buffer, binaryBuffer))
  267. {
  268. CARLA_ASSERT_INT2(len == sizeof(T), len, sizeof(T));
  269. T* const t = new T();
  270. std::memcpy(t, binaryBuffer, len);
  271. return t;
  272. }
  273. return nullptr;
  274. }
  275. // ---------------------------------------------
  276. // public operators
  277. operator const char*() const
  278. {
  279. return buffer;
  280. }
  281. char& operator[](const size_t pos)
  282. {
  283. return buffer[pos];
  284. }
  285. bool operator==(const char* const strBuf) const
  286. {
  287. return (strBuf != nullptr && std::strcmp(buffer, strBuf) == 0);
  288. }
  289. bool operator==(const CarlaString& str) const
  290. {
  291. return operator==(str.buffer);
  292. }
  293. bool operator!=(const char* const strBuf) const
  294. {
  295. return !operator==(strBuf);
  296. }
  297. bool operator!=(const CarlaString& str) const
  298. {
  299. return !operator==(str.buffer);
  300. }
  301. CarlaString& operator=(const char* const strBuf)
  302. {
  303. _dup(strBuf);
  304. return *this;
  305. }
  306. CarlaString& operator=(const CarlaString& str)
  307. {
  308. return operator=(str.buffer);
  309. }
  310. CarlaString& operator+=(const char* const strBuf)
  311. {
  312. const size_t newBufSize = bufferLen + ((strBuf != nullptr) ? std::strlen(strBuf) : 0) + 1;
  313. char newBuf[newBufSize];
  314. std::strcpy(newBuf, buffer);
  315. std::strcat(newBuf, strBuf);
  316. _dup(newBuf, newBufSize-1);
  317. return *this;
  318. }
  319. CarlaString& operator+=(const CarlaString& str)
  320. {
  321. return operator+=(str.buffer);
  322. }
  323. CarlaString operator+(const char* const strBuf)
  324. {
  325. const size_t newBufSize = bufferLen + ((strBuf != nullptr) ? std::strlen(strBuf) : 0) + 1;
  326. char newBuf[newBufSize];
  327. std::strcpy(newBuf, buffer);
  328. std::strcat(newBuf, strBuf);
  329. return CarlaString(newBuf);
  330. }
  331. CarlaString operator+(const CarlaString& str)
  332. {
  333. return operator+(str.buffer);
  334. }
  335. // ---------------------------------------------
  336. private:
  337. char* buffer;
  338. size_t bufferLen;
  339. bool firstInit;
  340. void _init()
  341. {
  342. buffer = nullptr;
  343. bufferLen = 0;
  344. firstInit = true;
  345. }
  346. // allocate string strBuf if not null
  347. // size > 0 only if strBuf is valid
  348. void _dup(const char* const strBuf, const size_t size = 0)
  349. {
  350. if (strBuf != nullptr)
  351. {
  352. // don't recreate string if contents match
  353. if (firstInit || std::strcmp(buffer, strBuf) != 0)
  354. {
  355. if (! firstInit)
  356. {
  357. CARLA_ASSERT(buffer != nullptr);
  358. delete[] buffer;
  359. }
  360. bufferLen = (size > 0) ? size : std::strlen(strBuf);
  361. buffer = new char[bufferLen+1];
  362. std::strcpy(buffer, strBuf);
  363. buffer[bufferLen] = '\0';
  364. firstInit = false;
  365. }
  366. }
  367. else
  368. {
  369. CARLA_ASSERT(size == 0);
  370. // don't recreate null string
  371. if (firstInit || bufferLen != 0)
  372. {
  373. if (! firstInit)
  374. {
  375. CARLA_ASSERT(buffer != nullptr);
  376. delete[] buffer;
  377. }
  378. bufferLen = 0;
  379. buffer = new char[1];
  380. buffer[0] = '\0';
  381. firstInit = false;
  382. }
  383. }
  384. }
  385. CARLA_LEAK_DETECTOR(CarlaString)
  386. CARLA_PREVENT_HEAP_ALLOCATION
  387. };
  388. static inline
  389. CarlaString operator+(const CarlaString& strBefore, const char* const strBufAfter)
  390. {
  391. const char* const strBufBefore = (const char*)strBefore;
  392. const size_t newBufSize = strBefore.length() + ((strBufAfter != nullptr) ? std::strlen(strBufAfter) : 0) + 1;
  393. char newBuf[newBufSize];
  394. std::strcpy(newBuf, strBufBefore);
  395. std::strcat(newBuf, strBufAfter);
  396. return CarlaString(newBuf);
  397. }
  398. static inline
  399. CarlaString operator+(const char* const strBufBefore, const CarlaString& strAfter)
  400. {
  401. const char* const strBufAfter = (const char*)strAfter;
  402. const size_t newBufSize = ((strBufBefore != nullptr) ? std::strlen(strBufBefore) : 0) + strAfter.length() + 1;
  403. char newBuf[newBufSize];
  404. std::strcpy(newBuf, strBufBefore);
  405. std::strcat(newBuf, strBufAfter);
  406. return CarlaString(newBuf);
  407. }
  408. // -------------------------------------------------
  409. #endif // __CARLA_UTILS_HPP__