-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #176 from PJK/streaming-enc
Improving streaming API docs
- Loading branch information
Showing
14 changed files
with
159 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
Streaming Encoding | ||
============================= | ||
|
||
`cbor/encoding.h <https://github.com/PJK/libcbor/blob/master/src/cbor/encoding.h>`_ | ||
exposes a low-level encoding API to encode CBOR objects on the fly. Unlike | ||
:func:`cbor_serialize`, these functions take logical values (integers, floats, | ||
strings, etc.) instead of :type:`cbor_item_t`. The client is responsible for | ||
constructing the compound types correctly (e.g. terminating arrays). | ||
|
||
Streaming encoding is typically used to create an streaming (indefinite length) CBOR :doc:`strings <type_2>`, :doc:`byte strings <type_3>`, :doc:`arrays <type_4>`, and :doc:`maps <type_5>`. Complete example: `examples/streaming_array.c <https://github.com/PJK/libcbor/blob/master/examples/streaming_array.c>`_ | ||
|
||
.. doxygenfunction:: cbor_encode_uint8 | ||
|
||
.. doxygenfunction:: cbor_encode_uint16 | ||
|
||
.. doxygenfunction:: cbor_encode_uint32 | ||
|
||
.. doxygenfunction:: cbor_encode_uint64 | ||
|
||
.. doxygenfunction:: cbor_encode_uint | ||
|
||
.. doxygenfunction:: cbor_encode_negint8 | ||
|
||
.. doxygenfunction:: cbor_encode_negint16 | ||
|
||
.. doxygenfunction:: cbor_encode_negint32 | ||
|
||
.. doxygenfunction:: cbor_encode_negint64 | ||
|
||
.. doxygenfunction:: cbor_encode_negint | ||
|
||
.. doxygenfunction:: cbor_encode_bytestring_start | ||
|
||
.. doxygenfunction:: cbor_encode_indef_bytestring_start | ||
|
||
.. doxygenfunction:: cbor_encode_string_start | ||
|
||
.. doxygenfunction:: cbor_encode_indef_string_start | ||
|
||
.. doxygenfunction:: cbor_encode_array_start | ||
|
||
.. doxygenfunction:: cbor_encode_indef_array_start | ||
|
||
.. doxygenfunction:: cbor_encode_map_start | ||
|
||
.. doxygenfunction:: cbor_encode_indef_map_start | ||
|
||
.. doxygenfunction:: cbor_encode_tag | ||
|
||
.. doxygenfunction:: cbor_encode_bool | ||
|
||
.. doxygenfunction:: cbor_encode_null | ||
|
||
.. doxygenfunction:: cbor_encode_undef | ||
|
||
.. doxygenfunction:: cbor_encode_half | ||
|
||
.. doxygenfunction:: cbor_encode_single | ||
|
||
.. doxygenfunction:: cbor_encode_double | ||
|
||
.. doxygenfunction:: cbor_encode_break | ||
|
||
.. doxygenfunction:: cbor_encode_ctrl | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,6 @@ Contents | |
getting_started | ||
using | ||
api | ||
streaming | ||
tests | ||
rfc_conformance | ||
internal | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* 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 <stdlib.h> | ||
#include "cbor.h" | ||
|
||
void usage() { | ||
printf("Usage: streaming_array <N>\n"); | ||
printf("Prints out serialized array [0, ..., N-1]\n"); | ||
exit(1); | ||
} | ||
|
||
#define BUFFER_SIZE 8 | ||
unsigned char buffer[BUFFER_SIZE]; | ||
FILE* out; | ||
|
||
void flush(size_t bytes) { | ||
if (bytes == 0) exit(1); // All items should be successfully encoded | ||
if (fwrite(buffer, sizeof(unsigned char), bytes, out) != bytes) exit(1); | ||
if (fflush(out)) exit(1); | ||
} | ||
|
||
/* | ||
* Example of using the streaming encoding API to create an array of integers | ||
* on the fly. Notice that a partial output is produced with every element. | ||
*/ | ||
int main(int argc, char* argv[]) { | ||
if (argc != 2) usage(); | ||
long n = strtol(argv[1], NULL, 10); | ||
out = freopen(NULL, "wb", stdout); | ||
if (!out) exit(1); | ||
|
||
// Start an indefinite-length array | ||
flush(cbor_encode_indef_array_start(buffer, BUFFER_SIZE)); | ||
// Write the array items one by one | ||
for (size_t i = 0; i < n; i++) { | ||
flush(cbor_encode_uint32(i, buffer, BUFFER_SIZE)); | ||
} | ||
// Close the array | ||
flush(cbor_encode_break(buffer, BUFFER_SIZE)); | ||
|
||
if (fclose(out)) exit(1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters