Collection of tools useful for audio production
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.

321 lines
7.5KB

  1. /*
  2. * Carla common utils
  3. * Copyright (C) 2011-2012 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. // carla_assert*
  30. static inline
  31. void carla_assert(const char* const assertion, const char* const file, const int line)
  32. {
  33. qCritical("Carla assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
  34. }
  35. static inline
  36. void carla_assert_int(const char* const assertion, const char* const file, const int line, const int value)
  37. {
  38. qCritical("Carla assertion failure: \"%s\" in file %s, line %i, value %i", assertion, file, line, value);
  39. }
  40. // carla_*sleep
  41. #ifdef Q_OS_WIN
  42. # define carla_sleep(t) Sleep(t * 1000)
  43. # define carla_msleep(t) Sleep(t)
  44. # define carla_usleep(t) Sleep(t / 1000)
  45. #else
  46. # define carla_sleep(t) sleep(t)
  47. # define carla_msleep(t) usleep(t * 1000)
  48. # define carla_usleep(t) usleep(t)
  49. #endif
  50. // carla_setenv
  51. #ifdef Q_OS_WIN
  52. # define carla_setenv(key, value) SetEnvironmentVariableA(key, value)
  53. #else
  54. # define carla_setenv(key, value) setenv(key, value, 1)
  55. #endif
  56. // carla_setprocname (not available on all platforms)
  57. static inline
  58. void carla_setprocname(const char* const name)
  59. {
  60. CARLA_ASSERT(name);
  61. #if defined(Q_OS_HAIKU)
  62. if ((thread_id this_thread = find_thread(nullptr)) != B_NAME_NOT_FOUND)
  63. rename_thread(this_thread, name);
  64. #elif defined(Q_OS_LINUX)
  65. prctl(PR_SET_NAME, name);
  66. #else
  67. qWarning("carla_setprocname(\"%s\") - unsupported on this platform");
  68. #endif
  69. }
  70. // math functions
  71. static inline
  72. float carla_absF(const float& value)
  73. {
  74. return (value < 0.0f) ? -value : value;
  75. }
  76. static inline
  77. float carla_minF(const float& x, const float& y)
  78. {
  79. return (x < y ? x : y);
  80. }
  81. static inline
  82. float carla_maxF(const float& x, const float& y)
  83. {
  84. return (x > y ? x : y);
  85. }
  86. static inline
  87. void carla_zeroF(float* data, const unsigned int size)
  88. {
  89. for (unsigned int i=0; i < size; i++)
  90. *data++ = 0.0f;
  91. }
  92. // other misc functions
  93. static inline
  94. const char* bool2str(const bool yesNo)
  95. {
  96. return yesNo ? "true" : "false";
  97. }
  98. static inline
  99. void pass() {}
  100. // -------------------------------------------------
  101. // carla_string class
  102. class carla_string
  103. {
  104. public:
  105. // ---------------------------------------------
  106. // constructors (no explicit conversions allowed)
  107. explicit carla_string()
  108. {
  109. buffer = ::strdup("");
  110. }
  111. explicit carla_string(char* const strBuf)
  112. {
  113. buffer = ::strdup(strBuf ? strBuf : "");
  114. }
  115. explicit carla_string(const char* const strBuf)
  116. {
  117. buffer = ::strdup(strBuf ? strBuf : "");
  118. }
  119. explicit carla_string(const int value)
  120. {
  121. const size_t strBufSize = ::abs(value/10) + 3;
  122. char strBuf[strBufSize];
  123. ::snprintf(strBuf, strBufSize, "%d", value);
  124. buffer = ::strdup(strBuf);
  125. }
  126. explicit carla_string(const unsigned int value, const bool hexadecimal = false)
  127. {
  128. const size_t strBufSize = value/10 + 2 + (hexadecimal ? 2 : 0);
  129. char strBuf[strBufSize];
  130. ::snprintf(strBuf, strBufSize, hexadecimal ? "%u" : "0x%x", value);
  131. buffer = ::strdup(strBuf);
  132. }
  133. explicit carla_string(const long int value)
  134. {
  135. const size_t strBufSize = ::labs(value/10) + 3;
  136. char strBuf[strBufSize];
  137. ::snprintf(strBuf, strBufSize, "%ld", value);
  138. buffer = ::strdup(strBuf);
  139. }
  140. explicit carla_string(const unsigned long int value, const bool hexadecimal = false)
  141. {
  142. const size_t strBufSize = value/10 + 2 + (hexadecimal ? 2 : 0);
  143. char strBuf[strBufSize];
  144. ::snprintf(strBuf, strBufSize, hexadecimal ? "%lu" : "0x%lx", value);
  145. buffer = ::strdup(strBuf);
  146. }
  147. explicit carla_string(const float value)
  148. {
  149. char strBuf[0xff];
  150. ::snprintf(strBuf, 0xff, "%f", value);
  151. buffer = ::strdup(strBuf);
  152. }
  153. explicit carla_string(const double value)
  154. {
  155. char strBuf[0xff];
  156. ::snprintf(strBuf, 0xff, "%g", value);
  157. buffer = ::strdup(strBuf);
  158. }
  159. // ---------------------------------------------
  160. // non-explicit constructor
  161. carla_string(const carla_string& str)
  162. {
  163. buffer = ::strdup(str.buffer);
  164. }
  165. // ---------------------------------------------
  166. // deconstructor
  167. ~carla_string()
  168. {
  169. ::free(buffer);
  170. }
  171. // ---------------------------------------------
  172. // public methods
  173. size_t length() const
  174. {
  175. return ::strlen(buffer);
  176. }
  177. bool isEmpty() const
  178. {
  179. return (*buffer == 0);
  180. }
  181. // ---------------------------------------------
  182. // public operators
  183. operator const char*() const
  184. {
  185. return buffer;
  186. }
  187. bool operator==(const char* const strBuf) const
  188. {
  189. return (strBuf && ::strcmp(buffer, strBuf) == 0);
  190. }
  191. bool operator==(const carla_string& str) const
  192. {
  193. return operator==(str.buffer);
  194. }
  195. bool operator!=(const char* const strBuf) const
  196. {
  197. return !operator==(strBuf);
  198. }
  199. bool operator!=(const carla_string& str) const
  200. {
  201. return !operator==(str.buffer);
  202. }
  203. carla_string& operator=(const char* const strBuf)
  204. {
  205. ::free(buffer);
  206. buffer = ::strdup(strBuf ? strBuf : "");
  207. return *this;
  208. }
  209. carla_string& operator=(const carla_string& str)
  210. {
  211. return operator=(str.buffer);
  212. }
  213. carla_string& operator+=(const char* const strBuf)
  214. {
  215. const size_t newBufSize = ::strlen(buffer) + (strBuf ? ::strlen(strBuf) : 0) + 1;
  216. char newBuf[newBufSize];
  217. ::strcpy(newBuf, buffer);
  218. ::strcat(newBuf, strBuf);
  219. ::free(buffer);
  220. buffer = ::strdup(newBuf);
  221. return *this;
  222. }
  223. carla_string& operator+=(const carla_string& str)
  224. {
  225. return operator+=(str.buffer);
  226. }
  227. carla_string operator+(const char* const strBuf)
  228. {
  229. const size_t newBufSize = ::strlen(buffer) + (strBuf ? ::strlen(strBuf) : 0) + 1;
  230. char newBuf[newBufSize];
  231. ::strcpy(newBuf, buffer);
  232. ::strcat(newBuf, strBuf);
  233. return carla_string(newBuf);
  234. }
  235. carla_string operator+(const carla_string& str)
  236. {
  237. return operator+(str.buffer);
  238. }
  239. // ---------------------------------------------
  240. private:
  241. char* buffer;
  242. };
  243. static inline
  244. carla_string operator+(const char* const strBufBefore, const carla_string& strAfter)
  245. {
  246. const char* const strBufAfter = (const char*)strAfter;
  247. const size_t newBufSize = (strBufBefore ? ::strlen(strBufBefore) : 0) + ::strlen(strBufAfter) + 1;
  248. char newBuf[newBufSize];
  249. ::strcpy(newBuf, strBufBefore);
  250. ::strcat(newBuf, strBufAfter);
  251. return carla_string(newBuf);
  252. }
  253. // -------------------------------------------------
  254. #endif // CARLA_UTILS_HPP