Thu, 23 Jan 2025 21:08:43 +0100
implement grid colspan/rowspan (Cocoa)
| 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" | |
| 51 | ||
| 440 | 52 | #ifdef __cplusplus |
| 174 | 53 | extern "C" { |
| 54 | #endif | |
| 55 | ||
| 56 | /** | |
| 57 | * No buffer features enabled (all flags cleared). | |
| 58 | */ | |
| 59 | #define CX_BUFFER_DEFAULT 0x00 | |
| 60 | ||
| 61 | /** | |
| 62 | * If this flag is enabled, the buffer will automatically free its contents when destroyed. | |
| 440 | 63 | * |
| 64 | * Do NOT set this flag together with #CX_BUFFER_COPY_ON_WRITE. It will be automatically | |
| 65 | * set when the copy-on-write operations is performed. | |
| 174 | 66 | */ |
| 67 | #define CX_BUFFER_FREE_CONTENTS 0x01 | |
| 68 | ||
| 69 | /** | |
| 440 | 70 | * If this flag is enabled, the buffer will automatically extend its capacity. |
| 174 | 71 | */ |
| 72 | #define CX_BUFFER_AUTO_EXTEND 0x02 | |
| 73 | ||
| 440 | 74 | /** |
| 75 | * If this flag is enabled, the buffer will allocate new memory when written to. | |
| 76 | * | |
| 77 | * The current contents of the buffer will be copied to the new memory and the flag | |
| 78 | * will be cleared while the #CX_BUFFER_FREE_CONTENTS flag will be set automatically. | |
| 79 | */ | |
| 80 | #define CX_BUFFER_COPY_ON_WRITE 0x04 | |
| 81 | ||
| 82 | /** | |
| 83 | * If this flag is enabled, the buffer will copy its contents to a new memory area on reallocation. | |
| 84 | * | |
| 85 | * After performing the copy, the flag is automatically cleared. | |
| 86 | * This flag has no effect on buffers which do not have #CX_BUFFER_AUTO_EXTEND set, which is why | |
| 87 | * buffers automatically admit the auto-extend flag when initialized with copy-on-extend enabled. | |
| 88 | */ | |
| 89 | #define CX_BUFFER_COPY_ON_EXTEND 0x08 | |
| 90 | ||
| 91 | /** | |
| 92 | * Configuration for automatic flushing. | |
| 93 | */ | |
| 94 | struct cx_buffer_flush_config_s { | |
| 95 | /** | |
| 96 | * The buffer may not extend beyond this threshold before starting to flush. | |
| 97 | * | |
| 98 | * Only used when the buffer uses #CX_BUFFER_AUTO_EXTEND. | |
| 99 | * The threshold will be the maximum capacity the buffer is extended to | |
| 100 | * before flushing. | |
| 101 | */ | |
| 102 | size_t threshold; | |
| 103 | /** | |
| 104 | * The block size for the elements to flush. | |
| 105 | */ | |
| 106 | size_t blksize; | |
| 107 | /** | |
| 108 | * The maximum number of blocks to flush in one cycle. | |
| 109 | * | |
| 110 | * @attention while it is guaranteed that cxBufferFlush() will not flush | |
| 111 | * more blocks, this is not necessarily the case for cxBufferWrite(). | |
| 112 | * After performing a flush cycle, cxBufferWrite() will retry the write | |
| 113 | * operation and potentially trigger another flush cycle, until the | |
| 114 | * flush target accepts no more data. | |
| 115 | */ | |
| 116 | size_t blkmax; | |
| 117 | ||
| 118 | /** | |
| 119 | * The target for write function. | |
| 120 | */ | |
| 121 | void *target; | |
| 122 | ||
| 123 | /** | |
| 124 | * The write-function used for flushing. | |
| 125 | * If NULL, the flushed content gets discarded. | |
| 126 | */ | |
| 127 | cx_write_func wfunc; | |
| 128 | }; | |
| 129 | ||
| 130 | /** | |
| 131 | * Type alais for the flush configuration struct. | |
| 132 | * | |
| 133 | * @code | |
| 134 | * struct cx_buffer_flush_config_s { | |
| 135 | * size_t threshold; | |
| 136 | * size_t blksize; | |
| 137 | * size_t blkmax; | |
| 138 | * void *target; | |
| 139 | * cx_write_func wfunc; | |
| 140 | * }; | |
| 141 | * @endcode | |
| 142 | */ | |
| 143 | typedef struct cx_buffer_flush_config_s CxBufferFlushConfig; | |
| 144 | ||
| 174 | 145 | /** Structure for the UCX buffer data. */ |
| 440 | 146 | struct cx_buffer_s { |
| 174 | 147 | /** A pointer to the buffer contents. */ |
| 148 | union { | |
| 149 | /** | |
| 150 | * Data is interpreted as text. | |
| 151 | */ | |
| 152 | char *space; | |
| 153 | /** | |
| 154 | * Data is interpreted as binary. | |
| 155 | */ | |
| 156 | unsigned char *bytes; | |
| 157 | }; | |
| 158 | /** The allocator to use for automatic memory management. */ | |
| 324 | 159 | const CxAllocator *allocator; |
| 440 | 160 | /** |
| 161 | * Optional flush configuration | |
| 162 | * | |
| 163 | * @see cxBufferEnableFlushing() | |
| 164 | */ | |
| 165 | CxBufferFlushConfig* flush; | |
| 174 | 166 | /** Current position of the buffer. */ |
| 167 | size_t pos; | |
| 168 | /** Current capacity (i.e. maximum size) of the buffer. */ | |
| 169 | size_t capacity; | |
| 170 | /** Current size of the buffer content. */ | |
| 171 | size_t size; | |
| 172 | /** | |
| 173 | * Flag register for buffer features. | |
| 174 | * @see #CX_BUFFER_DEFAULT | |
| 175 | * @see #CX_BUFFER_FREE_CONTENTS | |
| 176 | * @see #CX_BUFFER_AUTO_EXTEND | |
| 440 | 177 | * @see #CX_BUFFER_COPY_ON_WRITE |
| 174 | 178 | */ |
| 179 | int flags; | |
| 440 | 180 | }; |
| 174 | 181 | |
| 182 | /** | |
| 183 | * UCX buffer. | |
| 184 | */ | |
| 440 | 185 | typedef struct cx_buffer_s CxBuffer; |
| 174 | 186 | |
| 187 | /** | |
| 188 | * Initializes a fresh buffer. | |
| 189 | * | |
| 440 | 190 | * You may also provide a read-only @p space, in which case |
| 191 | * you will need to cast the pointer, and you should set the | |
| 192 | * #CX_BUFFER_COPY_ON_WRITE flag. | |
| 193 | * | |
| 194 | * You need to set the size manually after initialization, if | |
| 195 | * you provide @p space which already contains data. | |
| 196 | * | |
| 197 | * When you specify stack memory as @p space and decide to use | |
| 198 | * the auto-extension feature, you @em must use the | |
| 199 | * #CX_BUFFER_COPY_ON_EXTEND flag, instead of the | |
| 200 | * #CX_BUFFER_AUTO_EXTEND flag. | |
| 201 | * | |
| 202 | * @note You may provide @c NULL as argument for @p space. | |
| 174 | 203 | * Then this function will allocate the space and enforce |
| 440 | 204 | * the #CX_BUFFER_FREE_CONTENTS flag. In that case, specifying |
| 205 | * copy-on-write should be avoided, because the allocated | |
| 206 | * space will be leaking after the copy-on-write operation. | |
| 174 | 207 | * |
| 208 | * @param buffer the buffer to initialize | |
| 440 | 209 | * @param space pointer to the memory area, or @c NULL to allocate |
| 174 | 210 | * new memory |
| 211 | * @param capacity the capacity of the buffer | |
| 212 | * @param allocator the allocator this buffer shall use for automatic | |
| 440 | 213 | * memory management |
| 214 | * (if @c NULL, a default stdlib allocator will be used) | |
| 174 | 215 | * @param flags buffer features (see cx_buffer_s.flags) |
| 216 | * @return zero on success, non-zero if a required allocation failed | |
| 217 | */ | |
| 440 | 218 | cx_attr_nonnull_arg(1) |
| 174 | 219 | int cxBufferInit( |
| 220 | CxBuffer *buffer, | |
| 221 | void *space, | |
| 222 | size_t capacity, | |
| 324 | 223 | const CxAllocator *allocator, |
| 174 | 224 | int flags |
| 225 | ); | |
| 226 | ||
| 227 | /** | |
| 440 | 228 | * Configures the buffer for flushing. |
| 174 | 229 | * |
| 440 | 230 | * Flushing can happen automatically when data is written |
| 231 | * to the buffer (see cxBufferWrite()) or manually when | |
| 232 | * cxBufferFlush() is called. | |
| 174 | 233 | * |
| 440 | 234 | * @param buffer the buffer |
| 235 | * @param config the flush configuration | |
| 236 | * @retval zero success | |
| 237 | * @retval non-zero failure | |
| 238 | * @see cxBufferFlush() | |
| 239 | * @see cxBufferWrite() | |
| 174 | 240 | */ |
| 440 | 241 | cx_attr_nonnull |
| 242 | int cxBufferEnableFlushing( | |
| 243 | CxBuffer *buffer, | |
| 244 | CxBufferFlushConfig config | |
| 174 | 245 | ); |
| 246 | ||
| 247 | /** | |
| 248 | * Destroys the buffer contents. | |
| 249 | * | |
| 250 | * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled. | |
| 251 | * If you want to free the memory of the entire buffer, use cxBufferFree(). | |
| 252 | * | |
| 253 | * @param buffer the buffer which contents shall be destroyed | |
| 254 | * @see cxBufferInit() | |
| 255 | */ | |
| 440 | 256 | cx_attr_nonnull |
| 174 | 257 | void cxBufferDestroy(CxBuffer *buffer); |
| 258 | ||
| 259 | /** | |
| 260 | * Deallocates the buffer. | |
| 261 | * | |
| 262 | * If the #CX_BUFFER_FREE_CONTENTS feature is enabled, this function also destroys | |
| 440 | 263 | * the contents. If you @em only want to destroy the contents, use cxBufferDestroy(). |
| 264 | * | |
| 265 | * @remark As with all free() functions, this accepts @c NULL arguments in which | |
| 266 | * case it does nothing. | |
| 174 | 267 | * |
| 268 | * @param buffer the buffer to deallocate | |
| 269 | * @see cxBufferCreate() | |
| 270 | */ | |
| 271 | void cxBufferFree(CxBuffer *buffer); | |
| 272 | ||
| 273 | /** | |
| 440 | 274 | * Allocates and initializes a fresh buffer. |
| 275 | * | |
| 276 | * You may also provide a read-only @p space, in which case | |
| 277 | * you will need to cast the pointer, and you should set the | |
| 278 | * #CX_BUFFER_COPY_ON_WRITE flag. | |
| 279 | * When you specify stack memory as @p space and decide to use | |
| 280 | * the auto-extension feature, you @em must use the | |
| 281 | * #CX_BUFFER_COPY_ON_EXTEND flag, instead of the | |
| 282 | * #CX_BUFFER_AUTO_EXTEND flag. | |
| 283 | * | |
| 284 | * @note You may provide @c NULL as argument for @p space. | |
| 285 | * Then this function will allocate the space and enforce | |
| 286 | * the #CX_BUFFER_FREE_CONTENTS flag. | |
| 287 | * | |
| 288 | * @param space pointer to the memory area, or @c NULL to allocate | |
| 289 | * new memory | |
| 290 | * @param capacity the capacity of the buffer | |
| 291 | * @param allocator the allocator to use for allocating the structure and the automatic | |
| 292 | * memory management within the buffer | |
| 293 | * (if @c NULL, a default stdlib allocator will be used) | |
| 294 | * @param flags buffer features (see cx_buffer_s.flags) | |
| 295 | * @return a pointer to the buffer on success, @c NULL if a required allocation failed | |
| 296 | */ | |
| 297 | cx_attr_malloc | |
| 298 | cx_attr_dealloc(cxBufferFree, 1) | |
| 299 | cx_attr_nodiscard | |
| 300 | CxBuffer *cxBufferCreate( | |
| 301 | void *space, | |
| 302 | size_t capacity, | |
| 303 | const CxAllocator *allocator, | |
| 304 | int flags | |
| 305 | ); | |
| 306 | ||
| 307 | /** | |
| 174 | 308 | * Shifts the contents of the buffer by the given offset. |
| 309 | * | |
| 310 | * If the offset is positive, the contents are shifted to the right. | |
| 311 | * If auto extension is enabled, the buffer grows, if necessary. | |
| 312 | * In case the auto extension fails, this function returns a non-zero value and | |
| 313 | * no contents are changed. | |
| 314 | * If auto extension is disabled, the contents that do not fit into the buffer | |
| 315 | * are discarded. | |
| 316 | * | |
| 317 | * If the offset is negative, the contents are shifted to the left where the | |
| 440 | 318 | * first @p shift bytes are discarded. |
| 174 | 319 | * The new size of the buffer is the old size minus the absolute shift value. |
| 320 | * If this value is larger than the buffer size, the buffer is emptied (but | |
| 321 | * not cleared, see the security note below). | |
| 322 | * | |
| 323 | * The buffer position gets shifted alongside with the content but is kept | |
| 324 | * within the boundaries of the buffer. | |
| 325 | * | |
| 440 | 326 | * @note For situations where @c off_t is not large enough, there are specialized cxBufferShiftLeft() and |
| 327 | * cxBufferShiftRight() functions using a @c size_t as parameter type. | |
| 174 | 328 | * |
| 440 | 329 | * @attention |
| 330 | * Security Note: The shifting operation does @em not erase the previously occupied memory cells. | |
| 174 | 331 | * But you can easily do that manually, e.g. by calling |
| 332 | * <code>memset(buffer->bytes, 0, shift)</code> for a right shift or | |
| 333 | * <code>memset(buffer->bytes + buffer->size, 0, buffer->capacity - buffer->size)</code> | |
| 334 | * for a left shift. | |
| 335 | * | |
| 336 | * @param buffer the buffer | |
| 337 | * @param shift the shift offset (negative means left shift) | |
| 440 | 338 | * @retval zero success |
| 339 | * @retval non-zero if a required auto-extension or copy-on-write fails | |
| 340 | * @see cxBufferShiftLeft() | |
| 341 | * @see cxBufferShiftRight() | |
| 174 | 342 | */ |
| 440 | 343 | cx_attr_nonnull |
| 174 | 344 | int cxBufferShift( |
| 345 | CxBuffer *buffer, | |
| 346 | off_t shift | |
| 347 | ); | |
| 348 | ||
| 349 | /** | |
| 350 | * Shifts the buffer to the right. | |
| 351 | * See cxBufferShift() for details. | |
| 352 | * | |
| 353 | * @param buffer the buffer | |
| 354 | * @param shift the shift offset | |
| 440 | 355 | * @retval zero success |
| 356 | * @retval non-zero if a required auto-extension or copy-on-write fails | |
| 174 | 357 | * @see cxBufferShift() |
| 358 | */ | |
| 440 | 359 | cx_attr_nonnull |
| 174 | 360 | int cxBufferShiftRight( |
| 361 | CxBuffer *buffer, | |
| 362 | size_t shift | |
| 363 | ); | |
| 364 | ||
| 365 | /** | |
| 366 | * Shifts the buffer to the left. | |
| 367 | * See cxBufferShift() for details. | |
| 368 | * | |
| 369 | * @param buffer the buffer | |
| 370 | * @param shift the positive shift offset | |
| 440 | 371 | * @retval zero success |
| 372 | * @retval non-zero if the buffer uses copy-on-write and the allocation fails | |
| 174 | 373 | * @see cxBufferShift() |
| 374 | */ | |
| 440 | 375 | cx_attr_nonnull |
| 174 | 376 | int cxBufferShiftLeft( |
| 377 | CxBuffer *buffer, | |
| 378 | size_t shift | |
| 379 | ); | |
| 380 | ||
| 381 | ||
| 382 | /** | |
| 383 | * Moves the position of the buffer. | |
| 384 | * | |
| 440 | 385 | * The new position is relative to the @p whence argument. |
| 174 | 386 | * |
| 440 | 387 | * @li @c SEEK_SET marks the start of the buffer. |
| 388 | * @li @c SEEK_CUR marks the current position. | |
| 389 | * @li @c SEEK_END marks the end of the buffer. | |
| 174 | 390 | * |
| 391 | * With an offset of zero, this function sets the buffer position to zero | |
| 440 | 392 | * (@c SEEK_SET), the buffer size (@c SEEK_END) or leaves the buffer position |
| 393 | * unchanged (@c SEEK_CUR). | |
| 174 | 394 | * |
| 395 | * @param buffer the buffer | |
| 440 | 396 | * @param offset position offset relative to @p whence |
| 397 | * @param whence one of @c SEEK_SET, @c SEEK_CUR or @c SEEK_END | |
| 398 | * @retval zero success | |
| 399 | * @retval non-zero if the position is invalid | |
| 174 | 400 | * |
| 401 | */ | |
| 440 | 402 | cx_attr_nonnull |
| 174 | 403 | int cxBufferSeek( |
| 404 | CxBuffer *buffer, | |
| 405 | off_t offset, | |
| 406 | int whence | |
| 407 | ); | |
| 408 | ||
| 409 | /** | |
| 410 | * Clears the buffer by resetting the position and deleting the data. | |
| 411 | * | |
| 412 | * 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
|
413 | * If you do not need that, you can use the faster cxBufferReset(). |
| 174 | 414 | * |
| 440 | 415 | * @note If the #CX_BUFFER_COPY_ON_WRITE flag is set, this function |
| 416 | * will not erase the data and behave exactly as cxBufferReset(). | |
| 417 | * | |
| 174 | 418 | * @param buffer the buffer to be cleared |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
419 | * @see cxBufferReset() |
| 174 | 420 | */ |
| 440 | 421 | cx_attr_nonnull |
| 174 | 422 | void cxBufferClear(CxBuffer *buffer); |
| 423 | ||
| 424 | /** | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
425 | * 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
|
426 | * |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
427 | * 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
|
428 | * reset of the buffer, use cxBufferClear(). |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
429 | * |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
430 | * @param buffer the buffer to be cleared |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
431 | * @see cxBufferClear() |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
432 | */ |
| 440 | 433 | cx_attr_nonnull |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
434 | void cxBufferReset(CxBuffer *buffer); |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
435 | |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
436 | /** |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
437 | * Tests, if the buffer position has exceeded the buffer size. |
| 174 | 438 | * |
| 439 | * @param buffer the buffer to test | |
| 440 | 440 | * @retval true if the current buffer position has exceeded the last |
| 441 | * byte of the buffer's contents | |
| 442 | * @retval false otherwise | |
| 174 | 443 | */ |
| 440 | 444 | cx_attr_nonnull |
| 445 | cx_attr_nodiscard | |
| 446 | bool cxBufferEof(const CxBuffer *buffer); | |
| 174 | 447 | |
| 448 | ||
| 449 | /** | |
| 450 | * Ensures that the buffer has a minimum capacity. | |
| 451 | * | |
| 452 | * If the current capacity is not sufficient, the buffer will be extended. | |
| 453 | * | |
| 454 | * @param buffer the buffer | |
| 455 | * @param capacity the minimum required capacity for this buffer | |
| 440 | 456 | * @retval zero the capacity was already sufficient or successfully increased |
| 457 | * @retval non-zero on allocation failure | |
| 174 | 458 | */ |
| 440 | 459 | cx_attr_nonnull |
| 174 | 460 | int cxBufferMinimumCapacity( |
| 461 | CxBuffer *buffer, | |
| 462 | size_t capacity | |
| 463 | ); | |
| 464 | ||
| 465 | /** | |
| 466 | * Writes data to a CxBuffer. | |
| 467 | * | |
| 440 | 468 | * If automatic flushing is not enabled, the data is simply written into the |
| 469 | * buffer at the current position and the position of the buffer is increased | |
| 470 | * by the number of bytes written. | |
| 471 | * | |
| 174 | 472 | * If flushing is enabled and the buffer needs to flush, the data is flushed to |
| 473 | * the target until the target signals that it cannot take more data by | |
| 474 | * returning zero via the respective write function. In that case, the remaining | |
| 475 | * data in this buffer is shifted to the beginning of this buffer so that the | |
| 476 | * newly available space can be used to append as much data as possible. This | |
| 477 | * function only stops writing more elements, when the flush target and this | |
| 478 | * buffer are both incapable of taking more data or all data has been written. | |
| 440 | 479 | * If number of items that shall be written is larger than the buffer can hold, |
| 480 | * the first items from @c ptr are directly relayed to the flush target, if | |
| 481 | * possible. | |
| 482 | * The number returned by this function is only the number of elements from | |
| 483 | * @c ptr that could be written to either the flush target or the buffer. | |
| 174 | 484 | * |
| 440 | 485 | * @note The signature is compatible with the fwrite() family of functions. |
| 174 | 486 | * |
| 487 | * @param ptr a pointer to the memory area containing the bytes to be written | |
| 488 | * @param size the length of one element | |
| 489 | * @param nitems the element count | |
| 490 | * @param buffer the CxBuffer to write to | |
| 491 | * @return the total count of elements written | |
| 440 | 492 | * @see cxBufferAppend() |
| 493 | * @see cxBufferRead() | |
| 174 | 494 | */ |
| 440 | 495 | cx_attr_nonnull |
| 174 | 496 | size_t cxBufferWrite( |
| 324 | 497 | const void *ptr, |
| 174 | 498 | size_t size, |
| 499 | size_t nitems, | |
| 500 | CxBuffer *buffer | |
| 501 | ); | |
| 502 | ||
| 503 | /** | |
| 440 | 504 | * Appends data to a CxBuffer. |
| 505 | * | |
| 506 | * The data is always appended to current data within the buffer, | |
| 507 | * regardless of the current position. | |
| 508 | * This is especially useful when the buffer is primarily meant for reading | |
| 509 | * while additional data is added to the buffer occasionally. | |
| 510 | * Consequently, the position of the buffer is unchanged after this operation. | |
| 511 | * | |
| 512 | * @note The signature is compatible with the fwrite() family of functions. | |
| 513 | * | |
| 514 | * @param ptr a pointer to the memory area containing the bytes to be written | |
| 515 | * @param size the length of one element | |
| 516 | * @param nitems the element count | |
| 517 | * @param buffer the CxBuffer to write to | |
| 518 | * @return the total count of elements written | |
| 519 | * @see cxBufferWrite() | |
| 520 | * @see cxBufferRead() | |
| 521 | */ | |
| 522 | cx_attr_nonnull | |
| 523 | size_t cxBufferAppend( | |
| 524 | const void *ptr, | |
| 525 | size_t size, | |
| 526 | size_t nitems, | |
| 527 | CxBuffer *buffer | |
| 528 | ); | |
| 529 | ||
| 530 | /** | |
| 531 | * Performs a single flush-run on the specified buffer. | |
| 532 | * | |
| 533 | * Does nothing when the position in the buffer is zero. | |
| 534 | * Otherwise, the data until the current position minus | |
| 535 | * one is considered for flushing. | |
| 536 | * Note carefully that flushing will never exceed the | |
| 537 | * current @em position, even when the size of the | |
| 538 | * buffer is larger than the current position. | |
| 539 | * | |
| 540 | * One flush run will try to flush @c blkmax many | |
| 541 | * blocks of size @c blksize until either the @p buffer | |
| 542 | * has no more data to flush or the write function | |
| 543 | * used for flushing returns zero. | |
| 544 | * | |
| 545 | * The buffer is shifted left for that many bytes | |
| 546 | * the flush operation has successfully flushed. | |
| 547 | * | |
| 548 | * @par Example 1 | |
| 549 | * Assume you have a buffer with size 340 and you are | |
| 550 | * at position 200. The flush configuration is | |
| 551 | * @c blkmax=4 and @c blksize=64 . | |
| 552 | * Assume that the entire flush operation is successful. | |
| 553 | * All 200 bytes on the left hand-side from the current | |
| 554 | * position are written. | |
| 555 | * That means, the size of the buffer is now 140 and the | |
| 556 | * position is zero. | |
| 557 | * | |
| 558 | * @par Example 2 | |
| 559 | * Same as Example 1, but now the @c blkmax is 1. | |
| 560 | * The size of the buffer is now 276 and the position is 136. | |
| 561 | * | |
| 562 | * @par Example 3 | |
| 563 | * Same as Example 1, but now assume the flush target | |
| 564 | * only accepts 100 bytes before returning zero. | |
| 565 | * That means, the flush operations manages to flush | |
| 566 | * one complete block and one partial block, ending | |
| 567 | * up with a buffer with size 240 and position 100. | |
| 568 | * | |
| 569 | * @remark Just returns zero when flushing was not enabled with | |
| 570 | * cxBufferEnableFlushing(). | |
| 571 | * | |
| 572 | * @remark When the buffer uses copy-on-write, the memory | |
| 573 | * is copied first, before attempting any flush. | |
| 574 | * This is, however, considered an erroneous use of the | |
| 575 | * buffer, because it does not make much sense to put | |
| 576 | * readonly data into an UCX buffer for flushing, instead | |
| 577 | * of writing it directly to the target. | |
| 578 | * | |
| 579 | * @param buffer the buffer | |
| 580 | * @return the number of successfully flushed bytes | |
| 581 | * @see cxBufferEnableFlushing() | |
| 582 | */ | |
| 583 | cx_attr_nonnull | |
| 584 | size_t cxBufferFlush(CxBuffer *buffer); | |
| 585 | ||
| 586 | /** | |
| 174 | 587 | * Reads data from a CxBuffer. |
| 588 | * | |
| 589 | * The position of the buffer is increased by the number of bytes read. | |
| 590 | * | |
| 440 | 591 | * @note The signature is compatible with the fread() family of functions. |
| 174 | 592 | * |
| 593 | * @param ptr a pointer to the memory area where to store the read data | |
| 594 | * @param size the length of one element | |
| 595 | * @param nitems the element count | |
| 596 | * @param buffer the CxBuffer to read from | |
| 597 | * @return the total number of elements read | |
| 440 | 598 | * @see cxBufferWrite() |
| 599 | * @see cxBufferAppend() | |
| 174 | 600 | */ |
| 440 | 601 | cx_attr_nonnull |
| 174 | 602 | size_t cxBufferRead( |
| 603 | void *ptr, | |
| 604 | size_t size, | |
| 605 | size_t nitems, | |
| 606 | CxBuffer *buffer | |
| 607 | ); | |
| 608 | ||
| 609 | /** | |
| 610 | * Writes a character to a buffer. | |
| 611 | * | |
| 612 | * The least significant byte of the argument is written to the buffer. If the | |
| 613 | * end of the buffer is reached and #CX_BUFFER_AUTO_EXTEND feature is enabled, | |
| 440 | 614 | * the buffer capacity is extended by cxBufferMinimumCapacity(). If the feature |
| 615 | * is disabled or buffer extension fails, @c EOF is returned. | |
| 174 | 616 | * |
| 617 | * On successful write, the position of the buffer is increased. | |
| 618 | * | |
| 440 | 619 | * If you just want to write a null-terminator at the current position, you |
| 620 | * should use cxBufferTerminate() instead. | |
| 621 | * | |
| 174 | 622 | * @param buffer the buffer to write to |
| 623 | * @param c the character to write | |
| 440 | 624 | * @return the byte that has been written or @c EOF when the end of the stream is |
| 174 | 625 | * reached and automatic extension is not enabled or not possible |
| 440 | 626 | * @see cxBufferTerminate() |
| 174 | 627 | */ |
| 440 | 628 | cx_attr_nonnull |
| 174 | 629 | int cxBufferPut( |
| 630 | CxBuffer *buffer, | |
| 631 | int c | |
| 632 | ); | |
| 633 | ||
| 634 | /** | |
| 440 | 635 | * Writes a terminating zero to a buffer at the current position. |
| 636 | * | |
| 637 | * On successful write, @em neither the position @em nor the size of the buffer is | |
| 638 | * increased. | |
| 639 | * | |
| 640 | * The purpose of this function is to have the written data ready to be used as | |
| 641 | * a C string. | |
| 642 | * | |
| 643 | * @param buffer the buffer to write to | |
| 644 | * @return zero, if the terminator could be written, non-zero otherwise | |
| 645 | */ | |
| 646 | cx_attr_nonnull | |
| 647 | int cxBufferTerminate(CxBuffer *buffer); | |
| 648 | ||
| 649 | /** | |
| 174 | 650 | * Writes a string to a buffer. |
| 651 | * | |
| 440 | 652 | * This is a convenience function for <code>cxBufferWrite(str, 1, strlen(str), buffer)</code>. |
| 653 | * | |
| 174 | 654 | * @param buffer the buffer |
| 655 | * @param str the zero-terminated string | |
| 656 | * @return the number of bytes written | |
| 657 | */ | |
| 440 | 658 | cx_attr_nonnull |
| 659 | cx_attr_cstr_arg(2) | |
| 174 | 660 | size_t cxBufferPutString( |
| 661 | CxBuffer *buffer, | |
| 662 | const char *str | |
| 663 | ); | |
| 664 | ||
| 665 | /** | |
| 666 | * Gets a character from a buffer. | |
| 667 | * | |
| 668 | * The current position of the buffer is increased after a successful read. | |
| 669 | * | |
| 670 | * @param buffer the buffer to read from | |
| 440 | 671 | * @return the character or @c EOF, if the end of the buffer is reached |
| 174 | 672 | */ |
| 440 | 673 | cx_attr_nonnull |
| 174 | 674 | int cxBufferGet(CxBuffer *buffer); |
| 675 | ||
| 676 | #ifdef __cplusplus | |
| 677 | } | |
| 678 | #endif | |
| 679 | ||
| 680 | #endif // UCX_BUFFER_H |