ucx/utils.c

Sun, 17 May 2015 18:32:41 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 17 May 2015 18:32:41 +0200
changeset 194
0c1b7676e74c
parent 192
1e51558b9d09
child 222
e0f850709a5c
permissions
-rw-r--r--

finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()

universe@103 1 /*
universe@103 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@103 3 *
universe@192 4 * Copyright 2015 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@94 29 #include "utils.h"
universe@140 30 #include <math.h>
olaf@142 31 #include <stdio.h>
olaf@142 32 #include <limits.h>
olaf@142 33 #include <errno.h>
universe@89 34
universe@94 35 /* COPY FUCNTIONS */
universe@94 36 void* ucx_strcpy(void* s, void* data) {
universe@94 37 char *str = (char*) s;
universe@94 38 size_t n = 1+strlen(str);
universe@94 39 char *cpy = (char*) malloc(n);
universe@94 40 memcpy(cpy, str, n);
universe@94 41 return cpy;
universe@94 42 }
universe@94 43
universe@94 44 void* ucx_memcpy(void* m, void* n) {
universe@94 45 size_t k = *((size_t*)n);
universe@94 46 void *cpy = malloc(k);
universe@94 47 memcpy(cpy, m, k);
universe@94 48 return cpy;
universe@94 49 }
universe@94 50
universe@140 51 size_t ucx_stream_copy(void *src, void *dest, read_func readfnc,
universe@140 52 write_func writefnc, char* buf, size_t bufsize, size_t n) {
universe@140 53 if(n == 0 || bufsize == 0) {
universe@140 54 return 0;
universe@140 55 }
universe@140 56
universe@181 57 char *lbuf;
universe@140 58 size_t ncp = 0;
universe@181 59
universe@181 60 if(buf) {
universe@181 61 lbuf = buf;
universe@181 62 } else {
universe@181 63 lbuf = (char*)malloc(bufsize);
universe@181 64 if(lbuf == NULL) {
universe@140 65 return 0;
universe@140 66 }
universe@140 67 }
universe@140 68
universe@140 69 size_t r;
universe@140 70 size_t rn = bufsize > n ? n : bufsize;
universe@181 71 while((r = readfnc(lbuf, 1, rn, src)) != 0) {
universe@181 72 r = writefnc(lbuf, 1, r, dest);
universe@140 73 ncp += r;
universe@140 74 n -= r;
universe@140 75 rn = bufsize > n ? n : bufsize;
universe@140 76 if(r == 0 || n == 0) {
universe@140 77 break;
universe@140 78 }
universe@140 79 }
universe@140 80
universe@181 81 if (lbuf != buf) {
universe@181 82 free(lbuf);
universe@181 83 }
universe@181 84
universe@140 85 return ncp;
universe@140 86 }
universe@140 87
olaf@142 88 /* COMPARE FUNCTIONS */
universe@94 89
universe@89 90 int ucx_strcmp(void *s1, void *s2, void *data) {
universe@89 91 return strcmp((char*)s1, (char*)s2);
universe@89 92 }
universe@89 93
universe@89 94 int ucx_strncmp(void *s1, void *s2, void *n) {
universe@89 95 return strncmp((char*)s1, (char*)s2, *((size_t*) n));
universe@89 96 }
universe@89 97
universe@89 98 int ucx_intcmp(void *i1, void *i2, void *data) {
universe@89 99 int a = *((int*) i1);
universe@89 100 int b = *((int*) i2);
universe@89 101 if (a == b) {
universe@89 102 return 0;
universe@89 103 } else {
universe@89 104 return a < b ? -1 : 1;
universe@89 105 }
universe@89 106 }
universe@89 107
universe@92 108 int ucx_floatcmp(void *f1, void *f2, void *epsilon) {
universe@92 109 float a = *((float*) f1);
universe@92 110 float b = *((float*) f2);
universe@92 111 float e = !epsilon ? 1e-6f : *((float*)epsilon);
universe@92 112 if (fabsf(a - b) < e) {
universe@92 113 return 0;
universe@92 114 } else {
universe@92 115 return a < b ? -1 : 1;
universe@92 116 }
universe@92 117 }
universe@92 118
universe@92 119 int ucx_doublecmp(void *d1, void *d2, void *epsilon) {
universe@92 120 double a = *((float*) d1);
universe@92 121 double b = *((float*) d2);
universe@92 122 double e = !epsilon ? 1e-14 : *((double*)epsilon);
universe@92 123 if (fabs(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@89 130 int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) {
universe@194 131 intptr_t p1 = (intptr_t) ptr1;
universe@194 132 intptr_t p2 = (intptr_t) ptr2;
universe@194 133 if (p1 == p2) {
universe@89 134 return 0;
universe@89 135 } else {
universe@194 136 return p1 < p2 ? -1 : 1;
universe@89 137 }
universe@89 138 }
universe@91 139
universe@91 140 int ucx_memcmp(void *ptr1, void *ptr2, void *n) {
universe@91 141 return memcmp(ptr1, ptr2, *((size_t*)n));
universe@91 142 }
olaf@142 143
olaf@142 144 /* PRINTF FUNCTIONS */
olaf@142 145
universe@150 146 #ifdef va_copy
olaf@142 147 #define UCX_PRINTF_BUFSIZE 256
universe@150 148 #else
universe@150 149 #pragma message("WARNING: C99 va_copy macro not supported by this platform" \
universe@150 150 " - limiting ucx_*printf to 2 KiB")
universe@150 151 #define UCX_PRINTF_BUFSIZE 0x800
universe@150 152 #endif
olaf@142 153
olaf@142 154 int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...) {
universe@150 155 int ret;
olaf@142 156 va_list ap;
olaf@142 157 va_start(ap, fmt);
olaf@142 158 ret = ucx_vfprintf(stream, wfc, fmt, ap);
olaf@142 159 va_end(ap);
olaf@142 160 return ret;
olaf@142 161 }
olaf@142 162
olaf@142 163 int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap) {
olaf@142 164 char buf[UCX_PRINTF_BUFSIZE];
universe@150 165 #ifdef va_copy
olaf@144 166 va_list ap2;
olaf@144 167 va_copy(ap2, ap);
olaf@142 168 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
olaf@142 169 if (ret < 0) {
olaf@142 170 return ret;
olaf@142 171 } else if (ret < UCX_PRINTF_BUFSIZE) {
olaf@142 172 return (int)wfc(buf, 1, ret, stream);
olaf@142 173 } else {
olaf@142 174 if (ret == INT_MAX) {
olaf@142 175 errno = ENOMEM;
olaf@142 176 return -1;
olaf@142 177 }
olaf@142 178
olaf@142 179 int len = ret + 1;
olaf@142 180 char *newbuf = (char*)malloc(len);
olaf@142 181 if (!newbuf) {
olaf@142 182 return -1;
olaf@142 183 }
olaf@142 184
olaf@144 185 ret = vsnprintf(newbuf, len, fmt, ap2);
olaf@142 186 if (ret > 0) {
olaf@142 187 ret = (int)wfc(newbuf, 1, ret, stream);
olaf@142 188 }
olaf@142 189 free(newbuf);
olaf@142 190 }
olaf@142 191 return ret;
universe@150 192 #else
universe@150 193 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
universe@150 194 if (ret < 0) {
universe@150 195 return ret;
universe@150 196 } else if (ret < UCX_PRINTF_BUFSIZE) {
universe@150 197 return (int)wfc(buf, 1, ret, stream);
universe@150 198 } else {
universe@150 199 errno = ENOMEM;
universe@150 200 return -1;
universe@150 201 }
universe@150 202 #endif
olaf@142 203 }
olaf@142 204
olaf@142 205 sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...) {
olaf@142 206 va_list ap;
olaf@142 207 sstr_t ret;
olaf@142 208 va_start(ap, fmt);
olaf@142 209 ret = ucx_vasprintf(allocator, fmt, ap);
olaf@142 210 va_end(ap);
olaf@142 211 return ret;
olaf@142 212 }
olaf@142 213
olaf@142 214 sstr_t ucx_vasprintf(UcxAllocator *a, const char *fmt, va_list ap) {
olaf@142 215 sstr_t s;
olaf@142 216 s.ptr = NULL;
olaf@142 217 s.length = 0;
universe@150 218 char buf[UCX_PRINTF_BUFSIZE];
universe@150 219 #ifdef va_copy
olaf@144 220 va_list ap2;
olaf@144 221 va_copy(ap2, ap);
olaf@142 222 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
olaf@142 223 if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
universe@173 224 s.ptr = (char*)almalloc(a, ret + 1);
universe@174 225 if (s.ptr) {
universe@174 226 s.length = (size_t)ret;
universe@174 227 memcpy(s.ptr, buf, ret);
universe@174 228 s.ptr[s.length] = '\0';
universe@174 229 }
olaf@142 230 } else if (ret == INT_MAX) {
olaf@142 231 errno = ENOMEM;
olaf@142 232 } else {
olaf@142 233 int len = ret + 1;
universe@173 234 s.ptr = (char*)almalloc(a, len);
universe@174 235 if (s.ptr) {
universe@174 236 ret = vsnprintf(s.ptr, len, fmt, ap2);
universe@174 237 if (ret < 0) {
universe@174 238 free(s.ptr);
universe@174 239 s.ptr = NULL;
universe@174 240 } else {
universe@174 241 s.length = (size_t)ret;
universe@174 242 }
olaf@142 243 }
olaf@142 244 }
universe@150 245 #else
universe@150 246 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
universe@150 247 if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
universe@173 248 s.ptr = (char*)almalloc(a, ret + 1);
universe@174 249 if (s.ptr) {
universe@174 250 s.length = (size_t)ret;
universe@174 251 memcpy(s.ptr, buf, ret);
universe@174 252 s.ptr[s.length] = '\0';
universe@174 253 }
universe@150 254 } else {
universe@150 255 errno = ENOMEM;
universe@150 256 }
universe@150 257 #endif
olaf@142 258 return s;
olaf@142 259 }

mercurial