Fri, 30 Nov 2012 13:10:58 +0100
fixed buffer
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 { |
olaf@76 | 19 | char *space; |
universe@63 | 20 | size_t pos; |
olaf@76 | 21 | size_t capacity; |
universe@63 | 22 | size_t size; |
universe@63 | 23 | int flags; |
universe@63 | 24 | } UcxBuffer; |
universe@56 | 25 | |
universe@61 | 26 | /* if space is NULL, new space is allocated and the autofree flag is enforced */ |
olaf@76 | 27 | UcxBuffer *ucx_buffer_new(void *space, size_t size, int flags); |
universe@60 | 28 | void ucx_buffer_free(UcxBuffer* buffer); |
universe@56 | 29 | |
universe@56 | 30 | /* |
universe@62 | 31 | * the autofree flag is enforced for the new buffer |
universe@62 | 32 | * if length is zero, the whole remaining buffer shall be extracted |
universe@62 | 33 | * the position of the new buffer is set to zero |
universe@62 | 34 | */ |
olaf@76 | 35 | UcxBuffer* ucx_buffer_extract(UcxBuffer *src, |
universe@62 | 36 | size_t start, size_t length, int flags); |
universe@62 | 37 | #define ucx_buffer_clone(src,flags) \ |
universe@62 | 38 | ucx_buffer_extract(src, 0, 0, flags) |
universe@62 | 39 | |
universe@62 | 40 | /* |
universe@60 | 41 | * Moves the position of the buffer to a new position relative to whence. |
universe@56 | 42 | * |
universe@56 | 43 | * SEEK_SET marks the start of the buffer |
universe@56 | 44 | * SEEK_CUR marks the current position |
universe@56 | 45 | * SEEK_END marks the first 0-byte in the buffer |
universe@56 | 46 | * |
universe@56 | 47 | * ucx_memseek returns 0 on success and -1 if the new position is beyond the |
universe@60 | 48 | * bounds of the allocated buffer. In that case the position of the buffer |
universe@56 | 49 | * remains unchanged. |
universe@56 | 50 | * |
universe@56 | 51 | */ |
universe@60 | 52 | int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence); |
universe@56 | 53 | |
universe@56 | 54 | /* |
olaf@76 | 55 | * returns non-zero, if the current buffer position has exceeded the last |
universe@56 | 56 | * available byte of the underlying buffer |
universe@56 | 57 | * |
universe@56 | 58 | */ |
universe@60 | 59 | int ucx_buffer_eof(UcxBuffer *buffer); |
universe@56 | 60 | |
olaf@76 | 61 | |
olaf@76 | 62 | int ucx_buffere_extend(UcxBuffer *buffer, size_t len); |
olaf@76 | 63 | |
olaf@76 | 64 | size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems, |
olaf@76 | 65 | UcxBuffer *buffer); |
olaf@76 | 66 | |
olaf@76 | 67 | size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems, |
olaf@76 | 68 | UcxBuffer *buffer); |
olaf@76 | 69 | |
universe@64 | 70 | /* when autoextend is enabled, ensure you get the latest pointer to the data */ |
olaf@76 | 71 | //define ucx_buffer_write(data, itemsize, nitems, buffer) \ |
olaf@76 | 72 | // ucx_bufio(data, itemsize, nitems, buffer, 0) |
olaf@76 | 73 | //define ucx_buffer_read(data, itemsize, nitems, buffer) \ |
olaf@76 | 74 | // ucx_bufio(data, itemsize, nitems, buffer, 1) |
universe@60 | 75 | int ucx_buffer_putc(UcxBuffer *b, int c); |
universe@60 | 76 | int ucx_buffer_getc(UcxBuffer *b); |
universe@56 | 77 | |
olaf@76 | 78 | |
olaf@76 | 79 | /* |
olaf@76 | 80 | * copies all bytes from s1 to s2 |
olaf@76 | 81 | * uses the read function r to read from s1 und writes the data using the |
olaf@76 | 82 | * write function w to s2 |
olaf@76 | 83 | * returns the number of bytes copied |
olaf@76 | 84 | */ |
olaf@76 | 85 | size_t ucx_buffer_generic_copy(void *s1, void *s2, read_func r, write_func w, |
olaf@76 | 86 | size_t bufsize); |
olaf@76 | 87 | |
olaf@76 | 88 | |
olaf@76 | 89 | #define UCX_DEFAULT_BUFFER_SIZE 0x4000000 |
olaf@76 | 90 | |
olaf@76 | 91 | #define ucx_buffer_copy(s1,s2,r,w) \ |
olaf@76 | 92 | ucx_buffer_generic_copy(s1, s2, (read_func)r, (write_func)w, \ |
olaf@76 | 93 | UCX_DEFAULT_BUFFER_SIZE) |
olaf@76 | 94 | |
universe@56 | 95 | #ifdef __cplusplus |
universe@56 | 96 | } |
universe@56 | 97 | #endif |
universe@56 | 98 | |
universe@60 | 99 | #endif /* BUFFER_H */ |
universe@56 | 100 |