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