X-Git-Url: http://git.openwrt.org/?a=blobdiff_plain;f=blobmsg_json.c;h=8f208e0c5c62cc5544411d7b7aeb025ea2375152;hb=6e8e6aca1d919e9916b2a1c8702d0cdec68e1ada;hp=9835a885ee74583ad465d885860168022f0e9d0e;hpb=6f192a6fb04504e065c222be11a6e294229300fe;p=project%2Flibubox.git diff --git a/blobmsg_json.c b/blobmsg_json.c index 9835a88..8f208e0 100644 --- a/blobmsg_json.c +++ b/blobmsg_json.c @@ -13,6 +13,7 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include #include "blobmsg.h" #include "blobmsg_json.h" @@ -129,14 +130,11 @@ static bool blobmsg_puts(struct strbuf *s, const char *c, int len) static void add_separator(struct strbuf *s) { static char indent_chars[17] = "\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"; - static const char indent_space = ' '; int indent; char *start; - if (!s->indent) { - blobmsg_puts(s, &indent_space, 1); + if (!s->indent) return; - } indent = s->indent_level; if (indent > 16) @@ -219,20 +217,16 @@ static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, boo if (!array && blobmsg_name(attr)[0]) { blobmsg_format_string(s, blobmsg_name(attr)); - blobmsg_puts(s, ": ", 2); + blobmsg_puts(s, ": ", s->indent ? 2 : 1); } - if (head) { - data = blob_data(attr); - len = blob_len(attr); - } 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 = blobmsg_data(attr); + len = blobmsg_data_len(attr); + + if (!head && s->custom_format) { + data_str = s->custom_format(s->priv, attr); + if (data_str) + goto out; } data_str = buf; @@ -247,10 +241,10 @@ static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, boo sprintf(buf, "%d", be16_to_cpu(*(uint16_t *)data)); break; case BLOBMSG_TYPE_INT32: - sprintf(buf, "%d", be32_to_cpu(*(uint32_t *)data)); + sprintf(buf, "%d", (int32_t) be32_to_cpu(*(uint32_t *)data)); break; case BLOBMSG_TYPE_INT64: - sprintf(buf, "%lld", (long long int) be64_to_cpu(*(uint64_t *)data)); + sprintf(buf, "%" PRId64, (int64_t) be64_to_cpu(*(uint64_t *)data)); break; case BLOBMSG_TYPE_STRING: blobmsg_format_string(s, data); @@ -293,6 +287,7 @@ static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, i char *blobmsg_format_json_with_cb(struct blob_attr *attr, bool list, blobmsg_json_format_t cb, void *priv, int indent) { struct strbuf s; + bool array; s.len = blob_len(attr); s.buf = malloc(s.len); @@ -306,13 +301,18 @@ char *blobmsg_format_json_with_cb(struct blob_attr *attr, bool list, blobmsg_jso s.indent_level = indent; } + array = blob_is_extended(attr) && + blobmsg_type(attr) == BLOBMSG_TYPE_ARRAY; + if (list) - blobmsg_format_json_list(&s, blob_data(attr), blob_len(attr), false); + blobmsg_format_json_list(&s, blobmsg_data(attr), blobmsg_data_len(attr), array); else blobmsg_format_element(&s, attr, false, false); - if (!s.len) + if (!s.len) { + free(s.buf); return NULL; + } s.buf = realloc(s.buf, s.pos + 1); s.buf[s.pos] = 0;