1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/ucx/buffer.h Tue Aug 23 13:49:38 2016 +0200 1.3 @@ -0,0 +1,270 @@ 1.4 +/* 1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 1.6 + * 1.7 + * Copyright 2015 Olaf Wintermann. All rights reserved. 1.8 + * 1.9 + * Redistribution and use in source and binary forms, with or without 1.10 + * modification, are permitted provided that the following conditions are met: 1.11 + * 1.12 + * 1. Redistributions of source code must retain the above copyright 1.13 + * notice, this list of conditions and the following disclaimer. 1.14 + * 1.15 + * 2. Redistributions in binary form must reproduce the above copyright 1.16 + * notice, this list of conditions and the following disclaimer in the 1.17 + * documentation and/or other materials provided with the distribution. 1.18 + * 1.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 1.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 1.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 1.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 1.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 1.29 + * POSSIBILITY OF SUCH DAMAGE. 1.30 + */ 1.31 + 1.32 +/** 1.33 + * @file buffer.h 1.34 + * 1.35 + * Advanced buffer implementation. 1.36 + * 1.37 + * Instances of UcxBuffer can be used to read from or to write to like one 1.38 + * would do with a stream. This allows the use of ucx_stream_copy() to copy 1.39 + * contents from one buffer to another. 1.40 + * 1.41 + * Some features for convenient use of the buffer 1.42 + * can be enabled. See the documentation of the macro constants for more 1.43 + * information. 1.44 + * 1.45 + * @author Mike Becker 1.46 + * @author Olaf Wintermann 1.47 + */ 1.48 + 1.49 +#ifndef UCX_BUFFER_H 1.50 +#define UCX_BUFFER_H 1.51 + 1.52 +#include "ucx.h" 1.53 +#include <sys/types.h> 1.54 +#include <stdio.h> 1.55 + 1.56 +#ifdef __cplusplus 1.57 +extern "C" { 1.58 +#endif 1.59 + 1.60 +/** 1.61 + * No buffer features enabled (all flags cleared). 1.62 + */ 1.63 +#define UCX_BUFFER_DEFAULT 0x00 1.64 + 1.65 +/** 1.66 + * If this flag is enabled, the buffer will automatically free its contents. 1.67 + */ 1.68 +#define UCX_BUFFER_AUTOFREE 0x01 1.69 + 1.70 +/** 1.71 + * If this flag is enabled, the buffer will automatically extends its capacity. 1.72 + */ 1.73 +#define UCX_BUFFER_AUTOEXTEND 0x02 1.74 + 1.75 +/** UCX Buffer. */ 1.76 +typedef struct { 1.77 + /** A pointer to the buffer contents. */ 1.78 + char *space; 1.79 + /** Current position of the buffer. */ 1.80 + size_t pos; 1.81 + /** Current capacity (i.e. maximum size) of the buffer. */ 1.82 + size_t capacity; 1.83 + /** Current size of the buffer content. */ 1.84 + size_t size; 1.85 + /** 1.86 + * Flag register for buffer features. 1.87 + * @see #UCX_BUFFER_DEFAULT 1.88 + * @see #UCX_BUFFER_AUTOFREE 1.89 + * @see #UCX_BUFFER_AUTOEXTEND 1.90 + */ 1.91 + int flags; 1.92 +} UcxBuffer; 1.93 + 1.94 +/** 1.95 + * Creates a new buffer. 1.96 + * 1.97 + * <b>Note:</b> you may provide <code>NULL</code> as argument for 1.98 + * <code>space</code>. Then this function will allocate the space and enforce 1.99 + * the #UCX_BUFFER_AUTOFREE flag. 1.100 + * 1.101 + * @param space pointer to the memory area, or <code>NULL</code> to allocate 1.102 + * new memory 1.103 + * @param capacity the capacity of the buffer 1.104 + * @param flags buffer features (see UcxBuffer.flags) 1.105 + * @return the new buffer 1.106 + */ 1.107 +UcxBuffer *ucx_buffer_new(void *space, size_t capacity, int flags); 1.108 + 1.109 +/** 1.110 + * Destroys a buffer. 1.111 + * 1.112 + * If the #UCX_BUFFER_AUTOFREE feature is enabled, the contents of the buffer 1.113 + * are also freed. 1.114 + * 1.115 + * @param buffer the buffer to destroy 1.116 + */ 1.117 +void ucx_buffer_free(UcxBuffer* buffer); 1.118 + 1.119 +/** 1.120 + * Creates a new buffer and fills it with extracted content from another buffer. 1.121 + * 1.122 + * <b>Note:</b> the #UCX_BUFFER_AUTOFREE feature is enforced for the new buffer. 1.123 + * 1.124 + * @param src the source buffer 1.125 + * @param start the start position of extraction 1.126 + * @param length the count of bytes to extract (must not be zero) 1.127 + * @param flags feature mask for the new buffer 1.128 + * @return a new buffer containing the extraction 1.129 + */ 1.130 +UcxBuffer* ucx_buffer_extract(UcxBuffer *src, 1.131 + size_t start, size_t length, int flags); 1.132 + 1.133 +/** 1.134 + * A shorthand macro for the full extraction of the buffer. 1.135 + * 1.136 + * @param src the source buffer 1.137 + * @param flags feature mask for the new buffer 1.138 + * @return a new buffer with the extracted content 1.139 + */ 1.140 +#define ucx_buffer_clone(src,flags) \ 1.141 + ucx_buffer_extract(src, 0, (src)->capacity, flags) 1.142 + 1.143 +/** 1.144 + * Moves the position of the buffer. 1.145 + * 1.146 + * The new position is relative to the <code>whence</code> argument. 1.147 + * 1.148 + * SEEK_SET marks the start of the buffer. 1.149 + * SEEK_CUR marks the current position. 1.150 + * SEEK_END marks the end of the buffer. 1.151 + * 1.152 + * With an offset of zero, this function sets the buffer position to zero 1.153 + * (SEEK_SET), the buffer size (SEEK_END) or leaves the buffer position 1.154 + * unchanged (SEEK_CUR). 1.155 + * 1.156 + * @param buffer 1.157 + * @param offset position offset relative to <code>whence</code> 1.158 + * @param whence one of SEEK_SET, SEEK_CUR or SEEK_END 1.159 + * @return 0 on success, non-zero if the position is invalid 1.160 + * 1.161 + */ 1.162 +int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence); 1.163 + 1.164 +/** 1.165 + * Clears the buffer by resetting the position and deleting the data. 1.166 + * 1.167 + * The data is deleted by a zeroing it with call to <code>memset()</code>. 1.168 + * 1.169 + * @param buffer the buffer to be cleared 1.170 + */ 1.171 +#define ucx_buffer_clear(buffer) memset(buffer->space, 0, buffer->size); \ 1.172 + buffer->size = 0; buffer->pos = 0; 1.173 + 1.174 +/** 1.175 + * Tests, if the buffer position has exceeded the buffer capacity. 1.176 + * 1.177 + * @param buffer the buffer to test 1.178 + * @return non-zero, if the current buffer position has exceeded the last 1.179 + * available byte of the buffer. 1.180 + */ 1.181 +int ucx_buffer_eof(UcxBuffer *buffer); 1.182 + 1.183 + 1.184 +/** 1.185 + * Extends the capacity of the buffer. 1.186 + * 1.187 + * <b>Note:</b> The buffer capacity increased by a power of two. I.e. 1.188 + * the buffer capacity is doubled, as long as it would not hold the current 1.189 + * content plus the additional required bytes. 1.190 + * 1.191 + * <b>Attention:</b> the argument provided is the number of <i>additional</i> 1.192 + * bytes the buffer shall hold. It is <b>NOT</b> the total number of bytes the 1.193 + * buffer shall hold. 1.194 + * 1.195 + * @param buffer the buffer to extend 1.196 + * @param additional_bytes the number of additional bytes the buffer shall 1.197 + * <i>at least</i> hold 1.198 + * @return 0 on success or a non-zero value on failure 1.199 + */ 1.200 +int ucx_buffer_extend(UcxBuffer *buffer, size_t additional_bytes); 1.201 + 1.202 +/** 1.203 + * Writes data to an UcxBuffer. 1.204 + * 1.205 + * The position of the buffer is increased by the number of bytes written. 1.206 + * 1.207 + * @param ptr a pointer to the memory area containing the bytes to be written 1.208 + * @param size the length of one element 1.209 + * @param nitems the element count 1.210 + * @param buffer the UcxBuffer to write to 1.211 + * @return the total count of bytes written 1.212 + */ 1.213 +size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems, 1.214 + UcxBuffer *buffer); 1.215 + 1.216 +/** 1.217 + * Reads data from an UcxBuffer. 1.218 + * 1.219 + * The position of the buffer is increased by the number of bytes read. 1.220 + * 1.221 + * @param ptr a pointer to the memory area where to store the read data 1.222 + * @param size the length of one element 1.223 + * @param nitems the element count 1.224 + * @param buffer the UcxBuffer to read from 1.225 + * @return the total number of elements read 1.226 + */ 1.227 +size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems, 1.228 + UcxBuffer *buffer); 1.229 + 1.230 +/** 1.231 + * Writes a character to a buffer. 1.232 + * 1.233 + * The least significant byte of the argument is written to the buffer. If the 1.234 + * end of the buffer is reached and #UCX_BUFFER_AUTOEXTEND feature is enabled, 1.235 + * the buffer capacity is extended by ucx_buffer_extend(). If the feature is 1.236 + * disabled or buffer extension fails, <code>EOF</code> is returned. 1.237 + * 1.238 + * On successful write the position of the buffer is increased. 1.239 + * 1.240 + * @param buffer the buffer to write to 1.241 + * @param c the character to write as <code>int</code> value 1.242 + * @return the byte that has bean written as <code>int</code> value or 1.243 + * <code>EOF</code> when the end of the stream is reached and automatic 1.244 + * extension is not enabled or not possible 1.245 + */ 1.246 +int ucx_buffer_putc(UcxBuffer *buffer, int c); 1.247 + 1.248 +/** 1.249 + * Gets a character from a buffer. 1.250 + * 1.251 + * The current position of the buffer is increased after a successful read. 1.252 + * 1.253 + * @param buffer the buffer to read from 1.254 + * @return the character as <code>int</code> value or <code>EOF</code>, if the 1.255 + * end of the buffer is reached 1.256 + */ 1.257 +int ucx_buffer_getc(UcxBuffer *buffer); 1.258 + 1.259 +/** 1.260 + * Writes a string to a buffer. 1.261 + * 1.262 + * @param buffer the buffer 1.263 + * @param str the string 1.264 + * @return the number of bytes written 1.265 + */ 1.266 +size_t ucx_buffer_puts(UcxBuffer *buffer, char *str); 1.267 + 1.268 +#ifdef __cplusplus 1.269 +} 1.270 +#endif 1.271 + 1.272 +#endif /* UCX_BUFFER_H */ 1.273 +