Sat, 25 Jan 2025 14:05:35 +0100
fix minor bugs in hash_key.h and streams.h docs
relates to #451
1145
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
1 | # Data Streams |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
2 | |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
3 | Stream copy functions provide a way to copy all - or a limited amount of - data from one stream to another. |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
4 | Since the read/write functions of a [UCX buffer](buffer.h.md) are fully compatible with stream read/write functions, |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
5 | you can, for example, easily transfer data from a file or network stream to a UCX buffer or vice versa. |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
6 | |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
7 | ## Overview |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
8 | ```C |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
9 | #include <cx/streams.h> |
1141 | 10 | |
1145
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
11 | size_t cx_stream_copy( |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
12 | void *src, void *dest, |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
13 | cx_read_func rfnc, cx_write_func wfnc |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
14 | ); |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
15 | size_t cx_stream_ncopy( |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
16 | void *src, void *dest, |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
17 | cx_read_func rfnc, cx_write_func wfnc, |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
18 | size_t n |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
19 | ); |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
20 | |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
21 | size_t cx_stream_bcopy( |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
22 | void *src, void *dest, |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
23 | cx_read_func rfnc, cx_write_func wfnc, |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
24 | char *buf, size_t bufsize |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
25 | ); |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
26 | size_t cx_stream_bncopy( |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
27 | void *src, void *dest, |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
28 | cx_read_func rfnc, cx_write_func wfnc, |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
29 | char *buf, size_t bufsize, |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
30 | size_t n |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
31 | ); |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
32 | ``` |
1141 | 33 | |
1145
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
34 | ## Description |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
35 | |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
36 | All functions in the stream copy family use the `rfnc` to read data from `src` |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
37 | and use the `wfnc` to write the data to `dest`. |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
38 | |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
39 | The `cx_stream_copy()` function always uses internal stack memory as a temporary buffer for the read bytes. |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
40 | The `cx_stream_bcopy()` function uses either a pre-initialized buffer `buf` of length `bufsize` |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
41 | or, if `buf` is `NULL`, an internal heap-allocated buffer. |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
42 | |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
43 | The `cx_stream_ncopy()` function behaves like `cx_stream_copy()` except, that it reads at most `n` bytes |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
44 | (and the same is true for `cx_stream_bncopy()` and `cx_stream_bcopy()`). |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
45 | |
1141 | 46 | |
1145
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
47 | <warning> |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
48 | When you are reading from a stream where you cannot track the position, there is the possibility that |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
49 | data gets lost when the destination does not accept all the bytes read from the source. |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
50 | While the stream copy functions do report how many bytes were <emphasis>successfully</emphasis> copied |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
51 | to the destination, this might - in certain cases - not be the exact number of read items. |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
52 | |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
53 | To mitigate the risk, you should make sure that the destination can always accept all read bytes and |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
54 | a possible bottleneck is only introduced by the source. |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
55 | </warning> |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
56 | |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
57 | > The size of the internal _stack_ buffer in `cx_stream_copy()` and `cx_stream_ncopy()` can be |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
58 | > set during compilation via the `CX_STREAM_COPY_BUF_SIZE` macro. |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
59 | > Similarly, the size for the implicitly allocated _heap_ buffer in can be |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
60 | > configured via the `CX_STREAM_BCOPY_BUF_SIZE` macro. |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
61 | > Refer to the [build instructions](install.md#compile-time-options) for the details. |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
62 | |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
63 | ## Example |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
64 | |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
65 | The following example shows, how to read the contents of a file into a buffer: |
1141 | 66 | ```c |
67 | FILE *inputfile = fopen(infilename, "r"); | |
68 | if (inputfile) { | |
69 | CxBuffer fbuf; | |
70 | cxBufferInit(&fbuf, NULL, 4096, NULL, CX_BUFFER_AUTO_EXTEND); | |
71 | cx_stream_copy(inputfile, &fbuf, | |
72 | (cx_read_func) fread, | |
73 | cxBufferWriteFunc); | |
74 | fclose(inputfile); | |
75 | ||
76 | // ... do something meaningful with the contents ... | |
77 | ||
78 | cxBufferDestroy(&fbuf); | |
79 | } else { | |
80 | perror("Error opening input file"); | |
81 | } | |
82 | ``` | |
1142
9437530176bc
add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents:
1141
diff
changeset
|
83 | |
1145
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
84 | |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
85 | <seealso> |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
86 | <category ref="apidoc"> |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
87 | <a href="https://ucx.sourceforge.io/api/streams_8h.html">streams.h</a> |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
88 | </category> |
1a8fe7b7dd8a
add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
89 | </seealso> |