src/server/ucx/utils.c

Sat, 22 Jun 2013 13:08:36 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 22 Jun 2013 13:08:36 +0200
changeset 72
9b888965f3ee
parent 71
069c152f6272
permissions
-rw-r--r--

some fixes

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2013 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "utils.h"
#include "math.h"

/* COPY FUCNTIONS */
void* ucx_strcpy(void* s, void* data) {
    char *str = (char*) s;
    size_t n = 1+strlen(str);
    char *cpy = (char*) malloc(n);
    memcpy(cpy, str, n);
    return cpy;
}

void* ucx_memcpy(void* m, void* n) {
    size_t k = *((size_t*)n);
    void *cpy = malloc(k);
    memcpy(cpy, m, k);
    return cpy;
}

/* COMPARE FUNCTION */

int ucx_strcmp(void *s1, void *s2, void *data) {
    return strcmp((char*)s1, (char*)s2);
}

int ucx_strncmp(void *s1, void *s2, void *n) {
    return strncmp((char*)s1, (char*)s2, *((size_t*) n));
}

int ucx_intcmp(void *i1, void *i2, void *data) {
   int a = *((int*) i1);
   int b = *((int*) i2);
   if (a == b) {
       return 0;
   } else {
       return a < b ? -1 : 1;
   }
}

int ucx_floatcmp(void *f1, void *f2, void *epsilon) {
   float a = *((float*) f1);
   float b = *((float*) f2);
   float e = !epsilon ? 1e-6f : *((float*)epsilon);
   if (fabsf(a - b) < e) {
       return 0;
   } else {
       return a < b ? -1 : 1;
   }
}

int ucx_doublecmp(void *d1, void *d2, void *epsilon) {
   double a = *((float*) d1);
   double b = *((float*) d2);
   double e = !epsilon ? 1e-14 : *((double*)epsilon);
   if (fabs(a - b) < e) {
       return 0;
   } else {
       return a < b ? -1 : 1;
   }
}

int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) {
    if (ptr1 == ptr2) {
        return 0;
    } else {
        return ptr1 < ptr2 ? -1 : 1;
    }
}

int ucx_memcmp(void *ptr1, void *ptr2, void *n) {
    return memcmp(ptr1, ptr2, *((size_t*)n));
}

mercurial