libblobpack/examples/simple.c

195 lines
4.7 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)
2015-12-29 00:11:37 +01:00
static void dump_attr_data(struct blob_field *data, int indent, int next_indent);
2015-12-06 16:24:21 +01:00
static void
2015-12-29 00:11:37 +01:00
dump_table(struct blob_field *head, int len, int indent, bool array)
2015-12-06 16:24:21 +01:00
{
2015-12-29 00:11:37 +01:00
struct blob_field *attr;
2015-12-06 16:24:21 +01:00
indent_printf(indent, "{\n");
2015-12-29 00:11:37 +01:00
for(attr = blob_field_first_child(head); attr; attr = blob_field_next_child(head, attr)){
2015-12-16 15:24:40 +01:00
if (!array){
2015-12-29 00:11:37 +01:00
indent_printf(indent + 1, "%s : ", blob_field_get_string(attr));
attr = blob_field_next_child(head, attr);
2015-12-16 15:24:40 +01:00
}
2015-12-06 16:24:21 +01:00
dump_attr_data(attr, 0, indent + 1);
}
indent_printf(indent, "}\n");
}
2015-12-29 00:11:37 +01:00
static void dump_attr_data(struct blob_field *data, int indent, int next_indent)
2015-12-06 16:24:21 +01:00
{
2015-12-29 00:11:37 +01:00
int type = blob_field_type(data);
2015-12-06 16:24:21 +01:00
switch(type) {
2015-12-29 00:11:37 +01:00
case BLOB_FIELD_STRING:
indent_printf(indent, "%s\n", blob_field_get_string(data));
2015-12-06 16:24:21 +01:00
break;
2015-12-29 00:11:37 +01:00
case BLOB_FIELD_INT8:
case BLOB_FIELD_INT16:
case BLOB_FIELD_INT32:
case BLOB_FIELD_INT64:
indent_printf(indent, "%lli\n", blob_field_get_int(data));
2015-12-06 16:24:21 +01:00
break;
2015-12-29 00:11:37 +01:00
case BLOB_FIELD_FLOAT32:
case BLOB_FIELD_FLOAT64:
2016-01-11 09:39:05 +01:00
indent_printf(indent, "%Le\n", (long double) blob_field_get_real(data));
break;
2015-12-29 00:11:37 +01:00
case BLOB_FIELD_TABLE:
case BLOB_FIELD_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-29 00:11:37 +01:00
static const struct blob_field_policy pol[] = {
2015-12-06 16:24:21 +01:00
[FOO_MESSAGE] = {
.name = "message",
2015-12-29 00:11:37 +01:00
.type = BLOB_FIELD_STRING,
2015-12-06 16:24:21 +01:00
},
[FOO_LIST] = {
.name = "list",
2015-12-29 00:11:37 +01:00
.type = BLOB_FIELD_ARRAY,
2015-12-06 16:24:21 +01:00
},
[FOO_TESTDATA] = {
.name = "testdata",
2015-12-29 00:11:37 +01:00
.type = BLOB_FIELD_TABLE,
2015-12-06 16:24:21 +01:00
},
};
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#endif
2015-12-29 00:11:37 +01:00
static void dump_message(struct blob *buf)
2015-12-06 16:24:21 +01:00
{
2015-12-29 00:11:37 +01:00
//struct blob_field *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
2015-12-29 00:11:37 +01:00
fill_message(struct blob *buf)
2015-12-06 16:24:21 +01:00
{
void *tbl;
2015-12-29 00:11:37 +01:00
//blob_put_u32(buf, BLOB_FIELD_INT32, 0xbe);
//blob_put_string(buf, BLOB_FIELD_STRING, "a string");
2015-12-14 16:29:39 +01:00
2015-12-29 00:11:37 +01:00
void *root = blob_open_table(buf);
2015-12-16 15:24:40 +01:00
2015-12-29 00:11:37 +01:00
blob_put_string(buf, "message");
blob_put_string(buf, "Hello, world!");
2015-12-25 12:01:29 +01:00
2015-12-29 00:11:37 +01:00
blob_put_string(buf, "testtable");
tbl = blob_open_table(buf);
blob_put_string(buf, "hello");
blob_put_int(buf, 1);
blob_put_string(buf, "world");
blob_put_string(buf, "2");
blob_close_table(buf, tbl);
blob_close_table(buf, root);
2015-12-25 12:01:29 +01:00
2015-12-29 00:11:37 +01:00
blob_put_string(buf, "list");
tbl = blob_open_array(buf);
root = blob_open_table(buf);
blob_put_string(buf, "world");
blob_put_int(buf, 0);
blob_put_string(buf, "world");
blob_put_int(buf, 1);
blob_put_string(buf, "world");
blob_put_int(buf, 2);
blob_close_table(buf, root);
root = blob_open_table(buf);
blob_put_string(buf, "world");
blob_put_int(buf, 0);
blob_put_string(buf, "world");
blob_put_int(buf, 1);
blob_put_string(buf, "world");
blob_put_int(buf, 2);
blob_close_table(buf, root);
blob_close_array(buf, tbl);
2015-12-06 16:24:21 +01:00
}
int main(int argc, char **argv)
{
2015-12-29 00:11:37 +01:00
struct blob buf;
2015-12-06 16:24:21 +01:00
2015-12-29 00:11:37 +01:00
blob_init(&buf, 0, 0);
2015-12-06 16:24:21 +01:00
for(int c = 0; c < 10; c++){
2015-12-29 00:11:37 +01:00
blob_reset(&buf);
2015-12-06 16:24:21 +01:00
fill_message(&buf);
2015-12-25 12:01:29 +01:00
2015-12-29 00:11:37 +01:00
blob_dump(&buf);
2015-12-06 16:24:21 +01:00
dump_message(&buf);
2016-01-11 09:39:05 +01:00
char *json = blob_to_json(&buf);
2015-12-06 16:24:21 +01:00
printf("json: %s\n", json);
free(json);
2015-12-25 12:01:29 +01:00
const char *sig = "{sv}s[{si}]";
2015-12-29 00:11:37 +01:00
if(!blob_field_validate(blob_head(&buf), sig)) {
2015-12-25 12:01:29 +01:00
printf("invalid signature! %s\n", sig);
break;
} else {
printf("VALIDATION OK! %s\n", sig);
}
2015-12-06 16:24:21 +01:00
}
2015-12-16 15:24:40 +01:00
2015-12-29 00:11:37 +01:00
blob_reset(&buf);
const char *json = "{\"test\":[123,2,3,4,\"string\",{\"foo\":\"bar\"}],\"foo\":\"bar\",\"obj\":{\"arr\":[]},\"part\":1.4}";
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
blob_put_json(&buf, json);
clock_gettime(CLOCK_MONOTONIC, &end);
printf("encode json: %s\n", json);
printf("time taken %lums\n", (end.tv_nsec - start.tv_nsec) / 1000);
2015-12-29 00:11:37 +01:00
blob_dump(&buf);
blob_field_dump_json(blob_head(&buf));
// test the normal encoder/decoder
blob_reset(&buf);
clock_gettime(CLOCK_MONOTONIC, &start);
2016-01-11 09:39:05 +01:00
blob_put_json(&buf, json);
clock_gettime(CLOCK_MONOTONIC, &end);
printf("encode jsconc: %s\n", json);
printf("time taken %lums\n", (end.tv_nsec - start.tv_nsec) / 1000);
2015-12-14 16:29:39 +01:00
fflush(stdout);
2015-12-29 00:11:37 +01:00
blob_free(&buf);
2015-12-06 16:24:21 +01:00
return 0;
}