From af2f52a37bdbb34835da08b518a5f5a950d87a77 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Sun, 17 Feb 2013 16:42:12 +0100 Subject: [PATCH] blobmsg: implement blobmsg_printf and blobmsg_vprintf Signed-off-by: Felix Fietkau --- blobmsg.c | 25 +++++++++++++++++++++++++ blobmsg.h | 6 ++++++ 2 files changed, 31 insertions(+) diff --git a/blobmsg.c b/blobmsg.c index 8db2238..62f83cc 100644 --- a/blobmsg.c +++ b/blobmsg.c @@ -216,6 +216,31 @@ blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array) return (void *)offset; } +void +blobmsg_vprintf(struct blob_buf *buf, const char *name, const char *format, va_list arg) +{ + va_list arg2; + char cbuf; + int len; + + va_copy(arg2, arg); + len = vsnprintf(&cbuf, sizeof(cbuf), format, arg2); + va_end(arg2); + + vsprintf(blobmsg_alloc_string_buffer(buf, name, len + 1), format, arg); + blobmsg_add_string_buffer(buf); +} + +void +blobmsg_printf(struct blob_buf *buf, const char *name, const char *format, ...) +{ + va_list ap; + + va_start(ap, format); + blobmsg_vprintf(buf, name, format, ap); + va_end(ap); +} + void * blobmsg_alloc_string_buffer(struct blob_buf *buf, const char *name, int maxlen) { diff --git a/blobmsg.h b/blobmsg.h index c4bf10d..3eeec9b 100644 --- a/blobmsg.h +++ b/blobmsg.h @@ -16,6 +16,7 @@ #ifndef __BLOBMSG_H #define __BLOBMSG_H +#include #include "blob.h" #define BLOBMSG_ALIGN 2 @@ -195,6 +196,11 @@ void *blobmsg_alloc_string_buffer(struct blob_buf *buf, const char *name, int ma void *blobmsg_realloc_string_buffer(struct blob_buf *buf, int maxlen); void blobmsg_add_string_buffer(struct blob_buf *buf); +void blobmsg_vprintf(struct blob_buf *buf, const char *name, const char *format, va_list arg); +void blobmsg_printf(struct blob_buf *buf, const char *name, const char *format, ...) + __attribute__((format(printf, 3, 4))); + + /* blobmsg to json formatting */ #define blobmsg_for_each_attr(pos, attr, rem) \ -- 2.25.1