src/utils.c

Sat, 28 Oct 2017 15:59:16 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 28 Oct 2017 15:59:16 +0200
changeset 260
a6184aff5108
parent 259
2f5dea574a75
child 285
7be3ae7ffb58
permissions
-rw-r--r--

rename LICENSE to COPYING to be distributed by autoconf

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

mercurial