From 1cb7e396a70cb0cba4d1161c1abf2e158563326b Mon Sep 17 00:00:00 2001 From: Daniel Verkamp Date: Fri, 18 Nov 2016 10:50:14 -0700 Subject: [PATCH] json: add printf-style spdk_json_write_string_fmt() Change-Id: I9ea18072d4e54344f145a0b2d16aa6ab7f4d5e03 Signed-off-by: Daniel Verkamp --- include/spdk/json.h | 2 ++ lib/json/json_internal.h | 3 +++ lib/json/json_write.c | 20 ++++++++++++++++++++ mk/json.unittest.mk | 2 +- test/lib/json/jsoncat/Makefile | 2 +- 5 files changed, 27 insertions(+), 2 deletions(-) diff --git a/include/spdk/json.h b/include/spdk/json.h index a9f1d3ce1..dd6f00a6f 100644 --- a/include/spdk/json.h +++ b/include/spdk/json.h @@ -200,6 +200,8 @@ int spdk_json_write_int64(struct spdk_json_write_ctx *w, int64_t val); int spdk_json_write_uint64(struct spdk_json_write_ctx *w, uint64_t val); int spdk_json_write_string(struct spdk_json_write_ctx *w, const char *val); int spdk_json_write_string_raw(struct spdk_json_write_ctx *w, const char *val, size_t len); +int spdk_json_write_string_fmt(struct spdk_json_write_ctx *w, const char *fmt, + ...) __attribute__((__format__(__printf__, 2, 3))); int spdk_json_write_array_begin(struct spdk_json_write_ctx *w); int spdk_json_write_array_end(struct spdk_json_write_ctx *w); int spdk_json_write_object_begin(struct spdk_json_write_ctx *w); diff --git a/lib/json/json_internal.h b/lib/json/json_internal.h index c8a88b0cb..fcfe7a6a3 100644 --- a/lib/json/json_internal.h +++ b/lib/json/json_internal.h @@ -36,6 +36,7 @@ #include "spdk/json.h" +#include #include #include #include @@ -46,6 +47,8 @@ #include #include +#include "spdk/string.h" + #define SPDK_JSON_MAX_NESTING_DEPTH 64 static inline bool diff --git a/lib/json/json_write.c b/lib/json/json_write.c index 0cfb83559..70e3a204c 100644 --- a/lib/json/json_write.c +++ b/lib/json/json_write.c @@ -317,6 +317,26 @@ spdk_json_write_string(struct spdk_json_write_ctx *w, const char *val) return spdk_json_write_string_raw(w, val, strlen(val)); } +int +spdk_json_write_string_fmt(struct spdk_json_write_ctx *w, const char *fmt, ...) +{ + char *s; + va_list args; + int rc; + + va_start(args, fmt); + s = spdk_vsprintf_alloc(fmt, args); + va_end(args); + + if (s == NULL) { + return -1; + } + + rc = spdk_json_write_string(w, s); + free(s); + return rc; +} + int spdk_json_write_array_begin(struct spdk_json_write_ctx *w) { diff --git a/mk/json.unittest.mk b/mk/json.unittest.mk index 84821a949..b08655bc8 100644 --- a/mk/json.unittest.mk +++ b/mk/json.unittest.mk @@ -37,7 +37,7 @@ include $(SPDK_ROOT_DIR)/mk/spdk.app.mk JSON_DIR := $(SPDK_ROOT_DIR)/lib/json JSONRPC_DIR := $(SPDK_ROOT_DIR)/lib/jsonrpc -SPDK_LIB_LIST = json log +SPDK_LIB_LIST = json util log C_SRCS = $(TEST_FILE) $(OTHER_FILES) diff --git a/test/lib/json/jsoncat/Makefile b/test/lib/json/jsoncat/Makefile index 526c9beae..0e2336ede 100644 --- a/test/lib/json/jsoncat/Makefile +++ b/test/lib/json/jsoncat/Makefile @@ -39,7 +39,7 @@ APP = jsoncat C_SRCS = jsoncat.c -SPDK_LIB_LIST = json +SPDK_LIB_LIST = json util LIBS += $(SPDK_LIB_LINKER_ARGS)