blobmsg: make arrays structually the same as tables - simplifies library user code

This commit is contained in:
Felix Fietkau 2011-01-29 18:00:40 +01:00
parent 34b9f56464
commit 5129bc9401
3 changed files with 31 additions and 53 deletions

View file

@ -94,18 +94,20 @@ static void blobmsg_format_string(struct strbuf *s, char *str)
static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array); static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array);
static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, bool array) static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, bool array, bool head)
{ {
char buf[32]; char buf[32];
void *data; void *data;
int len; int len;
if (array) { if (!array) {
data = blob_data(attr);
len = blob_len(attr);
} else {
blobmsg_format_string(s, blobmsg_name(attr)); blobmsg_format_string(s, blobmsg_name(attr));
blobmsg_puts(s, ":", 1); blobmsg_puts(s, ":", 1);
}
if (head) {
data = blob_data(attr);
len = blob_len(attr);
} else {
data = blobmsg_data(attr); data = blobmsg_data(attr);
len = blobmsg_data_len(attr); len = blobmsg_data_len(attr);
} }
@ -147,7 +149,7 @@ static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, i
if (!first) if (!first)
blobmsg_puts(s, ", ", 2); blobmsg_puts(s, ", ", 2);
blobmsg_format_element(s, pos, array); blobmsg_format_element(s, pos, array, false);
first = false; first = false;
} }
blobmsg_puts(s, (array ? " ]" : " }"), 2); blobmsg_puts(s, (array ? " ]" : " }"), 2);
@ -161,7 +163,7 @@ char *blobmsg_format_json(struct blob_attr *attr)
s.buf = malloc(s.len); s.buf = malloc(s.len);
s.pos = 0; s.pos = 0;
blobmsg_format_element(&s, attr, false); blobmsg_format_element(&s, attr, true, true);
if (!s.len) if (!s.len)
return NULL; return NULL;
@ -244,14 +246,8 @@ blobmsg_new(struct blob_buf *buf, int type, const char *name, int payload_len, v
struct blobmsg_hdr *hdr; struct blobmsg_hdr *hdr;
int attrlen, namelen; int attrlen, namelen;
if (blob_id(buf->head) == BLOBMSG_TYPE_ARRAY && !name) { if (!name)
attr = blob_new(buf, type, payload_len); name = "";
*data = blob_data(attr);
return attr;
}
if (blob_id(buf->head) != BLOBMSG_TYPE_TABLE || !name)
return NULL;
namelen = strlen(name); namelen = strlen(name);
attrlen = blobmsg_hdrlen(namelen) + payload_len; attrlen = blobmsg_hdrlen(namelen) + payload_len;
@ -282,17 +278,13 @@ blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array)
unsigned long offset = attr_to_offset(buf, buf->head); unsigned long offset = attr_to_offset(buf, buf->head);
void *data; void *data;
if (blob_id(head) == BLOBMSG_TYPE_ARRAY && !name) if (!name)
return blob_nest_start(buf, type); name = "";
if (blob_id(head) == BLOBMSG_TYPE_TABLE && name) { head = blobmsg_new(buf, type, name, 0, &data);
head = blobmsg_new(buf, type, name, 0, &data); blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blobmsg_hdrlen(strlen(name)));
blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blobmsg_hdrlen(strlen(name))); buf->head = head;
buf->head = head; return (void *)offset;
return (void *)offset;
}
return NULL;
} }
int int

View file

@ -35,7 +35,7 @@ enum blobmsg_type {
}; };
struct blobmsg_hdr { struct blobmsg_hdr {
uint8_t namelen; uint16_t namelen;
uint8_t name[]; uint8_t name[];
} __packed; } __packed;
@ -58,7 +58,7 @@ static inline void *blobmsg_name(struct blob_attr *attr)
static inline void *blobmsg_data(struct blob_attr *attr) static inline void *blobmsg_data(struct blob_attr *attr)
{ {
struct blobmsg_hdr *hdr = blob_data(attr); struct blobmsg_hdr *hdr = blob_data(attr);
return &hdr->name[blobmsg_hdrlen(hdr->namelen) - 1]; return (char *) hdr + blobmsg_hdrlen(hdr->namelen);
} }
static inline int blobmsg_data_len(struct blob_attr *attr) static inline int blobmsg_data_len(struct blob_attr *attr)

View file

