src/utils.c

Mon, 14 May 2018 18:23:35 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 14 May 2018 18:23:35 +0200
changeset 313
b7753273f0fd
parent 312
e1e3b768ae8b
child 314
5d28dc8f0765
permissions
-rw-r--r--

renames ucx_doublecmp() and ucx_floatcmp() to ucx_cmp_double() and ucx_cmp_float()

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2017 Mike Becker, 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 "ucx/utils.h"
    31 #include <math.h>
    32 #include <stdio.h>
    33 #include <limits.h>
    34 #include <errno.h>
    36 /* COPY FUCNTIONS */
    37 void* ucx_strcpy(const void* s, void* data) {
    38     const char *str = (const char*) s;
    39     size_t n = 1+strlen(str);
    40     char *cpy = (char*) malloc(n);
    41     memcpy(cpy, str, n);
    42     return cpy;
    43 }
    45 void* ucx_memcpy(const void* m, void* n) {
    46     size_t k = *((size_t*)n);
    47     void *cpy = malloc(k);
    48     memcpy(cpy, m, k);
    49     return cpy;
    50 }
    52 size_t ucx_stream_bncopy(void *src, void *dest, read_func readfnc,
    53         write_func writefnc, char* buf, size_t bufsize, size_t n) {
    54     if(n == 0 || bufsize == 0) {
    55         return 0;
    56     }
    58     char *lbuf;    
    59     size_t ncp = 0;
    61     if(buf) {
    62         lbuf = buf;
    63     } else {
    64         lbuf = (char*)malloc(bufsize);
    65         if(lbuf == NULL) {
    66             return 0;
    67         }
    68     }
    70     size_t r;
    71     size_t rn = bufsize > n ? n : bufsize;
    72     while((r = readfnc(lbuf, 1, rn, src)) != 0) {
    73         r = writefnc(lbuf, 1, r, dest);
    74         ncp += r;
    75         n -= r;
    76         rn = bufsize > n ? n : bufsize;
    77         if(r == 0 || n == 0) {
    78             break;
    79         }
    80     }
    82     if (lbuf != buf) {
    83         free(lbuf);
    84     }
    86     return ncp;
    87 }
    89 /* COMPARE FUNCTIONS */
    91 int ucx_cmp_str(const void *s1, const void *s2, void *data) {
    92     return strcmp((const char*)s1, (const char*)s2);
    93 }
    95 int ucx_cmp_strn(const void *s1, const void *s2, void *n) {
    96     return strncmp((const char*)s1, (const char*)s2, *((size_t*) n));
    97 }
    99 int ucx_cmp_sstr(const void *s1, const void *s2, void *data) {
   100     sstr_t a = *(const sstr_t*) s1;
   101     sstr_t b = *(const sstr_t*) s2;
   102     return sstrcmp(a, b);
   103 }
   105 int ucx_intcmp(const void *i1, const void *i2, void *data) {
   106    int a = *((const int*) i1);
   107    int b = *((const int*) i2);
   108    if (a == b) {
   109        return 0;
   110    } else {
   111        return a < b ? -1 : 1;
   112    }
   113 }
   115 int ucx_longintcmp(const void *i1, const void *i2, void *data) {
   116    int a = *((const long int*) i1);
   117    int b = *((const long int*) i2);
   118    if (a == b) {
   119        return 0;
   120    } else {
   121        return a < b ? -1 : 1;
   122    }
   123 }
   125 intmax_t ucx_intdist(const void *i1, const void *i2, void *data) {
   126    intmax_t a = *((const int*) i1);
   127    intmax_t b = *((const int*) i2);
   128    return a - b;
   129 }
   131 intmax_t ucx_longintdist(const void *i1, const void *i2, void *data) {
   132    intmax_t a = *((const long int*) i1);
   133    intmax_t b = *((const long int*) i2);
   134    return a - b;
   135 }
   137 int ucx_cmp_float(const void *f1, const void *f2, void *epsilon) {
   138    float a = *((const float*) f1);
   139    float b = *((const float*) f2);
   140    float e = !epsilon ? 1e-6f : *((float*)epsilon);
   141    if (fabsf(a - b) < e) {
   142        return 0;
   143    } else {
   144        return a < b ? -1 : 1;
   145    }
   146 }
   148 int ucx_cmp_double(const void *d1, const void *d2, void *epsilon) {
   149    double a = *((const double*) d1);
   150    double b = *((const double*) d2);
   151    double e = !epsilon ? 1e-14 : *((double*)epsilon);
   152    if (fabs(a - b) < e) {
   153        return 0;
   154    } else {
   155        return a < b ? -1 : 1;
   156    }
   157 }
   159 int ucx_cmp_ptr(const void *ptr1, const void *ptr2, void *data) {
   160     const intptr_t p1 = (const intptr_t) ptr1;
   161     const intptr_t p2 = (const intptr_t) ptr2;
   162     if (p1 == p2) {
   163         return 0;
   164     } else {
   165         return p1  < p2 ? -1 : 1;
   166     }
   167 }
   169 int ucx_cmp_mem(const void *ptr1, const void *ptr2, void *n) {
   170     return memcmp(ptr1, ptr2, *((size_t*)n));
   171 }
   173 /* PRINTF FUNCTIONS */
   175 #ifdef va_copy
   176 #define UCX_PRINTF_BUFSIZE 256
   177 #else
   178 #pragma message("WARNING: C99 va_copy macro not supported by this platform" \
   179                 " - limiting ucx_*printf to 2 KiB")
   180 #define UCX_PRINTF_BUFSIZE 0x800
   181 #endif
   183 int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...) {
   184     int ret;
   185     va_list ap;
   186     va_start(ap, fmt);
   187     ret = ucx_vfprintf(stream, wfc, fmt, ap);
   188     va_end(ap);
   189     return ret;
   190 }
   192 int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap) {
   193     char buf[UCX_PRINTF_BUFSIZE];
   194 #ifdef va_copy
   195     va_list ap2;
   196     va_copy(ap2, ap);
   197     int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
   198     if (ret < 0) {
   199         return ret;
   200     } else if (ret < UCX_PRINTF_BUFSIZE) {
   201         return (int)wfc(buf, 1, ret, stream);
   202     } else {
   203         if (ret == INT_MAX) {
   204             errno = ENOMEM;
   205             return -1;
   206         }
   208         int len = ret + 1;
   209         char *newbuf = (char*)malloc(len);
   210         if (!newbuf) {
   211             return -1;
   212         }
   214         ret = vsnprintf(newbuf, len, fmt, ap2);
   215         if (ret > 0) {
   216             ret = (int)wfc(newbuf, 1, ret, stream);
   217         }
   218         free(newbuf);
   219     }
   220     return ret;
   221 #else
   222     int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
   223     if (ret < 0) {
   224         return ret;
   225     } else if (ret < UCX_PRINTF_BUFSIZE) {
   226         return (int)wfc(buf, 1, ret, stream);
   227     } else {
   228         errno = ENOMEM;
   229         return -1;
   230     }
   231 #endif
   232 }
   234 sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...) {
   235     va_list ap;
   236     sstr_t ret;
   237     va_start(ap, fmt);
   238     ret = ucx_vasprintf(allocator, fmt, ap);
   239     va_end(ap);
   240     return ret;
   241 }
   243 sstr_t ucx_vasprintf(UcxAllocator *a, const char *fmt, va_list ap) {
   244     sstr_t s;
   245     s.ptr = NULL;
   246     s.length = 0;
   247     char buf[UCX_PRINTF_BUFSIZE];
   248 #ifdef va_copy
   249     va_list ap2;
   250     va_copy(ap2, ap);
   251     int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
   252     if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
   253         s.ptr = (char*)almalloc(a, ret + 1);
   254         if (s.ptr) {
   255             s.length = (size_t)ret;
   256             memcpy(s.ptr, buf, ret);
   257             s.ptr[s.length] = '\0';
   258         }
   259     } else if (ret == INT_MAX) {
   260         errno = ENOMEM;
   261     } else  {
   262         int len = ret + 1;
   263         s.ptr = (char*)almalloc(a, len);
   264         if (s.ptr) {
   265             ret = vsnprintf(s.ptr, len, fmt, ap2);
   266             if (ret < 0) {
   267                 free(s.ptr);
   268                 s.ptr = NULL;
   269             } else {
   270                 s.length = (size_t)ret;
   271             }
   272         }
   273     }
   274 #else
   275     int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
   276     if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
   277         s.ptr = (char*)almalloc(a, ret + 1);
   278         if (s.ptr) {
   279             s.length = (size_t)ret;
   280             memcpy(s.ptr, buf, ret);
   281             s.ptr[s.length] = '\0';
   282         }
   283     } else {
   284         errno = ENOMEM;
   285     }
   286 #endif
   287     return s;
   288 }

mercurial