universe@39: /* universe@39: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@39: * universe@39: * Copyright 2015 Olaf Wintermann. All rights reserved. universe@39: * universe@39: * Redistribution and use in source and binary forms, with or without universe@39: * modification, are permitted provided that the following conditions are met: universe@39: * universe@39: * 1. Redistributions of source code must retain the above copyright universe@39: * notice, this list of conditions and the following disclaimer. universe@39: * universe@39: * 2. Redistributions in binary form must reproduce the above copyright universe@39: * notice, this list of conditions and the following disclaimer in the universe@39: * documentation and/or other materials provided with the distribution. universe@39: * universe@39: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@39: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@39: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@39: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@39: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@39: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@39: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@39: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@39: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@39: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@39: * POSSIBILITY OF SUCH DAMAGE. universe@39: */ universe@39: universe@39: #include "utils.h" universe@39: #include universe@39: #include universe@39: #include universe@39: #include universe@39: universe@39: /* COPY FUCNTIONS */ universe@39: void* ucx_strcpy(void* s, void* data) { universe@39: char *str = (char*) s; universe@39: size_t n = 1+strlen(str); universe@39: char *cpy = (char*) malloc(n); universe@39: memcpy(cpy, str, n); universe@39: return cpy; universe@39: } universe@39: universe@39: void* ucx_memcpy(void* m, void* n) { universe@39: size_t k = *((size_t*)n); universe@39: void *cpy = malloc(k); universe@39: memcpy(cpy, m, k); universe@39: return cpy; universe@39: } universe@39: universe@39: size_t ucx_stream_copy(void *src, void *dest, read_func readfnc, universe@39: write_func writefnc, char* buf, size_t bufsize, size_t n) { universe@39: if(n == 0 || bufsize == 0) { universe@39: return 0; universe@39: } universe@39: universe@39: char *lbuf; universe@39: size_t ncp = 0; universe@39: universe@39: if(buf) { universe@39: lbuf = buf; universe@39: } else { universe@39: lbuf = (char*)malloc(bufsize); universe@39: if(lbuf == NULL) { universe@39: return 0; universe@39: } universe@39: } universe@39: universe@39: size_t r; universe@39: size_t rn = bufsize > n ? n : bufsize; universe@39: while((r = readfnc(lbuf, 1, rn, src)) != 0) { universe@39: r = writefnc(lbuf, 1, r, dest); universe@39: ncp += r; universe@39: n -= r; universe@39: rn = bufsize > n ? n : bufsize; universe@39: if(r == 0 || n == 0) { universe@39: break; universe@39: } universe@39: } universe@39: universe@39: if (lbuf != buf) { universe@39: free(lbuf); universe@39: } universe@39: universe@39: return ncp; universe@39: } universe@39: universe@39: /* COMPARE FUNCTIONS */ universe@39: universe@39: int ucx_strcmp(void *s1, void *s2, void *data) { universe@39: return strcmp((char*)s1, (char*)s2); universe@39: } universe@39: universe@39: int ucx_strncmp(void *s1, void *s2, void *n) { universe@39: return strncmp((char*)s1, (char*)s2, *((size_t*) n)); universe@39: } universe@39: universe@39: int ucx_intcmp(void *i1, void *i2, void *data) { universe@39: int a = *((int*) i1); universe@39: int b = *((int*) i2); universe@39: if (a == b) { universe@39: return 0; universe@39: } else { universe@39: return a < b ? -1 : 1; universe@39: } universe@39: } universe@39: universe@39: int ucx_floatcmp(void *f1, void *f2, void *epsilon) { universe@39: float a = *((float*) f1); universe@39: float b = *((float*) f2); universe@39: float e = !epsilon ? 1e-6f : *((float*)epsilon); universe@39: if (fabsf(a - b) < e) { universe@39: return 0; universe@39: } else { universe@39: return a < b ? -1 : 1; universe@39: } universe@39: } universe@39: universe@39: int ucx_doublecmp(void *d1, void *d2, void *epsilon) { universe@39: double a = *((float*) d1); universe@39: double b = *((float*) d2); universe@39: double e = !epsilon ? 1e-14 : *((double*)epsilon); universe@39: if (fabs(a - b) < e) { universe@39: return 0; universe@39: } else { universe@39: return a < b ? -1 : 1; universe@39: } universe@39: } universe@39: universe@39: int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) { universe@39: intptr_t p1 = (intptr_t) ptr1; universe@39: intptr_t p2 = (intptr_t) ptr2; universe@39: if (p1 == p2) { universe@39: return 0; universe@39: } else { universe@39: return p1 < p2 ? -1 : 1; universe@39: } universe@39: } universe@39: universe@39: int ucx_memcmp(void *ptr1, void *ptr2, void *n) { universe@39: return memcmp(ptr1, ptr2, *((size_t*)n)); universe@39: } universe@39: universe@39: /* PRINTF FUNCTIONS */ universe@39: universe@39: #ifdef va_copy universe@39: #define UCX_PRINTF_BUFSIZE 256 universe@39: #else universe@39: #pragma message("WARNING: C99 va_copy macro not supported by this platform" \ universe@39: " - limiting ucx_*printf to 2 KiB") universe@39: #define UCX_PRINTF_BUFSIZE 0x800 universe@39: #endif universe@39: universe@39: int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...) { universe@39: int ret; universe@39: va_list ap; universe@39: va_start(ap, fmt); universe@39: ret = ucx_vfprintf(stream, wfc, fmt, ap); universe@39: va_end(ap); universe@39: return ret; universe@39: } universe@39: universe@39: int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap) { universe@39: char buf[UCX_PRINTF_BUFSIZE]; universe@39: #ifdef va_copy universe@39: va_list ap2; universe@39: va_copy(ap2, ap); universe@39: int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); universe@39: if (ret < 0) { universe@39: return ret; universe@39: } else if (ret < UCX_PRINTF_BUFSIZE) { universe@39: return (int)wfc(buf, 1, ret, stream); universe@39: } else { universe@39: if (ret == INT_MAX) { universe@39: errno = ENOMEM; universe@39: return -1; universe@39: } universe@39: universe@39: int len = ret + 1; universe@39: char *newbuf = (char*)malloc(len); universe@39: if (!newbuf) { universe@39: return -1; universe@39: } universe@39: universe@39: ret = vsnprintf(newbuf, len, fmt, ap2); universe@39: if (ret > 0) { universe@39: ret = (int)wfc(newbuf, 1, ret, stream); universe@39: } universe@39: free(newbuf); universe@39: } universe@39: return ret; universe@39: #else universe@39: int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); universe@39: if (ret < 0) { universe@39: return ret; universe@39: } else if (ret < UCX_PRINTF_BUFSIZE) { universe@39: return (int)wfc(buf, 1, ret, stream); universe@39: } else { universe@39: errno = ENOMEM; universe@39: return -1; universe@39: } universe@39: #endif universe@39: } universe@39: universe@39: sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...) { universe@39: va_list ap; universe@39: sstr_t ret; universe@39: va_start(ap, fmt); universe@39: ret = ucx_vasprintf(allocator, fmt, ap); universe@39: va_end(ap); universe@39: return ret; universe@39: } universe@39: universe@39: sstr_t ucx_vasprintf(UcxAllocator *a, const char *fmt, va_list ap) { universe@39: sstr_t s; universe@39: s.ptr = NULL; universe@39: s.length = 0; universe@39: char buf[UCX_PRINTF_BUFSIZE]; universe@39: #ifdef va_copy universe@39: va_list ap2; universe@39: va_copy(ap2, ap); universe@39: int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); universe@39: if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) { universe@39: s.ptr = (char*)almalloc(a, ret + 1); universe@39: if (s.ptr) { universe@39: s.length = (size_t)ret; universe@39: memcpy(s.ptr, buf, ret); universe@39: s.ptr[s.length] = '\0'; universe@39: } universe@39: } else if (ret == INT_MAX) { universe@39: errno = ENOMEM; universe@39: } else { universe@39: int len = ret + 1; universe@39: s.ptr = (char*)almalloc(a, len); universe@39: if (s.ptr) { universe@39: ret = vsnprintf(s.ptr, len, fmt, ap2); universe@39: if (ret < 0) { universe@39: free(s.ptr); universe@39: s.ptr = NULL; universe@39: } else { universe@39: s.length = (size_t)ret; universe@39: } universe@39: } universe@39: } universe@39: #else universe@39: int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); universe@39: if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) { universe@39: s.ptr = (char*)almalloc(a, ret + 1); universe@39: if (s.ptr) { universe@39: s.length = (size_t)ret; universe@39: memcpy(s.ptr, buf, ret); universe@39: s.ptr[s.length] = '\0'; universe@39: } universe@39: } else { universe@39: errno = ENOMEM; universe@39: } universe@39: #endif universe@39: return s; universe@39: }