Thu, 03 May 2018 09:49:38 +0200
adds ucx_longintcmp() compare function
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 int ucx_floatcmp(const void *f1, const void *f2, void *epsilon) {
120 float a = *((const float*) f1);
121 float b = *((const float*) f2);
122 float e = !epsilon ? 1e-6f : *((float*)epsilon);
123 if (fabsf(a - b) < e) {
124 return 0;
125 } else {
126 return a < b ? -1 : 1;
127 }
128 }
130 int ucx_doublecmp(const void *d1, const void *d2, void *epsilon) {
131 double a = *((const double*) d1);
132 double b = *((const double*) d2);
133 double e = !epsilon ? 1e-14 : *((double*)epsilon);
134 if (fabs(a - b) < e) {
135 return 0;
136 } else {
137 return a < b ? -1 : 1;
138 }
139 }
141 int ucx_ptrcmp(const void *ptr1, const void *ptr2, void *data) {
142 const intptr_t p1 = (const intptr_t) ptr1;
143 const intptr_t p2 = (const intptr_t) ptr2;
144 if (p1 == p2) {
145 return 0;
146 } else {
147 return p1 < p2 ? -1 : 1;
148 }
149 }
151 int ucx_memcmp(const void *ptr1, const void *ptr2, void *n) {
152 return memcmp(ptr1, ptr2, *((size_t*)n));
153 }
155 /* PRINTF FUNCTIONS */
157 #ifdef va_copy
158 #define UCX_PRINTF_BUFSIZE 256
159 #else
160 #pragma message("WARNING: C99 va_copy macro not supported by this platform" \
161 " - limiting ucx_*printf to 2 KiB")
162 #define UCX_PRINTF_BUFSIZE 0x800
163 #endif
165 int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...) {
166 int ret;
167 va_list ap;
168 va_start(ap, fmt);
169 ret = ucx_vfprintf(stream, wfc, fmt, ap);
170 va_end(ap);
171 return ret;
172 }
174 int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap) {
175 char buf[UCX_PRINTF_BUFSIZE];
176 #ifdef va_copy
177 va_list ap2;
178 va_copy(ap2, ap);
179 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
180 if (ret < 0) {
181 return ret;
182 } else if (ret < UCX_PRINTF_BUFSIZE) {
183 return (int)wfc(buf, 1, ret, stream);
184 } else {
185 if (ret == INT_MAX) {
186 errno = ENOMEM;
187 return -1;
188 }
190 int len = ret + 1;
191 char *newbuf = (char*)malloc(len);
192 if (!newbuf) {
193 return -1;
194 }
196 ret = vsnprintf(newbuf, len, fmt, ap2);
197 if (ret > 0) {
198 ret = (int)wfc(newbuf, 1, ret, stream);
199 }
200 free(newbuf);
201 }
202 return ret;
203 #else
204 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
205 if (ret < 0) {
206 return ret;
207 } else if (ret < UCX_PRINTF_BUFSIZE) {
208 return (int)wfc(buf, 1, ret, stream);
209 } else {
210 errno = ENOMEM;
211 return -1;
212 }
213 #endif
214 }
216 sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...) {
217 va_list ap;
218 sstr_t ret;
219 va_start(ap, fmt);
220 ret = ucx_vasprintf(allocator, fmt, ap);
221 va_end(ap);
222 return ret;
223 }
225 sstr_t ucx_vasprintf(UcxAllocator *a, const char *fmt, va_list ap) {
226 sstr_t s;
227 s.ptr = NULL;
228 s.length = 0;
229 char buf[UCX_PRINTF_BUFSIZE];
230 #ifdef va_copy
231 va_list ap2;
232 va_copy(ap2, ap);
233 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
234 if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
235 s.ptr = (char*)almalloc(a, ret + 1);
236 if (s.ptr) {
237 s.length = (size_t)ret;
238 memcpy(s.ptr, buf, ret);
239 s.ptr[s.length] = '\0';
240 }
241 } else if (ret == INT_MAX) {
242 errno = ENOMEM;
243 } else {
244 int len = ret + 1;
245 s.ptr = (char*)almalloc(a, len);
246 if (s.ptr) {
247 ret = vsnprintf(s.ptr, len, fmt, ap2);
248 if (ret < 0) {
249 free(s.ptr);
250 s.ptr = NULL;
251 } else {
252 s.length = (size_t)ret;
253 }
254 }
255 }
256 #else
257 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
258 if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
259 s.ptr = (char*)almalloc(a, ret + 1);
260 if (s.ptr) {
261 s.length = (size_t)ret;
262 memcpy(s.ptr, buf, ret);
263 s.ptr[s.length] = '\0';
264 }
265 } else {
266 errno = ENOMEM;
267 }
268 #endif
269 return s;
270 }