src/ucx/buffer.h

Tue, 23 Aug 2016 13:49:38 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Aug 2016 13:49:38 +0200
changeset 39
ac35daceb24c
permissions
-rw-r--r--

adds UCX + changes how the input file is read (uses an consecutive memory area now)

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

mercurial