src/utils.c

Thu, 03 May 2018 10:09:49 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 03 May 2018 10:09:49 +0200
changeset 286
85f55abea563
parent 285
7be3ae7ffb58
child 292
d9abf53b8397
permissions
-rw-r--r--

adds distance functions for integers as utils

     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_strcmp(const void *s1, const void *s2, void *data) {
    92     return strcmp((const char*)s1, (const char*)s2);
    93 }
    95 int ucx_strncmp(const void *s1, const void *s2, void *n) {
    96     return strncmp((const char*)s1, (const char*)s2, *((size_t*) n));
    97 }
    99 int ucx_intcmp(const void *i1, const void *i2, void *data) {
   100    int a = *((const int*) i1);
   101    int b = *((const int*) i2);
   102    if (a == b) {
   103        return 0;
   104    } else {
   105        return a < b ? -1 : 1;
   106    }
   107 }
   109 int ucx_longintcmp(const void *i1, const void *i2, void *data) {
   110    int a = *((const long int*) i1);
   111    int b = *((const long int*) i2);
   112    if (a == b) {
   113        return 0;
   114    } else {
   115        return a < b ? -1 : 1;
   116    }
   117 }
   119 intmax_t ucx_intdist(const void *i1, const void *i2, void *data) {
   120    intmax_t a = *((const int*) i1);
   121    intmax_t b = *((const int*) i2);
   122    return a - b;
   123 }
   125 intmax_t ucx_longintdist(const void *i1, const void *i2, void *data) {
   126    intmax_t a = *((const long int*) i1);
   127    intmax_t b = *((const long int*) i2);
   128    return a - b;
   129 }
   131 int ucx_floatcmp(const void *f1, const void *f2, void *epsilon) {
   132    float a = *((const float*) f1);
   133    float b = *((const float*) f2);
   134    float e = !epsilon ? 1e-6f : *((float*)epsilon);
   135    if (fabsf(a - b) < e) {
   136        return 0;
   137    } else {
   138        return a < b ? -1 : 1;
   139    }
   140 }
   142 int ucx_doublecmp(const void *d1, const void *d2, void *epsilon) {
   143    double a = *((const double*) d1);
   144    double b = *((const double*) d2);
   145    double e = !epsilon ? 1e-14 : *((double*)epsilon);
   146    if (fabs(a - b) < e) {
   147        return 0;
   148    } else {
   149        return a < b ? -1 : 1;
   150    }
   151 }
   153 int ucx_ptrcmp(const void *ptr1, const void *ptr2, void *data) {
   154     const intptr_t p1 = (const intptr_t) ptr1;
   155     const intptr_t p2 = (const intptr_t) ptr2;
   156     if (p1 == p2) {
   157         return 0;
   158     } else {
   159         return p1  < p2 ? -1 : 1;
   160     }
   161 }
   163 int ucx_memcmp(const void *ptr1, const void *ptr2, void *n) {
   164     return memcmp(ptr1, ptr2, *((size_t*)n));
   165 }
   167 /* PRINTF FUNCTIONS */
   169 #ifdef va_copy
   170 #define UCX_PRINTF_BUFSIZE 256
   171 #else
   172 #pragma message("WARNING: C99 va_copy macro not supported by this platform" \
   173                 " - limiting ucx_*printf to 2 KiB")
   174 #define UCX_PRINTF_BUFSIZE 0x800
   175 #endif
   177 int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...) {
   178     int ret;
   179     va_list ap;
   180     va_start(ap, fmt);
   181     ret = ucx_vfprintf(stream, wfc, fmt, ap);
   182     va_end(ap);
   183     return ret;
   184 }
   186 int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap) {
   187     char buf[UCX_PRINTF_BUFSIZE];
   188 #ifdef va_copy
   189     va_list ap2;
   190     va_copy(ap2, ap);
   191     int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
   192     if (ret < 0) {
   193         return ret;
   194     } else if (ret < UCX_PRINTF_BUFSIZE) {
   195         return (int)wfc(buf, 1, ret, stream);
   196     } else {
   197         if (ret == INT_MAX) {
   198             errno = ENOMEM;
   199             return -1;
   200         }
   202         int len = ret + 1;
   203         char *newbuf = (char*)malloc(len);
   204         if (!newbuf) {
   205             return -1;
   206         }
   208         ret = vsnprintf(newbuf, len, fmt, ap2);
   209         if (ret > 0) {
   210             ret = (int)wfc(newbuf, 1, ret, stream);
   211         }
   212         free(newbuf);
   213     }
   214     return ret;
   215 #else
   216     int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
   217     if (ret < 0) {
   218         return ret;
   219     } else if (ret < UCX_PRINTF_BUFSIZE) {
   220         return (int)wfc(buf, 1, ret, stream);
   221     } else {
   222         errno = ENOMEM;
   223         return -1;
   224     }
   225 #endif
   226 }
   228 sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...) {
   229     va_list ap;
   230     sstr_t ret;
   231     va_start(ap, fmt);
   232     ret = ucx_vasprintf(allocator, fmt, ap);
   233     va_end(ap);
   234     return ret;
   235 }
   237 sstr_t ucx_vasprintf(UcxAllocator *a, const char *fmt, va_list ap) {
   238     sstr_t s;
   239     s.ptr = NULL;
   240     s.length = 0;
   241     char buf[UCX_PRINTF_BUFSIZE];
   242 #ifdef va_copy
   243     va_list ap2;
   244     va_copy(ap2, ap);
   245     int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
   246     if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
   247         s.ptr = (char*)almalloc(a, ret + 1);
   248         if (s.ptr) {
   249             s.length = (size_t)ret;
   250             memcpy(s.ptr, buf, ret);
   251             s.ptr[s.length] = '\0';
   252         }
   253     } else if (ret == INT_MAX) {
   254         errno = ENOMEM;
   255     } else  {
   256         int len = ret + 1;
   257         s.ptr = (char*)almalloc(a, len);
   258         if (s.ptr) {
   259             ret = vsnprintf(s.ptr, len, fmt, ap2);
   260             if (ret < 0) {
   261                 free(s.ptr);
   262                 s.ptr = NULL;
   263             } else {
   264                 s.length = (size_t)ret;
   265             }
   266         }
   267     }
   268 #else
   269     int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
   270     if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
   271         s.ptr = (char*)almalloc(a, ret + 1);
   272         if (s.ptr) {
   273             s.length = (size_t)ret;
   274             memcpy(s.ptr, buf, ret);
   275             s.ptr[s.length] = '\0';
   276         }
   277     } else {
   278         errno = ENOMEM;
   279     }
   280 #endif
   281     return s;
   282 }

mercurial