2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
29 #include "ucx/utils.h"
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);
45 void* ucx_memcpy(const void* m, void* n) {
46 size_t k = *((size_t*)n);
47 void *cpy = malloc(k);
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) {
64 lbuf = (char*)malloc(bufsize);
71 size_t rn = bufsize > n ? n : bufsize;
72 while((r = readfnc(lbuf, 1, rn, src)) != 0) {
73 r = writefnc(lbuf, 1, r, dest);
76 rn = bufsize > n ? n : bufsize;
77 if(r == 0 || n == 0) {
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);
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));
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);
105 int ucx_cmp_int(const void *i1, const void *i2, void *data) {
106 int a = *((const int*) i1);
107 int b = *((const int*) i2);
111 return a < b ? -1 : 1;
115 int ucx_cmp_longint(const void *i1, const void *i2, void *data) {
116 long int a = *((const long int*) i1);
117 long int b = *((const long int*) i2);
121 return a < b ? -1 : 1;
125 int ucx_cmp_longlong(const void *i1, const void *i2, void *data) {
126 long long a = *((const long long*) i1);
127 long long b = *((const long long*) i2);
131 return a < b ? -1 : 1;
135 int ucx_cmp_int16(const void *i1, const void *i2, void *data) {
136 int16_t a = *((const int16_t*) i1);
137 int16_t b = *((const int16_t*) i2);
141 return a < b ? -1 : 1;
145 int ucx_cmp_int32(const void *i1, const void *i2, void *data) {
146 int32_t a = *((const int32_t*) i1);
147 int32_t b = *((const int32_t*) i2);
151 return a < b ? -1 : 1;
155 int ucx_cmp_int64(const void *i1, const void *i2, void *data) {
156 int64_t a = *((const int64_t*) i1);
157 int64_t b = *((const int64_t*) i2);
161 return a < b ? -1 : 1;
165 int ucx_cmp_uint(const void *i1, const void *i2, void *data) {
166 unsigned int a = *((const unsigned int*) i1);
167 unsigned int b = *((const unsigned int*) i2);
171 return a < b ? -1 : 1;
175 int ucx_cmp_ulongint(const void *i1, const void *i2, void *data) {
176 unsigned long int a = *((const unsigned long int*) i1);
177 unsigned long int b = *((const unsigned long int*) i2);
181 return a < b ? -1 : 1;
185 int ucx_cmp_ulonglong(const void *i1, const void *i2, void *data) {
186 unsigned long long a = *((const unsigned long long*) i1);
187 unsigned long long b = *((const unsigned long long*) i2);
191 return a < b ? -1 : 1;
195 int ucx_cmp_uint16(const void *i1, const void *i2, void *data) {
196 uint16_t a = *((const uint16_t*) i1);
197 uint16_t b = *((const uint16_t*) i2);
201 return a < b ? -1 : 1;
205 int ucx_cmp_uint32(const void *i1, const void *i2, void *data) {
206 uint32_t a = *((const uint32_t*) i1);
207 uint32_t b = *((const uint32_t*) i2);
211 return a < b ? -1 : 1;
215 int ucx_cmp_uint64(const void *i1, const void *i2, void *data) {
216 uint64_t a = *((const uint64_t*) i1);
217 uint64_t b = *((const uint64_t*) i2);
221 return a < b ? -1 : 1;
225 intmax_t ucx_dist_int(const void *i1, const void *i2, void *data) {
226 intmax_t a = *((const int*) i1);
227 intmax_t b = *((const int*) i2);
231 intmax_t ucx_dist_longint(const void *i1, const void *i2, void *data) {
232 intmax_t a = *((const long int*) i1);
233 intmax_t b = *((const long int*) i2);
237 intmax_t ucx_dist_longlong(const void *i1, const void *i2, void *data) {
238 intmax_t a = *((const long long*) i1);
239 intmax_t b = *((const long long*) i2);
243 intmax_t ucx_dist_int16(const void *i1, const void *i2, void *data) {
244 intmax_t a = *((const int16_t*) i1);
245 intmax_t b = *((const int16_t*) i2);
249 intmax_t ucx_dist_int32(const void *i1, const void *i2, void *data) {
250 intmax_t a = *((const int32_t*) i1);
251 intmax_t b = *((const int32_t*) i2);
255 intmax_t ucx_dist_int64(const void *i1, const void *i2, void *data) {
256 intmax_t a = *((const int64_t*) i1);
257 intmax_t b = *((const int64_t*) i2);
261 intmax_t ucx_dist_uint(const void *i1, const void *i2, void *data) {
262 uintmax_t a = *((const unsigned int*) i1);
263 uintmax_t b = *((const unsigned int*) i2);
264 return a > b ? (intmax_t)(a - b) : -(intmax_t)(b - a);
267 intmax_t ucx_dist_ulongint(const void *i1, const void *i2, void *data) {
268 uintmax_t a = *((const unsigned long int*) i1);
269 uintmax_t b = *((const unsigned long int*) i2);
270 return a > b ? (intmax_t)(a - b) : -(intmax_t)(b - a);
273 intmax_t ucx_dist_ulonglong(const void *i1, const void *i2, void *data) {
274 uintmax_t a = *((const unsigned long long*) i1);
275 uintmax_t b = *((const unsigned long long*) i2);
276 return a > b ? (intmax_t)(a - b) : -(intmax_t)(b - a);
279 intmax_t ucx_dist_uint16(const void *i1, const void *i2, void *data) {
280 uintmax_t a = *((const uint16_t*) i1);
281 uintmax_t b = *((const uint16_t*) i2);
282 return a > b ? (intmax_t)(a - b) : -(intmax_t)(b - a);
285 intmax_t ucx_dist_uint32(const void *i1, const void *i2, void *data) {
286 uintmax_t a = *((const uint32_t*) i1);
287 uintmax_t b = *((const uint32_t*) i2);
288 return a > b ? (intmax_t)(a - b) : -(intmax_t)(b - a);
291 intmax_t ucx_dist_uint64(const void *i1, const void *i2, void *data) {
292 uintmax_t a = *((const uint64_t*) i1);
293 uintmax_t b = *((const uint64_t*) i2);
294 return a > b ? (intmax_t)(a - b) : -(intmax_t)(b - a);
297 int ucx_cmp_float(const void *f1, const void *f2, void *epsilon) {
298 float a = *((const float*) f1);
299 float b = *((const float*) f2);
300 float e = !epsilon ? 1e-6f : *((float*)epsilon);
301 if (fabsf(a - b) < e) {
304 return a < b ? -1 : 1;
308 int ucx_cmp_double(const void *d1, const void *d2, void *epsilon) {
309 double a = *((const double*) d1);
310 double b = *((const double*) d2);
311 double e = !epsilon ? 1e-14 : *((double*)epsilon);
312 if (fabs(a - b) < e) {
315 return a < b ? -1 : 1;
319 int ucx_cmp_ptr(const void *ptr1, const void *ptr2, void *data) {
320 const intptr_t p1 = (const intptr_t) ptr1;
321 const intptr_t p2 = (const intptr_t) ptr2;
325 return p1 < p2 ? -1 : 1;
329 int ucx_cmp_mem(const void *ptr1, const void *ptr2, void *n) {
330 return memcmp(ptr1, ptr2, *((size_t*)n));
333 /* PRINTF FUNCTIONS */
336 #define UCX_PRINTF_BUFSIZE 256
338 #pragma message("WARNING: C99 va_copy macro not supported by this platform" \
339 " - limiting ucx_*printf to 2 KiB")
340 #define UCX_PRINTF_BUFSIZE 0x800
343 int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...) {
347 ret = ucx_vfprintf(stream, wfc, fmt, ap);
352 int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap) {
353 char buf[UCX_PRINTF_BUFSIZE];
357 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
360 } else if (ret < UCX_PRINTF_BUFSIZE) {
361 return (int)wfc(buf, 1, ret, stream);
363 if (ret == INT_MAX) {
369 char *newbuf = (char*)malloc(len);
374 ret = vsnprintf(newbuf, len, fmt, ap2);
376 ret = (int)wfc(newbuf, 1, ret, stream);
382 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
385 } else if (ret < UCX_PRINTF_BUFSIZE) {
386 return (int)wfc(buf, 1, ret, stream);
394 sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...) {
398 ret = ucx_vasprintf(allocator, fmt, ap);
403 sstr_t ucx_vasprintf(UcxAllocator *a, const char *fmt, va_list ap) {
407 char buf[UCX_PRINTF_BUFSIZE];
411 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
412 if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
413 s.ptr = (char*)almalloc(a, ret + 1);
415 s.length = (size_t)ret;
416 memcpy(s.ptr, buf, ret);
417 s.ptr[s.length] = '\0';
419 } else if (ret == INT_MAX) {
423 s.ptr = (char*)almalloc(a, len);
425 ret = vsnprintf(s.ptr, len, fmt, ap2);
430 s.length = (size_t)ret;
435 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
436 if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
437 s.ptr = (char*)almalloc(a, ret + 1);
439 s.length = (size_t)ret;
440 memcpy(s.ptr, buf, ret);
441 s.ptr[s.length] = '\0';