@ -13,19 +13,7 @@ static const char *indent_str = "\t\t\t\t\t\t\t\t\t\t\t\t\t";
static void dump_attr_data(void *data, int len, int type, int indent, int next_indent); static void dump_attr_data(void *data, int len, int type, int indent, int next_indent);
static void static void
dump_array(struct blob_attr *head, int len, int indent) dump_table(struct blob_attr *head, int len, int indent, bool array)
{
struct blob_attr *attr;
indent_printf(indent, "{\n");
__blob_for_each_attr(attr, head, len) {
dump_attr_data(blob_data(attr), blob_len(attr), blob_id(attr), indent + 1, indent + 1);
}
indent_printf(indent, "}\n");
}
static void
dump_table(struct blob_attr *head, int len, int indent)
{ {
struct blob_attr *attr, *last_attr; struct blob_attr *attr, *last_attr;
struct blobmsg_hdr *hdr; struct blobmsg_hdr *hdr;
@ -33,7 +21,8 @@ dump_table(struct blob_attr *head, int len, int indent)
indent_printf(indent, "{\n"); indent_printf(indent, "{\n");
__blob_for_each_attr(attr, head, len) { __blob_for_each_attr(attr, head, len) {
hdr = blob_data(attr); hdr = blob_data(attr);
indent_printf(indent + 1, "%s : ", hdr->name); if (!array)
indent_printf(indent + 1, "%s : ", hdr->name);
dump_attr_data(blobmsg_data(attr), blobmsg_data_len(attr), blob_id(attr), 0, indent + 1); dump_attr_data(blobmsg_data(attr), blobmsg_data_len(attr), blob_id(attr), 0, indent + 1);
last_attr = attr; last_attr = attr;
} }
@ -59,14 +48,10 @@ static void dump_attr_data(void *data, int len, int type, int indent, int next_i
indent_printf(indent, "%lld\n", *(uint64_t *)data); indent_printf(indent, "%lld\n", *(uint64_t *)data);
break; break;
case BLOBMSG_TYPE_TABLE: case BLOBMSG_TYPE_TABLE:
if (!indent)
indent_printf(indent, "\n");
dump_table(data, len, next_indent);
break;
case BLOBMSG_TYPE_ARRAY: case BLOBMSG_TYPE_ARRAY:
if (!indent) if (!indent)
indent_printf(indent, "\n"); indent_printf(indent, "\n");
dump_array(data, len, next_indent); dump_table(data, len, next_indent, type == BLOBMSG_TYPE_ARRAY);
break; break;
} }
} }
@ -109,11 +94,11 @@ static void dump_message(struct blob_buf *buf)
if (tb[FOO_LIST]) { if (tb[FOO_LIST]) {
fprintf(stderr, "List: "); fprintf(stderr, "List: ");
dump_array(blobmsg_data(tb[FOO_LIST]), blob_len(tb[FOO_LIST]), 0); dump_table(blobmsg_data(tb[FOO_LIST]), blob_len(tb[FOO_LIST]), 0, true);
} }
if (tb[FOO_TESTDATA]) { if (tb[FOO_TESTDATA]) {
fprintf(stderr, "Testdata: "); fprintf(stderr, "Testdata: ");
dump_table(blobmsg_data(tb[FOO_TESTDATA]), blob_len(tb[FOO_TESTDATA]), 0); dump_table(blobmsg_data(tb[FOO_TESTDATA]), blob_len(tb[FOO_TESTDATA]), 0, false);
} }
} }
@ -124,16 +109,16 @@ fill_message(struct blob_buf *buf)
blobmsg_add_string(buf, "message", "Hello, world!"); blobmsg_add_string(buf, "message", "Hello, world!");
tbl = blobmsg_open_table(buf, "testdata");
blobmsg_add_u32(buf, "hello", 1);
blobmsg_add_string(buf, "world", "2");
blobmsg_close_table(buf, tbl);
tbl = blobmsg_open_array(buf, "list"); tbl = blobmsg_open_array(buf, "list");
blobmsg_add_u32(buf, NULL, 0); blobmsg_add_u32(buf, NULL, 0);
blobmsg_add_u32(buf, NULL, 1); blobmsg_add_u32(buf, NULL, 1);
blobmsg_add_u32(buf, NULL, 2); blobmsg_add_u32(buf, NULL, 2);
blobmsg_close_table(buf, tbl); blobmsg_close_table(buf, tbl);
tbl = blobmsg_open_table(buf, "testdata");
blobmsg_add_u32(buf, "hello", 1);
blobmsg_add_string(buf, "world", "2");
blobmsg_close_table(buf, tbl);
} }
int main(int argc, char **argv) int main(int argc, char **argv)
@ -143,6 +128,7 @@ int main(int argc, char **argv)
blobmsg_buf_init(&buf); blobmsg_buf_init(&buf);
fill_message(&buf); fill_message(&buf);
dump_message(&buf); dump_message(&buf);
fprintf(stderr, "json: %s\n", blobmsg_format_json(buf.head));
if (buf.buf) if (buf.buf)
free(buf.buf); free(buf.buf);