src/ucx/utils.h

changeset 251
fae240d633fc
parent 250
b7d1317b138e
child 259
2f5dea574a75
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/ucx/utils.h	Tue Oct 17 16:15:41 2017 +0200
     1.3 @@ -0,0 +1,281 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * Copyright 2017 Olaf Wintermann. All rights reserved.
     1.8 + *
     1.9 + * Redistribution and use in source and binary forms, with or without
    1.10 + * modification, are permitted provided that the following conditions are met:
    1.11 + *
    1.12 + *   1. Redistributions of source code must retain the above copyright
    1.13 + *      notice, this list of conditions and the following disclaimer.
    1.14 + *
    1.15 + *   2. Redistributions in binary form must reproduce the above copyright
    1.16 + *      notice, this list of conditions and the following disclaimer in the
    1.17 + *      documentation and/or other materials provided with the distribution.
    1.18 + *
    1.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.29 + * POSSIBILITY OF SUCH DAMAGE.
    1.30 + */
    1.31 +
    1.32 +/**
    1.33 + * @file utils.h
    1.34 + * 
    1.35 + * Compare, copy and printf functions.
    1.36 + * 
    1.37 + * @author Mike Becker
    1.38 + * @author Olaf Wintermann
    1.39 + */
    1.40 +
    1.41 +#ifndef UCX_UTILS_H
    1.42 +#define UCX_UTILS_H
    1.43 +
    1.44 +#ifdef __cplusplus
    1.45 +extern "C" {
    1.46 +#endif
    1.47 +
    1.48 +#include <ucx/ucx.h>
    1.49 +#include <ucx/string.h>
    1.50 +#include <ucx/allocator.h>
    1.51 +#include <inttypes.h>
    1.52 +#include <string.h>
    1.53 +#include <stdarg.h>
    1.54 +
    1.55 +/**
    1.56 + * Default buffer size for ucx_stream_copy() and ucx_stream_ncopy().
    1.57 + */
    1.58 +#define UCX_STREAM_COPY_BUFSIZE 4096
    1.59 +
    1.60 +/**
    1.61 + * Copies a string.
    1.62 + * @param s the string to copy
    1.63 + * @param data omitted
    1.64 + * @return a pointer to a copy of s1 that can be passed to free(void*)
    1.65 + */
    1.66 +void *ucx_strcpy(const void *s, void *data);
    1.67 +
    1.68 +/**
    1.69 + * Copies a memory area.
    1.70 + * @param m a pointer to the memory area
    1.71 + * @param n a pointer to the size_t containing the size of the memory area
    1.72 + * @return a pointer to a copy of the specified memory area that can
    1.73 + * be passed to free(void*)
    1.74 + */
    1.75 +void *ucx_memcpy(const void *m, void *n);
    1.76 +
    1.77 +
    1.78 +/**
    1.79 + * Reads data from a stream and writes it to another stream.
    1.80 + * 
    1.81 + * @param src the source stream
    1.82 + * @param dest the destination stream
    1.83 + * @param rfnc the read function
    1.84 + * @param wfnc the write function
    1.85 + * @param buf a pointer to the copy buffer or <code>NULL</code> if a buffer
    1.86 + * shall be implicitly created on the heap
    1.87 + * @param bufsize the size of the copy buffer - if <code>NULL</code> was
    1.88 + * provided for <code>buf</code>, this is the size of the buffer that shall be
    1.89 + * implicitly created
    1.90 + * @param n the maximum number of bytes that shall be copied
    1.91 + * @return the total number of bytes copied
    1.92 +  */
    1.93 +size_t ucx_stream_bncopy(void *src, void *dest, read_func rfnc, write_func wfnc,
    1.94 +        char* buf, size_t bufsize, size_t n);
    1.95 +
    1.96 +/**
    1.97 + * Shorthand for an unbounded ucx_stream_bncopy call using a default buffer.
    1.98 + * 
    1.99 + * @param src the source stream
   1.100 + * @param dest the destination stream
   1.101 + * @param rfnc the read function
   1.102 + * @param wfnc the write function
   1.103 + * @return total number of bytes copied
   1.104 + * 
   1.105 + * @see #UCX_STREAM_COPY_BUFSIZE
   1.106 + */
   1.107 +#define ucx_stream_copy(src,dest,rfnc,wfnc) ucx_stream_bncopy(\
   1.108 +        src, dest, (read_func)rfnc, (write_func)wfnc, \
   1.109 +        NULL, UCX_STREAM_COPY_BUFSIZE, (size_t)-1)
   1.110 +
   1.111 +/**
   1.112 + * Shorthand for ucx_stream_bncopy using a default copy buffer.
   1.113 + * 
   1.114 + * @param src the source stream
   1.115 + * @param dest the destination stream
   1.116 + * @param rfnc the read function
   1.117 + * @param wfnc the write function
   1.118 + * @param n maximum number of bytes that shall be copied
   1.119 + * @return total number of bytes copied
   1.120 + */
   1.121 +#define ucx_stream_ncopy(src,dest,rfnc,wfnc, n) ucx_stream_bncopy(\
   1.122 +        src, dest, (read_func)rfnc, (write_func)wfnc, \
   1.123 +        NULL, UCX_STREAM_COPY_BUFSIZE, n)
   1.124 +
   1.125 +/**
   1.126 + * Shorthand for an unbounded ucx_stream_bncopy call using the specified buffer.
   1.127 + * 
   1.128 + * @param src the source stream
   1.129 + * @param dest the destination stream
   1.130 + * @param rfnc the read function
   1.131 + * @param wfnc the write function
   1.132 + * @param buf a pointer to the copy buffer or <code>NULL</code> if a buffer
   1.133 + * shall be implicitly created on the heap
   1.134 + * @param bufsize the size of the copy buffer - if <code>NULL</code> was
   1.135 + * provided for <code>buf</code>, this is the size of the buffer that shall be
   1.136 + * implicitly created
   1.137 + * @return total number of bytes copied
   1.138 + */
   1.139 +#define ucx_stream_bcopy(src,dest,rfnc,wfnc, buf, bufsize) ucx_stream_bncopy(\
   1.140 +        src, dest, (read_func)rfnc, (write_func)wfnc, \
   1.141 +        buf, bufsize, (size_t)-1)
   1.142 +
   1.143 +/**
   1.144 + * Wraps the strcmp function.
   1.145 + * @param s1 string one
   1.146 + * @param s2 string two
   1.147 + * @param data omitted
   1.148 + * @return the result of strcmp(s1, s2)
   1.149 + */
   1.150 +int ucx_strcmp(const void *s1, const void *s2, void *data);
   1.151 +
   1.152 +/**
   1.153 + * Wraps the strncmp function.
   1.154 + * @param s1 string one
   1.155 + * @param s2 string two
   1.156 + * @param n a pointer to the size_t containing the third strncmp parameter
   1.157 + * @return the result of strncmp(s1, s2, *n)
   1.158 + */
   1.159 +int ucx_strncmp(const void *s1, const void *s2, void *n);
   1.160 +
   1.161 +/**
   1.162 + * Compares two integers of type int.
   1.163 + * @param i1 pointer to integer one
   1.164 + * @param i2 pointer to integer two
   1.165 + * @param data omitted
   1.166 + * @return -1, if *i1 is less than *i2, 0 if both are equal,
   1.167 + * 1 if *i1 is greater than *i2
   1.168 + */
   1.169 +int ucx_intcmp(const void *i1, const void *i2, void *data);
   1.170 +
   1.171 +/**
   1.172 + * Compares two real numbers of type float.
   1.173 + * @param f1 pointer to float one
   1.174 + * @param f2 pointer to float two
   1.175 + * @param data if provided: a pointer to precision (default: 1e-6f)
   1.176 + * @return -1, if *f1 is less than *f2, 0 if both are equal,
   1.177 + * 1 if *f1 is greater than *f2
   1.178 + */
   1.179 +
   1.180 +int ucx_floatcmp(const void *f1, const void *f2, void *data);
   1.181 +
   1.182 +/**
   1.183 + * Compares two real numbers of type double.
   1.184 + * @param d1 pointer to double one
   1.185 + * @param d2 pointer to double two
   1.186 + * @param data if provided: a pointer to precision (default: 1e-14)
   1.187 + * @return -1, if *d1 is less than *d2, 0 if both are equal,
   1.188 + * 1 if *d1 is greater than *d2
   1.189 + */
   1.190 +int ucx_doublecmp(const void *d1, const void *d2, void *data);
   1.191 +
   1.192 +/**
   1.193 + * Compares two pointers.
   1.194 + * @param ptr1 pointer one
   1.195 + * @param ptr2 pointer two
   1.196 + * @param data omitted
   1.197 + * @return -1 if ptr1 is less than ptr2, 0 if both are equal,
   1.198 + * 1 if ptr1 is greater than ptr2
   1.199 + */
   1.200 +int ucx_ptrcmp(const void *ptr1, const void *ptr2, void *data);
   1.201 +
   1.202 +/**
   1.203 + * Compares two memory areas.
   1.204 + * @param ptr1 pointer one
   1.205 + * @param ptr2 pointer two
   1.206 + * @param n a pointer to the size_t containing the third parameter for memcmp
   1.207 + * @return the result of memcmp(ptr1, ptr2, *n)
   1.208 + */
   1.209 +int ucx_memcmp(const void *ptr1, const void *ptr2, void *n);
   1.210 +
   1.211 +/**
   1.212 + * A <code>printf()</code> like function which writes the output to a stream by
   1.213 + * using a write_func().
   1.214 + * @param stream the stream the data is written to
   1.215 + * @param wfc the write function
   1.216 + * @param fmt format string
   1.217 + * @param ... additional arguments
   1.218 + * @return the total number of bytes written
   1.219 + */
   1.220 +int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...);
   1.221 +
   1.222 +/**
   1.223 + * <code>va_list</code> version of ucx_fprintf().
   1.224 + * @param stream the stream the data is written to
   1.225 + * @param wfc the write function
   1.226 + * @param fmt format string
   1.227 + * @param ap argument list
   1.228 + * @return the total number of bytes written
   1.229 + * @see ucx_fprintf()
   1.230 + */
   1.231 +int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap);
   1.232 +
   1.233 +/**
   1.234 + * A <code>printf()</code> like function which allocates space for a sstr_t
   1.235 + * the result is written to.
   1.236 + * 
   1.237 + * <b>Attention</b>: The sstr_t data is allocated with the allocators
   1.238 + * ucx_allocator_malloc() function. So it is implementation dependent, if
   1.239 + * the returned sstr_t.ptr pointer must be passed to the allocators
   1.240 + * ucx_allocator_free() function manually.
   1.241 + * 
   1.242 + * <b>Note</b>: The sstr_t.ptr of the return value will <i>always</i> be
   1.243 + * <code>NULL</code>-terminated.
   1.244 + * 
   1.245 + * @param allocator the UcxAllocator used for allocating the result sstr_t
   1.246 + * @param fmt format string
   1.247 + * @param ... additional arguments
   1.248 + * @return a sstr_t containing the formatted string
   1.249 + */
   1.250 +sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...);
   1.251 +
   1.252 +/**
   1.253 + * <code>va_list</code> version of ucx_asprintf().
   1.254 + * 
   1.255 + * @param allocator the UcxAllocator used for allocating the result sstr_t
   1.256 + * @param fmt format string
   1.257 + * @param ap argument list
   1.258 + * @return a sstr_t containing the formatted string
   1.259 + * @see ucx_asprintf()
   1.260 + */
   1.261 +sstr_t ucx_vasprintf(UcxAllocator *allocator, const char *fmt, va_list ap);
   1.262 +
   1.263 +/** Shortcut for ucx_asprintf() with default allocator. */
   1.264 +#define ucx_sprintf(...) \
   1.265 +    ucx_asprintf(ucx_default_allocator(), __VA_ARGS__)
   1.266 +
   1.267 +/**
   1.268 + * A <code>printf()</code> like function which writes the output to a
   1.269 + * UcxBuffer.
   1.270 + * 
   1.271 + * @param buffer the buffer the data is written to
   1.272 + * @param ... format string and additional arguments
   1.273 + * @return the total number of bytes written
   1.274 + * @see ucx_fprintf()
   1.275 + */
   1.276 +#define ucx_bprintf(buffer, ...) ucx_fprintf((UcxBuffer*)buffer, \
   1.277 +        (write_func)ucx_buffer_write, __VA_ARGS__)
   1.278 +
   1.279 +#ifdef __cplusplus
   1.280 +}
   1.281 +#endif
   1.282 +
   1.283 +#endif /* UCX_UTILS_H */
   1.284 +

mercurial