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.

buffers.cpp 8.5KB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Carla REST API Server
  3. * Copyright (C) 2018 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 doc/GPL.txt file.
  16. */
  17. #include "buffers.hpp"
  18. #include "CarlaMathUtils.hpp"
  19. #include <cstdio>
  20. #include <cstring>
  21. // -------------------------------------------------------------------------------------------------------------------
  22. enum {
  23. kJsonBufSize = 4095,
  24. kStrBufSize = 1023,
  25. kSizeBufSize = 31,
  26. };
  27. // static buffer to return json
  28. // NOTE size is never checked for json, the buffer is big enough in order to assume it all always fits
  29. static char jsonBuf[kJsonBufSize+1];
  30. // static buffer to return size
  31. static char sizeBuf[kSizeBufSize+1];
  32. // static buffer to return regular strings
  33. static char strBuf[kStrBufSize+1];
  34. // -------------------------------------------------------------------------------------------------------------------
  35. const char* size_buf(const char* const buf)
  36. {
  37. const std::size_t size = std::strlen(buf);
  38. std::snprintf(sizeBuf, kSizeBufSize, P_SIZE, size);
  39. sizeBuf[kSizeBufSize] = '\0';
  40. return sizeBuf;
  41. }
  42. // -------------------------------------------------------------------------------------------------------------------
  43. const char* str_buf_bool(const bool value)
  44. {
  45. strBuf[0] = value ? '1' : '0';
  46. strBuf[1] = '\0';
  47. return strBuf;
  48. }
  49. // -------------------------------------------------------------------------------------------------------------------
  50. const char* str_buf_float(const double value)
  51. {
  52. std::snprintf(strBuf, kStrBufSize, "%f", value);
  53. strBuf[kStrBufSize] = '\0';
  54. return strBuf;
  55. }
  56. const char* str_buf_float_array(const double* const values, const char sep)
  57. {
  58. std::size_t bytesRead = 0;
  59. char tmpBuf[32];
  60. for (int i=0; carla_isNotZero(values[i]) && bytesRead < kStrBufSize; ++i)
  61. {
  62. std::snprintf(tmpBuf, 31, "%f", values[i]);
  63. tmpBuf[31] = '\0';
  64. const std::size_t size = std::strlen(tmpBuf);
  65. if (bytesRead + size > kStrBufSize)
  66. break;
  67. std::strncpy(strBuf+bytesRead, tmpBuf, kStrBufSize - bytesRead);
  68. bytesRead += size;
  69. strBuf[bytesRead] = sep;
  70. bytesRead += 1;
  71. }
  72. strBuf[bytesRead > 0 ? bytesRead-1 : 0] = '\0';
  73. return strBuf;
  74. }
  75. // -------------------------------------------------------------------------------------------------------------------
  76. const char* str_buf_string(const char* const string)
  77. {
  78. std::strncpy(strBuf, string, kStrBufSize);
  79. strBuf[kStrBufSize] = '\0';
  80. return strBuf;
  81. }
  82. const char* str_buf_string_array(const char* const* const array)
  83. {
  84. std::size_t bytesRead = 0;
  85. for (int i=0; array[i] != nullptr && bytesRead < kStrBufSize; ++i)
  86. {
  87. const std::size_t size = std::strlen(array[i]);
  88. if (bytesRead + size > kStrBufSize)
  89. break;
  90. std::strncpy(strBuf+bytesRead, array[i], kStrBufSize - bytesRead);
  91. bytesRead += size;
  92. strBuf[bytesRead] = '\n';
  93. bytesRead += 1;
  94. }
  95. strBuf[bytesRead > 0 ? bytesRead-1 : 0] = '\0';
  96. return strBuf;
  97. }
  98. const char* str_buf_string_quoted(const char* const string)
  99. {
  100. const std::size_t size = std::strlen(string);
  101. char* strBufPtr = strBuf;
  102. *strBufPtr++ = '"';
  103. for (std::size_t i=0, bytesWritten=0; i < size && bytesWritten < kStrBufSize-1; ++i)
  104. {
  105. switch (string[i])
  106. {
  107. case '"':
  108. case '\\':
  109. *strBufPtr++ = '\\';
  110. ++bytesWritten;
  111. break;
  112. case '\n':
  113. *strBufPtr++ = '\\';
  114. *strBufPtr++ = 'n';;
  115. bytesWritten += 2;
  116. continue;
  117. case '\f':
  118. *strBufPtr++ = '\\';
  119. *strBufPtr++ = 'f';;
  120. bytesWritten += 2;
  121. continue;
  122. }
  123. *strBufPtr++ = string[i];
  124. ++bytesWritten;
  125. }
  126. *strBufPtr++ = '"';
  127. *strBufPtr++ = '\0';
  128. return strBuf;
  129. }
  130. // -------------------------------------------------------------------------------------------------------------------
  131. const char* str_buf_int(const int value)
  132. {
  133. std::snprintf(strBuf, kStrBufSize, "%i", value);
  134. strBuf[kStrBufSize] = '\0';
  135. return strBuf;
  136. }
  137. const char* str_buf_int64(const int64_t value)
  138. {
  139. std::snprintf(strBuf, kStrBufSize, P_INT64, value);
  140. strBuf[kStrBufSize] = '\0';
  141. return strBuf;
  142. }
  143. const char* str_buf_uint(const uint value)
  144. {
  145. std::snprintf(strBuf, kStrBufSize, "%u", value);
  146. strBuf[kStrBufSize] = '\0';
  147. return strBuf;
  148. }
  149. const char* str_buf_uint64(const uint64_t value)
  150. {
  151. std::snprintf(strBuf, kStrBufSize, P_UINT64, value);
  152. strBuf[kStrBufSize] = '\0';
  153. return strBuf;
  154. }
  155. const char* str_buf_uint_array(const uint* const values, const char sep)
  156. {
  157. std::size_t bytesRead = 0;
  158. char tmpBuf[32];
  159. for (int i=0; values[i] != 0 && bytesRead < kStrBufSize; ++i)
  160. {
  161. std::snprintf(tmpBuf, 31, "%u", values[i]);
  162. tmpBuf[31] = '\0';
  163. const std::size_t size = std::strlen(tmpBuf);
  164. if (bytesRead + size > kStrBufSize)
  165. break;
  166. std::strncpy(strBuf+bytesRead, tmpBuf, kStrBufSize - bytesRead);
  167. bytesRead += size;
  168. strBuf[bytesRead] = sep;
  169. bytesRead += 1;
  170. }
  171. strBuf[bytesRead > 0 ? bytesRead-1 : 0] = '\0';
  172. return strBuf;
  173. }
  174. // -------------------------------------------------------------------------------------------------------------------
  175. char* json_buf_start()
  176. {
  177. std::strcpy(jsonBuf, "{");
  178. return jsonBuf + 1;
  179. }
  180. char* json_buf_add(char* jsonBufPtr, const char* const key, const char* const valueBuf)
  181. {
  182. if (jsonBufPtr != jsonBuf+1)
  183. *jsonBufPtr++ = ',';
  184. *jsonBufPtr++ = '"';
  185. std::strcpy(jsonBufPtr, key);
  186. jsonBufPtr += std::strlen(key);
  187. *jsonBufPtr++ = '"';
  188. *jsonBufPtr++ = ':';
  189. std::strcpy(jsonBufPtr, valueBuf);
  190. jsonBufPtr += std::strlen(valueBuf);
  191. return jsonBufPtr;
  192. }
  193. template <typename T, typename Fn>
  194. char* json_buf_add_fn(char* jsonBufPtr, const char* const key, const T value, const Fn fn)
  195. {
  196. return json_buf_add(jsonBufPtr, key, fn(value));
  197. }
  198. template <typename T, typename Fn>
  199. char* json_buf_add_fn_array(char* jsonBufPtr, const char* const key, const T value, const Fn fn)
  200. {
  201. return json_buf_add(jsonBufPtr, key, fn(value, ','));
  202. }
  203. // -------------------------------------------------------------------------------------------------------------------
  204. char* json_buf_add_bool(char* jsonBufPtr, const char* const key, const bool value)
  205. {
  206. static const char* const kTrue = "true";
  207. static const char* const kFalse = "false";
  208. return json_buf_add_fn(jsonBufPtr, key, value ? kTrue : kFalse, str_buf_string);
  209. }
  210. char* json_buf_add_float(char* jsonBufPtr, const char* const key, const double value)
  211. {
  212. return json_buf_add_fn(jsonBufPtr, key, value, str_buf_float);
  213. }
  214. char* json_buf_add_float_array(char* jsonBufPtr, const char* const key, const double* const values)
  215. {
  216. return json_buf_add_fn_array(jsonBufPtr, key, values, str_buf_float_array);
  217. }
  218. char* json_buf_add_string(char* jsonBufPtr, const char* const key, const char* const value)
  219. {
  220. return json_buf_add_fn(jsonBufPtr, key, value, str_buf_string_quoted);
  221. }
  222. char* json_buf_add_int(char* jsonBufPtr, const char* const key, const int value)
  223. {
  224. return json_buf_add_fn(jsonBufPtr, key, value, str_buf_int);
  225. }
  226. char* json_buf_add_int64(char* jsonBufPtr, const char* const key, const int64_t value)
  227. {
  228. return json_buf_add_fn(jsonBufPtr, key, value, str_buf_int64);
  229. }
  230. char* json_buf_add_uint(char* jsonBufPtr, const char* const key, const uint value)
  231. {
  232. return json_buf_add_fn(jsonBufPtr, key, value, str_buf_uint);
  233. }
  234. char* json_buf_add_uint_array(char* jsonBufPtr, const char* const key, const uint* const values)
  235. {
  236. return json_buf_add_fn_array(jsonBufPtr, key, values, str_buf_uint_array);
  237. }
  238. char* json_buf_add_uint64(char* jsonBufPtr, const char* const key, const uint64_t value)
  239. {
  240. return json_buf_add_fn(jsonBufPtr, key, value, str_buf_uint64);
  241. }
  242. const char* json_buf_end(char* jsonBufPtr)
  243. {
  244. *jsonBufPtr++ = '}';
  245. *jsonBufPtr++ = '\0';
  246. return jsonBuf;
  247. }
  248. // -------------------------------------------------------------------------------------------------------------------