Sat, 10 Aug 2019 08:44:36 +0200
adds a broader set of compare and distance functions
universe@103 | 1 | /* |
universe@103 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@103 | 3 | * |
universe@259 | 4 | * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. |
universe@103 | 5 | * |
universe@103 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@103 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@103 | 8 | * |
universe@103 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@103 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@103 | 11 | * |
universe@103 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@103 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@103 | 14 | * documentation and/or other materials provided with the distribution. |
universe@103 | 15 | * |
universe@103 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@103 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@103 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@103 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@103 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@103 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@103 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@103 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@103 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@103 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@103 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@103 | 27 | */ |
universe@103 | 28 | |
universe@135 | 29 | /** |
universe@135 | 30 | * @file utils.h |
universe@135 | 31 | * |
olaf@147 | 32 | * Compare, copy and printf functions. |
universe@135 | 33 | * |
universe@135 | 34 | * @author Mike Becker |
universe@135 | 35 | * @author Olaf Wintermann |
universe@135 | 36 | */ |
universe@135 | 37 | |
olaf@120 | 38 | #ifndef UCX_UTILS_H |
universe@150 | 39 | #define UCX_UTILS_H |
universe@89 | 40 | |
universe@259 | 41 | #include "ucx.h" |
universe@259 | 42 | #include "string.h" |
universe@259 | 43 | #include "allocator.h" |
universe@218 | 44 | #include <inttypes.h> |
universe@89 | 45 | #include <string.h> |
olaf@142 | 46 | #include <stdarg.h> |
universe@224 | 47 | |
universe@328 | 48 | #ifdef __cplusplus |
universe@328 | 49 | extern "C" { |
universe@328 | 50 | #endif |
universe@328 | 51 | |
universe@224 | 52 | /** |
universe@224 | 53 | * Default buffer size for ucx_stream_copy() and ucx_stream_ncopy(). |
universe@224 | 54 | */ |
universe@222 | 55 | #define UCX_STREAM_COPY_BUFSIZE 4096 |
universe@89 | 56 | |
universe@89 | 57 | /** |
universe@94 | 58 | * Copies a string. |
universe@94 | 59 | * @param s the string to copy |
universe@94 | 60 | * @param data omitted |
universe@94 | 61 | * @return a pointer to a copy of s1 that can be passed to free(void*) |
universe@94 | 62 | */ |
universe@244 | 63 | void *ucx_strcpy(const void *s, void *data); |
universe@94 | 64 | |
universe@94 | 65 | /** |
universe@94 | 66 | * Copies a memory area. |
universe@94 | 67 | * @param m a pointer to the memory area |
universe@94 | 68 | * @param n a pointer to the size_t containing the size of the memory area |
universe@94 | 69 | * @return a pointer to a copy of the specified memory area that can |
universe@94 | 70 | * be passed to free(void*) |
universe@94 | 71 | */ |
universe@244 | 72 | void *ucx_memcpy(const void *m, void *n); |
universe@94 | 73 | |
universe@140 | 74 | |
universe@140 | 75 | /** |
universe@140 | 76 | * Reads data from a stream and writes it to another stream. |
universe@140 | 77 | * |
universe@140 | 78 | * @param src the source stream |
universe@140 | 79 | * @param dest the destination stream |
universe@140 | 80 | * @param rfnc the read function |
universe@140 | 81 | * @param wfnc the write function |
universe@140 | 82 | * @param buf a pointer to the copy buffer or <code>NULL</code> if a buffer |
universe@140 | 83 | * shall be implicitly created on the heap |
universe@140 | 84 | * @param bufsize the size of the copy buffer - if <code>NULL</code> was |
universe@140 | 85 | * provided for <code>buf</code>, this is the size of the buffer that shall be |
universe@140 | 86 | * implicitly created |
universe@140 | 87 | * @param n the maximum number of bytes that shall be copied |
universe@140 | 88 | * @return the total number of bytes copied |
universe@140 | 89 | */ |
universe@222 | 90 | size_t ucx_stream_bncopy(void *src, void *dest, read_func rfnc, write_func wfnc, |
universe@140 | 91 | char* buf, size_t bufsize, size_t n); |
universe@140 | 92 | |
universe@140 | 93 | /** |
universe@222 | 94 | * Shorthand for an unbounded ucx_stream_bncopy call using a default buffer. |
universe@140 | 95 | * |
universe@140 | 96 | * @param src the source stream |
universe@140 | 97 | * @param dest the destination stream |
universe@140 | 98 | * @param rfnc the read function |
universe@140 | 99 | * @param wfnc the write function |
universe@140 | 100 | * @return total number of bytes copied |
universe@222 | 101 | * |
universe@222 | 102 | * @see #UCX_STREAM_COPY_BUFSIZE |
universe@140 | 103 | */ |
universe@222 | 104 | #define ucx_stream_copy(src,dest,rfnc,wfnc) ucx_stream_bncopy(\ |
universe@222 | 105 | src, dest, (read_func)rfnc, (write_func)wfnc, \ |
universe@222 | 106 | NULL, UCX_STREAM_COPY_BUFSIZE, (size_t)-1) |
universe@140 | 107 | |
universe@140 | 108 | /** |
universe@222 | 109 | * Shorthand for ucx_stream_bncopy using a default copy buffer. |
universe@140 | 110 | * |
universe@140 | 111 | * @param src the source stream |
universe@140 | 112 | * @param dest the destination stream |
universe@140 | 113 | * @param rfnc the read function |
universe@140 | 114 | * @param wfnc the write function |
universe@140 | 115 | * @param n maximum number of bytes that shall be copied |
universe@140 | 116 | * @return total number of bytes copied |
universe@140 | 117 | */ |
universe@222 | 118 | #define ucx_stream_ncopy(src,dest,rfnc,wfnc, n) ucx_stream_bncopy(\ |
universe@222 | 119 | src, dest, (read_func)rfnc, (write_func)wfnc, \ |
universe@222 | 120 | NULL, UCX_STREAM_COPY_BUFSIZE, n) |
universe@222 | 121 | |
universe@222 | 122 | /** |
universe@222 | 123 | * Shorthand for an unbounded ucx_stream_bncopy call using the specified buffer. |
universe@222 | 124 | * |
universe@222 | 125 | * @param src the source stream |
universe@222 | 126 | * @param dest the destination stream |
universe@222 | 127 | * @param rfnc the read function |
universe@222 | 128 | * @param wfnc the write function |
universe@222 | 129 | * @param buf a pointer to the copy buffer or <code>NULL</code> if a buffer |
universe@222 | 130 | * shall be implicitly created on the heap |
universe@222 | 131 | * @param bufsize the size of the copy buffer - if <code>NULL</code> was |
universe@222 | 132 | * provided for <code>buf</code>, this is the size of the buffer that shall be |
universe@222 | 133 | * implicitly created |
universe@222 | 134 | * @return total number of bytes copied |
universe@222 | 135 | */ |
universe@222 | 136 | #define ucx_stream_bcopy(src,dest,rfnc,wfnc, buf, bufsize) ucx_stream_bncopy(\ |
universe@222 | 137 | src, dest, (read_func)rfnc, (write_func)wfnc, \ |
universe@222 | 138 | buf, bufsize, (size_t)-1) |
universe@140 | 139 | |
universe@94 | 140 | /** |
universe@89 | 141 | * Wraps the strcmp function. |
universe@89 | 142 | * @param s1 string one |
universe@89 | 143 | * @param s2 string two |
universe@89 | 144 | * @param data omitted |
universe@89 | 145 | * @return the result of strcmp(s1, s2) |
universe@89 | 146 | */ |
universe@308 | 147 | int ucx_cmp_str(const void *s1, const void *s2, void *data); |
universe@89 | 148 | |
universe@89 | 149 | /** |
universe@89 | 150 | * Wraps the strncmp function. |
universe@89 | 151 | * @param s1 string one |
universe@89 | 152 | * @param s2 string two |
universe@89 | 153 | * @param n a pointer to the size_t containing the third strncmp parameter |
universe@89 | 154 | * @return the result of strncmp(s1, s2, *n) |
universe@89 | 155 | */ |
universe@309 | 156 | int ucx_cmp_strn(const void *s1, const void *s2, void *n); |
universe@89 | 157 | |
universe@89 | 158 | /** |
universe@292 | 159 | * Wraps the sstrcmp function. |
universe@292 | 160 | * @param s1 sstr one |
universe@292 | 161 | * @param s2 sstr two |
universe@292 | 162 | * @param data ignored |
universe@292 | 163 | * @return the result of sstrcmp(s1, s2) |
universe@292 | 164 | */ |
universe@310 | 165 | int ucx_cmp_sstr(const void *s1, const void *s2, void *data); |
universe@292 | 166 | |
universe@292 | 167 | /** |
universe@89 | 168 | * Compares two integers of type int. |
universe@89 | 169 | * @param i1 pointer to integer one |
universe@89 | 170 | * @param i2 pointer to integer two |
universe@89 | 171 | * @param data omitted |
universe@89 | 172 | * @return -1, if *i1 is less than *i2, 0 if both are equal, |
universe@89 | 173 | * 1 if *i1 is greater than *i2 |
universe@89 | 174 | */ |
universe@314 | 175 | int ucx_cmp_int(const void *i1, const void *i2, void *data); |
universe@89 | 176 | |
universe@89 | 177 | /** |
universe@285 | 178 | * Compares two integers of type long int. |
universe@285 | 179 | * @param i1 pointer to long integer one |
universe@285 | 180 | * @param i2 pointer to long integer two |
universe@285 | 181 | * @param data omitted |
universe@285 | 182 | * @return -1, if *i1 is less than *i2, 0 if both are equal, |
universe@285 | 183 | * 1 if *i1 is greater than *i2 |
universe@285 | 184 | */ |
universe@314 | 185 | int ucx_cmp_longint(const void *i1, const void *i2, void *data); |
universe@285 | 186 | |
universe@349 | 187 | /** |
universe@349 | 188 | * Compares two integers of type long long. |
universe@349 | 189 | * @param i1 pointer to long long one |
universe@349 | 190 | * @param i2 pointer to long long two |
universe@349 | 191 | * @param data omitted |
universe@349 | 192 | * @return -1, if *i1 is less than *i2, 0 if both are equal, |
universe@349 | 193 | * 1 if *i1 is greater than *i2 |
universe@349 | 194 | */ |
universe@349 | 195 | int ucx_cmp_longlong(const void *i1, const void *i2, void *data); |
universe@349 | 196 | |
universe@349 | 197 | /** |
universe@349 | 198 | * Compares two integers of type int16_t. |
universe@349 | 199 | * @param i1 pointer to int16_t one |
universe@349 | 200 | * @param i2 pointer to int16_t two |
universe@349 | 201 | * @param data omitted |
universe@349 | 202 | * @return -1, if *i1 is less than *i2, 0 if both are equal, |
universe@349 | 203 | * 1 if *i1 is greater than *i2 |
universe@349 | 204 | */ |
universe@349 | 205 | int ucx_cmp_int16(const void *i1, const void *i2, void *data); |
universe@349 | 206 | |
universe@349 | 207 | /** |
universe@349 | 208 | * Compares two integers of type int32_t. |
universe@349 | 209 | * @param i1 pointer to int32_t one |
universe@349 | 210 | * @param i2 pointer to int32_t two |
universe@349 | 211 | * @param data omitted |
universe@349 | 212 | * @return -1, if *i1 is less than *i2, 0 if both are equal, |
universe@349 | 213 | * 1 if *i1 is greater than *i2 |
universe@349 | 214 | */ |
universe@349 | 215 | int ucx_cmp_int32(const void *i1, const void *i2, void *data); |
universe@349 | 216 | |
universe@349 | 217 | /** |
universe@349 | 218 | * Compares two integers of type int64_t. |
universe@349 | 219 | * @param i1 pointer to int64_t one |
universe@349 | 220 | * @param i2 pointer to int64_t two |
universe@349 | 221 | * @param data omitted |
universe@349 | 222 | * @return -1, if *i1 is less than *i2, 0 if both are equal, |
universe@349 | 223 | * 1 if *i1 is greater than *i2 |
universe@349 | 224 | */ |
universe@349 | 225 | int ucx_cmp_int64(const void *i1, const void *i2, void *data); |
universe@349 | 226 | |
universe@349 | 227 | /** |
universe@349 | 228 | * Compares two integers of type unsigned int. |
universe@349 | 229 | * @param i1 pointer to unsigned integer one |
universe@349 | 230 | * @param i2 pointer to unsigned integer two |
universe@349 | 231 | * @param data omitted |
universe@349 | 232 | * @return -1, if *i1 is less than *i2, 0 if both are equal, |
universe@349 | 233 | * 1 if *i1 is greater than *i2 |
universe@349 | 234 | */ |
universe@349 | 235 | int ucx_cmp_uint(const void *i1, const void *i2, void *data); |
universe@349 | 236 | |
universe@349 | 237 | /** |
universe@349 | 238 | * Compares two integers of type unsigned long int. |
universe@349 | 239 | * @param i1 pointer to unsigned long integer one |
universe@349 | 240 | * @param i2 pointer to unsigned long integer two |
universe@349 | 241 | * @param data omitted |
universe@349 | 242 | * @return -1, if *i1 is less than *i2, 0 if both are equal, |
universe@349 | 243 | * 1 if *i1 is greater than *i2 |
universe@349 | 244 | */ |
universe@349 | 245 | int ucx_cmp_ulongint(const void *i1, const void *i2, void *data); |
universe@349 | 246 | |
universe@349 | 247 | /** |
universe@349 | 248 | * Compares two integers of type unsigned long long. |
universe@349 | 249 | * @param i1 pointer to unsigned long long one |
universe@349 | 250 | * @param i2 pointer to unsigned long long two |
universe@349 | 251 | * @param data omitted |
universe@349 | 252 | * @return -1, if *i1 is less than *i2, 0 if both are equal, |
universe@349 | 253 | * 1 if *i1 is greater than *i2 |
universe@349 | 254 | */ |
universe@349 | 255 | int ucx_cmp_ulonglong(const void *i1, const void *i2, void *data); |
universe@349 | 256 | |
universe@349 | 257 | /** |
universe@349 | 258 | * Compares two integers of type uint16_t. |
universe@349 | 259 | * @param i1 pointer to uint16_t one |
universe@349 | 260 | * @param i2 pointer to uint16_t two |
universe@349 | 261 | * @param data omitted |
universe@349 | 262 | * @return -1, if *i1 is less than *i2, 0 if both are equal, |
universe@349 | 263 | * 1 if *i1 is greater than *i2 |
universe@349 | 264 | */ |
universe@349 | 265 | int ucx_cmp_uint16(const void *i1, const void *i2, void *data); |
universe@349 | 266 | |
universe@349 | 267 | /** |
universe@349 | 268 | * Compares two integers of type uint32_t. |
universe@349 | 269 | * @param i1 pointer to uint32_t one |
universe@349 | 270 | * @param i2 pointer to uint32_t two |
universe@349 | 271 | * @param data omitted |
universe@349 | 272 | * @return -1, if *i1 is less than *i2, 0 if both are equal, |
universe@349 | 273 | * 1 if *i1 is greater than *i2 |
universe@349 | 274 | */ |
universe@349 | 275 | int ucx_cmp_uint32(const void *i1, const void *i2, void *data); |
universe@349 | 276 | |
universe@349 | 277 | /** |
universe@349 | 278 | * Compares two integers of type uint64_t. |
universe@349 | 279 | * @param i1 pointer to uint64_t one |
universe@349 | 280 | * @param i2 pointer to uint64_t two |
universe@349 | 281 | * @param data omitted |
universe@349 | 282 | * @return -1, if *i1 is less than *i2, 0 if both are equal, |
universe@349 | 283 | * 1 if *i1 is greater than *i2 |
universe@349 | 284 | */ |
universe@349 | 285 | int ucx_cmp_uint64(const void *i1, const void *i2, void *data); |
universe@286 | 286 | |
universe@286 | 287 | /** |
universe@286 | 288 | * Distance function for integers of type int. |
universe@286 | 289 | * @param i1 pointer to integer one |
universe@286 | 290 | * @param i2 pointer to integer two |
universe@286 | 291 | * @param data omitted |
universe@286 | 292 | * @return i1 minus i2 |
universe@286 | 293 | */ |
universe@314 | 294 | intmax_t ucx_dist_int(const void *i1, const void *i2, void *data); |
universe@286 | 295 | |
universe@286 | 296 | /** |
universe@286 | 297 | * Distance function for integers of type long int. |
universe@286 | 298 | * @param i1 pointer to long integer one |
universe@286 | 299 | * @param i2 pointer to long integer two |
universe@286 | 300 | * @param data omitted |
universe@286 | 301 | * @return i1 minus i2 |
universe@286 | 302 | */ |
universe@314 | 303 | intmax_t ucx_dist_longint(const void *i1, const void *i2, void *data); |
universe@286 | 304 | |
universe@285 | 305 | /** |
universe@349 | 306 | * Distance function for integers of type long long. |
universe@349 | 307 | * @param i1 pointer to long long one |
universe@349 | 308 | * @param i2 pointer to long long two |
universe@349 | 309 | * @param data omitted |
universe@349 | 310 | * @return i1 minus i2 |
universe@349 | 311 | */ |
universe@349 | 312 | intmax_t ucx_dist_longlong(const void *i1, const void *i2, void *data); |
universe@349 | 313 | |
universe@349 | 314 | /** |
universe@349 | 315 | * Distance function for integers of type int16_t. |
universe@349 | 316 | * @param i1 pointer to int16_t one |
universe@349 | 317 | * @param i2 pointer to int16_t two |
universe@349 | 318 | * @param data omitted |
universe@349 | 319 | * @return i1 minus i2 |
universe@349 | 320 | */ |
universe@349 | 321 | intmax_t ucx_dist_int16(const void *i1, const void *i2, void *data); |
universe@349 | 322 | |
universe@349 | 323 | /** |
universe@349 | 324 | * Distance function for integers of type int32_t. |
universe@349 | 325 | * @param i1 pointer to int32_t one |
universe@349 | 326 | * @param i2 pointer to int32_t two |
universe@349 | 327 | * @param data omitted |
universe@349 | 328 | * @return i1 minus i2 |
universe@349 | 329 | */ |
universe@349 | 330 | intmax_t ucx_dist_int32(const void *i1, const void *i2, void *data); |
universe@349 | 331 | |
universe@349 | 332 | /** |
universe@349 | 333 | * Distance function for integers of type int64_t. |
universe@349 | 334 | * @param i1 pointer to int64_t one |
universe@349 | 335 | * @param i2 pointer to int64_t two |
universe@349 | 336 | * @param data omitted |
universe@349 | 337 | * @return i1 minus i2 |
universe@349 | 338 | */ |
universe@349 | 339 | intmax_t ucx_dist_int64(const void *i1, const void *i2, void *data); |
universe@349 | 340 | |
universe@349 | 341 | /** |
universe@349 | 342 | * Distance function for integers of type unsigned int. |
universe@349 | 343 | * @param i1 pointer to unsigned integer one |
universe@349 | 344 | * @param i2 pointer to unsigned integer two |
universe@349 | 345 | * @param data omitted |
universe@349 | 346 | * @return i1 minus i2 |
universe@349 | 347 | */ |
universe@349 | 348 | intmax_t ucx_dist_uint(const void *i1, const void *i2, void *data); |
universe@349 | 349 | |
universe@349 | 350 | /** |
universe@349 | 351 | * Distance function for integers of type unsigned long int. |
universe@349 | 352 | * @param i1 pointer to unsigned long integer one |
universe@349 | 353 | * @param i2 pointer to unsigned long integer two |
universe@349 | 354 | * @param data omitted |
universe@349 | 355 | * @return i1 minus i2 |
universe@349 | 356 | */ |
universe@349 | 357 | intmax_t ucx_dist_ulongint(const void *i1, const void *i2, void *data); |
universe@349 | 358 | |
universe@349 | 359 | /** |
universe@349 | 360 | * Distance function for integers of type unsigned long long. |
universe@349 | 361 | * @param i1 pointer to unsigned long long one |
universe@349 | 362 | * @param i2 pointer to unsigned long long two |
universe@349 | 363 | * @param data omitted |
universe@349 | 364 | * @return i1 minus i2 |
universe@349 | 365 | */ |
universe@349 | 366 | intmax_t ucx_dist_ulonglong(const void *i1, const void *i2, void *data); |
universe@349 | 367 | |
universe@349 | 368 | /** |
universe@349 | 369 | * Distance function for integers of type uint16_t. |
universe@349 | 370 | * @param i1 pointer to uint16_t one |
universe@349 | 371 | * @param i2 pointer to uint16_t two |
universe@349 | 372 | * @param data omitted |
universe@349 | 373 | * @return i1 minus i2 |
universe@349 | 374 | */ |
universe@349 | 375 | intmax_t ucx_dist_uint16(const void *i1, const void *i2, void *data); |
universe@349 | 376 | |
universe@349 | 377 | /** |
universe@349 | 378 | * Distance function for integers of type uint32_t. |
universe@349 | 379 | * @param i1 pointer to uint32_t one |
universe@349 | 380 | * @param i2 pointer to uint32_t two |
universe@349 | 381 | * @param data omitted |
universe@349 | 382 | * @return i1 minus i2 |
universe@349 | 383 | */ |
universe@349 | 384 | intmax_t ucx_dist_uint32(const void *i1, const void *i2, void *data); |
universe@349 | 385 | |
universe@349 | 386 | /** |
universe@349 | 387 | * Distance function for integers of type uint64_t. |
universe@349 | 388 | * @param i1 pointer to uint64_t one |
universe@349 | 389 | * @param i2 pointer to uint64_t two |
universe@349 | 390 | * @param data omitted |
universe@349 | 391 | * @return i1 minus i2 |
universe@349 | 392 | */ |
universe@349 | 393 | intmax_t ucx_dist_uint64(const void *i1, const void *i2, void *data); |
universe@349 | 394 | |
universe@349 | 395 | /** |
universe@92 | 396 | * Compares two real numbers of type float. |
universe@92 | 397 | * @param f1 pointer to float one |
universe@92 | 398 | * @param f2 pointer to float two |
universe@138 | 399 | * @param data if provided: a pointer to precision (default: 1e-6f) |
universe@92 | 400 | * @return -1, if *f1 is less than *f2, 0 if both are equal, |
universe@92 | 401 | * 1 if *f1 is greater than *f2 |
universe@92 | 402 | */ |
universe@92 | 403 | |
universe@313 | 404 | int ucx_cmp_float(const void *f1, const void *f2, void *data); |
universe@92 | 405 | |
universe@92 | 406 | /** |
universe@92 | 407 | * Compares two real numbers of type double. |
universe@138 | 408 | * @param d1 pointer to double one |
universe@138 | 409 | * @param d2 pointer to double two |
universe@138 | 410 | * @param data if provided: a pointer to precision (default: 1e-14) |
universe@92 | 411 | * @return -1, if *d1 is less than *d2, 0 if both are equal, |
universe@92 | 412 | * 1 if *d1 is greater than *d2 |
universe@92 | 413 | */ |
universe@313 | 414 | int ucx_cmp_double(const void *d1, const void *d2, void *data); |
universe@92 | 415 | |
universe@92 | 416 | /** |
universe@89 | 417 | * Compares two pointers. |
universe@89 | 418 | * @param ptr1 pointer one |
universe@89 | 419 | * @param ptr2 pointer two |
universe@89 | 420 | * @param data omitted |
universe@89 | 421 | * @return -1 if ptr1 is less than ptr2, 0 if both are equal, |
universe@89 | 422 | * 1 if ptr1 is greater than ptr2 |
universe@89 | 423 | */ |
universe@312 | 424 | int ucx_cmp_ptr(const void *ptr1, const void *ptr2, void *data); |
universe@89 | 425 | |
universe@91 | 426 | /** |
universe@91 | 427 | * Compares two memory areas. |
universe@91 | 428 | * @param ptr1 pointer one |
universe@91 | 429 | * @param ptr2 pointer two |
universe@91 | 430 | * @param n a pointer to the size_t containing the third parameter for memcmp |
universe@91 | 431 | * @return the result of memcmp(ptr1, ptr2, *n) |
universe@91 | 432 | */ |
universe@311 | 433 | int ucx_cmp_mem(const void *ptr1, const void *ptr2, void *n); |
universe@91 | 434 | |
olaf@142 | 435 | /** |
universe@146 | 436 | * A <code>printf()</code> like function which writes the output to a stream by |
universe@146 | 437 | * using a write_func(). |
universe@146 | 438 | * @param stream the stream the data is written to |
universe@146 | 439 | * @param wfc the write function |
olaf@142 | 440 | * @param fmt format string |
olaf@142 | 441 | * @param ... additional arguments |
olaf@142 | 442 | * @return the total number of bytes written |
olaf@142 | 443 | */ |
olaf@142 | 444 | int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...); |
olaf@142 | 445 | |
olaf@142 | 446 | /** |
universe@146 | 447 | * <code>va_list</code> version of ucx_fprintf(). |
universe@146 | 448 | * @param stream the stream the data is written to |
universe@146 | 449 | * @param wfc the write function |
olaf@142 | 450 | * @param fmt format string |
olaf@142 | 451 | * @param ap argument list |
olaf@142 | 452 | * @return the total number of bytes written |
olaf@142 | 453 | * @see ucx_fprintf() |
olaf@142 | 454 | */ |
olaf@142 | 455 | int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap); |
olaf@142 | 456 | |
olaf@142 | 457 | /** |
universe@146 | 458 | * A <code>printf()</code> like function which allocates space for a sstr_t |
universe@146 | 459 | * the result is written to. |
olaf@142 | 460 | * |
universe@146 | 461 | * <b>Attention</b>: The sstr_t data is allocated with the allocators |
universe@146 | 462 | * ucx_allocator_malloc() function. So it is implementation dependent, if |
universe@146 | 463 | * the returned sstr_t.ptr pointer must be passed to the allocators |
universe@146 | 464 | * ucx_allocator_free() function manually. |
olaf@142 | 465 | * |
universe@146 | 466 | * <b>Note</b>: The sstr_t.ptr of the return value will <i>always</i> be |
universe@146 | 467 | * <code>NULL</code>-terminated. |
olaf@142 | 468 | * |
universe@146 | 469 | * @param allocator the UcxAllocator used for allocating the result sstr_t |
olaf@142 | 470 | * @param fmt format string |
olaf@142 | 471 | * @param ... additional arguments |
universe@146 | 472 | * @return a sstr_t containing the formatted string |
olaf@142 | 473 | */ |
olaf@142 | 474 | sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...); |
olaf@142 | 475 | |
olaf@142 | 476 | /** |
universe@146 | 477 | * <code>va_list</code> version of ucx_asprintf(). |
universe@146 | 478 | * |
universe@146 | 479 | * @param allocator the UcxAllocator used for allocating the result sstr_t |
olaf@142 | 480 | * @param fmt format string |
olaf@142 | 481 | * @param ap argument list |
universe@146 | 482 | * @return a sstr_t containing the formatted string |
universe@146 | 483 | * @see ucx_asprintf() |
olaf@142 | 484 | */ |
olaf@142 | 485 | sstr_t ucx_vasprintf(UcxAllocator *allocator, const char *fmt, va_list ap); |
olaf@142 | 486 | |
universe@223 | 487 | /** Shortcut for ucx_asprintf() with default allocator. */ |
universe@223 | 488 | #define ucx_sprintf(...) \ |
universe@223 | 489 | ucx_asprintf(ucx_default_allocator(), __VA_ARGS__) |
universe@223 | 490 | |
olaf@147 | 491 | /** |
universe@225 | 492 | * A <code>printf()</code> like function which writes the output to a |
olaf@147 | 493 | * UcxBuffer. |
olaf@147 | 494 | * |
olaf@147 | 495 | * @param buffer the buffer the data is written to |
olaf@147 | 496 | * @param ... format string and additional arguments |
olaf@147 | 497 | * @return the total number of bytes written |
olaf@147 | 498 | * @see ucx_fprintf() |
olaf@147 | 499 | */ |
olaf@147 | 500 | #define ucx_bprintf(buffer, ...) ucx_fprintf((UcxBuffer*)buffer, \ |
olaf@147 | 501 | (write_func)ucx_buffer_write, __VA_ARGS__) |
olaf@147 | 502 | |
universe@150 | 503 | #ifdef __cplusplus |
universe@89 | 504 | } |
universe@89 | 505 | #endif |
universe@89 | 506 | |
universe@150 | 507 | #endif /* UCX_UTILS_H */ |
universe@89 | 508 |