src/utils.c

Sat, 10 Aug 2019 08:44:36 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 10 Aug 2019 08:44:36 +0200
branch
feature/array
changeset 349
05957b1d10a5
parent 314
5d28dc8f0765
permissions
-rw-r--r--

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@251 29 #include "ucx/utils.h"
universe@251 30
universe@140 31 #include <math.h>
olaf@142 32 #include <stdio.h>
olaf@142 33 #include <limits.h>
olaf@142 34 #include <errno.h>
universe@89 35
universe@94 36 /* COPY FUCNTIONS */
universe@244 37 void* ucx_strcpy(const void* s, void* data) {
universe@244 38 const char *str = (const char*) s;
universe@94 39 size_t n = 1+strlen(str);
universe@94 40 char *cpy = (char*) malloc(n);
universe@94 41 memcpy(cpy, str, n);
universe@94 42 return cpy;
universe@94 43 }
universe@94 44
universe@244 45 void* ucx_memcpy(const void* m, void* n) {
universe@94 46 size_t k = *((size_t*)n);
universe@94 47 void *cpy = malloc(k);
universe@94 48 memcpy(cpy, m, k);
universe@94 49 return cpy;
universe@94 50 }
universe@94 51
universe@222 52 size_t ucx_stream_bncopy(void *src, void *dest, read_func readfnc,
universe@140 53 write_func writefnc, char* buf, size_t bufsize, size_t n) {
universe@140 54 if(n == 0 || bufsize == 0) {
universe@140 55 return 0;
universe@140 56 }
universe@140 57
universe@181 58 char *lbuf;
universe@140 59 size_t ncp = 0;
universe@181 60
universe@181 61 if(buf) {
universe@181 62 lbuf = buf;
universe@181 63 } else {
universe@181 64 lbuf = (char*)malloc(bufsize);
universe@181 65 if(lbuf == NULL) {
universe@140 66 return 0;
universe@140 67 }
universe@140 68 }
universe@140 69
universe@140 70 size_t r;
universe@140 71 size_t rn = bufsize > n ? n : bufsize;
universe@181 72 while((r = readfnc(lbuf, 1, rn, src)) != 0) {
universe@181 73 r = writefnc(lbuf, 1, r, dest);
universe@140 74 ncp += r;
universe@140 75 n -= r;
universe@140 76 rn = bufsize > n ? n : bufsize;
universe@140 77 if(r == 0 || n == 0) {
universe@140 78 break;
universe@140 79 }
universe@140 80 }
universe@140 81
universe@181 82 if (lbuf != buf) {
universe@181 83 free(lbuf);
universe@181 84 }
universe@181 85
universe@140 86 return ncp;
universe@140 87 }
universe@140 88
olaf@142 89 /* COMPARE FUNCTIONS */
universe@94 90
universe@308 91 int ucx_cmp_str(const void *s1, const void *s2, void *data) {
universe@244 92 return strcmp((const char*)s1, (const char*)s2);
universe@89 93 }
universe@89 94
universe@309 95 int ucx_cmp_strn(const void *s1, const void *s2, void *n) {
universe@244 96 return strncmp((const char*)s1, (const char*)s2, *((size_t*) n));
universe@89 97 }
universe@89 98
universe@310 99 int ucx_cmp_sstr(const void *s1, const void *s2, void *data) {
universe@292 100 sstr_t a = *(const sstr_t*) s1;
universe@292 101 sstr_t b = *(const sstr_t*) s2;
universe@292 102 return sstrcmp(a, b);
universe@292 103 }
universe@292 104
universe@314 105 int ucx_cmp_int(const void *i1, const void *i2, void *data) {
universe@244 106 int a = *((const int*) i1);
universe@244 107 int b = *((const int*) i2);
universe@89 108 if (a == b) {
universe@89 109 return 0;
universe@89 110 } else {
universe@89 111 return a < b ? -1 : 1;
universe@89 112 }
universe@89 113 }
universe@89 114
universe@314 115 int ucx_cmp_longint(const void *i1, const void *i2, void *data) {
universe@349 116 long int a = *((const long int*) i1);
universe@349 117 long int b = *((const long int*) i2);
universe@349 118 if (a == b) {
universe@349 119 return 0;
universe@349 120 } else {
universe@349 121 return a < b ? -1 : 1;
universe@349 122 }
universe@349 123 }
universe@349 124
universe@349 125 int ucx_cmp_longlong(const void *i1, const void *i2, void *data) {
universe@349 126 long long a = *((const long long*) i1);
universe@349 127 long long b = *((const long long*) i2);
universe@349 128 if (a == b) {
universe@349 129 return 0;
universe@349 130 } else {
universe@349 131 return a < b ? -1 : 1;
universe@349 132 }
universe@349 133 }
universe@349 134
universe@349 135 int ucx_cmp_int16(const void *i1, const void *i2, void *data) {
universe@349 136 int16_t a = *((const int16_t*) i1);
universe@349 137 int16_t b = *((const int16_t*) i2);
universe@349 138 if (a == b) {
universe@349 139 return 0;
universe@349 140 } else {
universe@349 141 return a < b ? -1 : 1;
universe@349 142 }
universe@349 143 }
universe@349 144
universe@349 145 int ucx_cmp_int32(const void *i1, const void *i2, void *data) {
universe@349 146 int32_t a = *((const int32_t*) i1);
universe@349 147 int32_t b = *((const int32_t*) i2);
universe@349 148 if (a == b) {
universe@349 149 return 0;
universe@349 150 } else {
universe@349 151 return a < b ? -1 : 1;
universe@349 152 }
universe@349 153 }
universe@349 154
universe@349 155 int ucx_cmp_int64(const void *i1, const void *i2, void *data) {
universe@349 156 int64_t a = *((const int64_t*) i1);
universe@349 157 int64_t b = *((const int64_t*) i2);
universe@349 158 if (a == b) {
universe@349 159 return 0;
universe@349 160 } else {
universe@349 161 return a < b ? -1 : 1;
universe@349 162 }
universe@349 163 }
universe@349 164
universe@349 165 int ucx_cmp_uint(const void *i1, const void *i2, void *data) {
universe@349 166 unsigned int a = *((const unsigned int*) i1);
universe@349 167 unsigned int b = *((const unsigned int*) i2);
universe@349 168 if (a == b) {
universe@349 169 return 0;
universe@349 170 } else {
universe@349 171 return a < b ? -1 : 1;
universe@349 172 }
universe@349 173 }
universe@349 174
universe@349 175 int ucx_cmp_ulongint(const void *i1, const void *i2, void *data) {
universe@349 176 unsigned long int a = *((const unsigned long int*) i1);
universe@349 177 unsigned long int b = *((const unsigned long int*) i2);
universe@349 178 if (a == b) {
universe@349 179 return 0;
universe@349 180 } else {
universe@349 181 return a < b ? -1 : 1;
universe@349 182 }
universe@349 183 }
universe@349 184
universe@349 185 int ucx_cmp_ulonglong(const void *i1, const void *i2, void *data) {
universe@349 186 unsigned long long a = *((const unsigned long long*) i1);
universe@349 187 unsigned long long b = *((const unsigned long long*) i2);
universe@349 188 if (a == b) {
universe@349 189 return 0;
universe@349 190 } else {
universe@349 191 return a < b ? -1 : 1;
universe@349 192 }
universe@349 193 }
universe@349 194
universe@349 195 int ucx_cmp_uint16(const void *i1, const void *i2, void *data) {
universe@349 196 uint16_t a = *((const uint16_t*) i1);
universe@349 197 uint16_t b = *((const uint16_t*) i2);
universe@349 198 if (a == b) {
universe@349 199 return 0;
universe@349 200 } else {
universe@349 201 return a < b ? -1 : 1;
universe@349 202 }
universe@349 203 }
universe@349 204
universe@349 205 int ucx_cmp_uint32(const void *i1, const void *i2, void *data) {
universe@349 206 uint32_t a = *((const uint32_t*) i1);
universe@349 207 uint32_t b = *((const uint32_t*) i2);
universe@349 208 if (a == b) {
universe@349 209 return 0;
universe@349 210 } else {
universe@349 211 return a < b ? -1 : 1;
universe@349 212 }
universe@349 213 }
universe@349 214
universe@349 215 int ucx_cmp_uint64(const void *i1, const void *i2, void *data) {
universe@349 216 uint64_t a = *((const uint64_t*) i1);
universe@349 217 uint64_t b = *((const uint64_t*) i2);
universe@285 218 if (a == b) {
universe@285 219 return 0;
universe@285 220 } else {
universe@285 221 return a < b ? -1 : 1;
universe@285 222 }
universe@285 223 }
universe@285 224
universe@314 225 intmax_t ucx_dist_int(const void *i1, const void *i2, void *data) {
universe@286 226 intmax_t a = *((const int*) i1);
universe@286 227 intmax_t b = *((const int*) i2);
universe@286 228 return a - b;
universe@286 229 }
universe@286 230
universe@314 231 intmax_t ucx_dist_longint(const void *i1, const void *i2, void *data) {
universe@286 232 intmax_t a = *((const long int*) i1);
universe@286 233 intmax_t b = *((const long int*) i2);
universe@286 234 return a - b;
universe@286 235 }
universe@286 236
universe@349 237 intmax_t ucx_dist_longlong(const void *i1, const void *i2, void *data) {
universe@349 238 intmax_t a = *((const long long*) i1);
universe@349 239 intmax_t b = *((const long long*) i2);
universe@349 240 return a - b;
universe@349 241 }
universe@349 242
universe@349 243 intmax_t ucx_dist_int16(const void *i1, const void *i2, void *data) {
universe@349 244 intmax_t a = *((const int16_t*) i1);
universe@349 245 intmax_t b = *((const int16_t*) i2);
universe@349 246 return a - b;
universe@349 247 }
universe@349 248
universe@349 249 intmax_t ucx_dist_int32(const void *i1, const void *i2, void *data) {
universe@349 250 intmax_t a = *((const int32_t*) i1);
universe@349 251 intmax_t b = *((const int32_t*) i2);
universe@349 252 return a - b;
universe@349 253 }
universe@349 254
universe@349 255 intmax_t ucx_dist_int64(const void *i1, const void *i2, void *data) {
universe@349 256 intmax_t a = *((const int64_t*) i1);
universe@349 257 intmax_t b = *((const int64_t*) i2);
universe@349 258 return a - b;
universe@349 259 }
universe@349 260
universe@349 261 intmax_t ucx_dist_uint(const void *i1, const void *i2, void *data) {
universe@349 262 uintmax_t a = *((const unsigned int*) i1);
universe@349 263 uintmax_t b = *((const unsigned int*) i2);
universe@349 264 return a > b ? (intmax_t)(a - b) : -(intmax_t)(b - a);
universe@349 265 }
universe@349 266
universe@349 267 intmax_t ucx_dist_ulongint(const void *i1, const void *i2, void *data) {
universe@349 268 uintmax_t a = *((const unsigned long int*) i1);
universe@349 269 uintmax_t b = *((const unsigned long int*) i2);
universe@349 270 return a > b ? (intmax_t)(a - b) : -(intmax_t)(b - a);
universe@349 271 }
universe@349 272
universe@349 273 intmax_t ucx_dist_ulonglong(const void *i1, const void *i2, void *data) {
universe@349 274 uintmax_t a = *((const unsigned long long*) i1);
universe@349 275 uintmax_t b = *((const unsigned long long*) i2);
universe@349 276 return a > b ? (intmax_t)(a - b) : -(intmax_t)(b - a);
universe@349 277 }
universe@349 278
universe@349 279 intmax_t ucx_dist_uint16(const void *i1, const void *i2, void *data) {
universe@349 280 uintmax_t a = *((const uint16_t*) i1);
universe@349 281 uintmax_t b = *((const uint16_t*) i2);
universe@349 282 return a > b ? (intmax_t)(a - b) : -(intmax_t)(b - a);
universe@349 283 }
universe@349 284
universe@349 285 intmax_t ucx_dist_uint32(const void *i1, const void *i2, void *data) {
universe@349 286 uintmax_t a = *((const uint32_t*) i1);
universe@349 287 uintmax_t b = *((const uint32_t*) i2);
universe@349 288 return a > b ? (intmax_t)(a - b) : -(intmax_t)(b - a);
universe@349 289 }
universe@349 290
universe@349 291 intmax_t ucx_dist_uint64(const void *i1, const void *i2, void *data) {
universe@349 292 uintmax_t a = *((const uint64_t*) i1);
universe@349 293 uintmax_t b = *((const uint64_t*) i2);
universe@349 294 return a > b ? (intmax_t)(a - b) : -(intmax_t)(b - a);
universe@349 295 }
universe@349 296
universe@313 297 int ucx_cmp_float(const void *f1, const void *f2, void *epsilon) {
universe@244 298 float a = *((const float*) f1);
universe@244 299 float b = *((const float*) f2);
universe@92 300 float e = !epsilon ? 1e-6f : *((float*)epsilon);
universe@92 301 if (fabsf(a - b) < e) {
universe@92 302 return 0;
universe@92 303 } else {
universe@92 304 return a < b ? -1 : 1;
universe@92 305 }
universe@92 306 }
universe@92 307
universe@313 308 int ucx_cmp_double(const void *d1, const void *d2, void *epsilon) {
universe@244 309 double a = *((const double*) d1);
universe@244 310 double b = *((const double*) d2);
universe@92 311 double e = !epsilon ? 1e-14 : *((double*)epsilon);
universe@92 312 if (fabs(a - b) < e) {
universe@92 313 return 0;
universe@92 314 } else {
universe@92 315 return a < b ? -1 : 1;
universe@92 316 }
universe@92 317 }
universe@92 318
universe@312 319 int ucx_cmp_ptr(const void *ptr1, const void *ptr2, void *data) {
universe@244 320 const intptr_t p1 = (const intptr_t) ptr1;
universe@244 321 const intptr_t p2 = (const intptr_t) ptr2;
universe@194 322 if (p1 == p2) {
universe@89 323 return 0;
universe@89 324 } else {
universe@194 325 return p1 < p2 ? -1 : 1;
universe@89 326 }
universe@89 327 }
universe@91 328
universe@311 329 int ucx_cmp_mem(const void *ptr1, const void *ptr2, void *n) {
universe@91 330 return memcmp(ptr1, ptr2, *((size_t*)n));
universe@91 331 }
olaf@142 332
olaf@142 333 /* PRINTF FUNCTIONS */
olaf@142 334
universe@150 335 #ifdef va_copy
olaf@142 336 #define UCX_PRINTF_BUFSIZE 256
universe@150 337 #else
universe@150 338 #pragma message("WARNING: C99 va_copy macro not supported by this platform" \
universe@150 339 " - limiting ucx_*printf to 2 KiB")
universe@150 340 #define UCX_PRINTF_BUFSIZE 0x800
universe@150 341 #endif
olaf@142 342
olaf@142 343 int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...) {
universe@150 344 int ret;
olaf@142 345 va_list ap;
olaf@142 346 va_start(ap, fmt);
olaf@142 347 ret = ucx_vfprintf(stream, wfc, fmt, ap);
olaf@142 348 va_end(ap);
olaf@142 349 return ret;
olaf@142 350 }
olaf@142 351
olaf@142 352 int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap) {
olaf@142 353 char buf[UCX_PRINTF_BUFSIZE];
universe@150 354 #ifdef va_copy
olaf@144 355 va_list ap2;
olaf@144 356 va_copy(ap2, ap);
olaf@142 357 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
olaf@142 358 if (ret < 0) {
olaf@142 359 return ret;
olaf@142 360 } else if (ret < UCX_PRINTF_BUFSIZE) {
olaf@142 361 return (int)wfc(buf, 1, ret, stream);
olaf@142 362 } else {
olaf@142 363 if (ret == INT_MAX) {
olaf@142 364 errno = ENOMEM;
olaf@142 365 return -1;
olaf@142 366 }
olaf@142 367
olaf@142 368 int len = ret + 1;
olaf@142 369 char *newbuf = (char*)malloc(len);
olaf@142 370 if (!newbuf) {
olaf@142 371 return -1;
olaf@142 372 }
olaf@142 373
olaf@144 374 ret = vsnprintf(newbuf, len, fmt, ap2);
olaf@142 375 if (ret > 0) {
olaf@142 376 ret = (int)wfc(newbuf, 1, ret, stream);
olaf@142 377 }
olaf@142 378 free(newbuf);
olaf@142 379 }
olaf@142 380 return ret;
universe@150 381 #else
universe@150 382 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
universe@150 383 if (ret < 0) {
universe@150 384 return ret;
universe@150 385 } else if (ret < UCX_PRINTF_BUFSIZE) {
universe@150 386 return (int)wfc(buf, 1, ret, stream);
universe@150 387 } else {
universe@150 388 errno = ENOMEM;
universe@150 389 return -1;
universe@150 390 }
universe@150 391 #endif
olaf@142 392 }
olaf@142 393
olaf@142 394 sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...) {
olaf@142 395 va_list ap;
olaf@142 396 sstr_t ret;
olaf@142 397 va_start(ap, fmt);
olaf@142 398 ret = ucx_vasprintf(allocator, fmt, ap);
olaf@142 399 va_end(ap);
olaf@142 400 return ret;
olaf@142 401 }
olaf@142 402
olaf@142 403 sstr_t ucx_vasprintf(UcxAllocator *a, const char *fmt, va_list ap) {
olaf@142 404 sstr_t s;
olaf@142 405 s.ptr = NULL;
olaf@142 406 s.length = 0;
universe@150 407 char buf[UCX_PRINTF_BUFSIZE];
universe@150 408 #ifdef va_copy
olaf@144 409 va_list ap2;
olaf@144 410 va_copy(ap2, ap);
olaf@142 411 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
olaf@142 412 if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
universe@173 413 s.ptr = (char*)almalloc(a, ret + 1);
universe@174 414 if (s.ptr) {
universe@174 415 s.length = (size_t)ret;
universe@174 416 memcpy(s.ptr, buf, ret);
universe@174 417 s.ptr[s.length] = '\0';
universe@174 418 }
olaf@142 419 } else if (ret == INT_MAX) {
olaf@142 420 errno = ENOMEM;
olaf@142 421 } else {
olaf@142 422 int len = ret + 1;
universe@173 423 s.ptr = (char*)almalloc(a, len);
universe@174 424 if (s.ptr) {
universe@174 425 ret = vsnprintf(s.ptr, len, fmt, ap2);
universe@174 426 if (ret < 0) {
universe@174 427 free(s.ptr);
universe@174 428 s.ptr = NULL;
universe@174 429 } else {
universe@174 430 s.length = (size_t)ret;
universe@174 431 }
olaf@142 432 }
olaf@142 433 }
universe@150 434 #else
universe@150 435 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
universe@150 436 if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
universe@173 437 s.ptr = (char*)almalloc(a, ret + 1);
universe@174 438 if (s.ptr) {
universe@174 439 s.length = (size_t)ret;
universe@174 440 memcpy(s.ptr, buf, ret);
universe@174 441 s.ptr[s.length] = '\0';
universe@174 442 }
universe@150 443 } else {
universe@150 444 errno = ENOMEM;
universe@150 445 }
universe@150 446 #endif
olaf@142 447 return s;
olaf@142 448 }

mercurial