ucx
UAP Common Extensions
buffer.h
Go to the documentation of this file.
1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3  *
4  * Copyright 2017 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 
46 #ifndef UCX_BUFFER_H
47 #define UCX_BUFFER_H
48 
49 #include "ucx.h"
50 #include <sys/types.h>
51 #include <stdio.h>
52 
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56 
60 #define UCX_BUFFER_DEFAULT 0x00
61 
65 #define UCX_BUFFER_AUTOFREE 0x01
66 
70 #define UCX_BUFFER_AUTOEXTEND 0x02
71 
73 typedef struct {
75  char *space;
77  size_t pos;
79  size_t capacity;
81  size_t size;
88  int flags;
89 } UcxBuffer;
90 
104 UcxBuffer *ucx_buffer_new(void *space, size_t capacity, int flags);
105 
114 void ucx_buffer_free(UcxBuffer* buffer);
115 
128  size_t start, size_t length, int flags);
129 
137 #define ucx_buffer_clone(src,flags) \
138  ucx_buffer_extract(src, 0, (src)->capacity, flags)
139 
140 
171 int ucx_buffer_shift(UcxBuffer* buffer, off_t shift);
172 
182 int ucx_buffer_shift_right(UcxBuffer* buffer, size_t shift);
183 
198 int ucx_buffer_shift_left(UcxBuffer* buffer, size_t shift);
199 
200 
220 int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence);
221 
229 #define ucx_buffer_clear(buffer) memset((buffer)->space, 0, (buffer)->size); \
230  (buffer)->size = 0; (buffer)->pos = 0;
231 
239 int ucx_buffer_eof(UcxBuffer *buffer);
240 
241 
258 int ucx_buffer_extend(UcxBuffer *buffer, size_t additional_bytes);
259 
271 size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems,
272  UcxBuffer *buffer);
273 
285 size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems,
286  UcxBuffer *buffer);
287 
304 int ucx_buffer_putc(UcxBuffer *buffer, int c);
305 
315 int ucx_buffer_getc(UcxBuffer *buffer);
316 
324 size_t ucx_buffer_puts(UcxBuffer *buffer, const char *str);
325 
332 #define ucx_buffer_to_sstr(buffer) sstrn((buffer)->space, (buffer)->size)
333 
334 #ifdef __cplusplus
335 }
336 #endif
337 
338 #endif /* UCX_BUFFER_H */
339 
int flags
Flag register for buffer features.
Definition: buffer.h:88
int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence)
Moves the position of the buffer.
Definition: buffer.c:90
size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems, UcxBuffer *buffer)
Reads data from a UcxBuffer.
Definition: buffer.c:189
int ucx_buffer_shift_left(UcxBuffer *buffer, size_t shift)
Shifts the buffer to the left.
Definition: buffer.c:244
Main UCX Header providing most common definitions.
int ucx_buffer_extend(UcxBuffer *buffer, size_t additional_bytes)
Extends the capacity of the buffer.
Definition: buffer.c:126
void ucx_buffer_free(UcxBuffer *buffer)
Destroys a buffer.
Definition: buffer.c:59
size_t pos
Current position of the buffer.
Definition: buffer.h:77
size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems, UcxBuffer *buffer)
Writes data to a UcxBuffer.
Definition: buffer.c:152
size_t capacity
Current capacity (i.e.
Definition: buffer.h:79
char * space
A pointer to the buffer contents.
Definition: buffer.h:75
int ucx_buffer_eof(UcxBuffer *buffer)
Tests, if the buffer position has exceeded the buffer capacity.
Definition: buffer.c:122
UCX Buffer.
Definition: buffer.h:73
size_t ucx_buffer_puts(UcxBuffer *buffer, const char *str)
Writes a string to a buffer.
Definition: buffer.c:240
int ucx_buffer_putc(UcxBuffer *buffer, int c)
Writes a character to a buffer.
Definition: buffer.c:210
int ucx_buffer_shift(UcxBuffer *buffer, off_t shift)
Shifts the contents of the buffer by the given offset.
Definition: buffer.c:289
UcxBuffer * ucx_buffer_new(void *space, size_t capacity, int flags)
Creates a new buffer.
Definition: buffer.c:35
UcxBuffer * ucx_buffer_extract(UcxBuffer *src, size_t start, size_t length, int flags)
Creates a new buffer and fills it with extracted content from another buffer.
Definition: buffer.c:66
size_t size
Current size of the buffer content.
Definition: buffer.h:81
int ucx_buffer_getc(UcxBuffer *buffer)
Gets a character from a buffer.
Definition: buffer.c:230
int ucx_buffer_shift_right(UcxBuffer *buffer, size_t shift)
Shifts the buffer to the right.
Definition: buffer.c:260