ucx
UAP Common Extensions
Loading...
Searching...
No Matches
buffer.h
Go to the documentation of this file.
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
45
46#ifndef UCX_BUFFER_H
47#define UCX_BUFFER_H
48
49#include "common.h"
50#include "allocator.h"
51#include "string.h"
52
56#define CX_BUFFER_DEFAULT 0x00
57
64#define CX_BUFFER_FREE_CONTENTS 0x01
65
69#define CX_BUFFER_AUTO_EXTEND 0x02
70
77#define CX_BUFFER_COPY_ON_WRITE 0x04
78
86#define CX_BUFFER_COPY_ON_EXTEND 0x08
87
93#define CX_BUFFER_DO_NOT_FREE 0x10
94
99#define cxBufferWriteFunc ((cx_write_func) cxBufferWrite)
104#define cxBufferReadFunc ((cx_read_func) cxBufferRead)
105
106
110 union {
114 char *space;
118 unsigned char *bytes;
119 };
123 size_t pos;
125 size_t capacity;
129 size_t size;
137 int flags;
138};
139
143typedef struct cx_buffer_s CxBuffer;
144
178 void *space, size_t capacity, int flags);
179
191
202void cxBufferFree(CxBuffer *buffer);
203
230 size_t capacity, int flags);
231
269int cxBufferShift(CxBuffer *buffer, off_t shift);
270
282int cxBufferShiftRight(CxBuffer *buffer, size_t shift);
283
295int cxBufferShiftLeft(CxBuffer *buffer, size_t shift);
296
297
319int cxBufferSeek(CxBuffer *buffer, off_t offset, int whence);
320
333size_t cxBufferPop(CxBuffer *buffer, size_t size, size_t nitems);
334
349
361
371bool cxBufferEof(const CxBuffer *buffer);
372
392int cxBufferReserve(CxBuffer *buffer, size_t capacity);
393
412
432
451void cxBufferShrink(CxBuffer *buffer, size_t reserve);
452
475size_t cxBufferWrite(const void *ptr, size_t size,
476 size_t nitems, CxBuffer *buffer);
477
498size_t cxBufferAppend(const void *ptr, size_t size,
499 size_t nitems, CxBuffer *buffer);
500
517size_t cxBufferRead(void *ptr, size_t size,
518 size_t nitems, CxBuffer *buffer);
519
541int cxBufferPut(CxBuffer *buffer, int c);
542
557
568
578#define cxBufferPutString(buffer, str) cx_buffer_put_string(buffer, cx_strcast(str))
579
590
600#define cxBufferAppendString(buffer, str) cx_buffer_append_string(buffer, cx_strcast(str))
601
612
621 return cx_strn(buffer->space, buffer->size);
622}
623
632 return cx_mutstrn(buffer->space, buffer->size);
633}
634
635#endif // UCX_BUFFER_H
Interface for custom allocators.
struct cx_allocator_s CxAllocator
High-Level type alias for the allocator type.
Definition allocator.h:80
size_t cx_buffer_put_string(CxBuffer *buffer, cxstring str)
Internal function - do not use.
void cxBufferReset(CxBuffer *buffer)
Resets the buffer by resetting the position and size to zero.
size_t cxBufferWrite(const void *ptr, size_t size, size_t nitems, CxBuffer *buffer)
Writes data to a CxBuffer.
bool cxBufferEof(const CxBuffer *buffer)
Tests, if the buffer position has exceeded the buffer size.
CxBuffer * cxBufferCreate(const CxAllocator *allocator, void *space, size_t capacity, int flags)
Allocates and initializes a fresh buffer.
int cxBufferReserve(CxBuffer *buffer, size_t capacity)
Ensures that the buffer has the required capacity.
void cxBufferClear(CxBuffer *buffer)
Clears the buffer by resetting the position and deleting the data.
int cxBufferShift(CxBuffer *buffer, off_t shift)
Shifts the contents of the buffer by the given offset.
size_t cxBufferAppend(const void *ptr, size_t size, size_t nitems, CxBuffer *buffer)
Appends data to a CxBuffer.
void cxBufferFree(CxBuffer *buffer)
Deallocates the buffer.
int cxBufferInit(CxBuffer *buffer, const CxAllocator *allocator, void *space, size_t capacity, int flags)
Initializes a fresh buffer.
int cxBufferShiftLeft(CxBuffer *buffer, size_t shift)
Shifts the buffer to the left.
int cxBufferGet(CxBuffer *buffer)
Gets a character from a buffer.
static cxmutstr cx_bstr_m(CxBuffer *buffer)
Gets the data in a buffer as a cxmutstr.
Definition buffer.h:631
struct cx_buffer_s CxBuffer
UCX buffer.
Definition buffer.h:143
int cxBufferMaximumCapacity(CxBuffer *buffer, size_t capacity)
Limits the buffer's capacity.
int cxBufferMinimumCapacity(CxBuffer *buffer, size_t capacity)
Ensures that the buffer has a minimum capacity.
void cxBufferDestroy(CxBuffer *buffer)
Destroys the buffer contents.
int cxBufferShiftRight(CxBuffer *buffer, size_t shift)
Shifts the buffer to the right.
size_t cxBufferPop(CxBuffer *buffer, size_t size, size_t nitems)
Discards items from the end of the buffer.
int cxBufferTerminate(CxBuffer *buffer)
Writes a terminating zero to a buffer at the current position.
int cxBufferSeek(CxBuffer *buffer, off_t offset, int whence)
Moves the position of the buffer.
static cxstring cx_bstr(CxBuffer *buffer)
Gets the data in a buffer as a cxstring.
Definition buffer.h:620
int cxBufferPut(CxBuffer *buffer, int c)
Writes a character to a buffer.
size_t cxBufferRead(void *ptr, size_t size, size_t nitems, CxBuffer *buffer)
Reads data from a CxBuffer.
void cxBufferShrink(CxBuffer *buffer, size_t reserve)
Shrinks the capacity of the buffer to fit its current size.
size_t cx_buffer_append_string(CxBuffer *buffer, cxstring str)
Internal function - do not use.
Common definitions and feature checks.
#define CX_MALLOC
The attributed function always returns freshly allocated memory.
Definition common.h:156
#define CX_DEALLOC(...)
Not supported in clang.
Definition common.h:172
#define CX_INLINE
Declares a function to be inlined.
Definition common.h:311
#define CX_NONNULL
All pointer arguments must be non-NULL.
Definition common.h:141
#define CX_NODISCARD
Warn about discarded return value.
Definition common.h:256
#define CX_NONNULL_ARG(...)
The specified pointer arguments must be non-NULL.
Definition common.h:146
#define CX_EXTERN
Declares a function with external linkage.
Definition common.h:297
Strings that know their length.
struct cx_string_s cxstring
An immutable string.
Definition string.h:92
struct cx_mutstr_s cxmutstr
A mutable string.
Definition string.h:74
static cxmutstr cx_mutstrn(char *cstring, size_t length)
Wraps a string that does not need to be zero-terminated.
Definition string.h:187
static cxstring cx_strn(const char *cstring, size_t length)
Wraps a string that does not need to be zero-terminated.
Definition string.h:237
Structure for the UCX buffer data.
Definition buffer.h:108
char * space
Data is interpreted as text.
Definition buffer.h:114
size_t capacity
Current capacity (i.e.
Definition buffer.h:125
unsigned char * bytes
Data is interpreted as binary.
Definition buffer.h:118
size_t pos
Current position of the buffer.
Definition buffer.h:123
size_t max_capacity
Maximum capacity that this buffer may grow to.
Definition buffer.h:127
int flags
Flag register for buffer features.
Definition buffer.h:137
size_t size
Current size of the buffer content.
Definition buffer.h:129
const CxAllocator * allocator
The allocator to use for automatic memory management.
Definition buffer.h:121