1.1 --- a/src/ucx/utils.h Mon Dec 30 09:54:10 2019 +0100 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,508 +0,0 @@ 1.4 -/* 1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 1.6 - * 1.7 - * Copyright 2017 Mike Becker, 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 -#include "ucx.h" 1.45 -#include "string.h" 1.46 -#include "allocator.h" 1.47 -#include <inttypes.h> 1.48 -#include <string.h> 1.49 -#include <stdarg.h> 1.50 - 1.51 -#ifdef __cplusplus 1.52 -extern "C" { 1.53 -#endif 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_cmp_str(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_cmp_strn(const void *s1, const void *s2, void *n); 1.160 - 1.161 -/** 1.162 - * Wraps the sstrcmp function. 1.163 - * @param s1 sstr one 1.164 - * @param s2 sstr two 1.165 - * @param data ignored 1.166 - * @return the result of sstrcmp(s1, s2) 1.167 - */ 1.168 -int ucx_cmp_sstr(const void *s1, const void *s2, void *data); 1.169 - 1.170 -/** 1.171 - * Compares two integers of type int. 1.172 - * @param i1 pointer to integer one 1.173 - * @param i2 pointer to integer two 1.174 - * @param data omitted 1.175 - * @return -1, if *i1 is less than *i2, 0 if both are equal, 1.176 - * 1 if *i1 is greater than *i2 1.177 - */ 1.178 -int ucx_cmp_int(const void *i1, const void *i2, void *data); 1.179 - 1.180 -/** 1.181 - * Compares two integers of type long int. 1.182 - * @param i1 pointer to long integer one 1.183 - * @param i2 pointer to long integer two 1.184 - * @param data omitted 1.185 - * @return -1, if *i1 is less than *i2, 0 if both are equal, 1.186 - * 1 if *i1 is greater than *i2 1.187 - */ 1.188 -int ucx_cmp_longint(const void *i1, const void *i2, void *data); 1.189 - 1.190 -/** 1.191 - * Compares two integers of type long long. 1.192 - * @param i1 pointer to long long one 1.193 - * @param i2 pointer to long long two 1.194 - * @param data omitted 1.195 - * @return -1, if *i1 is less than *i2, 0 if both are equal, 1.196 - * 1 if *i1 is greater than *i2 1.197 - */ 1.198 -int ucx_cmp_longlong(const void *i1, const void *i2, void *data); 1.199 - 1.200 -/** 1.201 - * Compares two integers of type int16_t. 1.202 - * @param i1 pointer to int16_t one 1.203 - * @param i2 pointer to int16_t two 1.204 - * @param data omitted 1.205 - * @return -1, if *i1 is less than *i2, 0 if both are equal, 1.206 - * 1 if *i1 is greater than *i2 1.207 - */ 1.208 -int ucx_cmp_int16(const void *i1, const void *i2, void *data); 1.209 - 1.210 -/** 1.211 - * Compares two integers of type int32_t. 1.212 - * @param i1 pointer to int32_t one 1.213 - * @param i2 pointer to int32_t two 1.214 - * @param data omitted 1.215 - * @return -1, if *i1 is less than *i2, 0 if both are equal, 1.216 - * 1 if *i1 is greater than *i2 1.217 - */ 1.218 -int ucx_cmp_int32(const void *i1, const void *i2, void *data); 1.219 - 1.220 -/** 1.221 - * Compares two integers of type int64_t. 1.222 - * @param i1 pointer to int64_t one 1.223 - * @param i2 pointer to int64_t two 1.224 - * @param data omitted 1.225 - * @return -1, if *i1 is less than *i2, 0 if both are equal, 1.226 - * 1 if *i1 is greater than *i2 1.227 - */ 1.228 -int ucx_cmp_int64(const void *i1, const void *i2, void *data); 1.229 - 1.230 -/** 1.231 - * Compares two integers of type unsigned int. 1.232 - * @param i1 pointer to unsigned integer one 1.233 - * @param i2 pointer to unsigned integer two 1.234 - * @param data omitted 1.235 - * @return -1, if *i1 is less than *i2, 0 if both are equal, 1.236 - * 1 if *i1 is greater than *i2 1.237 - */ 1.238 -int ucx_cmp_uint(const void *i1, const void *i2, void *data); 1.239 - 1.240 -/** 1.241 - * Compares two integers of type unsigned long int. 1.242 - * @param i1 pointer to unsigned long integer one 1.243 - * @param i2 pointer to unsigned long integer two 1.244 - * @param data omitted 1.245 - * @return -1, if *i1 is less than *i2, 0 if both are equal, 1.246 - * 1 if *i1 is greater than *i2 1.247 - */ 1.248 -int ucx_cmp_ulongint(const void *i1, const void *i2, void *data); 1.249 - 1.250 -/** 1.251 - * Compares two integers of type unsigned long long. 1.252 - * @param i1 pointer to unsigned long long one 1.253 - * @param i2 pointer to unsigned long long two 1.254 - * @param data omitted 1.255 - * @return -1, if *i1 is less than *i2, 0 if both are equal, 1.256 - * 1 if *i1 is greater than *i2 1.257 - */ 1.258 -int ucx_cmp_ulonglong(const void *i1, const void *i2, void *data); 1.259 - 1.260 -/** 1.261 - * Compares two integers of type uint16_t. 1.262 - * @param i1 pointer to uint16_t one 1.263 - * @param i2 pointer to uint16_t two 1.264 - * @param data omitted 1.265 - * @return -1, if *i1 is less than *i2, 0 if both are equal, 1.266 - * 1 if *i1 is greater than *i2 1.267 - */ 1.268 -int ucx_cmp_uint16(const void *i1, const void *i2, void *data); 1.269 - 1.270 -/** 1.271 - * Compares two integers of type uint32_t. 1.272 - * @param i1 pointer to uint32_t one 1.273 - * @param i2 pointer to uint32_t two 1.274 - * @param data omitted 1.275 - * @return -1, if *i1 is less than *i2, 0 if both are equal, 1.276 - * 1 if *i1 is greater than *i2 1.277 - */ 1.278 -int ucx_cmp_uint32(const void *i1, const void *i2, void *data); 1.279 - 1.280 -/** 1.281 - * Compares two integers of type uint64_t. 1.282 - * @param i1 pointer to uint64_t one 1.283 - * @param i2 pointer to uint64_t two 1.284 - * @param data omitted 1.285 - * @return -1, if *i1 is less than *i2, 0 if both are equal, 1.286 - * 1 if *i1 is greater than *i2 1.287 - */ 1.288 -int ucx_cmp_uint64(const void *i1, const void *i2, void *data); 1.289 - 1.290 -/** 1.291 - * Distance function for integers of type int. 1.292 - * @param i1 pointer to integer one 1.293 - * @param i2 pointer to integer two 1.294 - * @param data omitted 1.295 - * @return i1 minus i2 1.296 - */ 1.297 -intmax_t ucx_dist_int(const void *i1, const void *i2, void *data); 1.298 - 1.299 -/** 1.300 - * Distance function for integers of type long int. 1.301 - * @param i1 pointer to long integer one 1.302 - * @param i2 pointer to long integer two 1.303 - * @param data omitted 1.304 - * @return i1 minus i2 1.305 - */ 1.306 -intmax_t ucx_dist_longint(const void *i1, const void *i2, void *data); 1.307 - 1.308 -/** 1.309 - * Distance function for integers of type long long. 1.310 - * @param i1 pointer to long long one 1.311 - * @param i2 pointer to long long two 1.312 - * @param data omitted 1.313 - * @return i1 minus i2 1.314 - */ 1.315 -intmax_t ucx_dist_longlong(const void *i1, const void *i2, void *data); 1.316 - 1.317 -/** 1.318 - * Distance function for integers of type int16_t. 1.319 - * @param i1 pointer to int16_t one 1.320 - * @param i2 pointer to int16_t two 1.321 - * @param data omitted 1.322 - * @return i1 minus i2 1.323 - */ 1.324 -intmax_t ucx_dist_int16(const void *i1, const void *i2, void *data); 1.325 - 1.326 -/** 1.327 - * Distance function for integers of type int32_t. 1.328 - * @param i1 pointer to int32_t one 1.329 - * @param i2 pointer to int32_t two 1.330 - * @param data omitted 1.331 - * @return i1 minus i2 1.332 - */ 1.333 -intmax_t ucx_dist_int32(const void *i1, const void *i2, void *data); 1.334 - 1.335 -/** 1.336 - * Distance function for integers of type int64_t. 1.337 - * @param i1 pointer to int64_t one 1.338 - * @param i2 pointer to int64_t two 1.339 - * @param data omitted 1.340 - * @return i1 minus i2 1.341 - */ 1.342 -intmax_t ucx_dist_int64(const void *i1, const void *i2, void *data); 1.343 - 1.344 -/** 1.345 - * Distance function for integers of type unsigned int. 1.346 - * @param i1 pointer to unsigned integer one 1.347 - * @param i2 pointer to unsigned integer two 1.348 - * @param data omitted 1.349 - * @return i1 minus i2 1.350 - */ 1.351 -intmax_t ucx_dist_uint(const void *i1, const void *i2, void *data); 1.352 - 1.353 -/** 1.354 - * Distance function for integers of type unsigned long int. 1.355 - * @param i1 pointer to unsigned long integer one 1.356 - * @param i2 pointer to unsigned long integer two 1.357 - * @param data omitted 1.358 - * @return i1 minus i2 1.359 - */ 1.360 -intmax_t ucx_dist_ulongint(const void *i1, const void *i2, void *data); 1.361 - 1.362 -/** 1.363 - * Distance function for integers of type unsigned long long. 1.364 - * @param i1 pointer to unsigned long long one 1.365 - * @param i2 pointer to unsigned long long two 1.366 - * @param data omitted 1.367 - * @return i1 minus i2 1.368 - */ 1.369 -intmax_t ucx_dist_ulonglong(const void *i1, const void *i2, void *data); 1.370 - 1.371 -/** 1.372 - * Distance function for integers of type uint16_t. 1.373 - * @param i1 pointer to uint16_t one 1.374 - * @param i2 pointer to uint16_t two 1.375 - * @param data omitted 1.376 - * @return i1 minus i2 1.377 - */ 1.378 -intmax_t ucx_dist_uint16(const void *i1, const void *i2, void *data); 1.379 - 1.380 -/** 1.381 - * Distance function for integers of type uint32_t. 1.382 - * @param i1 pointer to uint32_t one 1.383 - * @param i2 pointer to uint32_t two 1.384 - * @param data omitted 1.385 - * @return i1 minus i2 1.386 - */ 1.387 -intmax_t ucx_dist_uint32(const void *i1, const void *i2, void *data); 1.388 - 1.389 -/** 1.390 - * Distance function for integers of type uint64_t. 1.391 - * @param i1 pointer to uint64_t one 1.392 - * @param i2 pointer to uint64_t two 1.393 - * @param data omitted 1.394 - * @return i1 minus i2 1.395 - */ 1.396 -intmax_t ucx_dist_uint64(const void *i1, const void *i2, void *data); 1.397 - 1.398 -/** 1.399 - * Compares two real numbers of type float. 1.400 - * @param f1 pointer to float one 1.401 - * @param f2 pointer to float two 1.402 - * @param data if provided: a pointer to precision (default: 1e-6f) 1.403 - * @return -1, if *f1 is less than *f2, 0 if both are equal, 1.404 - * 1 if *f1 is greater than *f2 1.405 - */ 1.406 - 1.407 -int ucx_cmp_float(const void *f1, const void *f2, void *data); 1.408 - 1.409 -/** 1.410 - * Compares two real numbers of type double. 1.411 - * @param d1 pointer to double one 1.412 - * @param d2 pointer to double two 1.413 - * @param data if provided: a pointer to precision (default: 1e-14) 1.414 - * @return -1, if *d1 is less than *d2, 0 if both are equal, 1.415 - * 1 if *d1 is greater than *d2 1.416 - */ 1.417 -int ucx_cmp_double(const void *d1, const void *d2, void *data); 1.418 - 1.419 -/** 1.420 - * Compares two pointers. 1.421 - * @param ptr1 pointer one 1.422 - * @param ptr2 pointer two 1.423 - * @param data omitted 1.424 - * @return -1 if ptr1 is less than ptr2, 0 if both are equal, 1.425 - * 1 if ptr1 is greater than ptr2 1.426 - */ 1.427 -int ucx_cmp_ptr(const void *ptr1, const void *ptr2, void *data); 1.428 - 1.429 -/** 1.430 - * Compares two memory areas. 1.431 - * @param ptr1 pointer one 1.432 - * @param ptr2 pointer two 1.433 - * @param n a pointer to the size_t containing the third parameter for memcmp 1.434 - * @return the result of memcmp(ptr1, ptr2, *n) 1.435 - */ 1.436 -int ucx_cmp_mem(const void *ptr1, const void *ptr2, void *n); 1.437 - 1.438 -/** 1.439 - * A <code>printf()</code> like function which writes the output to a stream by 1.440 - * using a write_func(). 1.441 - * @param stream the stream the data is written to 1.442 - * @param wfc the write function 1.443 - * @param fmt format string 1.444 - * @param ... additional arguments 1.445 - * @return the total number of bytes written 1.446 - */ 1.447 -int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...); 1.448 - 1.449 -/** 1.450 - * <code>va_list</code> version of ucx_fprintf(). 1.451 - * @param stream the stream the data is written to 1.452 - * @param wfc the write function 1.453 - * @param fmt format string 1.454 - * @param ap argument list 1.455 - * @return the total number of bytes written 1.456 - * @see ucx_fprintf() 1.457 - */ 1.458 -int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap); 1.459 - 1.460 -/** 1.461 - * A <code>printf()</code> like function which allocates space for a sstr_t 1.462 - * the result is written to. 1.463 - * 1.464 - * <b>Attention</b>: The sstr_t data is allocated with the allocators 1.465 - * ucx_allocator_malloc() function. So it is implementation dependent, if 1.466 - * the returned sstr_t.ptr pointer must be passed to the allocators 1.467 - * ucx_allocator_free() function manually. 1.468 - * 1.469 - * <b>Note</b>: The sstr_t.ptr of the return value will <i>always</i> be 1.470 - * <code>NULL</code>-terminated. 1.471 - * 1.472 - * @param allocator the UcxAllocator used for allocating the result sstr_t 1.473 - * @param fmt format string 1.474 - * @param ... additional arguments 1.475 - * @return a sstr_t containing the formatted string 1.476 - */ 1.477 -sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...); 1.478 - 1.479 -/** 1.480 - * <code>va_list</code> version of ucx_asprintf(). 1.481 - * 1.482 - * @param allocator the UcxAllocator used for allocating the result sstr_t 1.483 - * @param fmt format string 1.484 - * @param ap argument list 1.485 - * @return a sstr_t containing the formatted string 1.486 - * @see ucx_asprintf() 1.487 - */ 1.488 -sstr_t ucx_vasprintf(UcxAllocator *allocator, const char *fmt, va_list ap); 1.489 - 1.490 -/** Shortcut for ucx_asprintf() with default allocator. */ 1.491 -#define ucx_sprintf(...) \ 1.492 - ucx_asprintf(ucx_default_allocator(), __VA_ARGS__) 1.493 - 1.494 -/** 1.495 - * A <code>printf()</code> like function which writes the output to a 1.496 - * UcxBuffer. 1.497 - * 1.498 - * @param buffer the buffer the data is written to 1.499 - * @param ... format string and additional arguments 1.500 - * @return the total number of bytes written 1.501 - * @see ucx_fprintf() 1.502 - */ 1.503 -#define ucx_bprintf(buffer, ...) ucx_fprintf((UcxBuffer*)buffer, \ 1.504 - (write_func)ucx_buffer_write, __VA_ARGS__) 1.505 - 1.506 -#ifdef __cplusplus 1.507 -} 1.508 -#endif 1.509 - 1.510 -#endif /* UCX_UTILS_H */ 1.511 -