freebsd-dev/contrib/libcbor/examples/create_items.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

35 lines
1.0 KiB
C
Raw Normal View History

2021-10-01 23:46:00 +00:00
/*
* Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
*
* libcbor is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <stdio.h>
#include "cbor.h"
2023-04-20 23:17:42 +00:00
int main(void) {
2021-10-01 23:46:00 +00:00
/* Preallocate the map structure */
cbor_item_t* root = cbor_new_definite_map(2);
/* Add the content */
2023-04-20 23:17:42 +00:00
bool success = cbor_map_add(
root, (struct cbor_pair){
.key = cbor_move(cbor_build_string("Is CBOR awesome?")),
.value = cbor_move(cbor_build_bool(true))});
success &= cbor_map_add(
root, (struct cbor_pair){
.key = cbor_move(cbor_build_uint8(42)),
.value = cbor_move(cbor_build_string("Is the answer"))});
if (!success) return 1;
2021-10-01 23:46:00 +00:00
/* Output: `length` bytes of data in the `buffer` */
unsigned char* buffer;
2023-04-20 23:17:42 +00:00
size_t buffer_size;
cbor_serialize_alloc(root, &buffer, &buffer_size);
2021-10-01 23:46:00 +00:00
2023-04-20 23:17:42 +00:00
fwrite(buffer, 1, buffer_size, stdout);
2021-10-01 23:46:00 +00:00
free(buffer);
fflush(stdout);
cbor_decref(&root);
}