1.1 --- a/src/ucx/buffer.h Mon Dec 30 09:54:10 2019 +0100 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,339 +0,0 @@ 1.4 -/* 1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 1.6 - * 1.7 - * Copyright 2017 Mike Becker, 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 -/** 1.145 - * Shifts the contents of the buffer by the given offset. 1.146 - * 1.147 - * If the offset is positive, the contents are shifted to the right. 1.148 - * If auto extension is enabled, the buffer grows, if necessary. 1.149 - * In case the auto extension fails, this function returns a non-zero value and 1.150 - * no contents are changed. 1.151 - * If auto extension is disabled, the contents that do not fit into the buffer 1.152 - * are discarded. 1.153 - * 1.154 - * If the offset is negative, the contents are shifted to the left where the 1.155 - * first <code>shift</code> bytes are discarded. 1.156 - * The new size of the buffer is the old size minus 1.157 - * the absolute shift value. 1.158 - * If this value is larger than the buffer size, the buffer is emptied (but 1.159 - * not cleared, see the security note below). 1.160 - * 1.161 - * The buffer position gets shifted alongside with the content but is kept 1.162 - * within the boundaries of the buffer. 1.163 - * 1.164 - * <b>Security note:</b> the shifting operation does <em>not</em> erase the 1.165 - * previously occupied memory cells. You can easily do that manually, e.g. by 1.166 - * calling <code>memset(buffer->space, 0, shift)</code> for a right shift or 1.167 - * <code>memset(buffer->size, 0, buffer->capacity-buffer->size)</code> 1.168 - * for a left shift. 1.169 - * 1.170 - * @param buffer the buffer 1.171 - * @param shift the shift offset (negative means left shift) 1.172 - * @return 0 on success, non-zero if a required auto-extension fails 1.173 - */ 1.174 -int ucx_buffer_shift(UcxBuffer* buffer, off_t shift); 1.175 - 1.176 -/** 1.177 - * Shifts the buffer to the right. 1.178 - * See ucx_buffer_shift() for details. 1.179 - * 1.180 - * @param buffer the buffer 1.181 - * @param shift the shift offset 1.182 - * @return 0 on success, non-zero if a required auto-extension fails 1.183 - * @see ucx_buffer_shift() 1.184 - */ 1.185 -int ucx_buffer_shift_right(UcxBuffer* buffer, size_t shift); 1.186 - 1.187 -/** 1.188 - * Shifts the buffer to the left. 1.189 - * 1.190 - * See ucx_buffer_shift() for details. Note, however, that this method expects 1.191 - * a positive shift offset. 1.192 - * 1.193 - * Since a left shift cannot fail due to memory allocation problems, this 1.194 - * function always returns zero. 1.195 - * 1.196 - * @param buffer the buffer 1.197 - * @param shift the shift offset 1.198 - * @return always zero 1.199 - * @see ucx_buffer_shift() 1.200 - */ 1.201 -int ucx_buffer_shift_left(UcxBuffer* buffer, size_t shift); 1.202 - 1.203 - 1.204 -/** 1.205 - * Moves the position of the buffer. 1.206 - * 1.207 - * The new position is relative to the <code>whence</code> argument. 1.208 - * 1.209 - * SEEK_SET marks the start of the buffer. 1.210 - * SEEK_CUR marks the current position. 1.211 - * SEEK_END marks the end of the buffer. 1.212 - * 1.213 - * With an offset of zero, this function sets the buffer position to zero 1.214 - * (SEEK_SET), the buffer size (SEEK_END) or leaves the buffer position 1.215 - * unchanged (SEEK_CUR). 1.216 - * 1.217 - * @param buffer 1.218 - * @param offset position offset relative to <code>whence</code> 1.219 - * @param whence one of SEEK_SET, SEEK_CUR or SEEK_END 1.220 - * @return 0 on success, non-zero if the position is invalid 1.221 - * 1.222 - */ 1.223 -int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence); 1.224 - 1.225 -/** 1.226 - * Clears the buffer by resetting the position and deleting the data. 1.227 - * 1.228 - * The data is deleted by a zeroing it with call to <code>memset()</code>. 1.229 - * 1.230 - * @param buffer the buffer to be cleared 1.231 - */ 1.232 -#define ucx_buffer_clear(buffer) memset((buffer)->space, 0, (buffer)->size); \ 1.233 - (buffer)->size = 0; (buffer)->pos = 0; 1.234 - 1.235 -/** 1.236 - * Tests, if the buffer position has exceeded the buffer capacity. 1.237 - * 1.238 - * @param buffer the buffer to test 1.239 - * @return non-zero, if the current buffer position has exceeded the last 1.240 - * available byte of the buffer. 1.241 - */ 1.242 -int ucx_buffer_eof(UcxBuffer *buffer); 1.243 - 1.244 - 1.245 -/** 1.246 - * Extends the capacity of the buffer. 1.247 - * 1.248 - * <b>Note:</b> The buffer capacity increased by a power of two. I.e. 1.249 - * the buffer capacity is doubled, as long as it would not hold the current 1.250 - * content plus the additional required bytes. 1.251 - * 1.252 - * <b>Attention:</b> the argument provided is the number of <i>additional</i> 1.253 - * bytes the buffer shall hold. It is <b>NOT</b> the total number of bytes the 1.254 - * buffer shall hold. 1.255 - * 1.256 - * @param buffer the buffer to extend 1.257 - * @param additional_bytes the number of additional bytes the buffer shall 1.258 - * <i>at least</i> hold 1.259 - * @return 0 on success or a non-zero value on failure 1.260 - */ 1.261 -int ucx_buffer_extend(UcxBuffer *buffer, size_t additional_bytes); 1.262 - 1.263 -/** 1.264 - * Writes data to a UcxBuffer. 1.265 - * 1.266 - * The position of the buffer is increased by the number of bytes written. 1.267 - * 1.268 - * @param ptr a pointer to the memory area containing the bytes to be written 1.269 - * @param size the length of one element 1.270 - * @param nitems the element count 1.271 - * @param buffer the UcxBuffer to write to 1.272 - * @return the total count of bytes written 1.273 - */ 1.274 -size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems, 1.275 - UcxBuffer *buffer); 1.276 - 1.277 -/** 1.278 - * Reads data from a UcxBuffer. 1.279 - * 1.280 - * The position of the buffer is increased by the number of bytes read. 1.281 - * 1.282 - * @param ptr a pointer to the memory area where to store the read data 1.283 - * @param size the length of one element 1.284 - * @param nitems the element count 1.285 - * @param buffer the UcxBuffer to read from 1.286 - * @return the total number of elements read 1.287 - */ 1.288 -size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems, 1.289 - UcxBuffer *buffer); 1.290 - 1.291 -/** 1.292 - * Writes a character to a buffer. 1.293 - * 1.294 - * The least significant byte of the argument is written to the buffer. If the 1.295 - * end of the buffer is reached and #UCX_BUFFER_AUTOEXTEND feature is enabled, 1.296 - * the buffer capacity is extended by ucx_buffer_extend(). If the feature is 1.297 - * disabled or buffer extension fails, <code>EOF</code> is returned. 1.298 - * 1.299 - * On successful write the position of the buffer is increased. 1.300 - * 1.301 - * @param buffer the buffer to write to 1.302 - * @param c the character to write as <code>int</code> value 1.303 - * @return the byte that has bean written as <code>int</code> value or 1.304 - * <code>EOF</code> when the end of the stream is reached and automatic 1.305 - * extension is not enabled or not possible 1.306 - */ 1.307 -int ucx_buffer_putc(UcxBuffer *buffer, int c); 1.308 - 1.309 -/** 1.310 - * Gets a character from a buffer. 1.311 - * 1.312 - * The current position of the buffer is increased after a successful read. 1.313 - * 1.314 - * @param buffer the buffer to read from 1.315 - * @return the character as <code>int</code> value or <code>EOF</code>, if the 1.316 - * end of the buffer is reached 1.317 - */ 1.318 -int ucx_buffer_getc(UcxBuffer *buffer); 1.319 - 1.320 -/** 1.321 - * Writes a string to a buffer. 1.322 - * 1.323 - * @param buffer the buffer 1.324 - * @param str the string 1.325 - * @return the number of bytes written 1.326 - */ 1.327 -size_t ucx_buffer_puts(UcxBuffer *buffer, const char *str); 1.328 - 1.329 -/** 1.330 - * Returns the complete buffer content as sstr_t. 1.331 - * @param buffer the buffer 1.332 - * @return the result of <code>sstrn()</code> with the buffer space and size 1.333 - * as arguments 1.334 - */ 1.335 -#define ucx_buffer_to_sstr(buffer) sstrn((buffer)->space, (buffer)->size) 1.336 - 1.337 -#ifdef __cplusplus 1.338 -} 1.339 -#endif 1.340 - 1.341 -#endif /* UCX_BUFFER_H */ 1.342 -