src/ucx/buffer.h

Mon, 14 May 2018 18:27:23 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 14 May 2018 18:27:23 +0200
changeset 315
5b97de37aada
parent 290
d5d6ab809ad3
permissions
-rw-r--r--

finally removes the underscore of ugliness from ucx_str_cmp() and ucx_str_casecmp()

universe@103 1 /*
universe@103 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@103 3 *
universe@259 4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
universe@103 5 *
universe@103 6 * Redistribution and use in source and binary forms, with or without
universe@103 7 * modification, are permitted provided that the following conditions are met:
universe@103 8 *
universe@103 9 * 1. Redistributions of source code must retain the above copyright
universe@103 10 * notice, this list of conditions and the following disclaimer.
universe@103 11 *
universe@103 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@103 13 * notice, this list of conditions and the following disclaimer in the
universe@103 14 * documentation and/or other materials provided with the distribution.
universe@103 15 *
universe@103 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@103 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@103 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@103 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@103 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@103 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@103 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@103 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@103 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@103 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@103 26 * POSSIBILITY OF SUCH DAMAGE.
universe@103 27 */
universe@103 28
universe@140 29 /**
universe@140 30 * @file buffer.h
universe@140 31 *
universe@140 32 * Advanced buffer implementation.
universe@140 33 *
universe@140 34 * Instances of UcxBuffer can be used to read from or to write to like one
universe@140 35 * would do with a stream. This allows the use of ucx_stream_copy() to copy
universe@140 36 * contents from one buffer to another.
universe@140 37 *
universe@140 38 * Some features for convenient use of the buffer
universe@140 39 * can be enabled. See the documentation of the macro constants for more
universe@140 40 * information.
universe@140 41 *
universe@140 42 * @author Mike Becker
universe@140 43 * @author Olaf Wintermann
universe@140 44 */
universe@140 45
olaf@120 46 #ifndef UCX_BUFFER_H
olaf@120 47 #define UCX_BUFFER_H
universe@56 48
universe@259 49 #include "ucx.h"
universe@62 50 #include <sys/types.h>
universe@56 51 #include <stdio.h>
universe@56 52
universe@56 53 #ifdef __cplusplus
universe@56 54 extern "C" {
universe@56 55 #endif
universe@56 56
universe@140 57 /**
universe@140 58 * No buffer features enabled (all flags cleared).
universe@140 59 */
universe@61 60 #define UCX_BUFFER_DEFAULT 0x00
universe@146 61
universe@140 62 /**
universe@140 63 * If this flag is enabled, the buffer will automatically free its contents.
universe@140 64 */
universe@61 65 #define UCX_BUFFER_AUTOFREE 0x01
universe@146 66
universe@140 67 /**
universe@140 68 * If this flag is enabled, the buffer will automatically extends its capacity.
universe@140 69 */
universe@63 70 #define UCX_BUFFER_AUTOEXTEND 0x02
universe@56 71
universe@140 72 /** UCX Buffer. */
universe@63 73 typedef struct {
universe@140 74 /** A pointer to the buffer contents. */
olaf@76 75 char *space;
universe@140 76 /** Current position of the buffer. */
universe@63 77 size_t pos;
universe@140 78 /** Current capacity (i.e. maximum size) of the buffer. */
olaf@76 79 size_t capacity;
universe@140 80 /** Current size of the buffer content. */
universe@63 81 size_t size;
universe@140 82 /**
universe@140 83 * Flag register for buffer features.
universe@140 84 * @see #UCX_BUFFER_DEFAULT
universe@140 85 * @see #UCX_BUFFER_AUTOFREE
universe@140 86 * @see #UCX_BUFFER_AUTOEXTEND
universe@140 87 */
universe@63 88 int flags;
universe@63 89 } UcxBuffer;
universe@56 90
universe@140 91 /**
universe@140 92 * Creates a new buffer.
universe@140 93 *
universe@140 94 * <b>Note:</b> you may provide <code>NULL</code> as argument for
universe@140 95 * <code>space</code>. Then this function will allocate the space and enforce
universe@140 96 * the #UCX_BUFFER_AUTOFREE flag.
universe@140 97 *
universe@140 98 * @param space pointer to the memory area, or <code>NULL</code> to allocate
universe@140 99 * new memory
universe@169 100 * @param capacity the capacity of the buffer
universe@140 101 * @param flags buffer features (see UcxBuffer.flags)
universe@140 102 * @return the new buffer
universe@140 103 */
universe@169 104 UcxBuffer *ucx_buffer_new(void *space, size_t capacity, int flags);
universe@140 105
universe@140 106 /**
universe@140 107 * Destroys a buffer.
universe@140 108 *
universe@140 109 * If the #UCX_BUFFER_AUTOFREE feature is enabled, the contents of the buffer
universe@140 110 * are also freed.
universe@140 111 *
universe@140 112 * @param buffer the buffer to destroy
universe@140 113 */
universe@60 114 void ucx_buffer_free(UcxBuffer* buffer);
universe@56 115
universe@140 116 /**
universe@140 117 * Creates a new buffer and fills it with extracted content from another buffer.
universe@140 118 *
universe@140 119 * <b>Note:</b> the #UCX_BUFFER_AUTOFREE feature is enforced for the new buffer.
universe@140 120 *
universe@140 121 * @param src the source buffer
universe@140 122 * @param start the start position of extraction
universe@168 123 * @param length the count of bytes to extract (must not be zero)
universe@140 124 * @param flags feature mask for the new buffer
universe@163 125 * @return a new buffer containing the extraction
universe@62 126 */
olaf@76 127 UcxBuffer* ucx_buffer_extract(UcxBuffer *src,
universe@62 128 size_t start, size_t length, int flags);
universe@140 129
universe@140 130 /**
universe@140 131 * A shorthand macro for the full extraction of the buffer.
universe@140 132 *
universe@140 133 * @param src the source buffer
universe@140 134 * @param flags feature mask for the new buffer
universe@140 135 * @return a new buffer with the extracted content
universe@140 136 */
universe@62 137 #define ucx_buffer_clone(src,flags) \
universe@168 138 ucx_buffer_extract(src, 0, (src)->capacity, flags)
universe@62 139
universe@290 140
universe@290 141 /**
universe@290 142 * Shifts the contents of the buffer by the given offset.
universe@290 143 *
universe@290 144 * If the offset is positive, the contents are shifted to the right.
universe@290 145 * If auto extension is enabled, the buffer grows, if necessary.
universe@290 146 * In case the auto extension fails, this function returns a non-zero value and
universe@290 147 * no contents are changed.
universe@290 148 * If auto extension is disabled, the contents that do not fit into the buffer
universe@290 149 * are discarded.
universe@290 150 *
universe@290 151 * If the offset is negative, the contents are shifted to the left where the
universe@290 152 * first <code>shift</code> bytes are discarded.
universe@290 153 * The new size of the buffer is the old size minus
universe@290 154 * the absolute shift value.
universe@290 155 * If this value is larger than the buffer size, the buffer is emptied (but
universe@290 156 * not cleared, see the security note below).
universe@290 157 *
universe@290 158 * The buffer position gets shifted alongside with the content but is kept
universe@290 159 * within the boundaries of the buffer.
universe@290 160 *
universe@290 161 * <b>Security note:</b> the shifting operation does <em>not</em> erase the
universe@290 162 * previously occupied memory cells. You can easily do that manually, e.g. by
universe@290 163 * calling <code>memset(buffer->space, 0, shift)</code> for a right shift or
universe@290 164 * <code>memset(buffer->size, 0, buffer->capacity-buffer->size)</code>
universe@290 165 * for a left shift.
universe@290 166 *
universe@290 167 * @param buffer the buffer
universe@290 168 * @param shift the shift offset (negative means left shift)
universe@290 169 * @return 0 on success, non-zero if a required auto-extension fails
universe@290 170 */
universe@290 171 int ucx_buffer_shift(UcxBuffer* buffer, off_t shift);
universe@290 172
universe@290 173 /**
universe@290 174 * Shifts the buffer to the right.
universe@290 175 * See ucx_buffer_shift() for details.
universe@290 176 *
universe@290 177 * @param buffer the buffer
universe@290 178 * @param shift the shift offset
universe@290 179 * @return 0 on success, non-zero if a required auto-extension fails
universe@290 180 * @see ucx_buffer_shift()
universe@290 181 */
universe@290 182 int ucx_buffer_shift_right(UcxBuffer* buffer, size_t shift);
universe@290 183
universe@290 184 /**
universe@290 185 * Shifts the buffer to the left.
universe@290 186 *
universe@290 187 * See ucx_buffer_shift() for details. Note, however, that this method expects
universe@290 188 * a positive shift offset.
universe@290 189 *
universe@290 190 * Since a left shift cannot fail due to memory allocation problems, this
universe@290 191 * function always returns zero.
universe@290 192 *
universe@290 193 * @param buffer the buffer
universe@290 194 * @param shift the shift offset
universe@290 195 * @return always zero
universe@290 196 * @see ucx_buffer_shift()
universe@290 197 */
universe@290 198 int ucx_buffer_shift_left(UcxBuffer* buffer, size_t shift);
universe@290 199
universe@290 200
universe@140 201 /**
universe@140 202 * Moves the position of the buffer.
universe@140 203 *
universe@140 204 * The new position is relative to the <code>whence</code> argument.
universe@56 205 *
universe@140 206 * SEEK_SET marks the start of the buffer.
universe@140 207 * SEEK_CUR marks the current position.
universe@165 208 * SEEK_END marks the end of the buffer.
universe@165 209 *
universe@165 210 * With an offset of zero, this function sets the buffer position to zero
universe@165 211 * (SEEK_SET), the buffer size (SEEK_END) or leaves the buffer position
universe@165 212 * unchanged (SEEK_CUR).
universe@140 213 *
universe@140 214 * @param buffer
universe@140 215 * @param offset position offset relative to <code>whence</code>
universe@140 216 * @param whence one of SEEK_SET, SEEK_CUR or SEEK_END
universe@140 217 * @return 0 on success, non-zero if the position is invalid
universe@56 218 *
universe@56 219 */
universe@60 220 int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence);
universe@56 221
universe@140 222 /**
universe@140 223 * Clears the buffer by resetting the position and deleting the data.
universe@140 224 *
universe@140 225 * The data is deleted by a zeroing it with call to <code>memset()</code>.
universe@140 226 *
universe@140 227 * @param buffer the buffer to be cleared
universe@140 228 */
universe@289 229 #define ucx_buffer_clear(buffer) memset((buffer)->space, 0, (buffer)->size); \
universe@289 230 (buffer)->size = 0; (buffer)->pos = 0;
universe@85 231
universe@140 232 /**
universe@140 233 * Tests, if the buffer position has exceeded the buffer capacity.
universe@140 234 *
universe@140 235 * @param buffer the buffer to test
universe@140 236 * @return non-zero, if the current buffer position has exceeded the last
universe@140 237 * available byte of the buffer.
universe@56 238 */
universe@60 239 int ucx_buffer_eof(UcxBuffer *buffer);
universe@56 240
olaf@76 241
universe@140 242 /**
universe@140 243 * Extends the capacity of the buffer.
universe@140 244 *
universe@140 245 * <b>Note:</b> The buffer capacity increased by a power of two. I.e.
universe@140 246 * the buffer capacity is doubled, as long as it would not hold the current
universe@140 247 * content plus the additional required bytes.
universe@140 248 *
universe@168 249 * <b>Attention:</b> the argument provided is the number of <i>additional</i>
universe@168 250 * bytes the buffer shall hold. It is <b>NOT</b> the total number of bytes the
universe@140 251 * buffer shall hold.
universe@140 252 *
universe@140 253 * @param buffer the buffer to extend
universe@168 254 * @param additional_bytes the number of additional bytes the buffer shall
universe@140 255 * <i>at least</i> hold
universe@140 256 * @return 0 on success or a non-zero value on failure
universe@140 257 */
universe@140 258 int ucx_buffer_extend(UcxBuffer *buffer, size_t additional_bytes);
olaf@76 259
universe@140 260 /**
universe@225 261 * Writes data to a UcxBuffer.
universe@140 262 *
universe@215 263 * The position of the buffer is increased by the number of bytes written.
universe@140 264 *
universe@140 265 * @param ptr a pointer to the memory area containing the bytes to be written
universe@140 266 * @param size the length of one element
universe@140 267 * @param nitems the element count
universe@140 268 * @param buffer the UcxBuffer to write to
universe@140 269 * @return the total count of bytes written
universe@140 270 */
olaf@76 271 size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems,
olaf@76 272 UcxBuffer *buffer);
olaf@76 273
universe@140 274 /**
universe@225 275 * Reads data from a UcxBuffer.
universe@140 276 *
universe@140 277 * The position of the buffer is increased by the number of bytes read.
universe@140 278 *
universe@140 279 * @param ptr a pointer to the memory area where to store the read data
universe@140 280 * @param size the length of one element
universe@140 281 * @param nitems the element count
universe@140 282 * @param buffer the UcxBuffer to read from
universe@166 283 * @return the total number of elements read
universe@140 284 */
olaf@76 285 size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems,
olaf@76 286 UcxBuffer *buffer);
olaf@76 287
universe@140 288 /**
universe@140 289 * Writes a character to a buffer.
universe@140 290 *
universe@140 291 * The least significant byte of the argument is written to the buffer. If the
universe@140 292 * end of the buffer is reached and #UCX_BUFFER_AUTOEXTEND feature is enabled,
universe@140 293 * the buffer capacity is extended by ucx_buffer_extend(). If the feature is
universe@140 294 * disabled or buffer extension fails, <code>EOF</code> is returned.
universe@140 295 *
universe@140 296 * On successful write the position of the buffer is increased.
universe@140 297 *
universe@140 298 * @param buffer the buffer to write to
universe@140 299 * @param c the character to write as <code>int</code> value
universe@140 300 * @return the byte that has bean written as <code>int</code> value or
universe@140 301 * <code>EOF</code> when the end of the stream is reached and automatic
universe@140 302 * extension is not enabled or not possible
universe@140 303 */
universe@140 304 int ucx_buffer_putc(UcxBuffer *buffer, int c);
universe@56 305
universe@140 306 /**
universe@140 307 * Gets a character from a buffer.
universe@140 308 *
universe@140 309 * The current position of the buffer is increased after a successful read.
universe@140 310 *
universe@140 311 * @param buffer the buffer to read from
universe@140 312 * @return the character as <code>int</code> value or <code>EOF</code>, if the
universe@140 313 * end of the buffer is reached
universe@140 314 */
universe@140 315 int ucx_buffer_getc(UcxBuffer *buffer);
olaf@76 316
universe@140 317 /**
universe@140 318 * Writes a string to a buffer.
universe@140 319 *
universe@140 320 * @param buffer the buffer
universe@140 321 * @param str the string
universe@140 322 * @return the number of bytes written
olaf@76 323 */
universe@290 324 size_t ucx_buffer_puts(UcxBuffer *buffer, const char *str);
olaf@86 325
universe@289 326 /**
universe@289 327 * Returns the complete buffer content as sstr_t.
universe@289 328 * @param buffer the buffer
universe@289 329 * @return the result of <code>sstrn()</code> with the buffer space and size
universe@289 330 * as arguments
universe@289 331 */
universe@289 332 #define ucx_buffer_to_sstr(buffer) sstrn((buffer)->space, (buffer)->size)
universe@289 333
universe@56 334 #ifdef __cplusplus
universe@56 335 }
universe@56 336 #endif
universe@56 337
olaf@120 338 #endif /* UCX_BUFFER_H */
universe@56 339

mercurial