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