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

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2013 Olaf Wintermann. All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  */
    29 #include "utils.h"
    30 #include <math.h>
    32 /* COPY FUCNTIONS */
    33 void* ucx_strcpy(void* s, void* data) {
    34     char *str = (char*) s;
    35     size_t n = 1+strlen(str);
    36     char *cpy = (char*) malloc(n);
    37     memcpy(cpy, str, n);
    38     return cpy;
    39 }
    41 void* ucx_memcpy(void* m, void* n) {
    42     size_t k = *((size_t*)n);
    43     void *cpy = malloc(k);
    44     memcpy(cpy, m, k);
    45     return cpy;
    46 }
    48 size_t ucx_stream_copy(void *src, void *dest, read_func readfnc,
    49         write_func writefnc, char* buf, size_t bufsize, size_t n) {
    50     if(n == 0 || bufsize == 0) {
    51         return 0;
    52     }
    54     size_t ncp = 0;
    55     if (!buf) {
    56         buf = (char*)malloc(bufsize);
    57         if(buf == NULL) {
    58             return 0;
    59         }
    60     }
    62     size_t r;
    63     size_t rn = bufsize > n ? n : bufsize;
    64     while((r = readfnc(buf, 1, rn, src)) != 0) {
    65         r = writefnc(buf, 1, r, dest);
    66         ncp += r;
    67         n -= r;
    68         rn = bufsize > n ? n : bufsize;
    69         if(r == 0 || n == 0) {
    70             break;
    71         }
    72     }
    74     free(buf);
    75     return ncp;
    76 }
    78 /* COMPARE FUNCTION */
    80 int ucx_strcmp(void *s1, void *s2, void *data) {
    81     return strcmp((char*)s1, (char*)s2);
    82 }
    84 int ucx_strncmp(void *s1, void *s2, void *n) {
    85     return strncmp((char*)s1, (char*)s2, *((size_t*) n));
    86 }
    88 int ucx_intcmp(void *i1, void *i2, void *data) {
    89    int a = *((int*) i1);
    90    int b = *((int*) i2);
    91    if (a == b) {
    92        return 0;
    93    } else {
    94        return a < b ? -1 : 1;
    95    }
    96 }
    98 int ucx_floatcmp(void *f1, void *f2, void *epsilon) {
    99    float a = *((float*) f1);
   100    float b = *((float*) f2);
   101    float e = !epsilon ? 1e-6f : *((float*)epsilon);
   102    if (fabsf(a - b) < e) {
   103        return 0;
   104    } else {
   105        return a < b ? -1 : 1;
   106    }
   107 }
   109 int ucx_doublecmp(void *d1, void *d2, void *epsilon) {
   110    double a = *((float*) d1);
   111    double b = *((float*) d2);
   112    double e = !epsilon ? 1e-14 : *((double*)epsilon);
   113    if (fabs(a - b) < e) {
   114        return 0;
   115    } else {
   116        return a < b ? -1 : 1;
   117    }
   118 }
   120 int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) {
   121     if (ptr1 == ptr2) {
   122         return 0;
   123     } else {
   124         return ptr1 < ptr2 ? -1 : 1;
   125     }
   126 }
   128 int ucx_memcmp(void *ptr1, void *ptr2, void *n) {
   129     return memcmp(ptr1, ptr2, *((size_t*)n));
   130 }

mercurial