|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- /*
- * Carla REST API Server
- * Copyright (C) 2018 Filipe Coelho <falktx@falktx.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * For a full copy of the GNU General Public License see the doc/GPL.txt file.
- */
-
- #include "buffers.hpp"
- #include "CarlaMathUtils.hpp"
-
- #include <cstdio>
- #include <cstring>
-
- // -------------------------------------------------------------------------------------------------------------------
-
- enum {
- kJsonBufSize = 4095,
- kStrBufSize = 1023,
- kSizeBufSize = 31,
- };
-
- // static buffer to return json
- // NOTE size is never checked for json, the buffer is big enough in order to assume it all always fits
- static char jsonBuf[kJsonBufSize+1];
-
- // static buffer to return size
- static char sizeBuf[kSizeBufSize+1];
-
- // static buffer to return regular strings
- static char strBuf[kStrBufSize+1];
-
- // -------------------------------------------------------------------------------------------------------------------
-
- const char* size_buf(const char* const buf)
- {
- const std::size_t size = std::strlen(buf);
- std::snprintf(sizeBuf, kSizeBufSize, P_SIZE, size);
- sizeBuf[kSizeBufSize] = '\0';
- return sizeBuf;
- }
-
- // -------------------------------------------------------------------------------------------------------------------
-
- const char* str_buf_bool(const bool value)
- {
- strBuf[0] = value ? '1' : '0';
- strBuf[1] = '\0';
- return strBuf;
- }
-
- // -------------------------------------------------------------------------------------------------------------------
-
- const char* str_buf_float(const double value)
- {
- std::snprintf(strBuf, kStrBufSize, "%f", value);
- strBuf[kStrBufSize] = '\0';
- return strBuf;
- }
-
- const char* str_buf_float_array(const double* const values, const char sep)
- {
- std::size_t bytesRead = 0;
- char tmpBuf[32];
-
- for (int i=0; carla_isNotZero(values[i]) && bytesRead < kStrBufSize; ++i)
- {
- std::snprintf(tmpBuf, 31, "%f", values[i]);
- tmpBuf[31] = '\0';
-
- const std::size_t size = std::strlen(tmpBuf);
-
- if (bytesRead + size > kStrBufSize)
- break;
-
- std::strncpy(strBuf+bytesRead, tmpBuf, kStrBufSize - bytesRead);
- bytesRead += size;
- strBuf[bytesRead] = sep;
- bytesRead += 1;
- }
-
- strBuf[bytesRead > 0 ? bytesRead-1 : 0] = '\0';
- return strBuf;
- }
-
- // -------------------------------------------------------------------------------------------------------------------
-
- const char* str_buf_string(const char* const string)
- {
- std::strncpy(strBuf, string, kStrBufSize);
- strBuf[kStrBufSize] = '\0';
- return strBuf;
- }
-
- const char* str_buf_string_array(const char* const* const array)
- {
- std::size_t bytesRead = 0;
-
- for (int i=0; array[i] != nullptr && bytesRead < kStrBufSize; ++i)
- {
- const std::size_t size = std::strlen(array[i]);
-
- if (bytesRead + size > kStrBufSize)
- break;
-
- std::strncpy(strBuf+bytesRead, array[i], kStrBufSize - bytesRead);
- bytesRead += size;
- strBuf[bytesRead] = '\n';
- bytesRead += 1;
- }
-
- strBuf[bytesRead > 0 ? bytesRead-1 : 0] = '\0';
- return strBuf;
- }
-
- const char* str_buf_string_quoted(const char* const string)
- {
- const std::size_t size = std::strlen(string);
- char* strBufPtr = strBuf;
-
- *strBufPtr++ = '"';
-
- for (std::size_t i=0, bytesWritten=0; i < size && bytesWritten < kStrBufSize-1; ++i)
- {
- switch (string[i])
- {
- case '"':
- case '\\':
- *strBufPtr++ = '\\';
- ++bytesWritten;
- break;
- case '\n':
- *strBufPtr++ = '\\';
- *strBufPtr++ = 'n';;
- bytesWritten += 2;
- continue;
- case '\f':
- *strBufPtr++ = '\\';
- *strBufPtr++ = 'f';;
- bytesWritten += 2;
- continue;
- }
-
- *strBufPtr++ = string[i];
- ++bytesWritten;
- }
-
- *strBufPtr++ = '"';
- *strBufPtr++ = '\0';
- return strBuf;
- }
-
- // -------------------------------------------------------------------------------------------------------------------
-
- const char* str_buf_int(const int value)
- {
- std::snprintf(strBuf, kStrBufSize, "%i", value);
- strBuf[kStrBufSize] = '\0';
- return strBuf;
- }
-
- const char* str_buf_int64(const int64_t value)
- {
- std::snprintf(strBuf, kStrBufSize, P_INT64, value);
- strBuf[kStrBufSize] = '\0';
- return strBuf;
- }
-
- const char* str_buf_uint(const uint value)
- {
- std::snprintf(strBuf, kStrBufSize, "%u", value);
- strBuf[kStrBufSize] = '\0';
- return strBuf;
- }
-
- const char* str_buf_uint64(const uint64_t value)
- {
- std::snprintf(strBuf, kStrBufSize, P_UINT64, value);
- strBuf[kStrBufSize] = '\0';
- return strBuf;
- }
-
- const char* str_buf_uint_array(const uint* const values, const char sep)
- {
- std::size_t bytesRead = 0;
- char tmpBuf[32];
-
- for (int i=0; values[i] != 0 && bytesRead < kStrBufSize; ++i)
- {
- std::snprintf(tmpBuf, 31, "%u", values[i]);
- tmpBuf[31] = '\0';
-
- const std::size_t size = std::strlen(tmpBuf);
-
- if (bytesRead + size > kStrBufSize)
- break;
-
- std::strncpy(strBuf+bytesRead, tmpBuf, kStrBufSize - bytesRead);
- bytesRead += size;
- strBuf[bytesRead] = sep;
- bytesRead += 1;
- }
-
- strBuf[bytesRead > 0 ? bytesRead-1 : 0] = '\0';
- return strBuf;
- }
-
- // -------------------------------------------------------------------------------------------------------------------
-
- char* json_buf_start()
- {
- std::strcpy(jsonBuf, "{");
- return jsonBuf + 1;
- }
-
- char* json_buf_add(char* jsonBufPtr, const char* const key, const char* const valueBuf)
- {
- if (jsonBufPtr != jsonBuf+1)
- *jsonBufPtr++ = ',';
-
- *jsonBufPtr++ = '"';
-
- std::strcpy(jsonBufPtr, key);
- jsonBufPtr += std::strlen(key);
-
- *jsonBufPtr++ = '"';
- *jsonBufPtr++ = ':';
-
- std::strcpy(jsonBufPtr, valueBuf);
- jsonBufPtr += std::strlen(valueBuf);
-
- return jsonBufPtr;
- }
-
- template <typename T, typename Fn>
- char* json_buf_add_fn(char* jsonBufPtr, const char* const key, const T value, const Fn fn)
- {
- return json_buf_add(jsonBufPtr, key, fn(value));
- }
-
- template <typename T, typename Fn>
- char* json_buf_add_fn_array(char* jsonBufPtr, const char* const key, const T value, const Fn fn)
- {
- return json_buf_add(jsonBufPtr, key, fn(value, ','));
- }
-
- // -------------------------------------------------------------------------------------------------------------------
-
- char* json_buf_add_bool(char* jsonBufPtr, const char* const key, const bool value)
- {
- static const char* const kTrue = "true";
- static const char* const kFalse = "false";
- return json_buf_add_fn(jsonBufPtr, key, value ? kTrue : kFalse, str_buf_string);
- }
-
- char* json_buf_add_float(char* jsonBufPtr, const char* const key, const double value)
- {
- return json_buf_add_fn(jsonBufPtr, key, value, str_buf_float);
- }
-
- char* json_buf_add_float_array(char* jsonBufPtr, const char* const key, const double* const values)
- {
- return json_buf_add_fn_array(jsonBufPtr, key, values, str_buf_float_array);
- }
-
- char* json_buf_add_string(char* jsonBufPtr, const char* const key, const char* const value)
- {
- return json_buf_add_fn(jsonBufPtr, key, value, str_buf_string_quoted);
- }
-
- char* json_buf_add_int(char* jsonBufPtr, const char* const key, const int value)
- {
- return json_buf_add_fn(jsonBufPtr, key, value, str_buf_int);
- }
-
- char* json_buf_add_int64(char* jsonBufPtr, const char* const key, const int64_t value)
- {
- return json_buf_add_fn(jsonBufPtr, key, value, str_buf_int64);
- }
-
- char* json_buf_add_uint(char* jsonBufPtr, const char* const key, const uint value)
- {
- return json_buf_add_fn(jsonBufPtr, key, value, str_buf_uint);
- }
-
- char* json_buf_add_uint_array(char* jsonBufPtr, const char* const key, const uint* const values)
- {
- return json_buf_add_fn_array(jsonBufPtr, key, values, str_buf_uint_array);
- }
-
- char* json_buf_add_uint64(char* jsonBufPtr, const char* const key, const uint64_t value)
- {
- return json_buf_add_fn(jsonBufPtr, key, value, str_buf_uint64);
- }
-
- const char* json_buf_end(char* jsonBufPtr)
- {
- *jsonBufPtr++ = '}';
- *jsonBufPtr++ = '\0';
- return jsonBuf;
- }
-
- // -------------------------------------------------------------------------------------------------------------------
|