ucx/utils.c

Thu, 28 Feb 2013 08:50:24 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 28 Feb 2013 08:50:24 +0100
changeset 103
08018864fb91
parent 94
57ea041df22f
child 140
15f871f50bfd
permissions
-rw-r--r--

added license and copyright notice to all files

universe@103 1 /*
universe@103 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@103 3 *
universe@103 4 * Copyright 2013 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@92 30 #include "math.h"
universe@89 31
universe@94 32 /* COPY FUCNTIONS */
universe@94 33 void* ucx_strcpy(void* s, void* data) {
universe@94 34 char *str = (char*) s;
universe@94 35 size_t n = 1+strlen(str);
universe@94 36 char *cpy = (char*) malloc(n);
universe@94 37 memcpy(cpy, str, n);
universe@94 38 return cpy;
universe@94 39 }
universe@94 40
universe@94 41 void* ucx_memcpy(void* m, void* n) {
universe@94 42 size_t k = *((size_t*)n);
universe@94 43 void *cpy = malloc(k);
universe@94 44 memcpy(cpy, m, k);
universe@94 45 return cpy;
universe@94 46 }
universe@94 47
universe@94 48 /* COMPARE FUNCTION */
universe@94 49
universe@89 50 int ucx_strcmp(void *s1, void *s2, void *data) {
universe@89 51 return strcmp((char*)s1, (char*)s2);
universe@89 52 }
universe@89 53
universe@89 54 int ucx_strncmp(void *s1, void *s2, void *n) {
universe@89 55 return strncmp((char*)s1, (char*)s2, *((size_t*) n));
universe@89 56 }
universe@89 57
universe@89 58 int ucx_intcmp(void *i1, void *i2, void *data) {
universe@89 59 int a = *((int*) i1);
universe@89 60 int b = *((int*) i2);
universe@89 61 if (a == b) {
universe@89 62 return 0;
universe@89 63 } else {
universe@89 64 return a < b ? -1 : 1;
universe@89 65 }
universe@89 66 }
universe@89 67
universe@92 68 int ucx_floatcmp(void *f1, void *f2, void *epsilon) {
universe@92 69 float a = *((float*) f1);
universe@92 70 float b = *((float*) f2);
universe@92 71 float e = !epsilon ? 1e-6f : *((float*)epsilon);
universe@92 72 if (fabsf(a - b) < e) {
universe@92 73 return 0;
universe@92 74 } else {
universe@92 75 return a < b ? -1 : 1;
universe@92 76 }
universe@92 77 }
universe@92 78
universe@92 79 int ucx_doublecmp(void *d1, void *d2, void *epsilon) {
universe@92 80 double a = *((float*) d1);
universe@92 81 double b = *((float*) d2);
universe@92 82 double e = !epsilon ? 1e-14 : *((double*)epsilon);
universe@92 83 if (fabs(a - b) < e) {
universe@92 84 return 0;
universe@92 85 } else {
universe@92 86 return a < b ? -1 : 1;
universe@92 87 }
universe@92 88 }
universe@92 89
universe@89 90 int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) {
universe@89 91 if (ptr1 == ptr2) {
universe@89 92 return 0;
universe@89 93 } else {
universe@89 94 return ptr1 < ptr2 ? -1 : 1;
universe@89 95 }
universe@89 96 }
universe@91 97
universe@91 98 int ucx_memcmp(void *ptr1, void *ptr2, void *n) {
universe@91 99 return memcmp(ptr1, ptr2, *((size_t*)n));
universe@91 100 }

mercurial