ucx/buffer.h

Mon, 05 May 2014 14:52:40 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 05 May 2014 14:52:40 +0200
changeset 165
4d85da1f98db
parent 163
5ec9a2ca6328
child 166
350a0e3898bd
permissions
-rw-r--r--

hotfix for ucx_buffer_seek documentation

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

mercurial