Wed, 10 Oct 2012 10:46:20 +0200
added extract function
1 #ifndef BUFFER_H
2 #define BUFFER_H
4 #include <sys/types.h>
5 #include <stdio.h>
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
11 #define UCX_BUFFER_DEFAULT 0x00
12 #define UCX_BUFFER_AUTOFREE 0x01
14 /* the user shall not modify values */
15 typedef struct UcxBuffer UcxBuffer;
17 /* if space is NULL, new space is allocated and the autofree flag is enforced */
18 UcxBuffer *ucx_buffer_new(void *space, size_t length, int flags);
19 void ucx_buffer_free(UcxBuffer* buffer);
21 /*
22 * the autofree flag is enforced for the new buffer
23 * if length is zero, the whole remaining buffer shall be extracted
24 * the position of the new buffer is set to zero
25 */
26 UcxBuffer *ucx_buffer_extract(UcxBuffer *src,
27 size_t start, size_t length, int flags);
28 #define ucx_buffer_clone(src,flags) \
29 ucx_buffer_extract(src, 0, 0, flags)
31 /*
32 * Moves the position of the buffer to a new position relative to whence.
33 *
34 * SEEK_SET marks the start of the buffer
35 * SEEK_CUR marks the current position
36 * SEEK_END marks the first 0-byte in the buffer
37 *
38 * ucx_memseek returns 0 on success and -1 if the new position is beyond the
39 * bounds of the allocated buffer. In that case the position of the buffer
40 * remains unchanged.
41 *
42 */
43 int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence);
44 size_t ucx_buffer_tell(UcxBuffer *buffer);
45 size_t ucx_buffer_size(UcxBuffer *buffer);
46 /* returns non-zero, if at least the specified flags are set */
47 int ucx_buffer_testflags(UcxBuffer *buffer, int flags);
49 /*
50 * returns non-zero, iff the current buffer position has exceeded the last
51 * available byte of the underlying buffer
52 *
53 */
54 int ucx_buffer_eof(UcxBuffer *buffer);
56 size_t ucx_bufio(void *d, size_t s, size_t n, UcxBuffer* b, _Bool read);
57 #define ucx_buffer_write(data, itemsize, nitems, buffer) \
58 ucx_bufio(data, itemsize, nitems, buffer, 0)
59 #define ucx_buffer_read(data, itemsize, nitems, buffer) \
60 ucx_bufio(data, itemsize, nitems, buffer, 1)
61 int ucx_buffer_putc(UcxBuffer *b, int c);
62 int ucx_buffer_getc(UcxBuffer *b);
64 #ifdef __cplusplus
65 }
66 #endif
68 #endif /* BUFFER_H */