ucx/buffer.h

Sat, 20 Jul 2013 11:13:26 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 20 Jul 2013 11:13:26 +0200
changeset 120
8170f658f017
parent 103
08018864fb91
child 140
15f871f50bfd
permissions
-rw-r--r--

some fixes

     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 #ifndef UCX_BUFFER_H
    30 #define	UCX_BUFFER_H
    32 #include "ucx.h"
    33 #include <sys/types.h>
    34 #include <stdio.h>
    36 #ifdef	__cplusplus
    37 extern "C" {
    38 #endif
    40 /* no autoextend or autofree behaviour */
    41 #define UCX_BUFFER_DEFAULT      0x00
    42 /* the buffer shall free the occupied memory space */
    43 #define UCX_BUFFER_AUTOFREE     0x01
    44 /* the buffer may automatically double its size on write operations */
    45 #define UCX_BUFFER_AUTOEXTEND   0x02
    47 /* the user shall not modify values, but may get the latest pointer */
    48 typedef struct {
    49     char *space;
    50     size_t pos;
    51     size_t capacity;
    52     size_t size;
    53     int flags;
    54 } UcxBuffer;
    56 /* if space is NULL, new space is allocated and the autofree flag is enforced */
    57 UcxBuffer *ucx_buffer_new(void *space, size_t size, int flags);
    58 void ucx_buffer_free(UcxBuffer* buffer);
    60 /*
    61  * the autofree flag is enforced for the new buffer
    62  * if length is zero, the whole remaining buffer shall be extracted
    63  * the position of the new buffer is set to zero
    64  */
    65 UcxBuffer* ucx_buffer_extract(UcxBuffer *src,
    66         size_t start, size_t length, int flags);
    67 #define ucx_buffer_clone(src,flags) \
    68     ucx_buffer_extract(src, 0, 0, flags)
    70 /*
    71  * Moves the position of the buffer to a new position relative to whence.
    72  *
    73  * SEEK_SET marks the start of the buffer
    74  * SEEK_CUR marks the current position
    75  * SEEK_END marks the first 0-byte in the buffer
    76  *
    77  * ucx_buffer_seek returns 0 on success and -1 if the new position is beyond the
    78  * bounds of the allocated buffer. In that case the position of the buffer
    79  * remains unchanged.
    80  *
    81  */
    82 int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence);
    84 #define ucx_buffer_clear(buffer)    memset(buffer->space, 0, buffer->size); \
    85                                     buffer->size = 0; buffer->pos = 0;
    87 /*
    88  * returns non-zero, if the current buffer position has exceeded the last
    89  * available byte of the underlying buffer
    90  *
    91  */
    92 int ucx_buffer_eof(UcxBuffer *buffer);
    95 int ucx_buffere_extend(UcxBuffer *buffer, size_t len);
    97 size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems,
    98         UcxBuffer *buffer);
   100 size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems,
   101         UcxBuffer *buffer);
   103 int ucx_buffer_putc(UcxBuffer *b, int c);
   104 int ucx_buffer_getc(UcxBuffer *b);
   107 /*
   108  * copies all bytes from s1 to s2
   109  * uses the read function r to read from s1 und writes the data using the
   110  * write function w to s2
   111  * returns the number of bytes copied
   112  */
   113 size_t ucx_buffer_generic_copy(void *s1, void *s2, read_func r, write_func w,
   114         size_t bufsize);
   116 size_t ucx_buffer_generic_ncopy(void *s1, void *s2, read_func r, write_func w,
   117         size_t bufsize, size_t n);
   119 #define UCX_DEFAULT_BUFFER_SIZE 0x1000
   121 #define ucx_buffer_copy(s1,s2,r,w) \
   122     ucx_buffer_generic_copy(s1, s2, (read_func)r, (write_func)w, \
   123     UCX_DEFAULT_BUFFER_SIZE)
   125 #define ucx_buffer_ncopy(s1,s2,r,w, n) \
   126     ucx_buffer_generic_ncopy(s1, s2, (read_func)r, (write_func)w, \
   127     UCX_DEFAULT_BUFFER_SIZE, n)
   129 #ifdef	__cplusplus
   130 }
   131 #endif
   133 #endif	/* UCX_BUFFER_H */

mercurial