ucx/utils.c

Tue, 13 Aug 2013 14:20:12 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 13 Aug 2013 14:20:12 +0200
changeset 140
15f871f50bfd
parent 103
08018864fb91
child 142
ee8cb27d8b8e
permissions
-rw-r--r--

completed documentation + changed API for buffer/stream generic copy functions

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@140 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@140 48 size_t ucx_stream_copy(void *src, void *dest, read_func readfnc,
universe@140 49 write_func writefnc, char* buf, size_t bufsize, size_t n) {
universe@140 50 if(n == 0 || bufsize == 0) {
universe@140 51 return 0;
universe@140 52 }
universe@140 53
universe@140 54 size_t ncp = 0;
universe@140 55 if (!buf) {
universe@140 56 buf = (char*)malloc(bufsize);
universe@140 57 if(buf == NULL) {
universe@140 58 return 0;
universe@140 59 }
universe@140 60 }
universe@140 61
universe@140 62 size_t r;
universe@140 63 size_t rn = bufsize > n ? n : bufsize;
universe@140 64 while((r = readfnc(buf, 1, rn, src)) != 0) {
universe@140 65 r = writefnc(buf, 1, r, dest);
universe@140 66 ncp += r;
universe@140 67 n -= r;
universe@140 68 rn = bufsize > n ? n : bufsize;
universe@140 69 if(r == 0 || n == 0) {
universe@140 70 break;
universe@140 71 }
universe@140 72 }
universe@140 73
universe@140 74 free(buf);
universe@140 75 return ncp;
universe@140 76 }
universe@140 77
universe@94 78 /* COMPARE FUNCTION */
universe@94 79
universe@89 80 int ucx_strcmp(void *s1, void *s2, void *data) {
universe@89 81 return strcmp((char*)s1, (char*)s2);
universe@89 82 }
universe@89 83
universe@89 84 int ucx_strncmp(void *s1, void *s2, void *n) {
universe@89 85 return strncmp((char*)s1, (char*)s2, *((size_t*) n));
universe@89 86 }
universe@89 87
universe@89 88 int ucx_intcmp(void *i1, void *i2, void *data) {
universe@89 89 int a = *((int*) i1);
universe@89 90 int b = *((int*) i2);
universe@89 91 if (a == b) {
universe@89 92 return 0;
universe@89 93 } else {
universe@89 94 return a < b ? -1 : 1;
universe@89 95 }
universe@89 96 }
universe@89 97
universe@92 98 int ucx_floatcmp(void *f1, void *f2, void *epsilon) {
universe@92 99 float a = *((float*) f1);
universe@92 100 float b = *((float*) f2);
universe@92 101 float e = !epsilon ? 1e-6f : *((float*)epsilon);
universe@92 102 if (fabsf(a - b) < e) {
universe@92 103 return 0;
universe@92 104 } else {
universe@92 105 return a < b ? -1 : 1;
universe@92 106 }
universe@92 107 }
universe@92 108
universe@92 109 int ucx_doublecmp(void *d1, void *d2, void *epsilon) {
universe@92 110 double a = *((float*) d1);
universe@92 111 double b = *((float*) d2);
universe@92 112 double e = !epsilon ? 1e-14 : *((double*)epsilon);
universe@92 113 if (fabs(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@89 120 int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) {
universe@89 121 if (ptr1 == ptr2) {
universe@89 122 return 0;
universe@89 123 } else {
universe@89 124 return ptr1 < ptr2 ? -1 : 1;
universe@89 125 }
universe@89 126 }
universe@91 127
universe@91 128 int ucx_memcmp(void *ptr1, void *ptr2, void *n) {
universe@91 129 return memcmp(ptr1, ptr2, *((size_t*)n));
universe@91 130 }

mercurial