diff --git a/common/include/villas/compat.h b/common/include/villas/compat.h index 51cee94d7..bd907be9a 100644 --- a/common/include/villas/compat.h +++ b/common/include/villas/compat.h @@ -32,6 +32,9 @@ extern "C" { #if JANSSON_VERSION_HEX < 0x020A00 size_t json_dumpb(const json_t *json, char *buffer, size_t size, size_t flags); + +int json_dumpfd(const json_t *json, int output, size_t flags); +json_t *json_loadfd(int input, size_t flags, json_error_t *error); #endif #if defined(LIBCONFIG_FOUND) && (LIBCONFIG_VER_MAJOR <= 1) && (LIBCONFIG_VER_MINOR < 5) diff --git a/common/lib/compat.c b/common/lib/compat.c index 6121020b0..b72747cd7 100644 --- a/common/lib/compat.c +++ b/common/lib/compat.c @@ -22,6 +22,7 @@ #include #include +#include #include @@ -43,4 +44,34 @@ size_t json_dumpb(const json_t *json, char *buffer, size_t size, size_t flags) return len; } + +static size_t json_loadfd_callback(void *buffer, size_t buflen, void *data) +{ + int *fd = (int *) data; + + return (size_t) read(*fd, buffer, buflen); +} + +json_t *json_loadfd(int input, size_t flags, json_error_t *error) +{ + return json_load_callback(json_loadfd_callback, (void *) &input, flags, error); +} + + +static int json_dumpfd_callback(const char *buffer, size_t size, void *data) +{ +#ifdef HAVE_UNISTD_H + int *dest = (int *)data; + + if (write(*dest, buffer, size) == (ssize_t)size) + return 0; +#endif + + return -1; +} + +int json_dumpfd(const json_t *json, int output, size_t flags) +{ + return json_dump_callback(json, json_dumpfd_callback, (void *) &output, flags); +} #endif