src/utils.c

Mon, 14 May 2018 18:25:20 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 14 May 2018 18:25:20 +0200
changeset 314
5d28dc8f0765
parent 313
b7753273f0fd
child 349
05957b1d10a5
permissions
-rw-r--r--

renames int and longint distance and compare functions according to the new scheme

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

mercurial