1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/ucx/utils.h Tue Aug 23 13:49:38 2016 +0200 1.3 @@ -0,0 +1,254 @@ 1.4 +/* 1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 1.6 + * 1.7 + * Copyright 2015 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.h" 1.49 +#include "string.h" 1.50 +#include "allocator.h" 1.51 +#include <inttypes.h> 1.52 +#include <string.h> 1.53 +#include <stdarg.h> 1.54 + 1.55 +/** 1.56 + * Copies a string. 1.57 + * @param s the string to copy 1.58 + * @param data omitted 1.59 + * @return a pointer to a copy of s1 that can be passed to free(void*) 1.60 + */ 1.61 +void *ucx_strcpy(void *s, void *data); 1.62 + 1.63 +/** 1.64 + * Copies a memory area. 1.65 + * @param m a pointer to the memory area 1.66 + * @param n a pointer to the size_t containing the size of the memory area 1.67 + * @return a pointer to a copy of the specified memory area that can 1.68 + * be passed to free(void*) 1.69 + */ 1.70 +void *ucx_memcpy(void *m, void *n); 1.71 + 1.72 + 1.73 +/** 1.74 + * Reads data from a stream and writes it to another stream. 1.75 + * 1.76 + * @param src the source stream 1.77 + * @param dest the destination stream 1.78 + * @param rfnc the read function 1.79 + * @param wfnc the write function 1.80 + * @param buf a pointer to the copy buffer or <code>NULL</code> if a buffer 1.81 + * shall be implicitly created on the heap 1.82 + * @param bufsize the size of the copy buffer - if <code>NULL</code> was 1.83 + * provided for <code>buf</code>, this is the size of the buffer that shall be 1.84 + * implicitly created 1.85 + * @param n the maximum number of bytes that shall be copied 1.86 + * @return the total number of bytes copied 1.87 + */ 1.88 +size_t ucx_stream_copy(void *src, void *dest, read_func rfnc, write_func wfnc, 1.89 + char* buf, size_t bufsize, size_t n); 1.90 + 1.91 +/** 1.92 + * Shorthand for ucx_stream_copy using the default copy buffer. 1.93 + * 1.94 + * @param src the source stream 1.95 + * @param dest the destination stream 1.96 + * @param rfnc the read function 1.97 + * @param wfnc the write function 1.98 + * @return total number of bytes copied 1.99 + */ 1.100 +#define ucx_stream_hcopy(src,dest,rfnc,wfnc) ucx_stream_copy(\ 1.101 + src, dest, (read_func)rfnc, (write_func)wfnc, NULL, 0x100, (size_t)-1) 1.102 + 1.103 +/** 1.104 + * Shorthand for ucx_stream_copy using the default copy buffer and a copy limit. 1.105 + * 1.106 + * @param src the source stream 1.107 + * @param dest the destination stream 1.108 + * @param rfnc the read function 1.109 + * @param wfnc the write function 1.110 + * @param n maximum number of bytes that shall be copied 1.111 + * @return total number of bytes copied 1.112 + */ 1.113 +#define ucx_stream_ncopy(src,dest,rfnc,wfnc, n) ucx_stream_copy(\ 1.114 + src, dest, (read_func)rfnc, (write_func)wfnc, NULL, 0x100, n) 1.115 + 1.116 +/** 1.117 + * Wraps the strcmp function. 1.118 + * @param s1 string one 1.119 + * @param s2 string two 1.120 + * @param data omitted 1.121 + * @return the result of strcmp(s1, s2) 1.122 + */ 1.123 +int ucx_strcmp(void *s1, void *s2, void *data); 1.124 + 1.125 +/** 1.126 + * Wraps the strncmp function. 1.127 + * @param s1 string one 1.128 + * @param s2 string two 1.129 + * @param n a pointer to the size_t containing the third strncmp parameter 1.130 + * @return the result of strncmp(s1, s2, *n) 1.131 + */ 1.132 +int ucx_strncmp(void *s1, void *s2, void *n); 1.133 + 1.134 +/** 1.135 + * Compares two integers of type int. 1.136 + * @param i1 pointer to integer one 1.137 + * @param i2 pointer to integer two 1.138 + * @param data omitted 1.139 + * @return -1, if *i1 is less than *i2, 0 if both are equal, 1.140 + * 1 if *i1 is greater than *i2 1.141 + */ 1.142 +int ucx_intcmp(void *i1, void *i2, void *data); 1.143 + 1.144 +/** 1.145 + * Compares two real numbers of type float. 1.146 + * @param f1 pointer to float one 1.147 + * @param f2 pointer to float two 1.148 + * @param data if provided: a pointer to precision (default: 1e-6f) 1.149 + * @return -1, if *f1 is less than *f2, 0 if both are equal, 1.150 + * 1 if *f1 is greater than *f2 1.151 + */ 1.152 + 1.153 +int ucx_floatcmp(void *f1, void *f2, void *data); 1.154 + 1.155 +/** 1.156 + * Compares two real numbers of type double. 1.157 + * @param d1 pointer to double one 1.158 + * @param d2 pointer to double two 1.159 + * @param data if provided: a pointer to precision (default: 1e-14) 1.160 + * @return -1, if *d1 is less than *d2, 0 if both are equal, 1.161 + * 1 if *d1 is greater than *d2 1.162 + */ 1.163 +int ucx_doublecmp(void *d1, void *d2, void *data); 1.164 + 1.165 +/** 1.166 + * Compares two pointers. 1.167 + * @param ptr1 pointer one 1.168 + * @param ptr2 pointer two 1.169 + * @param data omitted 1.170 + * @return -1 if ptr1 is less than ptr2, 0 if both are equal, 1.171 + * 1 if ptr1 is greater than ptr2 1.172 + */ 1.173 +int ucx_ptrcmp(void *ptr1, void *ptr2, void *data); 1.174 + 1.175 +/** 1.176 + * Compares two memory areas. 1.177 + * @param ptr1 pointer one 1.178 + * @param ptr2 pointer two 1.179 + * @param n a pointer to the size_t containing the third parameter for memcmp 1.180 + * @return the result of memcmp(ptr1, ptr2, *n) 1.181 + */ 1.182 +int ucx_memcmp(void *ptr1, void *ptr2, void *n); 1.183 + 1.184 +/** 1.185 + * A <code>printf()</code> like function which writes the output to a stream by 1.186 + * using a write_func(). 1.187 + * @param stream the stream the data is written to 1.188 + * @param wfc the write function 1.189 + * @param fmt format string 1.190 + * @param ... additional arguments 1.191 + * @return the total number of bytes written 1.192 + */ 1.193 +int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...); 1.194 + 1.195 +/** 1.196 + * <code>va_list</code> version of ucx_fprintf(). 1.197 + * @param stream the stream the data is written to 1.198 + * @param wfc the write function 1.199 + * @param fmt format string 1.200 + * @param ap argument list 1.201 + * @return the total number of bytes written 1.202 + * @see ucx_fprintf() 1.203 + */ 1.204 +int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap); 1.205 + 1.206 +/** 1.207 + * A <code>printf()</code> like function which allocates space for a sstr_t 1.208 + * the result is written to. 1.209 + * 1.210 + * <b>Attention</b>: The sstr_t data is allocated with the allocators 1.211 + * ucx_allocator_malloc() function. So it is implementation dependent, if 1.212 + * the returned sstr_t.ptr pointer must be passed to the allocators 1.213 + * ucx_allocator_free() function manually. 1.214 + * 1.215 + * <b>Note</b>: The sstr_t.ptr of the return value will <i>always</i> be 1.216 + * <code>NULL</code>-terminated. 1.217 + * 1.218 + * @param allocator the UcxAllocator used for allocating the result sstr_t 1.219 + * @param fmt format string 1.220 + * @param ... additional arguments 1.221 + * @return a sstr_t containing the formatted string 1.222 + */ 1.223 +sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...); 1.224 + 1.225 +/** Shortcut for ucx_asprintf() with default allocator. */ 1.226 +#define ucx_sprintf(fmt, ...) \ 1.227 + ucx_asprintf(ucx_default_allocator(), fmt, __VA_ARGS__) 1.228 + 1.229 +/** 1.230 + * <code>va_list</code> version of ucx_asprintf(). 1.231 + * 1.232 + * @param allocator the UcxAllocator used for allocating the result sstr_t 1.233 + * @param fmt format string 1.234 + * @param ap argument list 1.235 + * @return a sstr_t containing the formatted string 1.236 + * @see ucx_asprintf() 1.237 + */ 1.238 +sstr_t ucx_vasprintf(UcxAllocator *allocator, const char *fmt, va_list ap); 1.239 + 1.240 +/** 1.241 + * A <code>printf()</code> like function which writes the output to an 1.242 + * UcxBuffer. 1.243 + * 1.244 + * @param buffer the buffer the data is written to 1.245 + * @param ... format string and additional arguments 1.246 + * @return the total number of bytes written 1.247 + * @see ucx_fprintf() 1.248 + */ 1.249 +#define ucx_bprintf(buffer, ...) ucx_fprintf((UcxBuffer*)buffer, \ 1.250 + (write_func)ucx_buffer_write, __VA_ARGS__) 1.251 + 1.252 +#ifdef __cplusplus 1.253 +} 1.254 +#endif 1.255 + 1.256 +#endif /* UCX_UTILS_H */ 1.257 +