Commit 1cb7e396 authored by Daniel Verkamp's avatar Daniel Verkamp
Browse files

json: add printf-style spdk_json_write_string_fmt()



Change-Id: I9ea18072d4e54344f145a0b2d16aa6ab7f4d5e03
Signed-off-by: default avatarDaniel Verkamp <daniel.verkamp@intel.com>
parent 022b8a6d
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -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);
+3 −0
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@

#include "spdk/json.h"

#include <stdarg.h>
#include <stdlib.h>
#include <inttypes.h>
#include <stdbool.h>
@@ -46,6 +47,8 @@
#include <string.h>
#include <errno.h>

#include "spdk/string.h"

#define SPDK_JSON_MAX_NESTING_DEPTH	64

static inline bool
+20 −0
Original line number Diff line number Diff line
@@ -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)
{
+1 −1
Original line number Diff line number Diff line
@@ -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)

+1 −1
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ APP = jsoncat

C_SRCS = jsoncat.c

SPDK_LIB_LIST = json
SPDK_LIB_LIST = json util

LIBS += $(SPDK_LIB_LINKER_ARGS)