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.

240 lines
4.5KB

  1. /*
  2. * DISTRHO Plugin Toolkit (DPT)
  3. * Copyright (C) 2012-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 Lesser General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * For a full copy of the license see the LGPL.txt file
  15. */
  16. #ifndef __DISTRHO_UTILS_H__
  17. #define __DISTRHO_UTILS_H__
  18. #include "src/DistrhoMacros.h"
  19. #if DISTRHO_OS_WINDOWS
  20. # include <windows.h>
  21. #else
  22. # include <unistd.h>
  23. #endif
  24. #include <cstdio>
  25. #include <cstdint>
  26. #include <cstdlib>
  27. #include <cstring>
  28. // -------------------------------------------------
  29. inline
  30. long d_cconst(int a, int b, int c, int d)
  31. {
  32. return (a << 24) | (b << 16) | (c << 8) | (d << 0);
  33. }
  34. inline
  35. void d_sleep(unsigned int secs)
  36. {
  37. #if DISTRHO_OS_WINDOWS
  38. Sleep(secs * 1000);
  39. #else
  40. sleep(secs);
  41. #endif
  42. }
  43. inline
  44. void d_msleep(unsigned int msecs)
  45. {
  46. #if DISTRHO_OS_WINDOWS
  47. Sleep(msecs);
  48. #else
  49. usleep(msecs * 1000);
  50. #endif
  51. }
  52. inline
  53. void d_usleep(unsigned int usecs)
  54. {
  55. #if DISTRHO_OS_WINDOWS
  56. Sleep(usecs / 1000);
  57. #else
  58. usleep(usecs);
  59. #endif
  60. }
  61. inline
  62. void d_setenv(const char* key, const char* value)
  63. {
  64. #if DISTRHO_OS_WINDOWS
  65. SetEnvironmentVariableA(key, value);
  66. #else
  67. setenv(key, value, 1);
  68. #endif
  69. }
  70. // -------------------------------------------------
  71. // TODO - import new stuff from Carla, when ready
  72. class d_string
  73. {
  74. public:
  75. d_string()
  76. {
  77. buffer = strdup("");
  78. }
  79. d_string(const char* strBuf)
  80. {
  81. buffer = strdup(strBuf ? strBuf : "");
  82. }
  83. d_string(const d_string& str)
  84. {
  85. buffer = strdup(str.buffer);
  86. }
  87. d_string(int value)
  88. {
  89. size_t strBufSize = (unsigned int)abs(value/10) + 3;
  90. char strBuf[strBufSize];
  91. snprintf(strBuf, strBufSize, "%d", value);
  92. buffer = strdup(strBuf);
  93. }
  94. d_string(unsigned int value)
  95. {
  96. size_t strBufSize = value/10 + 2;
  97. char strBuf[strBufSize];
  98. snprintf(strBuf, strBufSize, "%u", value);
  99. buffer = strdup(strBuf);
  100. }
  101. d_string(float value)
  102. {
  103. char strBuf[255];
  104. snprintf(strBuf, 255, "%f", value);
  105. buffer = strdup(strBuf);
  106. }
  107. ~d_string()
  108. {
  109. free(buffer);
  110. }
  111. size_t length() const
  112. {
  113. return strlen(buffer);
  114. }
  115. bool isEmpty() const
  116. {
  117. return (*buffer == 0);
  118. }
  119. // ---------------------------------------------
  120. operator const char*() const
  121. {
  122. return buffer;
  123. }
  124. bool operator==(const char* strBuf) const
  125. {
  126. return (strcmp(buffer, strBuf) == 0);
  127. }
  128. bool operator==(const d_string& str) const
  129. {
  130. return operator==(str.buffer);
  131. }
  132. bool operator!=(const char* strBuf) const
  133. {
  134. return !operator==(strBuf);
  135. }
  136. bool operator!=(const d_string& str) const
  137. {
  138. return !operator==(str.buffer);
  139. }
  140. d_string& operator=(const char* strBuf)
  141. {
  142. free(buffer);
  143. buffer = strdup(strBuf);
  144. return *this;
  145. }
  146. d_string& operator=(const d_string& str)
  147. {
  148. return operator=(str.buffer);
  149. }
  150. d_string& operator+=(const char* strBuf)
  151. {
  152. size_t newBufSize = strlen(buffer) + strlen(strBuf) + 1;
  153. char newBuf[newBufSize];
  154. strcpy(newBuf, buffer);
  155. strcat(newBuf, strBuf);
  156. free(buffer);
  157. buffer = strdup(newBuf);
  158. return *this;
  159. }
  160. d_string& operator+=(const d_string& str)
  161. {
  162. return operator+=(str.buffer);
  163. }
  164. d_string operator+(const char* strBuf)
  165. {
  166. size_t newBufSize = strlen(buffer) + strlen(strBuf) + 1;
  167. char newBuf[newBufSize];
  168. strcpy(newBuf, buffer);
  169. strcat(newBuf, strBuf);
  170. return d_string(newBuf);
  171. }
  172. d_string operator+(const d_string& str)
  173. {
  174. return operator+(str.buffer);
  175. }
  176. private:
  177. char* buffer;
  178. };
  179. static inline
  180. d_string operator+(const char* strBufBefore, const d_string& strAfter)
  181. {
  182. const char* strBufAfter = (const char*)strAfter;
  183. size_t newBufSize = strlen(strBufBefore) + strlen(strBufAfter) + 1;
  184. char newBuf[newBufSize];
  185. strcpy(newBuf, strBufBefore);
  186. strcat(newBuf, strBufAfter);
  187. return d_string(newBuf);
  188. }
  189. // -------------------------------------------------
  190. #endif // __DISTRHO_UTILS_H__