Wed, 02 Apr 2025 20:25:50 +0200
document buffer write functions
relates to #451
1143
0559812df10c
assign proper names to the documentation topics
Mike Becker <universe@uap-core.de>
parents:
1142
diff
changeset
|
1 | # Buffer |
1141 | 2 | |
1262
7cc81244b28a
document buffer read operations
Mike Becker <universe@uap-core.de>
parents:
1261
diff
changeset
|
3 | This buffer implementation can be used to read from or write to memory like you would do with a stream. |
1146
151c057faf7c
add marker to every incomplete page
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
4 | |
151c057faf7c
add marker to every incomplete page
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
5 | This allows the use of `cx_stream_copy()` (see [](streams.h.md)) to copy contents from one buffer to another, |
1190
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
6 | or from a file or network streams to the buffer and vice versa. |
1141 | 7 | |
1262
7cc81244b28a
document buffer read operations
Mike Becker <universe@uap-core.de>
parents:
1261
diff
changeset
|
8 | More features for convenient use of the buffer can be enabled, like automatic memory management, |
7cc81244b28a
document buffer read operations
Mike Becker <universe@uap-core.de>
parents:
1261
diff
changeset
|
9 | automatic resizing of the buffer space, or automatic flushing of contents. |
1141 | 10 | |
1262
7cc81244b28a
document buffer read operations
Mike Becker <universe@uap-core.de>
parents:
1261
diff
changeset
|
11 | The functions `cxBufferRead()` and `cxBufferWrite()` are `cx_read_func` and `cx_write_func` compatible, |
7cc81244b28a
document buffer read operations
Mike Becker <universe@uap-core.de>
parents:
1261
diff
changeset
|
12 | which in turn have a compatible signature to `fread()` and `fwrite()`. |
7cc81244b28a
document buffer read operations
Mike Becker <universe@uap-core.de>
parents:
1261
diff
changeset
|
13 | However, due to the different pointer type, the function pointers do not type check out of the box. |
7cc81244b28a
document buffer read operations
Mike Becker <universe@uap-core.de>
parents:
1261
diff
changeset
|
14 | For convenience, the macros `cxBufferReadFunc` and `cxBufferWriteFunc` are defined, which perform the necessary cast. |
1142
9437530176bc
add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents:
1141
diff
changeset
|
15 | |
1256
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
16 | ## Example |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
17 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
18 | <warning> |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
19 | TODO: add example |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
20 | </warning> |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
21 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
22 | ## Create |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
23 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
24 | ```C |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
25 | #include <cx/buffer.h> |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
26 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
27 | int cxBufferInit(CxBuffer *buffer, void *space, size_t capacity, |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
28 | const CxAllocator *allocator, int flags); |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
29 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
30 | CxBuffer *cxBufferCreate(void *space,size_t capacity, |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
31 | const CxAllocator *allocator, int flags); |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
32 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
33 | // available flags: |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
34 | #define CX_BUFFER_DEFAULT |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
35 | #define CX_BUFFER_FREE_CONTENTS |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
36 | #define CX_BUFFER_AUTO_EXTEND |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
37 | #define CX_BUFFER_COPY_ON_WRITE |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
38 | #define CX_BUFFER_COPY_ON_EXTEND |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
39 | ``` |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
40 | |
1264
8ff604356af7
document buffer create/init
Mike Becker <universe@uap-core.de>
parents:
1263
diff
changeset
|
41 | For creating a UCX buffer you have two options: initialize a pre-allocated structure, or allocate and initialize a new structure in one call. |
8ff604356af7
document buffer create/init
Mike Becker <universe@uap-core.de>
parents:
1263
diff
changeset
|
42 | |
8ff604356af7
document buffer create/init
Mike Becker <universe@uap-core.de>
parents:
1263
diff
changeset
|
43 | For the first option, you can call `cxBufferInit()` with the `buffer` argument pointing to an uninitialized `CxBuffer` structure. |
8ff604356af7
document buffer create/init
Mike Becker <universe@uap-core.de>
parents:
1263
diff
changeset
|
44 | You can pass a pre-allocated `space`, the desired `capacity`, and an `allocator`. |
8ff604356af7
document buffer create/init
Mike Becker <universe@uap-core.de>
parents:
1263
diff
changeset
|
45 | If `space` is `NULL`, the `allocator` is used to allocate enough space to match the desired `capacity`. |
8ff604356af7
document buffer create/init
Mike Becker <universe@uap-core.de>
parents:
1263
diff
changeset
|
46 | |
8ff604356af7
document buffer create/init
Mike Becker <universe@uap-core.de>
parents:
1263
diff
changeset
|
47 | The `flags` argument is a bit-wise-or combination of the constants listed below. |
8ff604356af7
document buffer create/init
Mike Becker <universe@uap-core.de>
parents:
1263
diff
changeset
|
48 | |
8ff604356af7
document buffer create/init
Mike Becker <universe@uap-core.de>
parents:
1263
diff
changeset
|
49 | The function `cxBufferCreate()` is equivalent, except that it uses the `allocator` to allocate a new `CxBuffer` structure, instead of initializing an existing one. |
8ff604356af7
document buffer create/init
Mike Becker <universe@uap-core.de>
parents:
1263
diff
changeset
|
50 | |
8ff604356af7
document buffer create/init
Mike Becker <universe@uap-core.de>
parents:
1263
diff
changeset
|
51 | If any allocation fails, `cxBufferInit()` returns non-zero, or `cxBufferCreate()` returns `NULL`, respectively. |
1256
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
52 | |
1263
f993745c60f0
document the buffer flags
Mike Becker <universe@uap-core.de>
parents:
1262
diff
changeset
|
53 | | Flag | Description | |
f993745c60f0
document the buffer flags
Mike Becker <universe@uap-core.de>
parents:
1262
diff
changeset
|
54 | |--------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
f993745c60f0
document the buffer flags
Mike Becker <universe@uap-core.de>
parents:
1262
diff
changeset
|
55 | | CX_BUFFER_DEFAULT | Alias for zero, meaning no flags are set. | |
f993745c60f0
document the buffer flags
Mike Becker <universe@uap-core.de>
parents:
1262
diff
changeset
|
56 | | CX_BUFFER_FREE_CONTENTS | When the buffer structure is [destroyed](#destroy), the memory for the contents is also automatically deallocated. | |
f993745c60f0
document the buffer flags
Mike Becker <universe@uap-core.de>
parents:
1262
diff
changeset
|
57 | | CX_BUFFER_AUTO_EXTEND | When a write operation would exceed the buffers capacity, the buffer is grown automatically with a call to `cxBufferMinimumCapacity()`. | |
f993745c60f0
document the buffer flags
Mike Becker <universe@uap-core.de>
parents:
1262
diff
changeset
|
58 | | CX_BUFFER_COPY_ON_WRITE | Any write operation will result in allocating a copy of the data, first. This is particularly useful, when a buffer is initialized with read-only memory. | |
f993745c60f0
document the buffer flags
Mike Becker <universe@uap-core.de>
parents:
1262
diff
changeset
|
59 | | CX_BUFFER_COPY_ON_EXTEND | Only when a write operation would exceed the buffers capacity, the copy-on-write operation is performed. This is useful, when a buffer is initialized with read-write memory which is allocated on the stack. | |
f993745c60f0
document the buffer flags
Mike Becker <universe@uap-core.de>
parents:
1262
diff
changeset
|
60 | |
f993745c60f0
document the buffer flags
Mike Becker <universe@uap-core.de>
parents:
1262
diff
changeset
|
61 | |
1256
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
62 | ## Destroy |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
63 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
64 | ```C |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
65 | #include <cx/buffer.h> |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
66 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
67 | void cxBufferDestroy(CxBuffer *buffer); |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
68 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
69 | void cxBufferFree(CxBuffer *buffer); |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
70 | ``` |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
71 | |
1259
7bc999fe285d
more documentation for buffer.h + set errno in cxBufferSeek() on invalid whence argument
Mike Becker <universe@uap-core.de>
parents:
1256
diff
changeset
|
72 | The above functions destroy the buffer and deallocate the buffer's memory when the `CX_BUFFER_FREE_CONTENTS` flag is set. |
7bc999fe285d
more documentation for buffer.h + set errno in cxBufferSeek() on invalid whence argument
Mike Becker <universe@uap-core.de>
parents:
1256
diff
changeset
|
73 | |
7bc999fe285d
more documentation for buffer.h + set errno in cxBufferSeek() on invalid whence argument
Mike Becker <universe@uap-core.de>
parents:
1256
diff
changeset
|
74 | The function `cxBufferDestroy()` is to be used when the buffer was initialized with `cxBufferInit()`, |
7bc999fe285d
more documentation for buffer.h + set errno in cxBufferSeek() on invalid whence argument
Mike Becker <universe@uap-core.de>
parents:
1256
diff
changeset
|
75 | and the function `cxBufferFree()` is to be used when the buffer was created with `cxBufferCreate()`. |
7bc999fe285d
more documentation for buffer.h + set errno in cxBufferSeek() on invalid whence argument
Mike Becker <universe@uap-core.de>
parents:
1256
diff
changeset
|
76 | The only difference is, that `cxBufferFree()` additionally deallocates the memory of the `CxBuffer` structure. |
1256
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
77 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
78 | ## Write |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
79 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
80 | ```C |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
81 | #include <cx/buffer.h> |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
82 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
83 | size_t cxBufferWrite(const void *ptr, size_t size, size_t nitems, |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
84 | CxBuffer *buffer); |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
85 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
86 | int cxBufferPut(CxBuffer *buffer, int c); |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
87 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
88 | size_t cxBufferPutString(CxBuffer *buffer, const char *str); |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
89 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
90 | int cxBufferTerminate(CxBuffer *buffer); |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
91 | |
1270
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
92 | size_t cxBufferAppend(const void *ptr, size_t size, size_t nitems, |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
93 | CxBuffer *buffer); |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
94 | |
1256
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
95 | int cxBufferMinimumCapacity(CxBuffer *buffer, size_t capacity); |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
96 | ``` |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
97 | |
1270
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
98 | The primary function for writing to a buffer is `cxBufferWrite()` |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
99 | which writes up to `nitems` with `size` bytes each from the memory pointed to by `ptr` to the buffer. |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
100 | |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
101 | When the capacity of the buffer is not sufficient and the `CX_BUFFER_AUTO_EXTEND` is not set in the buffer, |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
102 | items that do not fit into the buffer are discarded. |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
103 | The function always returns the actual number of items successfully written. |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
104 | This equals the number of bytes if and only if `size=1`. |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
105 | |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
106 | The function `cxBufferPut()` is a `putc()`-like wrapper for `cxBufferWrite()` which writes the character `c`, |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
107 | converted to an `unsigned char` to the buffer. |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
108 | Just like `putc()` this function returns the written character on success, and `EOF` on failure, |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
109 | but it does _not_ set `errno` on failure. |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
110 | |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
111 | The function `cxBufferPutString()` is a convenience function, |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
112 | that uses stdlib `strlen()` to compute the length of `str` and then invokes `cxBufferWrite()`. |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
113 | |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
114 | All of the above functions advance the buffers position by the number of bytes written, |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
115 | and cause the _size_ of the buffer to grow, if necessary, to contain all written bytes. |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
116 | On the other hand, `cxBufferTerminate()` writes a zero-byte at the current position, |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
117 | effectively creating a zero-terminated string whose size equals the buffer size. |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
118 | |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
119 | The function `cxBufferAppend()` writes the data to end of the buffer (given by its size) regardless of the current position, |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
120 | and it also does _not_ advance the position. |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
121 | If the write operation triggered a [flush](#flushing), however, the position will be shifted left alongside the shifted buffer contents. |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
122 | In case the data at which the current position points gets flushed, the new position will be zero. |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
123 | |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
124 | If you already (roughly) know how many bytes you will be writing to a buffer, |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
125 | you can save some allocations during auto-extension when you invoke `cxBufferMinimumCapacity()` before writing the data. |
503f0ad94c6c
document buffer write functions
Mike Becker <universe@uap-core.de>
parents:
1269
diff
changeset
|
126 | Usually you do not need to do this, unless you have many subsequence writes which impose the risk of multiple unnecessary reallocations. |
1256
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
127 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
128 | ## Read |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
129 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
130 | ```C |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
131 | #include <cx/buffer.h> |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
132 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
133 | size_t cxBufferRead(void *ptr, size_t size, size_t nitems, |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
134 | CxBuffer *buffer); |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
135 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
136 | int cxBufferGet(CxBuffer *buffer); |
1259
7bc999fe285d
more documentation for buffer.h + set errno in cxBufferSeek() on invalid whence argument
Mike Becker <universe@uap-core.de>
parents:
1256
diff
changeset
|
137 | |
7bc999fe285d
more documentation for buffer.h + set errno in cxBufferSeek() on invalid whence argument
Mike Becker <universe@uap-core.de>
parents:
1256
diff
changeset
|
138 | bool cxBufferEof(const CxBuffer *buffer); |
1256
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
139 | ``` |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
140 | |
1262
7cc81244b28a
document buffer read operations
Mike Becker <universe@uap-core.de>
parents:
1261
diff
changeset
|
141 | The function `cxBufferRead()` reads `nitems` number of items of `size` bytes each from the `buffer` |
7cc81244b28a
document buffer read operations
Mike Becker <universe@uap-core.de>
parents:
1261
diff
changeset
|
142 | and stores them into the memory pointed to by `ptr`, which must be sufficiently large to hold the contents. |
7cc81244b28a
document buffer read operations
Mike Becker <universe@uap-core.de>
parents:
1261
diff
changeset
|
143 | The function returns the actual bytes read, which might be lower, if the desired number of items is not available. |
7cc81244b28a
document buffer read operations
Mike Becker <universe@uap-core.de>
parents:
1261
diff
changeset
|
144 | |
7cc81244b28a
document buffer read operations
Mike Becker <universe@uap-core.de>
parents:
1261
diff
changeset
|
145 | The function `cxBufferGet()` is a `fgetc()`-like function which returns the next byte in the buffer converted to an `int`. |
7cc81244b28a
document buffer read operations
Mike Becker <universe@uap-core.de>
parents:
1261
diff
changeset
|
146 | Similar to `fgetc()` it returns `EOF` when there are no more bytes in the buffer. |
7cc81244b28a
document buffer read operations
Mike Becker <universe@uap-core.de>
parents:
1261
diff
changeset
|
147 | |
7cc81244b28a
document buffer read operations
Mike Becker <universe@uap-core.de>
parents:
1261
diff
changeset
|
148 | When all bytes from the buffer have been read, the function `cxBufferEof()` returns true. |
7cc81244b28a
document buffer read operations
Mike Becker <universe@uap-core.de>
parents:
1261
diff
changeset
|
149 | |
7cc81244b28a
document buffer read operations
Mike Becker <universe@uap-core.de>
parents:
1261
diff
changeset
|
150 | All read functions advance the position in the buffer by the number of read bytes. |
7cc81244b28a
document buffer read operations
Mike Becker <universe@uap-core.de>
parents:
1261
diff
changeset
|
151 | |
7cc81244b28a
document buffer read operations
Mike Becker <universe@uap-core.de>
parents:
1261
diff
changeset
|
152 | > When you want to read from a buffer that you previously used for writing, do not forget to set the position |
7cc81244b28a
document buffer read operations
Mike Becker <universe@uap-core.de>
parents:
1261
diff
changeset
|
153 | > in the buffer to zero, e.g. by calling `cxBufferSeek()`. |
1256
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
154 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
155 | ## Reset and Clear |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
156 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
157 | ```C |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
158 | #include <cx/buffer.h> |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
159 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
160 | void cxBufferReset(CxBuffer *buffer); |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
161 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
162 | void cxBufferClear(CxBuffer *buffer); |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
163 | ``` |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
164 | |
1261
6bbc308b7a20
add documentation for reset and clear
Mike Becker <universe@uap-core.de>
parents:
1260
diff
changeset
|
165 | The function `cxBufferReset()` sets both size and position of the buffer to zero, |
6bbc308b7a20
add documentation for reset and clear
Mike Becker <universe@uap-core.de>
parents:
1260
diff
changeset
|
166 | and `cxBufferClear()` additionally uses `memset()` to set every byte in the buffer to zero. |
6bbc308b7a20
add documentation for reset and clear
Mike Becker <universe@uap-core.de>
parents:
1260
diff
changeset
|
167 | |
6bbc308b7a20
add documentation for reset and clear
Mike Becker <universe@uap-core.de>
parents:
1260
diff
changeset
|
168 | > When clearing the buffer, only the "live" data, i.e. bytes with indices `[0..size)`, are cleared. |
6bbc308b7a20
add documentation for reset and clear
Mike Becker <universe@uap-core.de>
parents:
1260
diff
changeset
|
169 | > If you which to clear the entire buffer's memory, you would need to set the size to the capacity, first. |
6bbc308b7a20
add documentation for reset and clear
Mike Becker <universe@uap-core.de>
parents:
1260
diff
changeset
|
170 | |
6bbc308b7a20
add documentation for reset and clear
Mike Becker <universe@uap-core.de>
parents:
1260
diff
changeset
|
171 | > If the `CX_BUFFER_COPY_ON_WRITE` flag is set, `cxBufferClear()` behaves exactly like `cxBufferReset()`, |
6bbc308b7a20
add documentation for reset and clear
Mike Becker <universe@uap-core.de>
parents:
1260
diff
changeset
|
172 | > because writing to the contents is disallowed. |
6bbc308b7a20
add documentation for reset and clear
Mike Becker <universe@uap-core.de>
parents:
1260
diff
changeset
|
173 | >{style="note"} |
1256
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
174 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
175 | ## Random Access |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
176 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
177 | ```C |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
178 | #include <cx/buffer.h> |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
179 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
180 | int cxBufferSeek(CxBuffer *buffer, off_t offset, int whence); |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
181 | ``` |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
182 | |
1259
7bc999fe285d
more documentation for buffer.h + set errno in cxBufferSeek() on invalid whence argument
Mike Becker <universe@uap-core.de>
parents:
1256
diff
changeset
|
183 | The function `cxBufferSeek()` is a `fseek()`-like function that sets the current position in the buffer |
7bc999fe285d
more documentation for buffer.h + set errno in cxBufferSeek() on invalid whence argument
Mike Becker <universe@uap-core.de>
parents:
1256
diff
changeset
|
184 | relative to the start (when `whence` is `SEEK_SET`), the current position (when `whence` is `SEEK_CUR`), |
7bc999fe285d
more documentation for buffer.h + set errno in cxBufferSeek() on invalid whence argument
Mike Becker <universe@uap-core.de>
parents:
1256
diff
changeset
|
185 | or end (when `whence` is `SEEK_END`) of the buffer. |
7bc999fe285d
more documentation for buffer.h + set errno in cxBufferSeek() on invalid whence argument
Mike Becker <universe@uap-core.de>
parents:
1256
diff
changeset
|
186 | |
7bc999fe285d
more documentation for buffer.h + set errno in cxBufferSeek() on invalid whence argument
Mike Becker <universe@uap-core.de>
parents:
1256
diff
changeset
|
187 | If the resulting position is negative, or larger than the size (i.e. the first unused byte), |
7bc999fe285d
more documentation for buffer.h + set errno in cxBufferSeek() on invalid whence argument
Mike Becker <universe@uap-core.de>
parents:
1256
diff
changeset
|
188 | this function returns non-zero and sets `errno` to `EOVERFLOW`. |
7bc999fe285d
more documentation for buffer.h + set errno in cxBufferSeek() on invalid whence argument
Mike Becker <universe@uap-core.de>
parents:
1256
diff
changeset
|
189 | |
7bc999fe285d
more documentation for buffer.h + set errno in cxBufferSeek() on invalid whence argument
Mike Becker <universe@uap-core.de>
parents:
1256
diff
changeset
|
190 | > Note that the error behavior of `cxBufferSeek()` is not exactly the same as for POSIX `fseek()`. |
1256
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
191 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
192 | ## Shift Contents |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
193 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
194 | ```C |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
195 | #include <cx/buffer.h> |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
196 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
197 | int cxBufferShift(CxBuffer *buffer, off_t shift); |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
198 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
199 | int cxBufferShiftRight(CxBuffer *buffer, size_t shift); |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
200 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
201 | int cxBufferShiftLeft(CxBuffer *buffer, size_t shift); |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
202 | ``` |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
203 | |
1260
56a019cdb055
add documentation for the shift functions
Mike Becker <universe@uap-core.de>
parents:
1259
diff
changeset
|
204 | The function `cxBufferShift()` moves the contents within the buffer by the specified `shift` offset, |
56a019cdb055
add documentation for the shift functions
Mike Becker <universe@uap-core.de>
parents:
1259
diff
changeset
|
205 | where a negative offset means a shift to the left, and a positive offset means a shift to the right. |
56a019cdb055
add documentation for the shift functions
Mike Becker <universe@uap-core.de>
parents:
1259
diff
changeset
|
206 | It also adjusts the current position within the buffer, and in case of a right shift also the size, by the same offset. |
56a019cdb055
add documentation for the shift functions
Mike Becker <universe@uap-core.de>
parents:
1259
diff
changeset
|
207 | |
56a019cdb055
add documentation for the shift functions
Mike Becker <universe@uap-core.de>
parents:
1259
diff
changeset
|
208 | Data that is shift to the left, is always discarded when the new position of a byte would be smaller than zero. |
56a019cdb055
add documentation for the shift functions
Mike Becker <universe@uap-core.de>
parents:
1259
diff
changeset
|
209 | If the new position would be smaller than zero, it is set exactly to zero. |
56a019cdb055
add documentation for the shift functions
Mike Becker <universe@uap-core.de>
parents:
1259
diff
changeset
|
210 | |
56a019cdb055
add documentation for the shift functions
Mike Becker <universe@uap-core.de>
parents:
1259
diff
changeset
|
211 | When data is shift to the right, the behavior depends on the `CX_BUFFER_AUTO_EXTEND` flag. |
56a019cdb055
add documentation for the shift functions
Mike Becker <universe@uap-core.de>
parents:
1259
diff
changeset
|
212 | If set, the function extends the buffer's capacity before moving the data. |
56a019cdb055
add documentation for the shift functions
Mike Becker <universe@uap-core.de>
parents:
1259
diff
changeset
|
213 | Otherwise, the function discards all data that would exceed the buffer's capacity, and both the size and the position are equal to the capacity |
56a019cdb055
add documentation for the shift functions
Mike Becker <universe@uap-core.de>
parents:
1259
diff
changeset
|
214 | (which means, `cxBufferEof()` returns `true` after the operation). |
56a019cdb055
add documentation for the shift functions
Mike Becker <universe@uap-core.de>
parents:
1259
diff
changeset
|
215 | |
56a019cdb055
add documentation for the shift functions
Mike Becker <universe@uap-core.de>
parents:
1259
diff
changeset
|
216 | The functions `cxBufferShiftRight()` and `cxBufferShiftLeft()` accept a larger (but in both cases positive) shift offset, |
56a019cdb055
add documentation for the shift functions
Mike Becker <universe@uap-core.de>
parents:
1259
diff
changeset
|
217 | which usually does not make much sense on a 64-bit platform where `off_t` is already large enough to represent any reasonable offset. |
56a019cdb055
add documentation for the shift functions
Mike Becker <universe@uap-core.de>
parents:
1259
diff
changeset
|
218 | You may, however, still use those function to express more explicitly in your code in which direction you want the contents to be shifted. |
1256
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
219 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
220 | ## Flushing |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
221 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
222 | ```C |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
223 | #include <cx/buffer.h> |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
224 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
225 | typedef struct cx_buffer_flush_config_s { |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
226 | size_t threshold; |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
227 | size_t blksize; |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
228 | size_t blkmax; |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
229 | void *target; |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
230 | cx_write_func wfunc; |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
231 | } CxBufferFlushConfig; |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
232 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
233 | int cxBufferEnableFlushing(CxBuffer *buffer, |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
234 | CxBufferFlushConfig config); |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
235 | |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
236 | size_t cxBufferFlush(CxBuffer *buffer); |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
237 | ``` |
b9a940779877
define structure for buffer.h doc
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
238 | |
1269
e15ca477d448
document the flushing feature for the buffer
Mike Becker <universe@uap-core.de>
parents:
1264
diff
changeset
|
239 | With the function `cxBufferEnableFlushing()` you can configure a flushing strategy for the contents of the buffer. |
e15ca477d448
document the flushing feature for the buffer
Mike Becker <universe@uap-core.de>
parents:
1264
diff
changeset
|
240 | |
e15ca477d448
document the flushing feature for the buffer
Mike Becker <universe@uap-core.de>
parents:
1264
diff
changeset
|
241 | Flushing, once enabled, may happen in the following cases: |
e15ca477d448
document the flushing feature for the buffer
Mike Becker <universe@uap-core.de>
parents:
1264
diff
changeset
|
242 | 1. when data is written to the buffer, the capacity is insufficient, and `CX_BUFFER_AUTO_EXTEND` is _not_ enabled |
e15ca477d448
document the flushing feature for the buffer
Mike Becker <universe@uap-core.de>
parents:
1264
diff
changeset
|
243 | 2. when data is written to the buffer, and the required capacity exceeds the `threshold` configuration |
e15ca477d448
document the flushing feature for the buffer
Mike Becker <universe@uap-core.de>
parents:
1264
diff
changeset
|
244 | 3. when `cxBufferFlush()` is called explicitly |
e15ca477d448
document the flushing feature for the buffer
Mike Becker <universe@uap-core.de>
parents:
1264
diff
changeset
|
245 | |
e15ca477d448
document the flushing feature for the buffer
Mike Becker <universe@uap-core.de>
parents:
1264
diff
changeset
|
246 | > By combining the `CX_BUFFER_AUTO_EXTEND` flag and the `threshold` setting, |
e15ca477d448
document the flushing feature for the buffer
Mike Becker <universe@uap-core.de>
parents:
1264
diff
changeset
|
247 | > you can create a buffer that initially starts with a small capacity, grows up to a certain threshold, |
e15ca477d448
document the flushing feature for the buffer
Mike Becker <universe@uap-core.de>
parents:
1264
diff
changeset
|
248 | > and starts flushing only when that threshold is exceeded. |
e15ca477d448
document the flushing feature for the buffer
Mike Becker <universe@uap-core.de>
parents:
1264
diff
changeset
|
249 | |
e15ca477d448
document the flushing feature for the buffer
Mike Becker <universe@uap-core.de>
parents:
1264
diff
changeset
|
250 | Flushing happens by invoking the `wfunc` up to `blkmax` times, writing up to `blksize` bytes from the buffer to the `target` with each call. |
e15ca477d448
document the flushing feature for the buffer
Mike Becker <universe@uap-core.de>
parents:
1264
diff
changeset
|
251 | The target might not accept all bytes (i.e. the `wfunc` return value indicates that fewer items have been written than requested), |
e15ca477d448
document the flushing feature for the buffer
Mike Becker <universe@uap-core.de>
parents:
1264
diff
changeset
|
252 | in which case the remaining data remains in the buffer. |
e15ca477d448
document the flushing feature for the buffer
Mike Becker <universe@uap-core.de>
parents:
1264
diff
changeset
|
253 | That means, the buffer is effectively [shifted](#shift-contents) left by the number of successfully flushed bytes. |
e15ca477d448
document the flushing feature for the buffer
Mike Becker <universe@uap-core.de>
parents:
1264
diff
changeset
|
254 | |
e15ca477d448
document the flushing feature for the buffer
Mike Becker <universe@uap-core.de>
parents:
1264
diff
changeset
|
255 | > When you write large amounts of data to a buffer, multiple flush cycles might happen. |
e15ca477d448
document the flushing feature for the buffer
Mike Becker <universe@uap-core.de>
parents:
1264
diff
changeset
|
256 | > After the first flush operations completed, the reclaimed space in the buffer is filled first, but if that |
e15ca477d448
document the flushing feature for the buffer
Mike Becker <universe@uap-core.de>
parents:
1264
diff
changeset
|
257 | > is not sufficient, another flush may be triggered within the same invocation of the write operation. |
e15ca477d448
document the flushing feature for the buffer
Mike Becker <universe@uap-core.de>
parents:
1264
diff
changeset
|
258 | > |
e15ca477d448
document the flushing feature for the buffer
Mike Becker <universe@uap-core.de>
parents:
1264
diff
changeset
|
259 | > That means, as much data is written to the buffer and/or flushed as possible, until neither the flush target, nor the buffer accept more data. |
e15ca477d448
document the flushing feature for the buffer
Mike Becker <universe@uap-core.de>
parents:
1264
diff
changeset
|
260 | >{style="note"} |
e15ca477d448
document the flushing feature for the buffer
Mike Becker <universe@uap-core.de>
parents:
1264
diff
changeset
|
261 | |
e15ca477d448
document the flushing feature for the buffer
Mike Becker <universe@uap-core.de>
parents:
1264
diff
changeset
|
262 | > The function `cxBufferFlush()` simply returns zero when flushing was not enabled via `cxBufferEnableFlushing()`. |
1190
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
263 | |
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
264 | <seealso> |
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
265 | <category ref="apidoc"> |
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
266 | <a href="https://ucx.sourceforge.io/api/buffer_8h.html">buffer.h</a> |
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
267 | </category> |
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
268 | </seealso> |