universe@103: /* universe@103: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@103: * universe@103: * Copyright 2013 Olaf Wintermann. All rights reserved. universe@103: * universe@103: * Redistribution and use in source and binary forms, with or without universe@103: * modification, are permitted provided that the following conditions are met: universe@103: * universe@103: * 1. Redistributions of source code must retain the above copyright universe@103: * notice, this list of conditions and the following disclaimer. universe@103: * universe@103: * 2. Redistributions in binary form must reproduce the above copyright universe@103: * notice, this list of conditions and the following disclaimer in the universe@103: * documentation and/or other materials provided with the distribution. universe@103: * universe@103: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@103: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@103: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@103: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@103: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@103: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@103: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@103: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@103: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@103: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@103: * POSSIBILITY OF SUCH DAMAGE. universe@103: */ universe@103: olaf@120: #ifndef UCX_BUFFER_H olaf@120: #define UCX_BUFFER_H universe@56: universe@69: #include "ucx.h" universe@62: #include universe@56: #include universe@56: universe@56: #ifdef __cplusplus universe@56: extern "C" { universe@56: #endif universe@56: universe@85: /* no autoextend or autofree behaviour */ universe@61: #define UCX_BUFFER_DEFAULT 0x00 universe@85: /* the buffer shall free the occupied memory space */ universe@61: #define UCX_BUFFER_AUTOFREE 0x01 universe@64: /* the buffer may automatically double its size on write operations */ universe@63: #define UCX_BUFFER_AUTOEXTEND 0x02 universe@56: universe@64: /* the user shall not modify values, but may get the latest pointer */ universe@63: typedef struct { olaf@76: char *space; universe@63: size_t pos; olaf@76: size_t capacity; universe@63: size_t size; universe@63: int flags; universe@63: } UcxBuffer; universe@56: universe@61: /* if space is NULL, new space is allocated and the autofree flag is enforced */ olaf@76: UcxBuffer *ucx_buffer_new(void *space, size_t size, int flags); universe@60: void ucx_buffer_free(UcxBuffer* buffer); universe@56: universe@56: /* universe@62: * the autofree flag is enforced for the new buffer universe@62: * if length is zero, the whole remaining buffer shall be extracted universe@62: * the position of the new buffer is set to zero universe@62: */ olaf@76: UcxBuffer* ucx_buffer_extract(UcxBuffer *src, universe@62: size_t start, size_t length, int flags); universe@62: #define ucx_buffer_clone(src,flags) \ universe@62: ucx_buffer_extract(src, 0, 0, flags) universe@62: universe@62: /* universe@60: * Moves the position of the buffer to a new position relative to whence. universe@56: * universe@56: * SEEK_SET marks the start of the buffer universe@56: * SEEK_CUR marks the current position universe@56: * SEEK_END marks the first 0-byte in the buffer universe@56: * universe@85: * ucx_buffer_seek returns 0 on success and -1 if the new position is beyond the universe@60: * bounds of the allocated buffer. In that case the position of the buffer universe@56: * remains unchanged. universe@56: * universe@56: */ universe@60: int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence); universe@56: universe@85: #define ucx_buffer_clear(buffer) memset(buffer->space, 0, buffer->size); \ universe@85: buffer->size = 0; buffer->pos = 0; universe@85: universe@56: /* olaf@76: * returns non-zero, if the current buffer position has exceeded the last universe@56: * available byte of the underlying buffer universe@56: * universe@56: */ universe@60: int ucx_buffer_eof(UcxBuffer *buffer); universe@56: olaf@76: olaf@76: int ucx_buffere_extend(UcxBuffer *buffer, size_t len); olaf@76: olaf@76: size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems, olaf@76: UcxBuffer *buffer); olaf@76: olaf@76: size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems, olaf@76: UcxBuffer *buffer); olaf@76: universe@60: int ucx_buffer_putc(UcxBuffer *b, int c); universe@60: int ucx_buffer_getc(UcxBuffer *b); universe@56: olaf@76: olaf@76: /* olaf@76: * copies all bytes from s1 to s2 olaf@76: * uses the read function r to read from s1 und writes the data using the olaf@76: * write function w to s2 olaf@76: * returns the number of bytes copied olaf@76: */ olaf@76: size_t ucx_buffer_generic_copy(void *s1, void *s2, read_func r, write_func w, olaf@76: size_t bufsize); olaf@76: olaf@86: size_t ucx_buffer_generic_ncopy(void *s1, void *s2, read_func r, write_func w, olaf@86: size_t bufsize, size_t n); olaf@76: olaf@86: #define UCX_DEFAULT_BUFFER_SIZE 0x1000 olaf@76: olaf@76: #define ucx_buffer_copy(s1,s2,r,w) \ olaf@76: ucx_buffer_generic_copy(s1, s2, (read_func)r, (write_func)w, \ olaf@76: UCX_DEFAULT_BUFFER_SIZE) olaf@76: olaf@86: #define ucx_buffer_ncopy(s1,s2,r,w, n) \ olaf@86: ucx_buffer_generic_ncopy(s1, s2, (read_func)r, (write_func)w, \ olaf@86: UCX_DEFAULT_BUFFER_SIZE, n) olaf@86: universe@56: #ifdef __cplusplus universe@56: } universe@56: #endif universe@56: olaf@120: #endif /* UCX_BUFFER_H */ universe@56: