libblobpack/examples/simple.c

168 lines
4.1 KiB
C
Raw Normal View History

2015-12-06 16:24:21 +01:00
#include <stdio.h>
#include <inttypes.h>
#include <blobpack.h>
static const char *indent_str = "\t\t\t\t\t\t\t\t\t\t\t\t\t";
#define indent_printf(indent, ...) do { \
if (indent > 0) \
fwrite(indent_str, indent, 1, stderr); \
fprintf(stderr, __VA_ARGS__); \
} while(0)
static void dump_attr_data(struct blob_attr *data, int indent, int next_indent);
static void
dump_table(struct blob_attr *head, int len, int indent, bool array)
{
struct blob_attr *attr;
indent_printf(indent, "{\n");
2015-12-15 11:55:50 +01:00
for(attr = blob_attr_first_child(head); attr; attr = blob_attr_next_child(head, attr)){
2015-12-16 15:24:40 +01:00
if (!array){
indent_printf(indent + 1, "%s : ", blob_attr_get_string(attr));
attr = blob_attr_next_child(head, attr);
}
2015-12-06 16:24:21 +01:00
dump_attr_data(attr, 0, indent + 1);
}
indent_printf(indent, "}\n");
}
static void dump_attr_data(struct blob_attr *data, int indent, int next_indent)
{
2015-12-15 11:55:50 +01:00
int type = blob_attr_type(data);
2015-12-06 16:24:21 +01:00
switch(type) {
2015-12-15 11:55:50 +01:00
case BLOB_ATTR_STRING:
indent_printf(indent, "%s\n", blob_attr_get_string(data));
2015-12-06 16:24:21 +01:00
break;
2015-12-15 11:55:50 +01:00
case BLOB_ATTR_INT8:
indent_printf(indent, "%d\n", blob_attr_get_u8(data));
2015-12-06 16:24:21 +01:00
break;
2015-12-15 11:55:50 +01:00
case BLOB_ATTR_INT16:
indent_printf(indent, "%d\n", blob_attr_get_u16(data));
2015-12-06 16:24:21 +01:00
break;
2015-12-15 11:55:50 +01:00
case BLOB_ATTR_INT32:
indent_printf(indent, "%d\n", blob_attr_get_u32(data));
2015-12-06 16:24:21 +01:00
break;
2015-12-15 11:55:50 +01:00
case BLOB_ATTR_INT64:
indent_printf(indent, "%"PRIu64"\n", blob_attr_get_u64(data));
2015-12-06 16:24:21 +01:00
break;
2015-12-15 11:55:50 +01:00
case BLOB_ATTR_FLOAT32:
indent_printf(indent, "%f\n", blob_attr_get_float(data));
break;
2015-12-15 11:55:50 +01:00
case BLOB_ATTR_FLOAT64:
indent_printf(indent, "%Le\n", (long double) blob_attr_get_double(data));
break;
2015-12-15 11:55:50 +01:00
case BLOB_ATTR_TABLE:
case BLOB_ATTR_ARRAY:
2015-12-06 16:24:21 +01:00
if (!indent)
indent_printf(indent, "\n");
2015-12-15 11:55:50 +01:00
//dump_table(data, blob_data_len(data),
// next_indent, type == BLOBMSG_TYPE_ARRAY);
2015-12-06 16:24:21 +01:00
break;
}
}
enum {
FOO_MESSAGE,
FOO_LIST,
FOO_TESTDATA
};
2015-12-15 11:55:50 +01:00
static const struct blob_attr_policy pol[] = {
2015-12-06 16:24:21 +01:00
[FOO_MESSAGE] = {
.name = "message",
2015-12-15 11:55:50 +01:00
.type = BLOB_ATTR_STRING,
2015-12-06 16:24:21 +01:00
},
[FOO_LIST] = {
.name = "list",
2015-12-15 11:55:50 +01:00
.type = BLOB_ATTR_ARRAY,
2015-12-06 16:24:21 +01:00
},
[FOO_TESTDATA] = {
.name = "testdata",
2015-12-15 11:55:50 +01:00
.type = BLOB_ATTR_TABLE,
2015-12-06 16:24:21 +01:00
},
};
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#endif
static void dump_message(struct blob_buf *buf)
{
2015-12-15 11:55:50 +01:00
//struct blob_attr *tb[ARRAY_SIZE(pol)];
2015-12-06 16:24:21 +01:00
2015-12-15 11:55:50 +01:00
/*if (blob_parse(buf, pol, ARRAY_SIZE(pol), tb) != 0) {
2015-12-06 16:24:21 +01:00
fprintf(stderr, "Parse failed\n");
return;
}
if (tb[FOO_MESSAGE])
fprintf(stderr, "Message: %s\n", (char *) blobmsg_data(tb[FOO_MESSAGE]));
if (tb[FOO_LIST]) {
fprintf(stderr, "List: ");
2015-12-11 15:16:36 +01:00
dump_table(tb[FOO_LIST], blobmsg_data_len(tb[FOO_LIST]), 0, true);
2015-12-06 16:24:21 +01:00
}
if (tb[FOO_TESTDATA]) {
fprintf(stderr, "Testdata: ");
2015-12-11 15:16:36 +01:00
dump_table(tb[FOO_TESTDATA], blobmsg_data_len(tb[FOO_TESTDATA]), 0, false);
2015-12-15 11:55:50 +01:00
}*/
2015-12-06 16:24:21 +01:00
}
static void
fill_message(struct blob_buf *buf)
{
void *tbl;
2015-12-14 16:29:39 +01:00
//blob_buf_put_u32(buf, BLOB_ATTR_INT32, 0xbe);
//blob_buf_put_string(buf, BLOB_ATTR_STRING, "a string");
2015-12-16 15:24:40 +01:00
void *root = blob_buf_open_table(buf);
blob_buf_put_string(buf, "message");
blob_buf_put_string(buf, "Hello, world!");
blob_buf_put_string(buf, "testtable");
tbl = blob_buf_open_table(buf);
blob_buf_put_string(buf, "hello");
blob_buf_put_u32(buf, 1);
blob_buf_put_string(buf, "world");
blob_buf_put_string(buf, "2");
2015-12-15 11:55:50 +01:00
blob_buf_close_table(buf, tbl);
2015-12-06 16:24:21 +01:00
2015-12-16 15:24:40 +01:00
blob_buf_put_string(buf, "list");
tbl = blob_buf_open_array(buf);
blob_buf_put_u32(buf, 0);
blob_buf_put_u32(buf, 1);
blob_buf_put_u32(buf, 2);
blob_buf_put_float(buf, 0.123);
2015-12-15 11:55:50 +01:00
blob_buf_close_array(buf, tbl);
2015-12-15 11:55:50 +01:00
blob_buf_close_table(buf, root);
2015-12-06 16:24:21 +01:00
}
int main(int argc, char **argv)
{
struct blob_buf buf;
2015-12-06 20:54:58 +01:00
blob_buf_init(&buf, 0, 0);
2015-12-06 16:24:21 +01:00
for(int c = 0; c < 10; c++){
blob_buf_reset(&buf);
2015-12-06 16:24:21 +01:00
fill_message(&buf);
2015-12-14 16:29:39 +01:00
blob_buf_dump(&buf);
2015-12-06 16:24:21 +01:00
dump_message(&buf);
2015-12-15 11:55:50 +01:00
char *json = blob_buf_format_json(blob_buf_head(&buf), false);
2015-12-06 16:24:21 +01:00
printf("json: %s\n", json);
free(json);
}
2015-12-16 15:24:40 +01:00
blob_buf_reset(&buf);
//blob_buf_add_json_from_string(&buf, "{\"string\":\"Hello World\",\"array\":[1,2,3,4],\"object\":{\"foo\":\"bar\"}}");
blob_buf_add_json_from_string(&buf, "[123,2,3,4]");
blob_buf_dump(&buf);
2015-12-06 16:24:21 +01:00
2015-12-14 16:29:39 +01:00
fflush(stdout);
2015-12-06 16:24:21 +01:00
blob_buf_free(&buf);
return 0;
}