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.

474 lines
10KB

  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 modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * 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 COPYING file
  16. */
  17. #ifndef __CARLA_UTILS_HPP__
  18. #define __CARLA_UTILS_HPP__
  19. #include "carla_defines.hpp"
  20. #include <cstdio>
  21. #include <cstdlib>
  22. #include <cstring>
  23. #if defined(Q_OS_HAIKU)
  24. # include <kernel/OS.h>
  25. #elif defined(Q_OS_LINUX)
  26. # include <sys/prctl.h>
  27. # include <linux/prctl.h>
  28. #endif
  29. // -------------------------------------------------
  30. // carla_assert*
  31. static inline
  32. void carla_assert(const char* const assertion, const char* const file, const int line)
  33. {
  34. qCritical("Carla assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
  35. }
  36. static inline
  37. void carla_assert_int(const char* const assertion, const char* const file, const int line, const int value)
  38. {
  39. qCritical("Carla assertion failure: \"%s\" in file %s, line %i, value %i", assertion, file, line, value);
  40. }
  41. // -------------------------------------------------
  42. // carla_*sleep (carla_usleep not possible in Windows)
  43. static inline
  44. void carla_sleep(const unsigned int secs)
  45. {
  46. CARLA_ASSERT(secs > 0);
  47. #ifdef Q_OS_WIN
  48. Sleep(secs * 1000);
  49. #else
  50. sleep(secs);
  51. #endif
  52. }
  53. static inline
  54. void carla_msleep(const unsigned int msecs)
  55. {
  56. CARLA_ASSERT(msecs > 0);
  57. #ifdef Q_OS_WIN
  58. Sleep(msecs);
  59. #else
  60. usleep(msecs * 1000);
  61. #endif
  62. }
  63. static inline
  64. void carla_usleep(const unsigned int usecs)
  65. {
  66. CARLA_ASSERT(usecs > 0);
  67. #ifdef Q_OS_WIN
  68. Sleep(usecs / 1000);
  69. #else
  70. usleep(usecs);
  71. #endif
  72. }
  73. // -------------------------------------------------
  74. // carla_setenv
  75. static inline
  76. void carla_setenv(const char* const key, const char* const value)
  77. {
  78. CARLA_ASSERT(key);
  79. CARLA_ASSERT(value);
  80. #ifdef Q_OS_WIN
  81. SetEnvironmentVariableA(key, value);
  82. #else
  83. setenv(key, value, 1);
  84. #endif
  85. }
  86. // -------------------------------------------------
  87. // carla_setprocname (not available on all platforms)
  88. static inline
  89. void carla_setprocname(const char* const name)
  90. {
  91. CARLA_ASSERT(name);
  92. #if defined(Q_OS_HAIKU)
  93. if ((thread_id this_thread = find_thread(nullptr)) != B_NAME_NOT_FOUND)
  94. rename_thread(this_thread, name);
  95. #elif defined(Q_OS_LINUX)
  96. prctl(PR_SET_NAME, name);
  97. #else
  98. qWarning("carla_setprocname(\"%s\") - unsupported on this platform", name);
  99. #endif
  100. }
  101. // -------------------------------------------------
  102. // math functions
  103. template<typename T>
  104. static inline
  105. const T& carla_min(const T& v1, const T& v2, const T& min)
  106. {
  107. return ((v1 < min || v2 < min) ? min : (v1 < v2 ? v1 : v2));
  108. }
  109. template<typename T>
  110. static inline
  111. void carla_fill(T* data, const unsigned int size, const T v)
  112. {
  113. CARLA_ASSERT(data);
  114. CARLA_ASSERT(size > 0);
  115. for (unsigned int i=0; i < size; i++)
  116. *data++ = v;
  117. }
  118. static inline
  119. void carla_zeroDouble(double* data, const unsigned size)
  120. {
  121. carla_fill<double>(data, size, 0.0);
  122. }
  123. static inline
  124. void carla_zeroFloat(float* data, const unsigned size)
  125. {
  126. carla_fill<float>(data, size, 0.0f);
  127. }
  128. // -------------------------------------------------
  129. // other misc functions
  130. static inline
  131. const char* bool2str(const bool yesNo)
  132. {
  133. return yesNo ? "true" : "false";
  134. }
  135. static inline
  136. void pass() {}
  137. // -------------------------------------------------
  138. // CarlaString class
  139. class CarlaString
  140. {
  141. public:
  142. // ---------------------------------------------
  143. // constructors (no explicit conversions allowed)
  144. explicit CarlaString()
  145. {
  146. buffer = ::strdup("");
  147. }
  148. explicit CarlaString(char* const strBuf)
  149. {
  150. buffer = ::strdup(strBuf ? strBuf : "");
  151. }
  152. explicit CarlaString(const char* const strBuf)
  153. {
  154. buffer = ::strdup(strBuf ? strBuf : "");
  155. }
  156. explicit CarlaString(const int value)
  157. {
  158. const size_t strBufSize = ::abs(value/10) + 3;
  159. char strBuf[strBufSize];
  160. ::snprintf(strBuf, strBufSize, "%d", value);
  161. buffer = ::strdup(strBuf);
  162. }
  163. explicit CarlaString(const unsigned int value, const bool hexadecimal = false)
  164. {
  165. const size_t strBufSize = value/10 + 2 + (hexadecimal ? 2 : 0);
  166. char strBuf[strBufSize];
  167. ::snprintf(strBuf, strBufSize, hexadecimal ? "%u" : "0x%x", value);
  168. buffer = ::strdup(strBuf);
  169. }
  170. explicit CarlaString(const long int value)
  171. {
  172. const size_t strBufSize = ::labs(value/10) + 3;
  173. char strBuf[strBufSize];
  174. ::snprintf(strBuf, strBufSize, "%ld", value);
  175. buffer = ::strdup(strBuf);
  176. }
  177. explicit CarlaString(const unsigned long int value, const bool hexadecimal = false)
  178. {
  179. const size_t strBufSize = value/10 + 2 + (hexadecimal ? 2 : 0);
  180. char strBuf[strBufSize];
  181. ::snprintf(strBuf, strBufSize, hexadecimal ? "%lu" : "0x%lx", value);
  182. buffer = ::strdup(strBuf);
  183. }
  184. explicit CarlaString(const float value)
  185. {
  186. char strBuf[0xff];
  187. ::snprintf(strBuf, 0xff, "%f", value);
  188. buffer = ::strdup(strBuf);
  189. }
  190. explicit CarlaString(const double value)
  191. {
  192. char strBuf[0xff];
  193. ::snprintf(strBuf, 0xff, "%g", value);
  194. buffer = ::strdup(strBuf);
  195. }
  196. // ---------------------------------------------
  197. // non-explicit constructor
  198. CarlaString(const CarlaString& str)
  199. {
  200. buffer = ::strdup(str.buffer);
  201. }
  202. // ---------------------------------------------
  203. // deconstructor
  204. ~CarlaString()
  205. {
  206. CARLA_ASSERT(buffer);
  207. ::free(buffer);
  208. }
  209. // ---------------------------------------------
  210. // public methods
  211. size_t length() const
  212. {
  213. return ::strlen(buffer);
  214. }
  215. bool isEmpty() const
  216. {
  217. return (*buffer == 0);
  218. }
  219. bool isNotEmpty() const
  220. {
  221. return (*buffer != 0);
  222. }
  223. bool contains(const char* const strBuf) const
  224. {
  225. if (! strBuf)
  226. return false;
  227. if (*strBuf == 0)
  228. return false;
  229. size_t thisLen = ::strlen(buffer);
  230. size_t thatLen = ::strlen(strBuf)-1;
  231. for (size_t i=0, j=0; i < thisLen; i++)
  232. {
  233. if (buffer[i] == strBuf[j])
  234. j++;
  235. else
  236. j = 0;
  237. if (j == thatLen)
  238. return true;
  239. }
  240. return false;
  241. }
  242. bool contains(const CarlaString& str) const
  243. {
  244. return contains(str.buffer);
  245. }
  246. bool isDigit(const size_t pos) const
  247. {
  248. if (pos >= length())
  249. return false;
  250. return (buffer[pos] >= '0' && buffer[pos] <= '9');
  251. }
  252. void clear()
  253. {
  254. truncate(0);
  255. }
  256. void replace(const char before, const char after)
  257. {
  258. for (size_t i=0, len = ::strlen(buffer); i < len; i++)
  259. {
  260. if (buffer[i] == before)
  261. buffer[i] = after;
  262. }
  263. }
  264. void truncate(const unsigned int n)
  265. {
  266. for (size_t i=n, len = ::strlen(buffer); i < len; i++)
  267. buffer[i] = 0;
  268. }
  269. void toBasic()
  270. {
  271. for (size_t i=0, len = ::strlen(buffer); i < len; i++)
  272. {
  273. if (buffer[i] >= '0' && buffer[i] <= '9')
  274. continue;
  275. if (buffer[i] >= 'A' && buffer[i] <= 'Z')
  276. continue;
  277. if (buffer[i] >= 'a' && buffer[i] <= 'z')
  278. continue;
  279. if (buffer[i] == '_')
  280. continue;
  281. buffer[i] = '_';
  282. }
  283. }
  284. void toLower()
  285. {
  286. for (size_t i=0, len = ::strlen(buffer); i < len; i++)
  287. {
  288. if (buffer[i] >= 'A' && buffer[i] <= 'Z')
  289. buffer[i] += 32;
  290. }
  291. }
  292. void toUpper()
  293. {
  294. for (size_t i=0, len = ::strlen(buffer); i < len; i++)
  295. {
  296. if (buffer[i] >= 'a' && buffer[i] <= 'z')
  297. buffer[i] -= 32;
  298. }
  299. }
  300. // ---------------------------------------------
  301. // public operators
  302. operator const char*() const
  303. {
  304. return buffer;
  305. }
  306. char& operator[](const unsigned int pos)
  307. {
  308. return buffer[pos];
  309. }
  310. bool operator==(const char* const strBuf) const
  311. {
  312. return (strBuf && ::strcmp(buffer, strBuf) == 0);
  313. }
  314. bool operator==(const CarlaString& str) const
  315. {
  316. return operator==(str.buffer);
  317. }
  318. bool operator!=(const char* const strBuf) const
  319. {
  320. return !operator==(strBuf);
  321. }
  322. bool operator!=(const CarlaString& str) const
  323. {
  324. return !operator==(str.buffer);
  325. }
  326. CarlaString& operator=(const char* const strBuf)
  327. {
  328. ::free(buffer);
  329. buffer = ::strdup(strBuf ? strBuf : "");
  330. return *this;
  331. }
  332. CarlaString& operator=(const CarlaString& str)
  333. {
  334. return operator=(str.buffer);
  335. }
  336. CarlaString& operator+=(const char* const strBuf)
  337. {
  338. const size_t newBufSize = ::strlen(buffer) + (strBuf ? ::strlen(strBuf) : 0) + 1;
  339. char newBuf[newBufSize];
  340. ::strcpy(newBuf, buffer);
  341. ::strcat(newBuf, strBuf);
  342. ::free(buffer);
  343. buffer = ::strdup(newBuf);
  344. return *this;
  345. }
  346. CarlaString& operator+=(const CarlaString& str)
  347. {
  348. return operator+=(str.buffer);
  349. }
  350. CarlaString operator+(const char* const strBuf)
  351. {
  352. const size_t newBufSize = ::strlen(buffer) + (strBuf ? ::strlen(strBuf) : 0) + 1;
  353. char newBuf[newBufSize];
  354. ::strcpy(newBuf, buffer);
  355. ::strcat(newBuf, strBuf);
  356. return CarlaString(newBuf);
  357. }
  358. CarlaString operator+(const CarlaString& str)
  359. {
  360. return operator+(str.buffer);
  361. }
  362. // ---------------------------------------------
  363. private:
  364. char* buffer;
  365. };
  366. static inline
  367. CarlaString operator+(const char* const strBufBefore, const CarlaString& strAfter)
  368. {
  369. const char* const strBufAfter = (const char*)strAfter;
  370. const size_t newBufSize = (strBufBefore ? ::strlen(strBufBefore) : 0) + ::strlen(strBufAfter) + 1;
  371. char newBuf[newBufSize];
  372. ::strcpy(newBuf, strBufBefore);
  373. ::strcat(newBuf, strBufAfter);
  374. return CarlaString(newBuf);
  375. }
  376. // -------------------------------------------------
  377. #endif // __CARLA_UTILS_HPP__