Fri, 12 Oct 2012 10:54:55 +0200
made the code work with VC++ compiler (use make CONF=windows)
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 #define UCX_BUFFER_DEFAULT 0x00
13 #define UCX_BUFFER_AUTOFREE 0x01
14 /* the buffer may automatically double its size on write operations */
15 #define UCX_BUFFER_AUTOEXTEND 0x02
17 /* the user shall not modify values, but may get the latest pointer */
18 typedef struct {
19 void *space;
20 size_t pos;
21 size_t size;
22 int flags;
23 } UcxBuffer;
25 /* if space is NULL, new space is allocated and the autofree flag is enforced */
26 UcxBuffer *ucx_buffer_new(void *space, size_t length, int flags);
27 void ucx_buffer_free(UcxBuffer* buffer);
29 /*
30 * the autofree flag is enforced for the new buffer
31 * if length is zero, the whole remaining buffer shall be extracted
32 * the position of the new buffer is set to zero
33 */
34 UcxBuffer *restrict ucx_buffer_extract(UcxBuffer *restrict src,
35 size_t start, size_t length, int flags);
36 #define ucx_buffer_clone(src,flags) \
37 ucx_buffer_extract(src, 0, 0, flags)
39 /*
40 * Moves the position of the buffer to a new position relative to whence.
41 *
42 * SEEK_SET marks the start of the buffer
43 * SEEK_CUR marks the current position
44 * SEEK_END marks the first 0-byte in the buffer
45 *
46 * ucx_memseek returns 0 on success and -1 if the new position is beyond the
47 * bounds of the allocated buffer. In that case the position of the buffer
48 * remains unchanged.
49 *
50 */
51 int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence);
53 /*
54 * returns non-zero, iff the current buffer position has exceeded the last
55 * available byte of the underlying buffer
56 *
57 */
58 int ucx_buffer_eof(UcxBuffer *buffer);
60 size_t ucx_bufio(void *d, size_t s, size_t n, UcxBuffer* b, _Bool read);
61 /* when autoextend is enabled, ensure you get the latest pointer to the data */
62 #define ucx_buffer_write(data, itemsize, nitems, buffer) \
63 ucx_bufio(data, itemsize, nitems, buffer, 0)
64 #define ucx_buffer_read(data, itemsize, nitems, buffer) \
65 ucx_bufio(data, itemsize, nitems, buffer, 1)
66 int ucx_buffer_putc(UcxBuffer *b, int c);
67 int ucx_buffer_getc(UcxBuffer *b);
69 #ifdef __cplusplus
70 }
71 #endif
73 #endif /* BUFFER_H */