Mon, 25 Feb 2013 12:18:31 +0100
fixed comments + added ucx_buffer_clear
1 #ifndef BUFFER_H
2 #define BUFFER_H
4 #include "ucx.h"
5 #include <sys/types.h>
6 #include <stdio.h>
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
12 /* no autoextend or autofree behaviour */
13 #define UCX_BUFFER_DEFAULT 0x00
14 /* the buffer shall free the occupied memory space */
15 #define UCX_BUFFER_AUTOFREE 0x01
16 /* the buffer may automatically double its size on write operations */
17 #define UCX_BUFFER_AUTOEXTEND 0x02
19 /* the user shall not modify values, but may get the latest pointer */
20 typedef struct {
21 char *space;
22 size_t pos;
23 size_t capacity;
24 size_t size;
25 int flags;
26 } UcxBuffer;
28 /* if space is NULL, new space is allocated and the autofree flag is enforced */
29 UcxBuffer *ucx_buffer_new(void *space, size_t size, int flags);
30 void ucx_buffer_free(UcxBuffer* buffer);
32 /*
33 * the autofree flag is enforced for the new buffer
34 * if length is zero, the whole remaining buffer shall be extracted
35 * the position of the new buffer is set to zero
36 */
37 UcxBuffer* ucx_buffer_extract(UcxBuffer *src,
38 size_t start, size_t length, int flags);
39 #define ucx_buffer_clone(src,flags) \
40 ucx_buffer_extract(src, 0, 0, flags)
42 /*
43 * Moves the position of the buffer to a new position relative to whence.
44 *
45 * SEEK_SET marks the start of the buffer
46 * SEEK_CUR marks the current position
47 * SEEK_END marks the first 0-byte in the buffer
48 *
49 * ucx_buffer_seek returns 0 on success and -1 if the new position is beyond the
50 * bounds of the allocated buffer. In that case the position of the buffer
51 * remains unchanged.
52 *
53 */
54 int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence);
56 #define ucx_buffer_clear(buffer) memset(buffer->space, 0, buffer->size); \
57 buffer->size = 0; buffer->pos = 0;
59 /*
60 * returns non-zero, if the current buffer position has exceeded the last
61 * available byte of the underlying buffer
62 *
63 */
64 int ucx_buffer_eof(UcxBuffer *buffer);
67 int ucx_buffere_extend(UcxBuffer *buffer, size_t len);
69 size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems,
70 UcxBuffer *buffer);
72 size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems,
73 UcxBuffer *buffer);
75 int ucx_buffer_putc(UcxBuffer *b, int c);
76 int ucx_buffer_getc(UcxBuffer *b);
79 /*
80 * copies all bytes from s1 to s2
81 * uses the read function r to read from s1 und writes the data using the
82 * write function w to s2
83 * returns the number of bytes copied
84 */
85 size_t ucx_buffer_generic_copy(void *s1, void *s2, read_func r, write_func w,
86 size_t bufsize);
89 #define UCX_DEFAULT_BUFFER_SIZE 0x4000000
91 #define ucx_buffer_copy(s1,s2,r,w) \
92 ucx_buffer_generic_copy(s1, s2, (read_func)r, (write_func)w, \
93 UCX_DEFAULT_BUFFER_SIZE)
95 #ifdef __cplusplus
96 }
97 #endif
99 #endif /* BUFFER_H */