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
47#ifndef UCX_BUFFER_H
48#define UCX_BUFFER_H
49
50#include "common.h"
51#include "allocator.h"
52
53#ifdef __cplusplus
54extern "C" {
55#endif
56
60#define CX_BUFFER_DEFAULT 0x00
61
65#define CX_BUFFER_FREE_CONTENTS 0x01
66
70#define CX_BUFFER_AUTO_EXTEND 0x02
71
73typedef struct {
75 union {
79 char *space;
83 unsigned char *bytes;
84 };
88 size_t pos;
90 size_t capacity;
92 size_t size;
115
121
126
133 int flags;
135
140
157__attribute__((__nonnull__(1)))
159 CxBuffer *buffer,
160 void *space,
161 size_t capacity,
162 CxAllocator const *allocator,
163 int flags
164);
165
182 void *space,
183 size_t capacity,
184 CxAllocator const *allocator,
185 int flags
186);
187
197__attribute__((__nonnull__))
199
209__attribute__((__nonnull__))
210void cxBufferFree(CxBuffer *buffer);
211
245__attribute__((__nonnull__))
247 CxBuffer *buffer,
248 off_t shift
249);
250
260__attribute__((__nonnull__))
262 CxBuffer *buffer,
263 size_t shift
264);
265
278__attribute__((__nonnull__))
280 CxBuffer *buffer,
281 size_t shift
282);
283
284
304__attribute__((__nonnull__))
306 CxBuffer *buffer,
307 off_t offset,
308 int whence
309);
310
318__attribute__((__nonnull__))
320
328__attribute__((__nonnull__))
329int cxBufferEof(CxBuffer const *buffer);
330
331
341__attribute__((__nonnull__))
343 CxBuffer *buffer,
344 size_t capacity
345);
346
373__attribute__((__nonnull__))
375 void const *ptr,
376 size_t size,
377 size_t nitems,
378 CxBuffer *buffer
379);
380
394__attribute__((__nonnull__))
396 void *ptr,
397 size_t size,
398 size_t nitems,
399 CxBuffer *buffer
400);
401
417__attribute__((__nonnull__))
419 CxBuffer *buffer,
420 int c
421);
422
430__attribute__((__nonnull__))
432 CxBuffer *buffer,
433 const char *str
434);
435
444__attribute__((__nonnull__))
446
447#ifdef __cplusplus
448}
449#endif
450
451#endif // UCX_BUFFER_H
Interface for custom allocators.
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.
void cxBufferFree(CxBuffer *buffer)
Deallocates the buffer.
int cxBufferEof(CxBuffer const *buffer)
Tests, if the buffer position has exceeded the buffer capacity.
CxBuffer * cxBufferCreate(void *space, size_t capacity, CxAllocator const *allocator, int flags)
Allocates and 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.
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.
int cxBufferSeek(CxBuffer *buffer, off_t offset, int whence)
Moves the position of the buffer.
cx_buffer_s CxBuffer
UCX buffer.
Definition: buffer.h:139
int cxBufferInit(CxBuffer *buffer, void *space, size_t capacity, CxAllocator const *allocator, int flags)
Initializes a fresh buffer.
size_t cxBufferWrite(void const *ptr, size_t size, size_t nitems, CxBuffer *buffer)
Writes data to a CxBuffer.
size_t cxBufferPutString(CxBuffer *buffer, const char *str)
Writes a string to a buffer.
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.
Common definitions and feature checks.
#define __attribute__(x)
Removes GNU C attributes where they are not supported.
Definition: common.h:127
size_t(* cx_write_func)(void const *, size_t, size_t, void *)
Function pointer compatible with fwrite-like functions.
Definition: common.h:103
Structure holding the data for an allocator.
Definition: allocator.h:86
Structure for the UCX buffer data.
Definition: buffer.h:73
size_t flush_threshold
The buffer may not extend beyond this threshold before starting to flush.
Definition: buffer.h:97
void * flush_target
The target for flush_func.
Definition: buffer.h:125
size_t flush_blksize
The block size for the elements to flush.
Definition: buffer.h:102
size_t flush_blkmax
The maximum number of blocks to flush in one cycle.
Definition: buffer.h:114
char * space
Data is interpreted as text.
Definition: buffer.h:79
size_t capacity
Current capacity (i.e.
Definition: buffer.h:90
unsigned char * bytes
Data is interpreted as binary.
Definition: buffer.h:83
size_t pos
Current position of the buffer.
Definition: buffer.h:88
int flags
Flag register for buffer features.
Definition: buffer.h:133
CxAllocator const * allocator
The allocator to use for automatic memory management.
Definition: buffer.h:86
size_t size
Current size of the buffer content.
Definition: buffer.h:92
cx_write_func flush_func
The write function used for flushing.
Definition: buffer.h:120