src/ucx/utils.c

Tue, 23 Aug 2016 13:49:38 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Aug 2016 13:49:38 +0200
changeset 39
ac35daceb24c
permissions
-rw-r--r--

adds UCX + changes how the input file is read (uses an consecutive memory area now)

universe@39 1 /*
universe@39 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@39 3 *
universe@39 4 * Copyright 2015 Olaf Wintermann. All rights reserved.
universe@39 5 *
universe@39 6 * Redistribution and use in source and binary forms, with or without
universe@39 7 * modification, are permitted provided that the following conditions are met:
universe@39 8 *
universe@39 9 * 1. Redistributions of source code must retain the above copyright
universe@39 10 * notice, this list of conditions and the following disclaimer.
universe@39 11 *
universe@39 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@39 13 * notice, this list of conditions and the following disclaimer in the
universe@39 14 * documentation and/or other materials provided with the distribution.
universe@39 15 *
universe@39 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@39 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@39 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@39 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@39 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@39 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@39 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@39 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@39 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@39 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@39 26 * POSSIBILITY OF SUCH DAMAGE.
universe@39 27 */
universe@39 28
universe@39 29 #include "utils.h"
universe@39 30 #include <math.h>
universe@39 31 #include <stdio.h>
universe@39 32 #include <limits.h>
universe@39 33 #include <errno.h>
universe@39 34
universe@39 35 /* COPY FUCNTIONS */
universe@39 36 void* ucx_strcpy(void* s, void* data) {
universe@39 37 char *str = (char*) s;
universe@39 38 size_t n = 1+strlen(str);
universe@39 39 char *cpy = (char*) malloc(n);
universe@39 40 memcpy(cpy, str, n);
universe@39 41 return cpy;
universe@39 42 }
universe@39 43
universe@39 44 void* ucx_memcpy(void* m, void* n) {
universe@39 45 size_t k = *((size_t*)n);
universe@39 46 void *cpy = malloc(k);
universe@39 47 memcpy(cpy, m, k);
universe@39 48 return cpy;
universe@39 49 }
universe@39 50
universe@39 51 size_t ucx_stream_copy(void *src, void *dest, read_func readfnc,
universe@39 52 write_func writefnc, char* buf, size_t bufsize, size_t n) {
universe@39 53 if(n == 0 || bufsize == 0) {
universe@39 54 return 0;
universe@39 55 }
universe@39 56
universe@39 57 char *lbuf;
universe@39 58 size_t ncp = 0;
universe@39 59
universe@39 60 if(buf) {
universe@39 61 lbuf = buf;
universe@39 62 } else {
universe@39 63 lbuf = (char*)malloc(bufsize);
universe@39 64 if(lbuf == NULL) {
universe@39 65 return 0;
universe@39 66 }
universe@39 67 }
universe@39 68
universe@39 69 size_t r;
universe@39 70 size_t rn = bufsize > n ? n : bufsize;
universe@39 71 while((r = readfnc(lbuf, 1, rn, src)) != 0) {
universe@39 72 r = writefnc(lbuf, 1, r, dest);
universe@39 73 ncp += r;
universe@39 74 n -= r;
universe@39 75 rn = bufsize > n ? n : bufsize;
universe@39 76 if(r == 0 || n == 0) {
universe@39 77 break;
universe@39 78 }
universe@39 79 }
universe@39 80
universe@39 81 if (lbuf != buf) {
universe@39 82 free(lbuf);
universe@39 83 }
universe@39 84
universe@39 85 return ncp;
universe@39 86 }
universe@39 87
universe@39 88 /* COMPARE FUNCTIONS */
universe@39 89
universe@39 90 int ucx_strcmp(void *s1, void *s2, void *data) {
universe@39 91 return strcmp((char*)s1, (char*)s2);
universe@39 92 }
universe@39 93
universe@39 94 int ucx_strncmp(void *s1, void *s2, void *n) {
universe@39 95 return strncmp((char*)s1, (char*)s2, *((size_t*) n));
universe@39 96 }
universe@39 97
universe@39 98 int ucx_intcmp(void *i1, void *i2, void *data) {
universe@39 99 int a = *((int*) i1);
universe@39 100 int b = *((int*) i2);
universe@39 101 if (a == b) {
universe@39 102 return 0;
universe@39 103 } else {
universe@39 104 return a < b ? -1 : 1;
universe@39 105 }
universe@39 106 }
universe@39 107
universe@39 108 int ucx_floatcmp(void *f1, void *f2, void *epsilon) {
universe@39 109 float a = *((float*) f1);
universe@39 110 float b = *((float*) f2);
universe@39 111 float e = !epsilon ? 1e-6f : *((float*)epsilon);
universe@39 112 if (fabsf(a - b) < e) {
universe@39 113 return 0;
universe@39 114 } else {
universe@39 115 return a < b ? -1 : 1;
universe@39 116 }
universe@39 117 }
universe@39 118
universe@39 119 int ucx_doublecmp(void *d1, void *d2, void *epsilon) {
universe@39 120 double a = *((float*) d1);
universe@39 121 double b = *((float*) d2);
universe@39 122 double e = !epsilon ? 1e-14 : *((double*)epsilon);
universe@39 123 if (fabs(a - b) < e) {
universe@39 124 return 0;
universe@39 125 } else {
universe@39 126 return a < b ? -1 : 1;
universe@39 127 }
universe@39 128 }
universe@39 129
universe@39 130 int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) {
universe@39 131 intptr_t p1 = (intptr_t) ptr1;
universe@39 132 intptr_t p2 = (intptr_t) ptr2;
universe@39 133 if (p1 == p2) {
universe@39 134 return 0;
universe@39 135 } else {
universe@39 136 return p1 < p2 ? -1 : 1;
universe@39 137 }
universe@39 138 }
universe@39 139
universe@39 140 int ucx_memcmp(void *ptr1, void *ptr2, void *n) {
universe@39 141 return memcmp(ptr1, ptr2, *((size_t*)n));
universe@39 142 }
universe@39 143
universe@39 144 /* PRINTF FUNCTIONS */
universe@39 145
universe@39 146 #ifdef va_copy
universe@39 147 #define UCX_PRINTF_BUFSIZE 256
universe@39 148 #else
universe@39 149 #pragma message("WARNING: C99 va_copy macro not supported by this platform" \
universe@39 150 " - limiting ucx_*printf to 2 KiB")
universe@39 151 #define UCX_PRINTF_BUFSIZE 0x800
universe@39 152 #endif
universe@39 153
universe@39 154 int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...) {
universe@39 155 int ret;
universe@39 156 va_list ap;
universe@39 157 va_start(ap, fmt);
universe@39 158 ret = ucx_vfprintf(stream, wfc, fmt, ap);
universe@39 159 va_end(ap);
universe@39 160 return ret;
universe@39 161 }
universe@39 162
universe@39 163 int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap) {
universe@39 164 char buf[UCX_PRINTF_BUFSIZE];
universe@39 165 #ifdef va_copy
universe@39 166 va_list ap2;
universe@39 167 va_copy(ap2, ap);
universe@39 168 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
universe@39 169 if (ret < 0) {
universe@39 170 return ret;
universe@39 171 } else if (ret < UCX_PRINTF_BUFSIZE) {
universe@39 172 return (int)wfc(buf, 1, ret, stream);
universe@39 173 } else {
universe@39 174 if (ret == INT_MAX) {
universe@39 175 errno = ENOMEM;
universe@39 176 return -1;
universe@39 177 }
universe@39 178
universe@39 179 int len = ret + 1;
universe@39 180 char *newbuf = (char*)malloc(len);
universe@39 181 if (!newbuf) {
universe@39 182 return -1;
universe@39 183 }
universe@39 184
universe@39 185 ret = vsnprintf(newbuf, len, fmt, ap2);
universe@39 186 if (ret > 0) {
universe@39 187 ret = (int)wfc(newbuf, 1, ret, stream);
universe@39 188 }
universe@39 189 free(newbuf);
universe@39 190 }
universe@39 191 return ret;
universe@39 192 #else
universe@39 193 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
universe@39 194 if (ret < 0) {
universe@39 195 return ret;
universe@39 196 } else if (ret < UCX_PRINTF_BUFSIZE) {
universe@39 197 return (int)wfc(buf, 1, ret, stream);
universe@39 198 } else {
universe@39 199 errno = ENOMEM;
universe@39 200 return -1;
universe@39 201 }
universe@39 202 #endif
universe@39 203 }
universe@39 204
universe@39 205 sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...) {
universe@39 206 va_list ap;
universe@39 207 sstr_t ret;
universe@39 208 va_start(ap, fmt);
universe@39 209 ret = ucx_vasprintf(allocator, fmt, ap);
universe@39 210 va_end(ap);
universe@39 211 return ret;
universe@39 212 }
universe@39 213
universe@39 214 sstr_t ucx_vasprintf(UcxAllocator *a, const char *fmt, va_list ap) {
universe@39 215 sstr_t s;
universe@39 216 s.ptr = NULL;
universe@39 217 s.length = 0;
universe@39 218 char buf[UCX_PRINTF_BUFSIZE];
universe@39 219 #ifdef va_copy
universe@39 220 va_list ap2;
universe@39 221 va_copy(ap2, ap);
universe@39 222 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
universe@39 223 if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
universe@39 224 s.ptr = (char*)almalloc(a, ret + 1);
universe@39 225 if (s.ptr) {
universe@39 226 s.length = (size_t)ret;
universe@39 227 memcpy(s.ptr, buf, ret);
universe@39 228 s.ptr[s.length] = '\0';
universe@39 229 }
universe@39 230 } else if (ret == INT_MAX) {
universe@39 231 errno = ENOMEM;
universe@39 232 } else {
universe@39 233 int len = ret + 1;
universe@39 234 s.ptr = (char*)almalloc(a, len);
universe@39 235 if (s.ptr) {
universe@39 236 ret = vsnprintf(s.ptr, len, fmt, ap2);
universe@39 237 if (ret < 0) {
universe@39 238 free(s.ptr);
universe@39 239 s.ptr = NULL;
universe@39 240 } else {
universe@39 241 s.length = (size_t)ret;
universe@39 242 }
universe@39 243 }
universe@39 244 }
universe@39 245 #else
universe@39 246 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
universe@39 247 if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
universe@39 248 s.ptr = (char*)almalloc(a, ret + 1);
universe@39 249 if (s.ptr) {
universe@39 250 s.length = (size_t)ret;
universe@39 251 memcpy(s.ptr, buf, ret);
universe@39 252 s.ptr[s.length] = '\0';
universe@39 253 }
universe@39 254 } else {
universe@39 255 errno = ENOMEM;
universe@39 256 }
universe@39 257 #endif
universe@39 258 return s;
universe@39 259 }

mercurial