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.

257 lines
4.8KB

  1. /*
  2. * DISTHRO Plugin Toolkit (DPT)
  3. * Copyright (C) 2012 Filipe Coelho <falktx@gmail.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 license see the GPL.txt file
  16. */
  17. #ifndef __DISTRHO_UTILS_H__
  18. #define __DISTRHO_UTILS_H__
  19. #include "src/DistrhoDefines.h"
  20. #if DISTRHO_OS_WINDOWS
  21. # include <windows.h>
  22. #else
  23. # include <unistd.h>
  24. #endif
  25. #include <cstdio>
  26. #include <cstdint>
  27. #include <cstdlib>
  28. #include <cstring>
  29. // -------------------------------------------------
  30. inline
  31. float d_absf(const float& value)
  32. {
  33. return (value < 0.0f) ? -value : value;
  34. }
  35. inline
  36. float d_minf(const float& x, const float& y)
  37. {
  38. return (x < y ? x : y);
  39. }
  40. inline
  41. float d_maxf(const float& x, const float& y)
  42. {
  43. return (x > y ? x : y);
  44. }
  45. inline
  46. long d_cconst(int a, int b, int c, int d)
  47. {
  48. return (a << 24) | (b << 16) | (c << 8) | (d << 0);
  49. }
  50. inline
  51. void d_sleep(unsigned int seconds)
  52. {
  53. #if DISTRHO_OS_WINDOWS
  54. Sleep(seconds * 1000);
  55. #else
  56. sleep(seconds);
  57. #endif
  58. }
  59. inline
  60. void d_msleep(unsigned int mseconds)
  61. {
  62. #if DISTRHO_OS_WINDOWS
  63. Sleep(mseconds);
  64. #else
  65. usleep(mseconds * 1000);
  66. #endif
  67. }
  68. inline
  69. void d_usleep(unsigned int useconds)
  70. {
  71. #if DISTRHO_OS_WINDOWS
  72. Sleep(useconds / 1000);
  73. #else
  74. usleep(useconds);
  75. #endif
  76. }
  77. inline
  78. void d_setenv(const char* key, const char* value)
  79. {
  80. #if DISTRHO_OS_WINDOWS
  81. SetEnvironmentVariableA(key, value);
  82. #else
  83. setenv(key, value, 1);
  84. #endif
  85. }
  86. // -------------------------------------------------
  87. class d_string
  88. {
  89. public:
  90. d_string()
  91. {
  92. buffer = strdup("");
  93. }
  94. d_string(const char* strBuf)
  95. {
  96. buffer = strdup(strBuf ? strBuf : "");
  97. }
  98. d_string(const d_string& str)
  99. {
  100. buffer = strdup(str.buffer);
  101. }
  102. d_string(int value)
  103. {
  104. size_t strBufSize = abs(value/10) + 3;
  105. char strBuf[strBufSize];
  106. snprintf(strBuf, strBufSize, "%d", value);
  107. buffer = strdup(strBuf);
  108. }
  109. d_string(unsigned int value)
  110. {
  111. size_t strBufSize = value/10 + 2;
  112. char strBuf[strBufSize];
  113. snprintf(strBuf, strBufSize, "%u", value);
  114. buffer = strdup(strBuf);
  115. }
  116. d_string(float value)
  117. {
  118. char strBuf[255];
  119. snprintf(strBuf, 255, "%f", value);
  120. buffer = strdup(strBuf);
  121. }
  122. ~d_string()
  123. {
  124. free(buffer);
  125. }
  126. size_t length() const
  127. {
  128. return strlen(buffer);
  129. }
  130. bool isEmpty() const
  131. {
  132. return (*buffer == 0);
  133. }
  134. // ---------------------------------------------
  135. operator const char*() const
  136. {
  137. return buffer;
  138. }
  139. bool operator==(const char* strBuf) const
  140. {
  141. return (strcmp(buffer, strBuf) == 0);
  142. }
  143. bool operator==(const d_string& str) const
  144. {
  145. return operator==(str.buffer);
  146. }
  147. bool operator!=(const char* strBuf) const
  148. {
  149. return !operator==(strBuf);
  150. }
  151. bool operator!=(const d_string& str) const
  152. {
  153. return !operator==(str.buffer);
  154. }
  155. d_string& operator=(const char* strBuf)
  156. {
  157. free(buffer);
  158. buffer = strdup(strBuf);
  159. return *this;
  160. }
  161. d_string& operator=(const d_string& str)
  162. {
  163. return operator=(str.buffer);
  164. }
  165. d_string& operator+=(const char* strBuf)
  166. {
  167. size_t newBufSize = strlen(buffer) + strlen(strBuf) + 1;
  168. char newBuf[newBufSize];
  169. strcpy(newBuf, buffer);
  170. strcat(newBuf, strBuf);
  171. free(buffer);
  172. buffer = strdup(newBuf);
  173. return *this;
  174. }
  175. d_string& operator+=(const d_string& str)
  176. {
  177. return operator+=(str.buffer);
  178. }
  179. d_string operator+(const char* strBuf)
  180. {
  181. size_t newBufSize = strlen(buffer) + strlen(strBuf) + 1;
  182. char newBuf[newBufSize];
  183. strcpy(newBuf, buffer);
  184. strcat(newBuf, strBuf);
  185. return d_string(newBuf);
  186. }
  187. d_string operator+(const d_string& str)
  188. {
  189. return operator+(str.buffer);
  190. }
  191. private:
  192. char* buffer;
  193. };
  194. static inline
  195. d_string operator+(const char* strBufBefore, const d_string& strAfter)
  196. {
  197. const char* strBufAfter = (const char*)strAfter;
  198. size_t newBufSize = strlen(strBufBefore) + strlen(strBufAfter) + 1;
  199. char newBuf[newBufSize];
  200. strcpy(newBuf, strBufBefore);
  201. strcat(newBuf, strBufAfter);
  202. return d_string(newBuf);
  203. }
  204. // -------------------------------------------------
  205. #endif // __DISTRHO_UTILS_H__