ucx/cx/buffer.h

changeset 22
112b85020dc9
parent 21
5ea41679e15d
equal deleted inserted replaced
21:5ea41679e15d 22:112b85020dc9
60 60
61 /** 61 /**
62 * If this flag is enabled, the buffer will automatically free its contents when destroyed. 62 * If this flag is enabled, the buffer will automatically free its contents when destroyed.
63 * 63 *
64 * Do NOT set this flag together with #CX_BUFFER_COPY_ON_WRITE. It will be automatically 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. 65 * set when the copy-on-write operation is performed.
66 */ 66 */
67 #define CX_BUFFER_FREE_CONTENTS 0x01 67 #define CX_BUFFER_FREE_CONTENTS 0x01
68 68
69 /** 69 /**
70 * If this flag is enabled, the buffer will automatically extend its capacity. 70 * If this flag is enabled, the buffer will automatically extend its capacity.
72 #define CX_BUFFER_AUTO_EXTEND 0x02 72 #define CX_BUFFER_AUTO_EXTEND 0x02
73 73
74 /** 74 /**
75 * If this flag is enabled, the buffer will allocate new memory when written to. 75 * If this flag is enabled, the buffer will allocate new memory when written to.
76 * 76 *
77 * The current contents of the buffer will be copied to the new memory and the flag 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. 78 * will be cleared while the #CX_BUFFER_FREE_CONTENTS flag will be set automatically.
79 */ 79 */
80 #define CX_BUFFER_COPY_ON_WRITE 0x04 80 #define CX_BUFFER_COPY_ON_WRITE 0x04
81 81
82 /** 82 /**
125 * flush target accepts no more data. 125 * flush target accepts no more data.
126 */ 126 */
127 size_t blkmax; 127 size_t blkmax;
128 128
129 /** 129 /**
130 * The target for write function. 130 * The target for the write function.
131 */ 131 */
132 void *target; 132 void *target;
133 133
134 /** 134 /**
135 * The write-function used for flushing. 135 * The write-function used for flushing.
200 * 200 *
201 * You may also provide a read-only @p space, in which case 201 * You may also provide a read-only @p space, in which case
202 * you will need to cast the pointer, and you should set the 202 * you will need to cast the pointer, and you should set the
203 * #CX_BUFFER_COPY_ON_WRITE flag. 203 * #CX_BUFFER_COPY_ON_WRITE flag.
204 * 204 *
205 * You need to set the size manually after initialization, if 205 * You need to set the size manually after initialization if
206 * you provide @p space which already contains data. 206 * you provide @p space which already contains data.
207 * 207 *
208 * When you specify stack memory as @p space and decide to use 208 * When you specify stack memory as @p space and decide to use
209 * the auto-extension feature, you @em must use the 209 * the auto-extension feature, you @em must use the
210 * #CX_BUFFER_COPY_ON_EXTEND flag, instead of the 210 * #CX_BUFFER_COPY_ON_EXTEND flag, instead of the
211 * #CX_BUFFER_AUTO_EXTEND flag. 211 * #CX_BUFFER_AUTO_EXTEND flag.
212 * 212 *
213 * @note You may provide @c NULL as argument for @p space. 213 * @note You may provide @c NULL as the argument for @p space.
214 * Then this function will allocate the space and enforce 214 * Then this function will allocate the space and enforce
215 * the #CX_BUFFER_FREE_CONTENTS flag. In that case, specifying 215 * the #CX_BUFFER_FREE_CONTENTS flag. In that case, specifying
216 * copy-on-write should be avoided, because the allocated 216 * copy-on-write should be avoided, because the allocated
217 * space will be leaking after the copy-on-write operation. 217 * space will be leaking after the copy-on-write operation.
218 * 218 *
225 * (if @c NULL, the cxDefaultAllocator will be used) 225 * (if @c NULL, the cxDefaultAllocator will be used)
226 * @param flags buffer features (see cx_buffer_s.flags) 226 * @param flags buffer features (see cx_buffer_s.flags)
227 * @return zero on success, non-zero if a required allocation failed 227 * @return zero on success, non-zero if a required allocation failed
228 */ 228 */
229 cx_attr_nonnull_arg(1) 229 cx_attr_nonnull_arg(1)
230 cx_attr_export 230 CX_EXPORT int cxBufferInit(CxBuffer *buffer, void *space, size_t capacity,
231 int cxBufferInit( 231 const CxAllocator *allocator, int flags);
232 CxBuffer *buffer,
233 void *space,
234 size_t capacity,
235 const CxAllocator *allocator,
236 int flags
237 );
238 232
239 /** 233 /**
240 * Configures the buffer for flushing. 234 * Configures the buffer for flushing.
241 * 235 *
242 * Flushing can happen automatically when data is written 236 * Flushing can happen automatically when data is written
249 * @retval non-zero failure 243 * @retval non-zero failure
250 * @see cxBufferFlush() 244 * @see cxBufferFlush()
251 * @see cxBufferWrite() 245 * @see cxBufferWrite()
252 */ 246 */
253 cx_attr_nonnull 247 cx_attr_nonnull
254 cx_attr_export 248 CX_EXPORT int cxBufferEnableFlushing(CxBuffer *buffer, CxBufferFlushConfig config);
255 int cxBufferEnableFlushing(
256 CxBuffer *buffer,
257 CxBufferFlushConfig config
258 );
259 249
260 /** 250 /**
261 * Destroys the buffer contents. 251 * Destroys the buffer contents.
262 * 252 *
263 * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled. 253 * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled.
265 * 255 *
266 * @param buffer the buffer which contents shall be destroyed 256 * @param buffer the buffer which contents shall be destroyed
267 * @see cxBufferInit() 257 * @see cxBufferInit()
268 */ 258 */
269 cx_attr_nonnull 259 cx_attr_nonnull
270 cx_attr_export 260 CX_EXPORT void cxBufferDestroy(CxBuffer *buffer);
271 void cxBufferDestroy(CxBuffer *buffer);
272 261
273 /** 262 /**
274 * Deallocates the buffer. 263 * Deallocates the buffer.
275 * 264 *
276 * If the #CX_BUFFER_FREE_CONTENTS feature is enabled, this function also destroys 265 * If the #CX_BUFFER_FREE_CONTENTS feature is enabled, this function also destroys
277 * the contents. If you @em only want to destroy the contents, use cxBufferDestroy(). 266 * the contents. If you @em only want to destroy the contents, use cxBufferDestroy().
278 * 267 *
279 * @remark As with all free() functions, this accepts @c NULL arguments in which
280 * case it does nothing.
281 *
282 * @param buffer the buffer to deallocate 268 * @param buffer the buffer to deallocate
283 * @see cxBufferCreate() 269 * @see cxBufferCreate()
284 */ 270 */
285 cx_attr_export 271 CX_EXPORT void cxBufferFree(CxBuffer *buffer);
286 void cxBufferFree(CxBuffer *buffer);
287 272
288 /** 273 /**
289 * Allocates and initializes a fresh buffer. 274 * Allocates and initializes a fresh buffer.
290 * 275 *
291 * You may also provide a read-only @p space, in which case 276 * You may also provide a read-only @p space, in which case
294 * When you specify stack memory as @p space and decide to use 279 * When you specify stack memory as @p space and decide to use
295 * the auto-extension feature, you @em must use the 280 * the auto-extension feature, you @em must use the
296 * #CX_BUFFER_COPY_ON_EXTEND flag, instead of the 281 * #CX_BUFFER_COPY_ON_EXTEND flag, instead of the
297 * #CX_BUFFER_AUTO_EXTEND flag. 282 * #CX_BUFFER_AUTO_EXTEND flag.
298 * 283 *
299 * @note You may provide @c NULL as argument for @p space. 284 * @note You may provide @c NULL as the argument for @p space.
300 * Then this function will allocate the space and enforce 285 * Then this function will allocate the space and enforce
301 * the #CX_BUFFER_FREE_CONTENTS flag. 286 * the #CX_BUFFER_FREE_CONTENTS flag.
302 * 287 *
303 * @param space pointer to the memory area, or @c NULL to allocate 288 * @param space pointer to the memory area, or @c NULL to allocate
304 * new memory 289 * new memory
307 * memory management within the buffer 292 * memory management within the buffer
308 * (if @c NULL, the cxDefaultAllocator will be used) 293 * (if @c NULL, the cxDefaultAllocator will be used)
309 * @param flags buffer features (see cx_buffer_s.flags) 294 * @param flags buffer features (see cx_buffer_s.flags)
310 * @return a pointer to the buffer on success, @c NULL if a required allocation failed 295 * @return a pointer to the buffer on success, @c NULL if a required allocation failed
311 */ 296 */
312 cx_attr_malloc 297 cx_attr_malloc cx_attr_dealloc(cxBufferFree, 1) cx_attr_nodiscard
313 cx_attr_dealloc(cxBufferFree, 1) 298 CX_EXPORT CxBuffer *cxBufferCreate(void *space, size_t capacity,
314 cx_attr_nodiscard 299 const CxAllocator *allocator, int flags);
315 cx_attr_export
316 CxBuffer *cxBufferCreate(
317 void *space,
318 size_t capacity,
319 const CxAllocator *allocator,
320 int flags
321 );
322 300
323 /** 301 /**
324 * Shifts the contents of the buffer by the given offset. 302 * Shifts the contents of the buffer by the given offset.
325 * 303 *
326 * If the offset is positive, the contents are shifted to the right. 304 * If the offset is positive, the contents are shifted to the right.
327 * If auto extension is enabled, the buffer grows, if necessary. 305 * If auto extension is enabled, the buffer grows, if necessary.
328 * In case the auto extension fails, this function returns a non-zero value and 306 * In case the auto extension fails, this function returns a non-zero value and
329 * no contents are changed. 307 * no contents are changed.
330 * If auto extension is disabled, the contents that do not fit into the buffer 308 * When the auto extension is disabled, the contents that do not fit into the
331 * are discarded. 309 * buffer are discarded.
332 * 310 *
333 * If the offset is negative, the contents are shifted to the left where the 311 * If the offset is negative, the contents are shifted to the left where the
334 * first @p shift bytes are discarded. 312 * first @p shift bytes are discarded.
335 * The new size of the buffer is the old size minus the absolute shift value. 313 * The new size of the buffer is the old size minus the absolute shift value.
336 * If this value is larger than the buffer size, the buffer is emptied (but 314 * If this value is larger than the buffer size, the buffer is emptied (but
337 * not cleared, see the security note below). 315 * not cleared, see the security note below).
338 * 316 *
339 * The buffer position gets shifted alongside with the content but is kept 317 * The buffer position gets shifted alongside the content but is kept
340 * within the boundaries of the buffer. 318 * within the boundaries of the buffer.
341 * 319 *
342 * @note For situations where @c off_t is not large enough, there are specialized cxBufferShiftLeft() and 320 * @note For situations where @c off_t is not large enough, there are specialized cxBufferShiftLeft() and
343 * cxBufferShiftRight() functions using a @c size_t as parameter type. 321 * cxBufferShiftRight() functions using a @c size_t as the parameter type.
344 * 322 *
345 * @attention 323 * @attention
346 * Security Note: The shifting operation does @em not erase the previously occupied memory cells. 324 * Security Note: The shifting operation does @em not erase the previously occupied memory cells.
347 * But you can easily do that manually, e.g. by calling 325 * But you can do that manually by calling
348 * <code>memset(buffer->bytes, 0, shift)</code> for a right shift or 326 * <code>memset(buffer->bytes, 0, shift)</code> for a right shift or
349 * <code>memset(buffer->bytes + buffer->size, 0, buffer->capacity - buffer->size)</code> 327 * <code>memset(buffer->bytes + buffer->size, 0, buffer->capacity - buffer->size)</code>
350 * for a left shift. 328 * for a left shift.
351 * 329 *
352 * @param buffer the buffer 330 * @param buffer the buffer
355 * @retval non-zero if a required auto-extension or copy-on-write fails 333 * @retval non-zero if a required auto-extension or copy-on-write fails
356 * @see cxBufferShiftLeft() 334 * @see cxBufferShiftLeft()
357 * @see cxBufferShiftRight() 335 * @see cxBufferShiftRight()
358 */ 336 */
359 cx_attr_nonnull 337 cx_attr_nonnull
360 cx_attr_export 338 CX_EXPORT int cxBufferShift(CxBuffer *buffer, off_t shift);
361 int cxBufferShift(
362 CxBuffer *buffer,
363 off_t shift
364 );
365 339
366 /** 340 /**
367 * Shifts the buffer to the right. 341 * Shifts the buffer to the right.
368 * See cxBufferShift() for details. 342 * See cxBufferShift() for details.
369 * 343 *
372 * @retval zero success 346 * @retval zero success
373 * @retval non-zero if a required auto-extension or copy-on-write fails 347 * @retval non-zero if a required auto-extension or copy-on-write fails
374 * @see cxBufferShift() 348 * @see cxBufferShift()
375 */ 349 */
376 cx_attr_nonnull 350 cx_attr_nonnull
377 cx_attr_export 351 CX_EXPORT int cxBufferShiftRight(CxBuffer *buffer, size_t shift);
378 int cxBufferShiftRight(
379 CxBuffer *buffer,
380 size_t shift
381 );
382 352
383 /** 353 /**
384 * Shifts the buffer to the left. 354 * Shifts the buffer to the left.
385 * See cxBufferShift() for details. 355 * See cxBufferShift() for details.
386 * 356 *
389 * @retval zero success 359 * @retval zero success
390 * @retval non-zero if the buffer uses copy-on-write and the allocation fails 360 * @retval non-zero if the buffer uses copy-on-write and the allocation fails
391 * @see cxBufferShift() 361 * @see cxBufferShift()
392 */ 362 */
393 cx_attr_nonnull 363 cx_attr_nonnull
394 cx_attr_export 364 CX_EXPORT int cxBufferShiftLeft(CxBuffer *buffer, size_t shift);
395 int cxBufferShiftLeft(
396 CxBuffer *buffer,
397 size_t shift
398 );
399 365
400 366
401 /** 367 /**
402 * Moves the position of the buffer. 368 * Moves the position of the buffer.
403 * 369 *
417 * @retval zero success 383 * @retval zero success
418 * @retval non-zero if the position is invalid 384 * @retval non-zero if the position is invalid
419 * 385 *
420 */ 386 */
421 cx_attr_nonnull 387 cx_attr_nonnull
422 cx_attr_export 388 CX_EXPORT int cxBufferSeek(CxBuffer *buffer, off_t offset, int whence);
423 int cxBufferSeek(
424 CxBuffer *buffer,
425 off_t offset,
426 int whence
427 );
428 389
429 /** 390 /**
430 * Clears the buffer by resetting the position and deleting the data. 391 * Clears the buffer by resetting the position and deleting the data.
431 * 392 *
432 * The data is deleted by zeroing it with a call to memset(). 393 * The data is deleted by zeroing it with a call to memset().
437 * 398 *
438 * @param buffer the buffer to be cleared 399 * @param buffer the buffer to be cleared
439 * @see cxBufferReset() 400 * @see cxBufferReset()
440 */ 401 */
441 cx_attr_nonnull 402 cx_attr_nonnull
442 cx_attr_export 403 CX_EXPORT void cxBufferClear(CxBuffer *buffer);
443 void cxBufferClear(CxBuffer *buffer);
444 404
445 /** 405 /**
446 * Resets the buffer by resetting the position and size to zero. 406 * Resets the buffer by resetting the position and size to zero.
447 * 407 *
448 * The data in the buffer is not deleted. If you need a safe 408 * The data in the buffer is not deleted. If you need a safe
450 * 410 *
451 * @param buffer the buffer to be cleared 411 * @param buffer the buffer to be cleared
452 * @see cxBufferClear() 412 * @see cxBufferClear()
453 */ 413 */
454 cx_attr_nonnull 414 cx_attr_nonnull
455 cx_attr_export 415 CX_EXPORT void cxBufferReset(CxBuffer *buffer);
456 void cxBufferReset(CxBuffer *buffer);
457 416
458 /** 417 /**
459 * Tests, if the buffer position has exceeded the buffer size. 418 * Tests, if the buffer position has exceeded the buffer size.
460 * 419 *
461 * @param buffer the buffer to test 420 * @param buffer the buffer to test
462 * @retval true if the current buffer position has exceeded the last 421 * @retval true if the current buffer position has exceeded the last
463 * byte of the buffer's contents 422 * byte of the buffer's contents
464 * @retval false otherwise 423 * @retval false otherwise
465 */ 424 */
466 cx_attr_nonnull 425 cx_attr_nonnull cx_attr_nodiscard
467 cx_attr_nodiscard 426 CX_EXPORT bool cxBufferEof(const CxBuffer *buffer);
468 cx_attr_export
469 bool cxBufferEof(const CxBuffer *buffer);
470 427
471 428
472 /** 429 /**
473 * Ensures that the buffer has a minimum capacity. 430 * Ensures that the buffer has a minimum capacity.
474 * 431 *
482 * @retval zero the capacity was already sufficient or successfully increased 439 * @retval zero the capacity was already sufficient or successfully increased
483 * @retval non-zero on allocation failure 440 * @retval non-zero on allocation failure
484 * @see cxBufferShrink() 441 * @see cxBufferShrink()
485 */ 442 */
486 cx_attr_nonnull 443 cx_attr_nonnull
487 cx_attr_export 444 CX_EXPORT int cxBufferMinimumCapacity(CxBuffer *buffer, size_t capacity);
488 int cxBufferMinimumCapacity(
489 CxBuffer *buffer,
490 size_t capacity
491 );
492 445
493 /** 446 /**
494 * Shrinks the capacity of the buffer to fit its current size. 447 * Shrinks the capacity of the buffer to fit its current size.
495 * 448 *
496 * If @p reserve is larger than zero, the buffer is shrunk to its size plus 449 * If @p reserve is larger than zero, the buffer is shrunk to its size plus
505 * @param buffer the buffer 458 * @param buffer the buffer
506 * @param reserve the number of bytes that shall remain reserved 459 * @param reserve the number of bytes that shall remain reserved
507 * @see cxBufferMinimumCapacity() 460 * @see cxBufferMinimumCapacity()
508 */ 461 */
509 cx_attr_nonnull 462 cx_attr_nonnull
510 cx_attr_export 463 CX_EXPORT void cxBufferShrink(CxBuffer *buffer, size_t reserve);
511 void cxBufferShrink(
512 CxBuffer *buffer,
513 size_t reserve
514 );
515 464
516 /** 465 /**
517 * Writes data to a CxBuffer. 466 * Writes data to a CxBuffer.
518 * 467 *
519 * If automatic flushing is not enabled, the data is simply written into the 468 * If automatic flushing is not enabled, the data is simply written into the
520 * buffer at the current position and the position of the buffer is increased 469 * buffer at the current position, and the position of the buffer is increased
521 * by the number of bytes written. 470 * by the number of bytes written.
522 * 471 *
523 * If flushing is enabled and the buffer needs to flush, the data is flushed to 472 * If flushing is enabled and the buffer needs to flush, the data is flushed to
524 * the target until the target signals that it cannot take more data by 473 * the target until the target signals that it cannot take more data by
525 * returning zero via the respective write function. In that case, the remaining 474 * returning zero via the respective write function. In that case, the remaining
526 * data in this buffer is shifted to the beginning of this buffer so that the 475 * data in this buffer is shifted to the beginning of this buffer so that the
527 * newly available space can be used to append as much data as possible. 476 * newly available space can be used to append as much data as possible.
528 * 477 *
529 * This function only stops writing more elements, when the flush target and this 478 * This function only stops writing more elements when the flush target and this
530 * buffer are both incapable of taking more data or all data has been written. 479 * buffer are both incapable of taking more data or all data has been written.
531 * 480 *
532 * If, after flushing, the number of items that shall be written still exceeds 481 * If, after flushing, the number of items that shall be written still exceeds
533 * the capacity or flush threshold, this function tries to write all items directly 482 * the capacity or flush threshold, this function tries to write all items directly
534 * to the flush target, if possible. 483 * to the flush target, if possible.
535 * 484 *
536 * The number returned by this function is the number of elements from 485 * The number returned by this function is the number of elements from
537 * @c ptr that could be written to either the flush target or the buffer 486 * @c ptr that could be written to either the flush target or the buffer.
538 * (so it does not include the number of items that had been already in the buffer 487 * That means it does @em not include the number of items that were already in
539 * in were flushed during the process). 488 * the buffer and were also flushed during the process.
540 * 489 *
541 * @attention 490 * @attention
542 * When @p size is larger than one and the contents of the buffer are not aligned 491 * When @p size is larger than one and the contents of the buffer are not aligned
543 * with @p size, flushing stops after all complete items have been flushed, leaving 492 * with @p size, flushing stops after all complete items have been flushed, leaving
544 * the mis-aligned part in the buffer. 493 * the misaligned part in the buffer.
545 * Afterward, this function only writes as many items as possible to the buffer. 494 * Afterward, this function only writes as many items as possible to the buffer.
546 * 495 *
547 * @note The signature is compatible with the fwrite() family of functions. 496 * @note The signature is compatible with the fwrite() family of functions.
548 * 497 *
549 * @param ptr a pointer to the memory area containing the bytes to be written 498 * @param ptr a pointer to the memory area containing the bytes to be written
553 * @return the total count of elements written 502 * @return the total count of elements written
554 * @see cxBufferAppend() 503 * @see cxBufferAppend()
555 * @see cxBufferRead() 504 * @see cxBufferRead()
556 */ 505 */
557 cx_attr_nonnull 506 cx_attr_nonnull
558 cx_attr_export 507 CX_EXPORT size_t cxBufferWrite(const void *ptr, size_t size,
559 size_t cxBufferWrite( 508 size_t nitems, CxBuffer *buffer);
560 const void *ptr,
561 size_t size,
562 size_t nitems,
563 CxBuffer *buffer
564 );
565 509
566 /** 510 /**
567 * Appends data to a CxBuffer. 511 * Appends data to a CxBuffer.
568 * 512 *
569 * The data is always appended to current data within the buffer, 513 * The data is always appended to current data within the buffer,
581 * @return the total count of elements written 525 * @return the total count of elements written
582 * @see cxBufferWrite() 526 * @see cxBufferWrite()
583 * @see cxBufferRead() 527 * @see cxBufferRead()
584 */ 528 */
585 cx_attr_nonnull 529 cx_attr_nonnull
586 cx_attr_export 530 CX_EXPORT size_t cxBufferAppend(const void *ptr, size_t size,
587 size_t cxBufferAppend( 531 size_t nitems, CxBuffer *buffer);
588 const void *ptr,
589 size_t size,
590 size_t nitems,
591 CxBuffer *buffer
592 );
593 532
594 /** 533 /**
595 * Performs a single flush-run on the specified buffer. 534 * Performs a single flush-run on the specified buffer.
596 * 535 *
597 * Does nothing when the position in the buffer is zero. 536 * Does nothing when the position in the buffer is zero.
612 * @par Example 1 551 * @par Example 1
613 * Assume you have a buffer with size 340 and you are 552 * Assume you have a buffer with size 340 and you are
614 * at position 200. The flush configuration is 553 * at position 200. The flush configuration is
615 * @c blkmax=4 and @c blksize=64 . 554 * @c blkmax=4 and @c blksize=64 .
616 * Assume that the entire flush operation is successful. 555 * Assume that the entire flush operation is successful.
617 * All 200 bytes on the left hand-side from the current 556 * All 200 bytes on the left-hand-side from the current
618 * position are written. 557 * position are written.
619 * That means, the size of the buffer is now 140 and the 558 * That means the size of the buffer is now 140 and the
620 * position is zero. 559 * position is zero.
621 * 560 *
622 * @par Example 2 561 * @par Example 2
623 * Same as Example 1, but now the @c blkmax is 1. 562 * Same as Example 1, but now the @c blkmax is 1.
624 * The size of the buffer is now 276 and the position is 136. 563 * The size of the buffer is now 276, and the position is 136.
625 * 564 *
626 * @par Example 3 565 * @par Example 3
627 * Same as Example 1, but now assume the flush target 566 * Same as Example 1, but now assume the flush target
628 * only accepts 100 bytes before returning zero. 567 * only accepts 100 bytes before returning zero.
629 * That means, the flush operations manages to flush 568 * That means the flush operation manages to flush
630 * one complete block and one partial block, ending 569 * one complete block and one partial block, ending
631 * up with a buffer with size 240 and position 100. 570 * up with a buffer with size 240 and position 100.
632 * 571 *
633 * @remark Just returns zero when flushing was not enabled with 572 * @remark Just returns zero when flushing was not enabled with
634 * cxBufferEnableFlushing(). 573 * cxBufferEnableFlushing().
635 * 574 *
636 * @remark When the buffer uses copy-on-write, the memory 575 * @remark When the buffer uses copy-on-write, the memory
637 * is copied first, before attempting any flush. 576 * is copied first, before attempting any flush.
638 * This is, however, considered an erroneous use of the 577 * This is, however, considered an erroneous use of the
639 * buffer, because it does not make much sense to put 578 * buffer because it makes little sense to put
640 * readonly data into an UCX buffer for flushing, instead 579 * readonly data into an UCX buffer for flushing instead
641 * of writing it directly to the target. 580 * of writing it directly to the target.
642 * 581 *
643 * @param buffer the buffer 582 * @param buffer the buffer
644 * @return the number of successfully flushed bytes 583 * @return the number of successfully flushed bytes
645 * @see cxBufferEnableFlushing() 584 * @see cxBufferEnableFlushing()
646 */ 585 */
647 cx_attr_nonnull 586 cx_attr_nonnull
648 cx_attr_export 587 CX_EXPORT size_t cxBufferFlush(CxBuffer *buffer);
649 size_t cxBufferFlush(CxBuffer *buffer);
650 588
651 /** 589 /**
652 * Reads data from a CxBuffer. 590 * Reads data from a CxBuffer.
653 * 591 *
654 * The position of the buffer is increased by the number of bytes read. 592 * The position of the buffer is increased by the number of bytes read.
662 * @return the total number of elements read 600 * @return the total number of elements read
663 * @see cxBufferWrite() 601 * @see cxBufferWrite()
664 * @see cxBufferAppend() 602 * @see cxBufferAppend()
665 */ 603 */
666 cx_attr_nonnull 604 cx_attr_nonnull
667 cx_attr_export 605 CX_EXPORT size_t cxBufferRead(void *ptr, size_t size,
668 size_t cxBufferRead( 606 size_t nitems, CxBuffer *buffer);
669 void *ptr,
670 size_t size,
671 size_t nitems,
672 CxBuffer *buffer
673 );
674 607
675 /** 608 /**
676 * Writes a character to a buffer. 609 * Writes a character to a buffer.
677 * 610 *
678 * The least significant byte of the argument is written to the buffer. If the 611 * The least significant byte of the argument is written to the buffer. If the
679 * end of the buffer is reached and #CX_BUFFER_AUTO_EXTEND feature is enabled, 612 * end of the buffer is reached and #CX_BUFFER_AUTO_EXTEND feature is enabled,
680 * the buffer capacity is extended by cxBufferMinimumCapacity(). If the feature 613 * the buffer capacity is extended by cxBufferMinimumCapacity(). If the feature
681 * is disabled or buffer extension fails, @c EOF is returned. 614 * is disabled or the buffer extension fails, @c EOF is returned.
682 * 615 *
683 * On successful write, the position of the buffer is increased. 616 * On successful writing, the position of the buffer is increased.
684 * 617 *
685 * If you just want to write a null-terminator at the current position, you 618 * If you just want to write a null-terminator at the current position, you
686 * should use cxBufferTerminate() instead. 619 * should use cxBufferTerminate() instead.
687 * 620 *
688 * @param buffer the buffer to write to 621 * @param buffer the buffer to write to
689 * @param c the character to write 622 * @param c the character to write
690 * @return the byte that has been written or @c EOF when the end of the stream is 623 * @return the byte that has been written or @c EOF when the end of the stream is
691 * reached and automatic extension is not enabled or not possible 624 * reached, and automatic extension is not enabled or not possible
692 * @see cxBufferTerminate() 625 * @see cxBufferTerminate()
693 */ 626 */
694 cx_attr_nonnull 627 cx_attr_nonnull
695 cx_attr_export 628 CX_EXPORT int cxBufferPut(CxBuffer *buffer, int c);
696 int cxBufferPut(
697 CxBuffer *buffer,
698 int c
699 );
700 629
701 /** 630 /**
702 * Writes a terminating zero to a buffer at the current position. 631 * Writes a terminating zero to a buffer at the current position.
703 * 632 *
704 * If successful, sets the size to the current position and advances the position by one. 633 * If successful, sets the size to the current position and advances the position by one.
708 * 637 *
709 * @param buffer the buffer to write to 638 * @param buffer the buffer to write to
710 * @return zero, if the terminator could be written, non-zero otherwise 639 * @return zero, if the terminator could be written, non-zero otherwise
711 */ 640 */
712 cx_attr_nonnull 641 cx_attr_nonnull
713 cx_attr_export 642 CX_EXPORT int cxBufferTerminate(CxBuffer *buffer);
714 int cxBufferTerminate(CxBuffer *buffer);
715 643
716 /** 644 /**
717 * Writes a string to a buffer. 645 * Writes a string to a buffer.
718 * 646 *
719 * This is a convenience function for <code>cxBufferWrite(str, 1, strlen(str), buffer)</code>. 647 * This is a convenience function for <code>cxBufferWrite(str, 1, strlen(str), buffer)</code>.
720 * 648 *
721 * @param buffer the buffer 649 * @param buffer the buffer
722 * @param str the zero-terminated string 650 * @param str the zero-terminated string
723 * @return the number of bytes written 651 * @return the number of bytes written
724 */ 652 */
725 cx_attr_nonnull 653 cx_attr_nonnull cx_attr_cstr_arg(2)
726 cx_attr_cstr_arg(2) 654 CX_EXPORT size_t cxBufferPutString(CxBuffer *buffer, const char *str);
727 cx_attr_export
728 size_t cxBufferPutString(
729 CxBuffer *buffer,
730 const char *str
731 );
732 655
733 /** 656 /**
734 * Gets a character from a buffer. 657 * Gets a character from a buffer.
735 * 658 *
736 * The current position of the buffer is increased after a successful read. 659 * The current position of the buffer is increased after a successful read.
737 * 660 *
738 * @param buffer the buffer to read from 661 * @param buffer the buffer to read from
739 * @return the character or @c EOF, if the end of the buffer is reached 662 * @return the character or @c EOF, if the end of the buffer is reached
740 */ 663 */
741 cx_attr_nonnull 664 cx_attr_nonnull
742 cx_attr_export 665 CX_EXPORT int cxBufferGet(CxBuffer *buffer);
743 int cxBufferGet(CxBuffer *buffer);
744 666
745 #ifdef __cplusplus 667 #ifdef __cplusplus
746 } 668 }
747 #endif 669 #endif
748 670

mercurial