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 
159 int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence);
160 
168 #define ucx_buffer_clear(buffer) memset(buffer->space, 0, buffer->size); \
169  buffer->size = 0; buffer->pos = 0;
170 
178 int ucx_buffer_eof(UcxBuffer *buffer);
179 
180 
197 int ucx_buffer_extend(UcxBuffer *buffer, size_t additional_bytes);
198 
210 size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems,
211  UcxBuffer *buffer);
212 
224 size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems,
225  UcxBuffer *buffer);
226 
243 int ucx_buffer_putc(UcxBuffer *buffer, int c);
244 
254 int ucx_buffer_getc(UcxBuffer *buffer);
255 
263 size_t ucx_buffer_puts(UcxBuffer *buffer, char *str);
264 
265 #ifdef __cplusplus
266 }
267 #endif
268 
269 #endif /* UCX_BUFFER_H */
270 
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:186
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
int ucx_buffer_putc(UcxBuffer *buffer, int c)
Writes a character to a buffer.
Definition: buffer.c:204
UcxBuffer * ucx_buffer_new(void *space, size_t capacity, int flags)
Creates a new buffer.
Definition: buffer.c:35
size_t ucx_buffer_puts(UcxBuffer *buffer, char *str)
Writes a string to a buffer.
Definition: buffer.c:234
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:224