Mon, 25 Feb 2013 13:25:07 +0100
added generic ncopy
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@85 | 12 | /* no autoextend or autofree behaviour */ |
universe@61 | 13 | #define UCX_BUFFER_DEFAULT 0x00 |
universe@85 | 14 | /* the buffer shall free the occupied memory space */ |
universe@61 | 15 | #define UCX_BUFFER_AUTOFREE 0x01 |
universe@64 | 16 | /* the buffer may automatically double its size on write operations */ |
universe@63 | 17 | #define UCX_BUFFER_AUTOEXTEND 0x02 |
universe@56 | 18 | |
universe@64 | 19 | /* the user shall not modify values, but may get the latest pointer */ |
universe@63 | 20 | typedef struct { |
olaf@76 | 21 | char *space; |
universe@63 | 22 | size_t pos; |
olaf@76 | 23 | size_t capacity; |
universe@63 | 24 | size_t size; |
universe@63 | 25 | int flags; |
universe@63 | 26 | } UcxBuffer; |
universe@56 | 27 | |
universe@61 | 28 | /* if space is NULL, new space is allocated and the autofree flag is enforced */ |
olaf@76 | 29 | UcxBuffer *ucx_buffer_new(void *space, size_t size, int flags); |
universe@60 | 30 | void ucx_buffer_free(UcxBuffer* buffer); |
universe@56 | 31 | |
universe@56 | 32 | /* |
universe@62 | 33 | * the autofree flag is enforced for the new buffer |
universe@62 | 34 | * if length is zero, the whole remaining buffer shall be extracted |
universe@62 | 35 | * the position of the new buffer is set to zero |
universe@62 | 36 | */ |
olaf@76 | 37 | UcxBuffer* ucx_buffer_extract(UcxBuffer *src, |
universe@62 | 38 | size_t start, size_t length, int flags); |
universe@62 | 39 | #define ucx_buffer_clone(src,flags) \ |
universe@62 | 40 | ucx_buffer_extract(src, 0, 0, flags) |
universe@62 | 41 | |
universe@62 | 42 | /* |
universe@60 | 43 | * Moves the position of the buffer to a new position relative to whence. |
universe@56 | 44 | * |
universe@56 | 45 | * SEEK_SET marks the start of the buffer |
universe@56 | 46 | * SEEK_CUR marks the current position |
universe@56 | 47 | * SEEK_END marks the first 0-byte in the buffer |
universe@56 | 48 | * |
universe@85 | 49 | * ucx_buffer_seek returns 0 on success and -1 if the new position is beyond the |
universe@60 | 50 | * bounds of the allocated buffer. In that case the position of the buffer |
universe@56 | 51 | * remains unchanged. |
universe@56 | 52 | * |
universe@56 | 53 | */ |
universe@60 | 54 | int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence); |
universe@56 | 55 | |
universe@85 | 56 | #define ucx_buffer_clear(buffer) memset(buffer->space, 0, buffer->size); \ |
universe@85 | 57 | buffer->size = 0; buffer->pos = 0; |
universe@85 | 58 | |
universe@56 | 59 | /* |
olaf@76 | 60 | * returns non-zero, if the current buffer position has exceeded the last |
universe@56 | 61 | * available byte of the underlying buffer |
universe@56 | 62 | * |
universe@56 | 63 | */ |
universe@60 | 64 | int ucx_buffer_eof(UcxBuffer *buffer); |
universe@56 | 65 | |
olaf@76 | 66 | |
olaf@76 | 67 | int ucx_buffere_extend(UcxBuffer *buffer, size_t len); |
olaf@76 | 68 | |
olaf@76 | 69 | size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems, |
olaf@76 | 70 | UcxBuffer *buffer); |
olaf@76 | 71 | |
olaf@76 | 72 | size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems, |
olaf@76 | 73 | UcxBuffer *buffer); |
olaf@76 | 74 | |
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@86 | 88 | size_t ucx_buffer_generic_ncopy(void *s1, void *s2, read_func r, write_func w, |
olaf@86 | 89 | size_t bufsize, size_t n); |
olaf@76 | 90 | |
olaf@86 | 91 | #define UCX_DEFAULT_BUFFER_SIZE 0x1000 |
olaf@76 | 92 | |
olaf@76 | 93 | #define ucx_buffer_copy(s1,s2,r,w) \ |
olaf@76 | 94 | ucx_buffer_generic_copy(s1, s2, (read_func)r, (write_func)w, \ |
olaf@76 | 95 | UCX_DEFAULT_BUFFER_SIZE) |
olaf@76 | 96 | |
olaf@86 | 97 | #define ucx_buffer_ncopy(s1,s2,r,w, n) \ |
olaf@86 | 98 | ucx_buffer_generic_ncopy(s1, s2, (read_func)r, (write_func)w, \ |
olaf@86 | 99 | UCX_DEFAULT_BUFFER_SIZE, n) |
olaf@86 | 100 | |
universe@56 | 101 | #ifdef __cplusplus |
universe@56 | 102 | } |
universe@56 | 103 | #endif |
universe@56 | 104 | |
universe@60 | 105 | #endif /* BUFFER_H */ |
universe@56 | 106 |