Fri, 30 Nov 2012 21:18:13 +0100
added existing source code
#ifndef BUFFER_H #define BUFFER_H #include "ucx.h" #include <sys/types.h> #include <stdio.h> #ifdef __cplusplus extern "C" { #endif #define UCX_BUFFER_DEFAULT 0x00 #define UCX_BUFFER_AUTOFREE 0x01 /* the buffer may automatically double its size on write operations */ #define UCX_BUFFER_AUTOEXTEND 0x02 /* the user shall not modify values, but may get the latest pointer */ typedef struct { char *space; size_t pos; size_t capacity; size_t size; int flags; } UcxBuffer; /* if space is NULL, new space is allocated and the autofree flag is enforced */ UcxBuffer *ucx_buffer_new(void *space, size_t size, int flags); void ucx_buffer_free(UcxBuffer* buffer); /* * the autofree flag is enforced for the new buffer * if length is zero, the whole remaining buffer shall be extracted * the position of the new buffer is set to zero */ UcxBuffer* ucx_buffer_extract(UcxBuffer *src, size_t start, size_t length, int flags); #define ucx_buffer_clone(src,flags) \ ucx_buffer_extract(src, 0, 0, flags) /* * Moves the position of the buffer to a new position relative to whence. * * SEEK_SET marks the start of the buffer * SEEK_CUR marks the current position * SEEK_END marks the first 0-byte in the buffer * * ucx_memseek returns 0 on success and -1 if the new position is beyond the * bounds of the allocated buffer. In that case the position of the buffer * remains unchanged. * */ int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence); /* * returns non-zero, if the current buffer position has exceeded the last * available byte of the underlying buffer * */ int ucx_buffer_eof(UcxBuffer *buffer); int ucx_buffere_extend(UcxBuffer *buffer, size_t len); size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems, UcxBuffer *buffer); size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems, UcxBuffer *buffer); /* when autoextend is enabled, ensure you get the latest pointer to the data */ //define ucx_buffer_write(data, itemsize, nitems, buffer) \ // ucx_bufio(data, itemsize, nitems, buffer, 0) //define ucx_buffer_read(data, itemsize, nitems, buffer) \ // ucx_bufio(data, itemsize, nitems, buffer, 1) int ucx_buffer_putc(UcxBuffer *b, int c); int ucx_buffer_getc(UcxBuffer *b); /* * copies all bytes from s1 to s2 * uses the read function r to read from s1 und writes the data using the * write function w to s2 * returns the number of bytes copied */ size_t ucx_buffer_generic_copy(void *s1, void *s2, read_func r, write_func w, size_t bufsize); #define UCX_DEFAULT_BUFFER_SIZE 0x4000000 #define ucx_buffer_copy(s1,s2,r,w) \ ucx_buffer_generic_copy(s1, s2, (read_func)r, (write_func)w, \ UCX_DEFAULT_BUFFER_SIZE) #ifdef __cplusplus } #endif #endif /* BUFFER_H */