Thu, 03 May 2018 10:09:49 +0200
adds distance functions for integers as utils
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@244 | 91 | int ucx_strcmp(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@244 | 95 | int ucx_strncmp(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@244 | 99 | int ucx_intcmp(const void *i1, const void *i2, void *data) { |
universe@244 | 100 | int a = *((const int*) i1); |
universe@244 | 101 | int b = *((const int*) i2); |
universe@89 | 102 | if (a == b) { |
universe@89 | 103 | return 0; |
universe@89 | 104 | } else { |
universe@89 | 105 | return a < b ? -1 : 1; |
universe@89 | 106 | } |
universe@89 | 107 | } |
universe@89 | 108 | |
universe@285 | 109 | int ucx_longintcmp(const void *i1, const void *i2, void *data) { |
universe@285 | 110 | int a = *((const long int*) i1); |
universe@285 | 111 | int b = *((const long int*) i2); |
universe@285 | 112 | if (a == b) { |
universe@285 | 113 | return 0; |
universe@285 | 114 | } else { |
universe@285 | 115 | return a < b ? -1 : 1; |
universe@285 | 116 | } |
universe@285 | 117 | } |
universe@285 | 118 | |
universe@286 | 119 | intmax_t ucx_intdist(const void *i1, const void *i2, void *data) { |
universe@286 | 120 | intmax_t a = *((const int*) i1); |
universe@286 | 121 | intmax_t b = *((const int*) i2); |
universe@286 | 122 | return a - b; |
universe@286 | 123 | } |
universe@286 | 124 | |
universe@286 | 125 | intmax_t ucx_longintdist(const void *i1, const void *i2, void *data) { |
universe@286 | 126 | intmax_t a = *((const long int*) i1); |
universe@286 | 127 | intmax_t b = *((const long int*) i2); |
universe@286 | 128 | return a - b; |
universe@286 | 129 | } |
universe@286 | 130 | |
universe@244 | 131 | int ucx_floatcmp(const void *f1, const void *f2, void *epsilon) { |
universe@244 | 132 | float a = *((const float*) f1); |
universe@244 | 133 | float b = *((const float*) f2); |
universe@92 | 134 | float e = !epsilon ? 1e-6f : *((float*)epsilon); |
universe@92 | 135 | if (fabsf(a - b) < e) { |
universe@92 | 136 | return 0; |
universe@92 | 137 | } else { |
universe@92 | 138 | return a < b ? -1 : 1; |
universe@92 | 139 | } |
universe@92 | 140 | } |
universe@92 | 141 | |
universe@244 | 142 | int ucx_doublecmp(const void *d1, const void *d2, void *epsilon) { |
universe@244 | 143 | double a = *((const double*) d1); |
universe@244 | 144 | double b = *((const double*) d2); |
universe@92 | 145 | double e = !epsilon ? 1e-14 : *((double*)epsilon); |
universe@92 | 146 | if (fabs(a - b) < e) { |
universe@92 | 147 | return 0; |
universe@92 | 148 | } else { |
universe@92 | 149 | return a < b ? -1 : 1; |
universe@92 | 150 | } |
universe@92 | 151 | } |
universe@92 | 152 | |
universe@244 | 153 | int ucx_ptrcmp(const void *ptr1, const void *ptr2, void *data) { |
universe@244 | 154 | const intptr_t p1 = (const intptr_t) ptr1; |
universe@244 | 155 | const intptr_t p2 = (const intptr_t) ptr2; |
universe@194 | 156 | if (p1 == p2) { |
universe@89 | 157 | return 0; |
universe@89 | 158 | } else { |
universe@194 | 159 | return p1 < p2 ? -1 : 1; |
universe@89 | 160 | } |
universe@89 | 161 | } |
universe@91 | 162 | |
universe@244 | 163 | int ucx_memcmp(const void *ptr1, const void *ptr2, void *n) { |
universe@91 | 164 | return memcmp(ptr1, ptr2, *((size_t*)n)); |
universe@91 | 165 | } |
olaf@142 | 166 | |
olaf@142 | 167 | /* PRINTF FUNCTIONS */ |
olaf@142 | 168 | |
universe@150 | 169 | #ifdef va_copy |
olaf@142 | 170 | #define UCX_PRINTF_BUFSIZE 256 |
universe@150 | 171 | #else |
universe@150 | 172 | #pragma message("WARNING: C99 va_copy macro not supported by this platform" \ |
universe@150 | 173 | " - limiting ucx_*printf to 2 KiB") |
universe@150 | 174 | #define UCX_PRINTF_BUFSIZE 0x800 |
universe@150 | 175 | #endif |
olaf@142 | 176 | |
olaf@142 | 177 | int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...) { |
universe@150 | 178 | int ret; |
olaf@142 | 179 | va_list ap; |
olaf@142 | 180 | va_start(ap, fmt); |
olaf@142 | 181 | ret = ucx_vfprintf(stream, wfc, fmt, ap); |
olaf@142 | 182 | va_end(ap); |
olaf@142 | 183 | return ret; |
olaf@142 | 184 | } |
olaf@142 | 185 | |
olaf@142 | 186 | int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap) { |
olaf@142 | 187 | char buf[UCX_PRINTF_BUFSIZE]; |
universe@150 | 188 | #ifdef va_copy |
olaf@144 | 189 | va_list ap2; |
olaf@144 | 190 | va_copy(ap2, ap); |
olaf@142 | 191 | int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); |
olaf@142 | 192 | if (ret < 0) { |
olaf@142 | 193 | return ret; |
olaf@142 | 194 | } else if (ret < UCX_PRINTF_BUFSIZE) { |
olaf@142 | 195 | return (int)wfc(buf, 1, ret, stream); |
olaf@142 | 196 | } else { |
olaf@142 | 197 | if (ret == INT_MAX) { |
olaf@142 | 198 | errno = ENOMEM; |
olaf@142 | 199 | return -1; |
olaf@142 | 200 | } |
olaf@142 | 201 | |
olaf@142 | 202 | int len = ret + 1; |
olaf@142 | 203 | char *newbuf = (char*)malloc(len); |
olaf@142 | 204 | if (!newbuf) { |
olaf@142 | 205 | return -1; |
olaf@142 | 206 | } |
olaf@142 | 207 | |
olaf@144 | 208 | ret = vsnprintf(newbuf, len, fmt, ap2); |
olaf@142 | 209 | if (ret > 0) { |
olaf@142 | 210 | ret = (int)wfc(newbuf, 1, ret, stream); |
olaf@142 | 211 | } |
olaf@142 | 212 | free(newbuf); |
olaf@142 | 213 | } |
olaf@142 | 214 | return ret; |
universe@150 | 215 | #else |
universe@150 | 216 | int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); |
universe@150 | 217 | if (ret < 0) { |
universe@150 | 218 | return ret; |
universe@150 | 219 | } else if (ret < UCX_PRINTF_BUFSIZE) { |
universe@150 | 220 | return (int)wfc(buf, 1, ret, stream); |
universe@150 | 221 | } else { |
universe@150 | 222 | errno = ENOMEM; |
universe@150 | 223 | return -1; |
universe@150 | 224 | } |
universe@150 | 225 | #endif |
olaf@142 | 226 | } |
olaf@142 | 227 | |
olaf@142 | 228 | sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...) { |
olaf@142 | 229 | va_list ap; |
olaf@142 | 230 | sstr_t ret; |
olaf@142 | 231 | va_start(ap, fmt); |
olaf@142 | 232 | ret = ucx_vasprintf(allocator, fmt, ap); |
olaf@142 | 233 | va_end(ap); |
olaf@142 | 234 | return ret; |
olaf@142 | 235 | } |
olaf@142 | 236 | |
olaf@142 | 237 | sstr_t ucx_vasprintf(UcxAllocator *a, const char *fmt, va_list ap) { |
olaf@142 | 238 | sstr_t s; |
olaf@142 | 239 | s.ptr = NULL; |
olaf@142 | 240 | s.length = 0; |
universe@150 | 241 | char buf[UCX_PRINTF_BUFSIZE]; |
universe@150 | 242 | #ifdef va_copy |
olaf@144 | 243 | va_list ap2; |
olaf@144 | 244 | va_copy(ap2, ap); |
olaf@142 | 245 | int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); |
olaf@142 | 246 | if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) { |
universe@173 | 247 | s.ptr = (char*)almalloc(a, ret + 1); |
universe@174 | 248 | if (s.ptr) { |
universe@174 | 249 | s.length = (size_t)ret; |
universe@174 | 250 | memcpy(s.ptr, buf, ret); |
universe@174 | 251 | s.ptr[s.length] = '\0'; |
universe@174 | 252 | } |
olaf@142 | 253 | } else if (ret == INT_MAX) { |
olaf@142 | 254 | errno = ENOMEM; |
olaf@142 | 255 | } else { |
olaf@142 | 256 | int len = ret + 1; |
universe@173 | 257 | s.ptr = (char*)almalloc(a, len); |
universe@174 | 258 | if (s.ptr) { |
universe@174 | 259 | ret = vsnprintf(s.ptr, len, fmt, ap2); |
universe@174 | 260 | if (ret < 0) { |
universe@174 | 261 | free(s.ptr); |
universe@174 | 262 | s.ptr = NULL; |
universe@174 | 263 | } else { |
universe@174 | 264 | s.length = (size_t)ret; |
universe@174 | 265 | } |
olaf@142 | 266 | } |
olaf@142 | 267 | } |
universe@150 | 268 | #else |
universe@150 | 269 | int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); |
universe@150 | 270 | if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) { |
universe@173 | 271 | s.ptr = (char*)almalloc(a, ret + 1); |
universe@174 | 272 | if (s.ptr) { |
universe@174 | 273 | s.length = (size_t)ret; |
universe@174 | 274 | memcpy(s.ptr, buf, ret); |
universe@174 | 275 | s.ptr[s.length] = '\0'; |
universe@174 | 276 | } |
universe@150 | 277 | } else { |
universe@150 | 278 | errno = ENOMEM; |
universe@150 | 279 | } |
universe@150 | 280 | #endif |
olaf@142 | 281 | return s; |
olaf@142 | 282 | } |