src/utils.c

Thu, 03 May 2018 09:49:38 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 03 May 2018 09:49:38 +0200
changeset 285
7be3ae7ffb58
parent 259
2f5dea574a75
child 286
85f55abea563
permissions
-rw-r--r--

adds ucx_longintcmp() compare function

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

mercurial