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.

139 lines
3.7KB

  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 <cstdio>
  19. #include <cstring>
  20. // -------------------------------------------------------------------------------------------------------------------
  21. enum {
  22. kJsonBufSize = 4095,
  23. kStrBufSize = 1023,
  24. kSizeBufSize = 31,
  25. };
  26. // static buffer to return json
  27. // NOTE size is never checked for json, the buffer is big enough in order to assume it all always fits
  28. static char jsonBuf[kJsonBufSize+1];
  29. // static buffer to return size
  30. static char sizeBuf[kSizeBufSize+1];
  31. // static buffer to return regular strings
  32. static char strBuf[kStrBufSize+1];
  33. // -------------------------------------------------------------------------------------------------------------------
  34. const char* size_buf(const char* const buf)
  35. {
  36. const std::size_t size = std::strlen(buf);
  37. std::snprintf(sizeBuf, kSizeBufSize, P_SIZE, size);
  38. sizeBuf[kSizeBufSize] = '\0';
  39. return sizeBuf;
  40. }
  41. // -------------------------------------------------------------------------------------------------------------------
  42. const char* str_buf_string(const char* const string)
  43. {
  44. std::strncpy(strBuf, string, kStrBufSize);
  45. strBuf[kStrBufSize] = '\0';
  46. return strBuf;
  47. }
  48. const char* str_buf_string_array(const char* const* const array)
  49. {
  50. size_t bytesRead = 0;
  51. for (int i=0; array[i] != nullptr && bytesRead < kStrBufSize; ++i)
  52. {
  53. const std::size_t size = std::strlen(array[i]);
  54. if (bytesRead + size > kStrBufSize)
  55. break;
  56. std::strncpy(strBuf+bytesRead, array[i], kStrBufSize);
  57. bytesRead += size;
  58. strBuf[bytesRead] = '\n';
  59. bytesRead += 1;
  60. }
  61. strBuf[bytesRead] = '\0';
  62. return strBuf;
  63. }
  64. const char* str_buf_uint(const uint value)
  65. {
  66. std::snprintf(strBuf, kStrBufSize, "%u", value);
  67. strBuf[kStrBufSize] = '\0';
  68. return strBuf;
  69. }
  70. // -------------------------------------------------------------------------------------------------------------------
  71. char* json_buf_start()
  72. {
  73. std::strcpy(jsonBuf, "{");
  74. return jsonBuf + 1;
  75. }
  76. template <typename T, typename Fn>
  77. char* json_buf_add(char* jsonBufPtr, const char* const key, const T value, const Fn fn)
  78. {
  79. fn(value);
  80. if (jsonBufPtr != jsonBuf+1)
  81. {
  82. *jsonBufPtr = ',';
  83. jsonBufPtr += 1;
  84. }
  85. *jsonBufPtr++ = '"';
  86. std::strcpy(jsonBufPtr, key);
  87. jsonBufPtr += std::strlen(key);
  88. *jsonBufPtr++ = '"';
  89. *jsonBufPtr++ = ':';
  90. std::strcpy(jsonBufPtr, strBuf);
  91. jsonBufPtr += std::strlen(strBuf);
  92. return jsonBufPtr;
  93. }
  94. char* json_buf_add_string(char* jsonBufPtr, const char* const key, const char* const value)
  95. {
  96. return json_buf_add(jsonBufPtr, key, value, str_buf_string);
  97. }
  98. char* json_buf_add_uint(char* jsonBufPtr, const char* const key, const uint value)
  99. {
  100. return json_buf_add(jsonBufPtr, key, value, str_buf_uint);
  101. }
  102. const char* json_buf_end(char* jsonBufPtr)
  103. {
  104. *jsonBufPtr++ = '}';
  105. *jsonBufPtr++ = '\0';
  106. return jsonBuf;
  107. }
  108. // -------------------------------------------------------------------------------------------------------------------