ucx/utils.h

Fri, 26 Feb 2016 16:33:04 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 26 Feb 2016 16:33:04 +0100
changeset 218
b20d6088795c
parent 192
1e51558b9d09
child 222
e0f850709a5c
permissions
-rw-r--r--

fixed further usages of SIZE_MAX

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2015 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 utils.h
    31  * 
    32  * Compare, copy and printf functions.
    33  * 
    34  * @author Mike Becker
    35  * @author Olaf Wintermann
    36  */
    38 #ifndef UCX_UTILS_H
    39 #define UCX_UTILS_H
    41 #ifdef __cplusplus
    42 extern "C" {
    43 #endif
    45 #include "ucx.h"
    46 #include "string.h"
    47 #include "allocator.h"
    48 #include <inttypes.h>
    49 #include <string.h>
    50 #include <stdarg.h>
    52 /**
    53  * Copies a string.
    54  * @param s the string to copy
    55  * @param data omitted
    56  * @return a pointer to a copy of s1 that can be passed to free(void*)
    57  */
    58 void *ucx_strcpy(void *s, void *data);
    60 /**
    61  * Copies a memory area.
    62  * @param m a pointer to the memory area
    63  * @param n a pointer to the size_t containing the size of the memory area
    64  * @return a pointer to a copy of the specified memory area that can
    65  * be passed to free(void*)
    66  */
    67 void *ucx_memcpy(void *m, void *n);
    70 /**
    71  * Reads data from a stream and writes it to another stream.
    72  * 
    73  * @param src the source stream
    74  * @param dest the destination stream
    75  * @param rfnc the read function
    76  * @param wfnc the write function
    77  * @param buf a pointer to the copy buffer or <code>NULL</code> if a buffer
    78  * shall be implicitly created on the heap
    79  * @param bufsize the size of the copy buffer - if <code>NULL</code> was
    80  * provided for <code>buf</code>, this is the size of the buffer that shall be
    81  * implicitly created
    82  * @param n the maximum number of bytes that shall be copied
    83  * @return the total number of bytes copied
    84   */
    85 size_t ucx_stream_copy(void *src, void *dest, read_func rfnc, write_func wfnc,
    86         char* buf, size_t bufsize, size_t n);
    88 /**
    89  * Shorthand for ucx_stream_copy using the default copy buffer.
    90  * 
    91  * @param src the source stream
    92  * @param dest the destination stream
    93  * @param rfnc the read function
    94  * @param wfnc the write function
    95  * @return total number of bytes copied
    96  */
    97 #define ucx_stream_hcopy(src,dest,rfnc,wfnc) ucx_stream_copy(\
    98         src, dest, (read_func)rfnc, (write_func)wfnc, NULL, 0x100, (size_t)-1)
   100 /**
   101  * Shorthand for ucx_stream_copy using the default copy buffer and a copy limit.
   102  * 
   103  * @param src the source stream
   104  * @param dest the destination stream
   105  * @param rfnc the read function
   106  * @param wfnc the write function
   107  * @param n maximum number of bytes that shall be copied
   108  * @return total number of bytes copied
   109  */
   110 #define ucx_stream_ncopy(src,dest,rfnc,wfnc, n) ucx_stream_copy(\
   111         src, dest, (read_func)rfnc, (write_func)wfnc, NULL, 0x100, n)
   113 /**
   114  * Wraps the strcmp function.
   115  * @param s1 string one
   116  * @param s2 string two
   117  * @param data omitted
   118  * @return the result of strcmp(s1, s2)
   119  */
   120 int ucx_strcmp(void *s1, void *s2, void *data);
   122 /**
   123  * Wraps the strncmp function.
   124  * @param s1 string one
   125  * @param s2 string two
   126  * @param n a pointer to the size_t containing the third strncmp parameter
   127  * @return the result of strncmp(s1, s2, *n)
   128  */
   129 int ucx_strncmp(void *s1, void *s2, void *n);
   131 /**
   132  * Compares two integers of type int.
   133  * @param i1 pointer to integer one
   134  * @param i2 pointer to integer two
   135  * @param data omitted
   136  * @return -1, if *i1 is less than *i2, 0 if both are equal,
   137  * 1 if *i1 is greater than *i2
   138  */
   139 int ucx_intcmp(void *i1, void *i2, void *data);
   141 /**
   142  * Compares two real numbers of type float.
   143  * @param f1 pointer to float one
   144  * @param f2 pointer to float two
   145  * @param data if provided: a pointer to precision (default: 1e-6f)
   146  * @return -1, if *f1 is less than *f2, 0 if both are equal,
   147  * 1 if *f1 is greater than *f2
   148  */
   150 int ucx_floatcmp(void *f1, void *f2, void *data);
   152 /**
   153  * Compares two real numbers of type double.
   154  * @param d1 pointer to double one
   155  * @param d2 pointer to double two
   156  * @param data if provided: a pointer to precision (default: 1e-14)
   157  * @return -1, if *d1 is less than *d2, 0 if both are equal,
   158  * 1 if *d1 is greater than *d2
   159  */
   160 int ucx_doublecmp(void *d1, void *d2, void *data);
   162 /**
   163  * Compares two pointers.
   164  * @param ptr1 pointer one
   165  * @param ptr2 pointer two
   166  * @param data omitted
   167  * @return -1 if ptr1 is less than ptr2, 0 if both are equal,
   168  * 1 if ptr1 is greater than ptr2
   169  */
   170 int ucx_ptrcmp(void *ptr1, void *ptr2, void *data);
   172 /**
   173  * Compares two memory areas.
   174  * @param ptr1 pointer one
   175  * @param ptr2 pointer two
   176  * @param n a pointer to the size_t containing the third parameter for memcmp
   177  * @return the result of memcmp(ptr1, ptr2, *n)
   178  */
   179 int ucx_memcmp(void *ptr1, void *ptr2, void *n);
   181 /**
   182  * A <code>printf()</code> like function which writes the output to a stream by
   183  * using a write_func().
   184  * @param stream the stream the data is written to
   185  * @param wfc the write function
   186  * @param fmt format string
   187  * @param ... additional arguments
   188  * @return the total number of bytes written
   189  */
   190 int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...);
   192 /**
   193  * <code>va_list</code> version of ucx_fprintf().
   194  * @param stream the stream the data is written to
   195  * @param wfc the write function
   196  * @param fmt format string
   197  * @param ap argument list
   198  * @return the total number of bytes written
   199  * @see ucx_fprintf()
   200  */
   201 int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap);
   203 /**
   204  * A <code>printf()</code> like function which allocates space for a sstr_t
   205  * the result is written to.
   206  * 
   207  * <b>Attention</b>: The sstr_t data is allocated with the allocators
   208  * ucx_allocator_malloc() function. So it is implementation dependent, if
   209  * the returned sstr_t.ptr pointer must be passed to the allocators
   210  * ucx_allocator_free() function manually.
   211  * 
   212  * <b>Note</b>: The sstr_t.ptr of the return value will <i>always</i> be
   213  * <code>NULL</code>-terminated.
   214  * 
   215  * @param allocator the UcxAllocator used for allocating the result sstr_t
   216  * @param fmt format string
   217  * @param ... additional arguments
   218  * @return a sstr_t containing the formatted string
   219  */
   220 sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...);
   222 /** Shortcut for ucx_asprintf() with default allocator. */
   223 #define ucx_sprintf(fmt, ...) \
   224     ucx_asprintf(ucx_default_allocator(), fmt, __VA_ARGS__)
   226 /**
   227  * <code>va_list</code> version of ucx_asprintf().
   228  * 
   229  * @param allocator the UcxAllocator used for allocating the result sstr_t
   230  * @param fmt format string
   231  * @param ap argument list
   232  * @return a sstr_t containing the formatted string
   233  * @see ucx_asprintf()
   234  */
   235 sstr_t ucx_vasprintf(UcxAllocator *allocator, const char *fmt, va_list ap);
   237 /**
   238  * A <code>printf()</code> like function which writes the output to an
   239  * UcxBuffer.
   240  * 
   241  * @param buffer the buffer the data is written to
   242  * @param ... format string and additional arguments
   243  * @return the total number of bytes written
   244  * @see ucx_fprintf()
   245  */
   246 #define ucx_bprintf(buffer, ...) ucx_fprintf((UcxBuffer*)buffer, \
   247         (write_func)ucx_buffer_write, __VA_ARGS__)
   249 #ifdef __cplusplus
   250 }
   251 #endif
   253 #endif /* UCX_UTILS_H */

mercurial