From 01ad5162b27b29fd0bfe5b862a1a719ba4870e0a Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Sun, 6 Feb 2011 16:48:28 +0100 Subject: [PATCH] add a callback to the blobmsg-to-json function to override the formatting of specific attributes --- blobmsg.c | 19 +++++++++++++++++-- blobmsg.h | 13 ++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/blobmsg.c b/blobmsg.c index a77f158..26604b1 100644 --- a/blobmsg.c +++ b/blobmsg.c @@ -19,6 +19,9 @@ struct strbuf { int len; int pos; char *buf; + + blobmsg_json_format_t custom_format; + void *priv; }; static bool blobmsg_puts(struct strbuf *s, const char *c, int len) @@ -96,6 +99,7 @@ static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, i static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, bool array, bool head) { + const char *data_str; char buf[32]; void *data; int len; @@ -110,8 +114,15 @@ static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, boo } else { data = blobmsg_data(attr); len = blobmsg_data_len(attr); + + if (s->custom_format) { + data_str = s->custom_format(s->priv, attr); + if (data_str) + goto out; + } } + data_str = buf; switch(blob_id(attr)) { case BLOBMSG_TYPE_INT8: sprintf(buf, "%d", *(uint8_t *)data); @@ -135,7 +146,9 @@ static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, boo blobmsg_format_json_list(s, data, len, false); return; } - blobmsg_puts(s, buf, strlen(buf)); + +out: + blobmsg_puts(s, data_str, strlen(data_str)); } static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array) @@ -155,13 +168,15 @@ static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, i blobmsg_puts(s, (array ? " ]" : " }"), 2); } -char *blobmsg_format_json(struct blob_attr *attr, bool list) +char *blobmsg_format_json_with_cb(struct blob_attr *attr, bool list, blobmsg_json_format_t cb, void *priv) { struct strbuf s; s.len = blob_len(attr); s.buf = malloc(s.len); s.pos = 0; + s.custom_format = cb; + s.priv = priv; if (list) blobmsg_format_json_list(&s, blob_data(attr), blob_len(attr), false); diff --git a/blobmsg.h b/blobmsg.h index e51c39d..728f9e3 100644 --- a/blobmsg.h +++ b/blobmsg.h @@ -162,7 +162,18 @@ static inline uint64_t blobmsg_get_u64(struct blob_attr *attr) void *blobmsg_alloc_string_buffer(struct blob_buf *buf, const char *name, int maxlen); void blobmsg_add_string_buffer(struct blob_buf *buf); -char *blobmsg_format_json(struct blob_attr *attr, bool list); +/* blobmsg to json formatting */ + +typedef const char *(*blobmsg_json_format_t)(void *priv, struct blob_attr *attr); + +char *blobmsg_format_json_with_cb(struct blob_attr *attr, bool list, + blobmsg_json_format_t cb, void *priv); + +static inline char *blobmsg_format_json(struct blob_attr *attr, bool list) +{ + return blobmsg_format_json_with_cb(attr, list, NULL, NULL); +} + #define blobmsg_for_each_attr(pos, attr, rem) \ for (rem = blobmsg_data_len(attr), pos = blobmsg_data(attr); \