Wed, 31 Dec 2025 16:40:12 +0100
update ucx to version 4.0
| 174 | 1 | /* |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
| 3 | * | |
| 4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * | |
| 9 | * 1. Redistributions of source code must retain the above copyright | |
| 10 | * notice, this list of conditions and the following disclaimer. | |
| 11 | * | |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 13 | * notice, this list of conditions and the following disclaimer in the | |
| 14 | * documentation and/or other materials provided with the distribution. | |
| 15 | * | |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 26 | * POSSIBILITY OF SUCH DAMAGE. | |
| 27 | */ | |
| 28 | ||
| 29 | /** | |
| 440 | 30 | * @file buffer.h |
| 174 | 31 | * |
| 440 | 32 | * @brief Advanced buffer implementation. |
| 174 | 33 | * |
| 34 | * Instances of CxBuffer can be used to read from or to write to like one | |
| 35 | * would do with a stream. | |
| 36 | * | |
| 37 | * Some features for convenient use of the buffer | |
| 38 | * can be enabled. See the documentation of the macro constants for more | |
| 39 | * information. | |
| 40 | * | |
| 440 | 41 | * @author Mike Becker |
| 42 | * @author Olaf Wintermann | |
| 43 | * @copyright 2-Clause BSD License | |
| 174 | 44 | */ |
| 45 | ||
| 46 | #ifndef UCX_BUFFER_H | |
| 47 | #define UCX_BUFFER_H | |
| 48 | ||
| 49 | #include "common.h" | |
| 50 | #include "allocator.h" | |
| 995 | 51 | #include "string.h" |
| 174 | 52 | |
| 53 | /** | |
| 54 | * No buffer features enabled (all flags cleared). | |
| 55 | */ | |
| 56 | #define CX_BUFFER_DEFAULT 0x00 | |
| 57 | ||
| 58 | /** | |
| 59 | * If this flag is enabled, the buffer will automatically free its contents when destroyed. | |
| 440 | 60 | * |
| 61 | * Do NOT set this flag together with #CX_BUFFER_COPY_ON_WRITE. It will be automatically | |
| 845 | 62 | * set when the copy-on-write operation is performed. |
| 174 | 63 | */ |
| 64 | #define CX_BUFFER_FREE_CONTENTS 0x01 | |
| 65 | ||
| 66 | /** | |
| 440 | 67 | * If this flag is enabled, the buffer will automatically extend its capacity. |
| 174 | 68 | */ |
| 69 | #define CX_BUFFER_AUTO_EXTEND 0x02 | |
| 70 | ||
| 440 | 71 | /** |
| 72 | * If this flag is enabled, the buffer will allocate new memory when written to. | |
| 73 | * | |
| 845 | 74 | * The current contents of the buffer will be copied to the new memory, and the flag |
| 440 | 75 | * will be cleared while the #CX_BUFFER_FREE_CONTENTS flag will be set automatically. |
| 76 | */ | |
| 77 | #define CX_BUFFER_COPY_ON_WRITE 0x04 | |
| 78 | ||
| 79 | /** | |
| 80 | * If this flag is enabled, the buffer will copy its contents to a new memory area on reallocation. | |
| 81 | * | |
| 82 | * After performing the copy, the flag is automatically cleared. | |
| 83 | * This flag has no effect on buffers which do not have #CX_BUFFER_AUTO_EXTEND set, which is why | |
| 84 | * buffers automatically admit the auto-extend flag when initialized with copy-on-extend enabled. | |
| 85 | */ | |
| 86 | #define CX_BUFFER_COPY_ON_EXTEND 0x08 | |
| 87 | ||
| 88 | /** | |
| 995 | 89 | * If this flag is enabled, the buffer will never free its contents regardless of #CX_BUFFER_FREE_CONTENTS. |
| 90 | * | |
| 91 | * This is useful, for example, when you want to keep a pointer to the data after destroying the buffer. | |
| 92 | */ | |
| 93 | #define CX_BUFFER_DO_NOT_FREE 0x10 | |
| 94 | ||
| 95 | /** | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
96 | * Function pointer for cxBufferWrite that is compatible with cx_write_func. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
97 | * @see cx_write_func |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
98 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
99 | #define cxBufferWriteFunc ((cx_write_func) cxBufferWrite) |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
100 | /** |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
101 | * Function pointer for cxBufferRead that is compatible with cx_read_func. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
102 | * @see cx_read_func |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
103 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
104 | #define cxBufferReadFunc ((cx_read_func) cxBufferRead) |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
105 | |
| 440 | 106 | |
| 174 | 107 | /** Structure for the UCX buffer data. */ |
| 440 | 108 | struct cx_buffer_s { |
| 174 | 109 | /** A pointer to the buffer contents. */ |
| 110 | union { | |
| 111 | /** | |
| 112 | * Data is interpreted as text. | |
| 113 | */ | |
| 114 | char *space; | |
| 115 | /** | |
| 116 | * Data is interpreted as binary. | |
| 117 | */ | |
| 118 | unsigned char *bytes; | |
| 119 | }; | |
| 120 | /** The allocator to use for automatic memory management. */ | |
| 324 | 121 | const CxAllocator *allocator; |
| 174 | 122 | /** Current position of the buffer. */ |
| 123 | size_t pos; | |
| 124 | /** Current capacity (i.e. maximum size) of the buffer. */ | |
| 125 | size_t capacity; | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
126 | /** Maximum capacity that this buffer may grow to. */ |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
127 | size_t max_capacity; |
| 174 | 128 | /** Current size of the buffer content. */ |
| 129 | size_t size; | |
| 130 | /** | |
| 131 | * Flag register for buffer features. | |
| 132 | * @see #CX_BUFFER_DEFAULT | |
| 133 | * @see #CX_BUFFER_FREE_CONTENTS | |
| 134 | * @see #CX_BUFFER_AUTO_EXTEND | |
| 440 | 135 | * @see #CX_BUFFER_COPY_ON_WRITE |
| 174 | 136 | */ |
| 137 | int flags; | |
| 440 | 138 | }; |
| 174 | 139 | |
| 140 | /** | |
| 141 | * UCX buffer. | |
| 142 | */ | |
| 440 | 143 | typedef struct cx_buffer_s CxBuffer; |
| 174 | 144 | |
| 145 | /** | |
| 146 | * Initializes a fresh buffer. | |
| 147 | * | |
| 440 | 148 | * You may also provide a read-only @p space, in which case |
| 149 | * you will need to cast the pointer, and you should set the | |
| 150 | * #CX_BUFFER_COPY_ON_WRITE flag. | |
| 151 | * | |
| 845 | 152 | * You need to set the size manually after initialization if |
| 440 | 153 | * you provide @p space which already contains data. |
| 154 | * | |
| 155 | * When you specify stack memory as @p space and decide to use | |
| 156 | * the auto-extension feature, you @em must use the | |
| 157 | * #CX_BUFFER_COPY_ON_EXTEND flag, instead of the | |
| 158 | * #CX_BUFFER_AUTO_EXTEND flag. | |
| 159 | * | |
| 845 | 160 | * @note You may provide @c NULL as the argument for @p space. |
| 174 | 161 | * Then this function will allocate the space and enforce |
| 440 | 162 | * the #CX_BUFFER_FREE_CONTENTS flag. In that case, specifying |
| 163 | * copy-on-write should be avoided, because the allocated | |
| 164 | * space will be leaking after the copy-on-write operation. | |
| 174 | 165 | * |
| 166 | * @param buffer the buffer to initialize | |
| 1016 | 167 | * @param allocator the allocator this buffer shall use for automatic |
| 168 | * memory management | |
| 169 | * (if @c NULL, the cxDefaultAllocator will be used) | |
| 440 | 170 | * @param space pointer to the memory area, or @c NULL to allocate |
| 174 | 171 | * new memory |
| 172 | * @param capacity the capacity of the buffer | |
| 173 | * @param flags buffer features (see cx_buffer_s.flags) | |
| 174 | * @return zero on success, non-zero if a required allocation failed | |
| 175 | */ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
176 | CX_EXTERN CX_NONNULL_ARG(1) |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
177 | int cxBufferInit(CxBuffer *buffer, const CxAllocator *allocator, |
| 1016 | 178 | void *space, size_t capacity, int flags); |
| 174 | 179 | |
| 180 | /** | |
| 181 | * Destroys the buffer contents. | |
| 182 | * | |
| 183 | * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled. | |
| 184 | * If you want to free the memory of the entire buffer, use cxBufferFree(). | |
| 185 | * | |
| 186 | * @param buffer the buffer which contents shall be destroyed | |
| 187 | * @see cxBufferInit() | |
| 188 | */ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
189 | CX_EXTERN CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
190 | void cxBufferDestroy(CxBuffer *buffer); |
| 174 | 191 | |
| 192 | /** | |
| 193 | * Deallocates the buffer. | |
| 194 | * | |
| 195 | * If the #CX_BUFFER_FREE_CONTENTS feature is enabled, this function also destroys | |
| 440 | 196 | * the contents. If you @em only want to destroy the contents, use cxBufferDestroy(). |
| 197 | * | |
| 174 | 198 | * @param buffer the buffer to deallocate |
| 199 | * @see cxBufferCreate() | |
| 200 | */ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
201 | CX_EXTERN |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
202 | void cxBufferFree(CxBuffer *buffer); |
| 174 | 203 | |
| 204 | /** | |
| 440 | 205 | * Allocates and initializes a fresh buffer. |
| 206 | * | |
| 207 | * You may also provide a read-only @p space, in which case | |
| 208 | * you will need to cast the pointer, and you should set the | |
| 209 | * #CX_BUFFER_COPY_ON_WRITE flag. | |
| 210 | * When you specify stack memory as @p space and decide to use | |
| 211 | * the auto-extension feature, you @em must use the | |
| 212 | * #CX_BUFFER_COPY_ON_EXTEND flag, instead of the | |
| 213 | * #CX_BUFFER_AUTO_EXTEND flag. | |
| 214 | * | |
| 845 | 215 | * @note You may provide @c NULL as the argument for @p space. |
| 440 | 216 | * Then this function will allocate the space and enforce |
| 217 | * the #CX_BUFFER_FREE_CONTENTS flag. | |
| 218 | * | |
| 1016 | 219 | * @param allocator the allocator to use for allocating the structure and the automatic |
| 220 | * memory management within the buffer | |
| 221 | * (if @c NULL, the cxDefaultAllocator will be used) | |
| 440 | 222 | * @param space pointer to the memory area, or @c NULL to allocate |
| 223 | * new memory | |
| 224 | * @param capacity the capacity of the buffer | |
| 225 | * @param flags buffer features (see cx_buffer_s.flags) | |
| 226 | * @return a pointer to the buffer on success, @c NULL if a required allocation failed | |
| 227 | */ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
228 | CX_EXTERN CX_MALLOC CX_DEALLOC(cxBufferFree, 1) CX_NODISCARD |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
229 | CxBuffer *cxBufferCreate(const CxAllocator *allocator, void *space, |
| 1016 | 230 | size_t capacity, int flags); |
| 440 | 231 | |
| 232 | /** | |
| 174 | 233 | * Shifts the contents of the buffer by the given offset. |
| 234 | * | |
| 235 | * If the offset is positive, the contents are shifted to the right. | |
| 236 | * If auto extension is enabled, the buffer grows, if necessary. | |
| 237 | * In case the auto extension fails, this function returns a non-zero value and | |
| 238 | * no contents are changed. | |
| 845 | 239 | * When the auto extension is disabled, the contents that do not fit into the |
| 240 | * buffer are discarded. | |
| 174 | 241 | * |
| 242 | * If the offset is negative, the contents are shifted to the left where the | |
| 440 | 243 | * first @p shift bytes are discarded. |
| 174 | 244 | * The new size of the buffer is the old size minus the absolute shift value. |
| 245 | * If this value is larger than the buffer size, the buffer is emptied (but | |
| 246 | * not cleared, see the security note below). | |
| 247 | * | |
| 845 | 248 | * The buffer position gets shifted alongside the content but is kept |
| 174 | 249 | * within the boundaries of the buffer. |
| 250 | * | |
| 440 | 251 | * @note For situations where @c off_t is not large enough, there are specialized cxBufferShiftLeft() and |
| 845 | 252 | * cxBufferShiftRight() functions using a @c size_t as the parameter type. |
| 174 | 253 | * |
| 440 | 254 | * @attention |
| 255 | * Security Note: The shifting operation does @em not erase the previously occupied memory cells. | |
| 845 | 256 | * But you can do that manually by calling |
| 174 | 257 | * <code>memset(buffer->bytes, 0, shift)</code> for a right shift or |
| 258 | * <code>memset(buffer->bytes + buffer->size, 0, buffer->capacity - buffer->size)</code> | |
| 259 | * for a left shift. | |
| 260 | * | |
| 261 | * @param buffer the buffer | |
| 262 | * @param shift the shift offset (negative means left shift) | |
| 440 | 263 | * @retval zero success |
| 264 | * @retval non-zero if a required auto-extension or copy-on-write fails | |
| 265 | * @see cxBufferShiftLeft() | |
| 266 | * @see cxBufferShiftRight() | |
| 174 | 267 | */ |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
268 | CX_EXTERN CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
269 | int cxBufferShift(CxBuffer *buffer, off_t shift); |
| 174 | 270 | |
| 271 | /** | |
| 272 | * Shifts the buffer to the right. | |
| 273 | * See cxBufferShift() for details. | |
| 274 | * | |
| 275 | * @param buffer the buffer | |
| 276 | * @param shift the shift offset | |
| 440 | 277 | * @retval zero success |
| 278 | * @retval non-zero if a required auto-extension or copy-on-write fails | |
| 174 | 279 | * @see cxBufferShift() |
| 280 | */ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
281 | CX_EXTERN CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
282 | int cxBufferShiftRight(CxBuffer *buffer, size_t shift); |
| 174 | 283 | |
| 284 | /** | |
| 285 | * Shifts the buffer to the left. | |
| 286 | * See cxBufferShift() for details. | |
| 287 | * | |
| 288 | * @param buffer the buffer | |
| 289 | * @param shift the positive shift offset | |
| 440 | 290 | * @retval zero success |
| 291 | * @retval non-zero if the buffer uses copy-on-write and the allocation fails | |
| 174 | 292 | * @see cxBufferShift() |
| 293 | */ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
294 | CX_EXTERN CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
295 | int cxBufferShiftLeft(CxBuffer *buffer, size_t shift); |
| 174 | 296 | |
| 297 | ||
| 298 | /** | |
| 299 | * Moves the position of the buffer. | |
| 300 | * | |
| 440 | 301 | * The new position is relative to the @p whence argument. |
| 174 | 302 | * |
| 440 | 303 | * @li @c SEEK_SET marks the start of the buffer. |
| 304 | * @li @c SEEK_CUR marks the current position. | |
| 305 | * @li @c SEEK_END marks the end of the buffer. | |
| 174 | 306 | * |
| 307 | * With an offset of zero, this function sets the buffer position to zero | |
| 440 | 308 | * (@c SEEK_SET), the buffer size (@c SEEK_END) or leaves the buffer position |
| 309 | * unchanged (@c SEEK_CUR). | |
| 174 | 310 | * |
| 311 | * @param buffer the buffer | |
| 440 | 312 | * @param offset position offset relative to @p whence |
| 313 | * @param whence one of @c SEEK_SET, @c SEEK_CUR or @c SEEK_END | |
| 314 | * @retval zero success | |
| 315 | * @retval non-zero if the position is invalid | |
| 174 | 316 | * |
| 317 | */ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
318 | CX_EXTERN CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
319 | int cxBufferSeek(CxBuffer *buffer, off_t offset, int whence); |
| 174 | 320 | |
| 321 | /** | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
322 | * Discards items from the end of the buffer. |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
323 | * |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
324 | * When the current position points to a byte that gets discarded, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
325 | * the position is set to the buffer size. |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
326 | * |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
327 | * @param buffer the buffer |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
328 | * @param size the size of one item |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
329 | * @param nitems the number of items to discard |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
330 | * @return the actual number of discarded items |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
331 | */ |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
332 | CX_EXTERN CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
333 | size_t cxBufferPop(CxBuffer *buffer, size_t size, size_t nitems); |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
334 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
335 | /** |
| 174 | 336 | * Clears the buffer by resetting the position and deleting the data. |
| 337 | * | |
| 338 | * The data is deleted by zeroing it with a call to memset(). | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
339 | * If you do not need that, you can use the faster cxBufferReset(). |
| 174 | 340 | * |
| 440 | 341 | * @note If the #CX_BUFFER_COPY_ON_WRITE flag is set, this function |
| 342 | * will not erase the data and behave exactly as cxBufferReset(). | |
| 343 | * | |
| 174 | 344 | * @param buffer the buffer to be cleared |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
345 | * @see cxBufferReset() |
| 174 | 346 | */ |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
347 | CX_EXTERN CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
348 | void cxBufferClear(CxBuffer *buffer); |
| 174 | 349 | |
| 350 | /** | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
351 | * Resets the buffer by resetting the position and size to zero. |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
352 | * |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
353 | * The data in the buffer is not deleted. If you need a safe |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
354 | * reset of the buffer, use cxBufferClear(). |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
355 | * |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
356 | * @param buffer the buffer to be cleared |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
357 | * @see cxBufferClear() |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
358 | */ |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
359 | CX_EXTERN CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
360 | void cxBufferReset(CxBuffer *buffer); |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
361 | |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
362 | /** |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
363 | * Tests, if the buffer position has exceeded the buffer size. |
| 174 | 364 | * |
| 365 | * @param buffer the buffer to test | |
| 440 | 366 | * @retval true if the current buffer position has exceeded the last |
| 367 | * byte of the buffer's contents | |
| 368 | * @retval false otherwise | |
| 174 | 369 | */ |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
370 | CX_EXTERN CX_NONNULL CX_NODISCARD |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
371 | bool cxBufferEof(const CxBuffer *buffer); |
| 174 | 372 | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
373 | /** |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
374 | * Ensures that the buffer has the required capacity. |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
375 | * |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
376 | * If the current capacity is not sufficient, the buffer will be extended. |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
377 | * If the current capacity is larger, the buffer is shrunk and superfluous |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
378 | * content is discarded. |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
379 | * |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
380 | * This function will reserve no more bytes than requested, in contrast to |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
381 | * cxBufferMinimumCapacity(), which may reserve more bytes to improve the |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
382 | * number of future necessary reallocations. |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
383 | * |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
384 | * @param buffer the buffer |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
385 | * @param capacity the required capacity for this buffer |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
386 | * @retval zero on success |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
387 | * @retval non-zero on allocation failure |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
388 | * @see cxBufferShrink() |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
389 | * @see cxBufferMinimumCapacity() |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
390 | */ |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
391 | CX_EXTERN CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
392 | int cxBufferReserve(CxBuffer *buffer, size_t capacity); |
| 174 | 393 | |
| 394 | /** | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
395 | * Limits the buffer's capacity. |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
396 | * |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
397 | * If the current capacity is already larger, this function fails and returns |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
398 | * non-zero. |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
399 | * |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
400 | * The capacity limit will affect auto-extension features, as well as future |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
401 | * calls to cxBufferMinimumCapacity() and cxBufferReserve(). |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
402 | * |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
403 | * @param buffer the buffer |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
404 | * @param capacity the maximum allowed capacity for this buffer |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
405 | * @retval zero the limit is applied |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
406 | * @retval non-zero the new limit is smaller than the current capacity |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
407 | * @see cxBufferReserve() |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
408 | * @see cxBufferMinimumCapacity() |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
409 | */ |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
410 | CX_EXTERN CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
411 | int cxBufferMaximumCapacity(CxBuffer *buffer, size_t capacity); |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
412 | |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
413 | /** |
| 174 | 414 | * Ensures that the buffer has a minimum capacity. |
| 415 | * | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
416 | * If the current capacity is not sufficient, the buffer will be generously |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
417 | * extended. |
| 174 | 418 | * |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
419 | * The new capacity will be a power of two until the system's page size is reached. |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
420 | * Then, the new capacity will be a multiple of the page size. |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
421 | * |
| 174 | 422 | * @param buffer the buffer |
| 423 | * @param capacity the minimum required capacity for this buffer | |
| 440 | 424 | * @retval zero the capacity was already sufficient or successfully increased |
| 425 | * @retval non-zero on allocation failure | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
426 | * @see cxBufferMaximumCapacity() |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
427 | * @see cxBufferReserve() |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
428 | * @see cxBufferShrink() |
| 174 | 429 | */ |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
430 | CX_EXTERN CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
431 | int cxBufferMinimumCapacity(CxBuffer *buffer, size_t capacity); |
| 174 | 432 | |
| 433 | /** | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
434 | * Shrinks the capacity of the buffer to fit its current size. |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
435 | * |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
436 | * If @p reserve is larger than zero, the buffer is shrunk to its size plus |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
437 | * the number of reserved bytes. |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
438 | * |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
439 | * If the current capacity is not larger than the size plus the reserved bytes, |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
440 | * nothing happens. |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
441 | * |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
442 | * If the #CX_BUFFER_COPY_ON_WRITE or #CX_BUFFER_COPY_ON_EXTEND flag is set, |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
443 | * this function does nothing. |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
444 | * |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
445 | * @param buffer the buffer |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
446 | * @param reserve the number of bytes that shall remain reserved |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
447 | * @see cxBufferReserve() |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
448 | * @see cxBufferMinimumCapacity() |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
449 | */ |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
450 | CX_EXTERN CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
451 | void cxBufferShrink(CxBuffer *buffer, size_t reserve); |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
452 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
453 | /** |
| 174 | 454 | * Writes data to a CxBuffer. |
| 455 | * | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
456 | * If auto-extension is enabled, the buffer's capacity is automatically |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
457 | * increased when it is not large enough to hold all data. |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
458 | * By default, the capacity grows indefinitely, unless limited with |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
459 | * cxBufferMaximumCapacity(). |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
460 | * When auto-extension fails, this function writes no data and returns zero. |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
461 | * |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
462 | * The position of the buffer is moved alongside the written data. |
| 174 | 463 | * |
| 440 | 464 | * @note The signature is compatible with the fwrite() family of functions. |
| 174 | 465 | * |
| 466 | * @param ptr a pointer to the memory area containing the bytes to be written | |
| 467 | * @param size the length of one element | |
| 468 | * @param nitems the element count | |
| 469 | * @param buffer the CxBuffer to write to | |
| 470 | * @return the total count of elements written | |
| 440 | 471 | * @see cxBufferAppend() |
| 472 | * @see cxBufferRead() | |
| 174 | 473 | */ |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
474 | CX_EXTERN CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
475 | size_t cxBufferWrite(const void *ptr, size_t size, |
| 870 | 476 | size_t nitems, CxBuffer *buffer); |
| 174 | 477 | |
| 478 | /** | |
| 440 | 479 | * Appends data to a CxBuffer. |
| 480 | * | |
| 481 | * The data is always appended to current data within the buffer, | |
| 482 | * regardless of the current position. | |
| 483 | * This is especially useful when the buffer is primarily meant for reading | |
| 484 | * while additional data is added to the buffer occasionally. | |
| 485 | * Consequently, the position of the buffer is unchanged after this operation. | |
| 486 | * | |
| 487 | * @note The signature is compatible with the fwrite() family of functions. | |
| 488 | * | |
| 489 | * @param ptr a pointer to the memory area containing the bytes to be written | |
| 490 | * @param size the length of one element | |
| 491 | * @param nitems the element count | |
| 492 | * @param buffer the CxBuffer to write to | |
| 493 | * @return the total count of elements written | |
| 494 | * @see cxBufferWrite() | |
| 495 | * @see cxBufferRead() | |
| 496 | */ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
497 | CX_EXTERN CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
498 | size_t cxBufferAppend(const void *ptr, size_t size, |
| 870 | 499 | size_t nitems, CxBuffer *buffer); |
| 440 | 500 | |
| 501 | /** | |
| 174 | 502 | * Reads data from a CxBuffer. |
| 503 | * | |
| 504 | * The position of the buffer is increased by the number of bytes read. | |
| 505 | * | |
| 440 | 506 | * @note The signature is compatible with the fread() family of functions. |
| 174 | 507 | * |
| 508 | * @param ptr a pointer to the memory area where to store the read data | |
| 509 | * @param size the length of one element | |
| 510 | * @param nitems the element count | |
| 511 | * @param buffer the CxBuffer to read from | |
| 512 | * @return the total number of elements read | |
| 440 | 513 | * @see cxBufferWrite() |
| 514 | * @see cxBufferAppend() | |
| 174 | 515 | */ |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
516 | CX_EXTERN CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
517 | size_t cxBufferRead(void *ptr, size_t size, |
| 870 | 518 | size_t nitems, CxBuffer *buffer); |
| 174 | 519 | |
| 520 | /** | |
| 521 | * Writes a character to a buffer. | |
| 522 | * | |
| 523 | * The least significant byte of the argument is written to the buffer. If the | |
| 524 | * end of the buffer is reached and #CX_BUFFER_AUTO_EXTEND feature is enabled, | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
525 | * the buffer capacity is extended, unless a limit set by |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
526 | * cxBufferMaximumCapacity() is reached. |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
527 | * If the feature is disabled or the buffer extension fails, @c EOF is returned. |
| 174 | 528 | * |
| 845 | 529 | * On successful writing, the position of the buffer is increased. |
| 174 | 530 | * |
| 440 | 531 | * If you just want to write a null-terminator at the current position, you |
| 532 | * should use cxBufferTerminate() instead. | |
| 533 | * | |
| 174 | 534 | * @param buffer the buffer to write to |
| 535 | * @param c the character to write | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
536 | * @return the byte that has been written or @c EOF when the end of the |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
537 | * stream is reached, and automatic extension is not enabled or not possible |
| 440 | 538 | * @see cxBufferTerminate() |
| 174 | 539 | */ |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
540 | CX_EXTERN CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
541 | int cxBufferPut(CxBuffer *buffer, int c); |
| 174 | 542 | |
| 543 | /** | |
| 440 | 544 | * Writes a terminating zero to a buffer at the current position. |
| 545 | * | |
| 995 | 546 | * If successful, also sets the size to the current position and shrinks the buffer. |
| 440 | 547 | * |
| 548 | * The purpose of this function is to have the written data ready to be used as | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
549 | * a C string with the buffer's size being the length of that string. |
| 440 | 550 | * |
| 551 | * @param buffer the buffer to write to | |
| 552 | * @return zero, if the terminator could be written, non-zero otherwise | |
| 995 | 553 | * @see cxBufferShrink() |
| 440 | 554 | */ |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
555 | CX_EXTERN CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
556 | int cxBufferTerminate(CxBuffer *buffer); |
| 440 | 557 | |
| 558 | /** | |
| 995 | 559 | * Internal function - do not use. |
| 440 | 560 | * |
| 174 | 561 | * @param buffer the buffer |
| 995 | 562 | * @param str the string |
| 174 | 563 | * @return the number of bytes written |
| 995 | 564 | * @see cxBufferPutString() |
| 565 | */ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
566 | CX_EXTERN CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
567 | size_t cx_buffer_put_string(CxBuffer *buffer, cxstring str); |
| 995 | 568 | |
| 569 | /** | |
| 570 | * Writes a string to a buffer with cxBufferWrite(). | |
| 571 | * | |
| 572 | * @param buffer (@c CxBuffer*) the buffer | |
| 573 | * @param str (any string) the zero-terminated string | |
| 574 | * @return (@c size_t) the number of bytes written | |
| 575 | * @see cxBufferWrite() | |
| 576 | * @see cx_strcast() | |
| 174 | 577 | */ |
| 995 | 578 | #define cxBufferPutString(buffer, str) cx_buffer_put_string(buffer, cx_strcast(str)) |
| 579 | ||
| 580 | /** | |
| 581 | * Internal function - do not use. | |
| 582 | * | |
| 583 | * @param buffer the buffer | |
| 584 | * @param str the string | |
| 585 | * @return the number of bytes written | |
| 586 | * @see cxBufferPutString() | |
| 587 | */ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
588 | CX_EXTERN CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
589 | size_t cx_buffer_append_string(CxBuffer *buffer, cxstring str); |
| 995 | 590 | |
| 591 | /** | |
| 592 | * Appends a string to a buffer with cxBufferAppend(). | |
| 593 | * | |
| 594 | * @param buffer (@c CxBuffer*) the buffer | |
| 595 | * @param str (any string) the zero-terminated string | |
| 596 | * @return (@c size_t) the number of bytes written | |
| 597 | * @see cxBufferAppend() | |
| 598 | * @see cx_strcast() | |
| 599 | */ | |
| 600 | #define cxBufferAppendString(buffer, str) cx_buffer_append_string(buffer, cx_strcast(str)) | |
| 174 | 601 | |
| 602 | /** | |
| 603 | * Gets a character from a buffer. | |
| 604 | * | |
| 605 | * The current position of the buffer is increased after a successful read. | |
| 606 | * | |
| 607 | * @param buffer the buffer to read from | |
| 440 | 608 | * @return the character or @c EOF, if the end of the buffer is reached |
| 174 | 609 | */ |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
610 | CX_EXTERN CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
611 | int cxBufferGet(CxBuffer *buffer); |
| 174 | 612 | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
613 | /** |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
614 | * Gets the data in a buffer as a @c cxstring. |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
615 | * |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
616 | * @param buffer the buffer |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
617 | * @return the data in the buffer interpreted as a @c cxstring |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
618 | */ |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
619 | CX_NONNULL CX_INLINE |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
620 | cxstring cx_bstr(CxBuffer *buffer) { |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
621 | return cx_strn(buffer->space, buffer->size); |
| 174 | 622 | } |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
623 | |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
624 | /** |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
625 | * Gets the data in a buffer as a @c cxmutstr. |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
626 | * |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
627 | * @param buffer the buffer |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
628 | * @return the data in the buffer interpreted as a @c cxmutstr |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
629 | */ |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
630 | CX_NONNULL CX_INLINE |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
631 | cxmutstr cx_bstr_m(CxBuffer *buffer) { |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
632 | return cx_mutstrn(buffer->space, buffer->size); |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
633 | } |
| 174 | 634 | |
| 635 | #endif // UCX_BUFFER_H |