Home | History | Annotate | Line # | Download | only in examples
      1  1.1  christos /*
      2  1.1  christos  * Copyright (c) 2014-2019 Pavel Kalvoda <me (at) pavelkalvoda.com>
      3  1.1  christos  *
      4  1.1  christos  * libcbor is free software; you can redistribute it and/or modify
      5  1.1  christos  * it under the terms of the MIT license. See LICENSE for details.
      6  1.1  christos  */
      7  1.1  christos 
      8  1.1  christos #include <stdio.h>
      9  1.1  christos #include "cbor.h"
     10  1.1  christos 
     11  1.1  christos int main(int argc, char* argv[]) {
     12  1.1  christos   /* Preallocate the map structure */
     13  1.1  christos   cbor_item_t* root = cbor_new_definite_map(2);
     14  1.1  christos   /* Add the content */
     15  1.1  christos   cbor_map_add(root,
     16  1.1  christos                (struct cbor_pair){
     17  1.1  christos                    .key = cbor_move(cbor_build_string("Is CBOR awesome?")),
     18  1.1  christos                    .value = cbor_move(cbor_build_bool(true))});
     19  1.1  christos   cbor_map_add(root,
     20  1.1  christos                (struct cbor_pair){
     21  1.1  christos                    .key = cbor_move(cbor_build_uint8(42)),
     22  1.1  christos                    .value = cbor_move(cbor_build_string("Is the answer"))});
     23  1.1  christos   /* Output: `length` bytes of data in the `buffer` */
     24  1.1  christos   unsigned char* buffer;
     25  1.1  christos   size_t buffer_size,
     26  1.1  christos       length = cbor_serialize_alloc(root, &buffer, &buffer_size);
     27  1.1  christos 
     28  1.1  christos   fwrite(buffer, 1, length, stdout);
     29  1.1  christos   free(buffer);
     30  1.1  christos 
     31  1.1  christos   fflush(stdout);
     32  1.1  christos   cbor_decref(&root);
     33  1.1  christos }
     34