src/ucx/utils.c

changeset 61
47a5fc33590a
parent 60
9f25df78925e
child 62
3fff4c364ffc
     1.1 --- a/src/ucx/utils.c	Thu Nov 10 18:44:48 2016 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,259 +0,0 @@
     1.4 -/*
     1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 - *
     1.7 - * Copyright 2015 Olaf Wintermann. All rights reserved.
     1.8 - *
     1.9 - * Redistribution and use in source and binary forms, with or without
    1.10 - * modification, are permitted provided that the following conditions are met:
    1.11 - *
    1.12 - *   1. Redistributions of source code must retain the above copyright
    1.13 - *      notice, this list of conditions and the following disclaimer.
    1.14 - *
    1.15 - *   2. Redistributions in binary form must reproduce the above copyright
    1.16 - *      notice, this list of conditions and the following disclaimer in the
    1.17 - *      documentation and/or other materials provided with the distribution.
    1.18 - *
    1.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.29 - * POSSIBILITY OF SUCH DAMAGE.
    1.30 - */
    1.31 -
    1.32 -#include "utils.h"
    1.33 -#include <math.h>
    1.34 -#include <stdio.h>
    1.35 -#include <limits.h>
    1.36 -#include <errno.h>
    1.37 -
    1.38 -/* COPY FUCNTIONS */
    1.39 -void* ucx_strcpy(void* s, void* data) {
    1.40 -    char *str = (char*) s;
    1.41 -    size_t n = 1+strlen(str);
    1.42 -    char *cpy = (char*) malloc(n);
    1.43 -    memcpy(cpy, str, n);
    1.44 -    return cpy;
    1.45 -}
    1.46 -
    1.47 -void* ucx_memcpy(void* m, void* n) {
    1.48 -    size_t k = *((size_t*)n);
    1.49 -    void *cpy = malloc(k);
    1.50 -    memcpy(cpy, m, k);
    1.51 -    return cpy;
    1.52 -}
    1.53 -
    1.54 -size_t ucx_stream_copy(void *src, void *dest, read_func readfnc,
    1.55 -        write_func writefnc, char* buf, size_t bufsize, size_t n) {
    1.56 -    if(n == 0 || bufsize == 0) {
    1.57 -        return 0;
    1.58 -    }
    1.59 -    
    1.60 -    char *lbuf;    
    1.61 -    size_t ncp = 0;
    1.62 -    
    1.63 -    if(buf) {
    1.64 -        lbuf = buf;
    1.65 -    } else {
    1.66 -        lbuf = (char*)malloc(bufsize);
    1.67 -        if(lbuf == NULL) {
    1.68 -            return 0;
    1.69 -        }
    1.70 -    }
    1.71 -    
    1.72 -    size_t r;
    1.73 -    size_t rn = bufsize > n ? n : bufsize;
    1.74 -    while((r = readfnc(lbuf, 1, rn, src)) != 0) {
    1.75 -        r = writefnc(lbuf, 1, r, dest);
    1.76 -        ncp += r;
    1.77 -        n -= r;
    1.78 -        rn = bufsize > n ? n : bufsize;
    1.79 -        if(r == 0 || n == 0) {
    1.80 -            break;
    1.81 -        }
    1.82 -    }
    1.83 -    
    1.84 -    if (lbuf != buf) {
    1.85 -        free(lbuf);
    1.86 -    }
    1.87 -    
    1.88 -    return ncp;
    1.89 -}
    1.90 -
    1.91 -/* COMPARE FUNCTIONS */
    1.92 -
    1.93 -int ucx_strcmp(void *s1, void *s2, void *data) {
    1.94 -    return strcmp((char*)s1, (char*)s2);
    1.95 -}
    1.96 -
    1.97 -int ucx_strncmp(void *s1, void *s2, void *n) {
    1.98 -    return strncmp((char*)s1, (char*)s2, *((size_t*) n));
    1.99 -}
   1.100 -
   1.101 -int ucx_intcmp(void *i1, void *i2, void *data) {
   1.102 -   int a = *((int*) i1);
   1.103 -   int b = *((int*) i2);
   1.104 -   if (a == b) {
   1.105 -       return 0;
   1.106 -   } else {
   1.107 -       return a < b ? -1 : 1;
   1.108 -   }
   1.109 -}
   1.110 -
   1.111 -int ucx_floatcmp(void *f1, void *f2, void *epsilon) {
   1.112 -   float a = *((float*) f1);
   1.113 -   float b = *((float*) f2);
   1.114 -   float e = !epsilon ? 1e-6f : *((float*)epsilon);
   1.115 -   if (fabsf(a - b) < e) {
   1.116 -       return 0;
   1.117 -   } else {
   1.118 -       return a < b ? -1 : 1;
   1.119 -   }
   1.120 -}
   1.121 -
   1.122 -int ucx_doublecmp(void *d1, void *d2, void *epsilon) {
   1.123 -   double a = *((float*) d1);
   1.124 -   double b = *((float*) d2);
   1.125 -   double e = !epsilon ? 1e-14 : *((double*)epsilon);
   1.126 -   if (fabs(a - b) < e) {
   1.127 -       return 0;
   1.128 -   } else {
   1.129 -       return a < b ? -1 : 1;
   1.130 -   }
   1.131 -}
   1.132 -
   1.133 -int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) {
   1.134 -    intptr_t p1 = (intptr_t) ptr1;
   1.135 -    intptr_t p2 = (intptr_t) ptr2;
   1.136 -    if (p1 == p2) {
   1.137 -        return 0;
   1.138 -    } else {
   1.139 -        return p1  < p2 ? -1 : 1;
   1.140 -    }
   1.141 -}
   1.142 -
   1.143 -int ucx_memcmp(void *ptr1, void *ptr2, void *n) {
   1.144 -    return memcmp(ptr1, ptr2, *((size_t*)n));
   1.145 -}
   1.146 -
   1.147 -/* PRINTF FUNCTIONS */
   1.148 -
   1.149 -#ifdef va_copy
   1.150 -#define UCX_PRINTF_BUFSIZE 256
   1.151 -#else
   1.152 -#pragma message("WARNING: C99 va_copy macro not supported by this platform" \
   1.153 -                " - limiting ucx_*printf to 2 KiB")
   1.154 -#define UCX_PRINTF_BUFSIZE 0x800
   1.155 -#endif
   1.156 -
   1.157 -int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...) {
   1.158 -    int ret;
   1.159 -    va_list ap;
   1.160 -    va_start(ap, fmt);
   1.161 -    ret = ucx_vfprintf(stream, wfc, fmt, ap);
   1.162 -    va_end(ap);
   1.163 -    return ret;
   1.164 -}
   1.165 -
   1.166 -int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap) {
   1.167 -    char buf[UCX_PRINTF_BUFSIZE];
   1.168 -#ifdef va_copy
   1.169 -    va_list ap2;
   1.170 -    va_copy(ap2, ap);
   1.171 -    int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
   1.172 -    if (ret < 0) {
   1.173 -        return ret;
   1.174 -    } else if (ret < UCX_PRINTF_BUFSIZE) {
   1.175 -        return (int)wfc(buf, 1, ret, stream);
   1.176 -    } else {
   1.177 -        if (ret == INT_MAX) {
   1.178 -            errno = ENOMEM;
   1.179 -            return -1;
   1.180 -        }
   1.181 -        
   1.182 -        int len = ret + 1;
   1.183 -        char *newbuf = (char*)malloc(len);
   1.184 -        if (!newbuf) {
   1.185 -            return -1;
   1.186 -        }
   1.187 -        
   1.188 -        ret = vsnprintf(newbuf, len, fmt, ap2);
   1.189 -        if (ret > 0) {
   1.190 -            ret = (int)wfc(newbuf, 1, ret, stream);
   1.191 -        }
   1.192 -        free(newbuf);
   1.193 -    }
   1.194 -    return ret;
   1.195 -#else
   1.196 -    int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
   1.197 -    if (ret < 0) {
   1.198 -        return ret;
   1.199 -    } else if (ret < UCX_PRINTF_BUFSIZE) {
   1.200 -        return (int)wfc(buf, 1, ret, stream);
   1.201 -    } else {
   1.202 -        errno = ENOMEM;
   1.203 -        return -1;
   1.204 -    }
   1.205 -#endif
   1.206 -}
   1.207 -
   1.208 -sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...) {
   1.209 -    va_list ap;
   1.210 -    sstr_t ret;
   1.211 -    va_start(ap, fmt);
   1.212 -    ret = ucx_vasprintf(allocator, fmt, ap);
   1.213 -    va_end(ap);
   1.214 -    return ret;
   1.215 -}
   1.216 -
   1.217 -sstr_t ucx_vasprintf(UcxAllocator *a, const char *fmt, va_list ap) {
   1.218 -    sstr_t s;
   1.219 -    s.ptr = NULL;
   1.220 -    s.length = 0;
   1.221 -    char buf[UCX_PRINTF_BUFSIZE];
   1.222 -#ifdef va_copy
   1.223 -    va_list ap2;
   1.224 -    va_copy(ap2, ap);
   1.225 -    int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
   1.226 -    if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
   1.227 -        s.ptr = (char*)almalloc(a, ret + 1);
   1.228 -        if (s.ptr) {
   1.229 -            s.length = (size_t)ret;
   1.230 -            memcpy(s.ptr, buf, ret);
   1.231 -            s.ptr[s.length] = '\0';
   1.232 -        }
   1.233 -    } else if (ret == INT_MAX) {
   1.234 -        errno = ENOMEM;
   1.235 -    } else  {
   1.236 -        int len = ret + 1;
   1.237 -        s.ptr = (char*)almalloc(a, len);
   1.238 -        if (s.ptr) {
   1.239 -            ret = vsnprintf(s.ptr, len, fmt, ap2);
   1.240 -            if (ret < 0) {
   1.241 -                free(s.ptr);
   1.242 -                s.ptr = NULL;
   1.243 -            } else {
   1.244 -                s.length = (size_t)ret;
   1.245 -            }
   1.246 -        }
   1.247 -    }
   1.248 -#else
   1.249 -    int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
   1.250 -    if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
   1.251 -        s.ptr = (char*)almalloc(a, ret + 1);
   1.252 -        if (s.ptr) {
   1.253 -            s.length = (size_t)ret;
   1.254 -            memcpy(s.ptr, buf, ret);
   1.255 -            s.ptr[s.length] = '\0';
   1.256 -        }
   1.257 -    } else {
   1.258 -        errno = ENOMEM;
   1.259 -    }
   1.260 -#endif
   1.261 -    return s;
   1.262 -}

mercurial