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.

280 lines
7.6KB

  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 uint 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. 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. 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. // -------------------------------------------------------------------------------------------------------------------
  99. const char* str_buf_int(const int value)
  100. {
  101. std::snprintf(strBuf, kStrBufSize, "%i", value);
  102. strBuf[kStrBufSize] = '\0';
  103. return strBuf;
  104. }
  105. const char* str_buf_uint(const uint value)
  106. {
  107. std::snprintf(strBuf, kStrBufSize, "%u", value);
  108. strBuf[kStrBufSize] = '\0';
  109. return strBuf;
  110. }
  111. const char* str_buf_uint64(const uint64_t value)
  112. {
  113. std::snprintf(strBuf, kStrBufSize, P_UINT64, value);
  114. strBuf[kStrBufSize] = '\0';
  115. return strBuf;
  116. }
  117. const char* str_buf_uint_array(const uint* const values, const char sep)
  118. {
  119. size_t bytesRead = 0;
  120. char tmpBuf[32];
  121. for (int i=0; values[i] != 0 && bytesRead < kStrBufSize; ++i)
  122. {
  123. std::snprintf(tmpBuf, 31, "%u", values[i]);
  124. tmpBuf[31] = '\0';
  125. const std::size_t size = std::strlen(tmpBuf);
  126. if (bytesRead + size > kStrBufSize)
  127. break;
  128. std::strncpy(strBuf+bytesRead, tmpBuf, kStrBufSize - bytesRead);
  129. bytesRead += size;
  130. strBuf[bytesRead] = sep;
  131. bytesRead += 1;
  132. }
  133. strBuf[bytesRead > 0 ? bytesRead-1 : 0] = '\0';
  134. return strBuf;
  135. }
  136. // -------------------------------------------------------------------------------------------------------------------
  137. char* json_buf_start()
  138. {
  139. std::strcpy(jsonBuf, "{");
  140. return jsonBuf + 1;
  141. }
  142. template <typename T, typename Fn>
  143. char* json_buf_add(char* jsonBufPtr, const char* const key, const T value, const Fn fn)
  144. {
  145. const char* const valueBuf = fn(value);
  146. if (jsonBufPtr != jsonBuf+1)
  147. *jsonBufPtr++ = ',';
  148. *jsonBufPtr++ = '"';
  149. std::strcpy(jsonBufPtr, key);
  150. jsonBufPtr += std::strlen(key);
  151. *jsonBufPtr++ = '"';
  152. *jsonBufPtr++ = ':';
  153. std::strcpy(jsonBufPtr, valueBuf);
  154. jsonBufPtr += std::strlen(valueBuf);
  155. return jsonBufPtr;
  156. }
  157. template <typename T, typename Fn>
  158. char* json_buf_add_array(char* jsonBufPtr, const char* const key, const T value, const Fn fn)
  159. {
  160. const char* const valueBuf = fn(value, ',');
  161. if (jsonBufPtr != jsonBuf+1)
  162. *jsonBufPtr++ = ',';
  163. *jsonBufPtr++ = '"';
  164. std::strcpy(jsonBufPtr, key);
  165. jsonBufPtr += std::strlen(key);
  166. *jsonBufPtr++ = '"';
  167. *jsonBufPtr++ = ':';
  168. *jsonBufPtr++ = '[';
  169. std::strcpy(jsonBufPtr, valueBuf);
  170. jsonBufPtr += std::strlen(valueBuf);
  171. *jsonBufPtr++ = ']';
  172. return jsonBufPtr;
  173. }
  174. // -------------------------------------------------------------------------------------------------------------------
  175. char* json_buf_add_bool(char* jsonBufPtr, const char* const key, const bool value)
  176. {
  177. static const char* const kTrue = "true";
  178. static const char* const kFalse = "false";
  179. return json_buf_add(jsonBufPtr, key, value ? kTrue : kFalse, str_buf_string);
  180. }
  181. char* json_buf_add_float(char* jsonBufPtr, const char* const key, const double value)
  182. {
  183. return json_buf_add(jsonBufPtr, key, value, str_buf_float);
  184. }
  185. char* json_buf_add_float_array(char* jsonBufPtr, const char* const key, const double* const values)
  186. {
  187. return json_buf_add_array(jsonBufPtr, key, values, str_buf_float_array);
  188. }
  189. char* json_buf_add_string(char* jsonBufPtr, const char* const key, const char* const value)
  190. {
  191. return json_buf_add(jsonBufPtr, key, value, str_buf_string);
  192. }
  193. char* json_buf_add_int(char* jsonBufPtr, const char* const key, const int value)
  194. {
  195. return json_buf_add(jsonBufPtr, key, value, str_buf_int);
  196. }
  197. char* json_buf_add_uint(char* jsonBufPtr, const char* const key, const uint value)
  198. {
  199. return json_buf_add(jsonBufPtr, key, value, str_buf_uint);
  200. }
  201. char* json_buf_add_uint_array(char* jsonBufPtr, const char* const key, const uint* const values)
  202. {
  203. return json_buf_add_array(jsonBufPtr, key, values, str_buf_uint_array);
  204. }
  205. char* json_buf_add_uint64(char* jsonBufPtr, const char* const key, const uint64_t value)
  206. {
  207. return json_buf_add(jsonBufPtr, key, value, str_buf_uint64);
  208. }
  209. const char* json_buf_end(char* jsonBufPtr)
  210. {
  211. *jsonBufPtr++ = '}';
  212. *jsonBufPtr++ = '\0';
  213. return jsonBuf;
  214. }
  215. // -------------------------------------------------------------------------------------------------------------------