ucx/utils.c

changeset 0
1f419bd32da1
child 124
80609f9675f1
equal deleted inserted replaced
-1:000000000000 0:1f419bd32da1
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 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 */
28
29 #include "utils.h"
30 #include <math.h>
31 #include <stdio.h>
32 #include <limits.h>
33 #include <errno.h>
34
35 /* COPY FUCNTIONS */
36 void* ucx_strcpy(void* s, void* data) {
37 char *str = (char*) s;
38 size_t n = 1+strlen(str);
39 char *cpy = (char*) malloc(n);
40 memcpy(cpy, str, n);
41 return cpy;
42 }
43
44 void* ucx_memcpy(void* m, void* n) {
45 size_t k = *((size_t*)n);
46 void *cpy = malloc(k);
47 memcpy(cpy, m, k);
48 return cpy;
49 }
50
51 size_t ucx_stream_copy(void *src, void *dest, read_func readfnc,
52 write_func writefnc, char* buf, size_t bufsize, size_t n) {
53 if(n == 0 || bufsize == 0) {
54 return 0;
55 }
56
57 size_t ncp = 0;
58 if (!buf) {
59 buf = (char*)malloc(bufsize);
60 if(buf == NULL) {
61 return 0;
62 }
63 }
64
65 size_t r;
66 size_t rn = bufsize > n ? n : bufsize;
67 while((r = readfnc(buf, 1, rn, src)) != 0) {
68 r = writefnc(buf, 1, r, dest);
69 ncp += r;
70 n -= r;
71 rn = bufsize > n ? n : bufsize;
72 if(r == 0 || n == 0) {
73 break;
74 }
75 }
76
77 free(buf);
78 return ncp;
79 }
80
81 /* COMPARE FUNCTIONS */
82
83 int ucx_strcmp(void *s1, void *s2, void *data) {
84 return strcmp((char*)s1, (char*)s2);
85 }
86
87 int ucx_strncmp(void *s1, void *s2, void *n) {
88 return strncmp((char*)s1, (char*)s2, *((size_t*) n));
89 }
90
91 int ucx_intcmp(void *i1, void *i2, void *data) {
92 int a = *((int*) i1);
93 int b = *((int*) i2);
94 if (a == b) {
95 return 0;
96 } else {
97 return a < b ? -1 : 1;
98 }
99 }
100
101 int ucx_floatcmp(void *f1, void *f2, void *epsilon) {
102 float a = *((float*) f1);
103 float b = *((float*) f2);
104 float e = !epsilon ? 1e-6f : *((float*)epsilon);
105 if (fabsf(a - b) < e) {
106 return 0;
107 } else {
108 return a < b ? -1 : 1;
109 }
110 }
111
112 int ucx_doublecmp(void *d1, void *d2, void *epsilon) {
113 double a = *((float*) d1);
114 double b = *((float*) d2);
115 double e = !epsilon ? 1e-14 : *((double*)epsilon);
116 if (fabs(a - b) < e) {
117 return 0;
118 } else {
119 return a < b ? -1 : 1;
120 }
121 }
122
123 int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) {
124 if (ptr1 == ptr2) {
125 return 0;
126 } else {
127 return ptr1 < ptr2 ? -1 : 1;
128 }
129 }
130
131 int ucx_memcmp(void *ptr1, void *ptr2, void *n) {
132 return memcmp(ptr1, ptr2, *((size_t*)n));
133 }
134
135 /* PRINTF FUNCTIONS */
136
137 #ifdef va_copy
138 #define UCX_PRINTF_BUFSIZE 256
139 #else
140 #pragma message("WARNING: C99 va_copy macro not supported by this platform" \
141 " - limiting ucx_*printf to 2 KiB")
142 #define UCX_PRINTF_BUFSIZE 0x800
143 #endif
144
145 int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...) {
146 int ret;
147 va_list ap;
148 va_start(ap, fmt);
149 ret = ucx_vfprintf(stream, wfc, fmt, ap);
150 va_end(ap);
151 return ret;
152 }
153
154 int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap) {
155 char buf[UCX_PRINTF_BUFSIZE];
156 #ifdef va_copy
157 va_list ap2;
158 va_copy(ap2, ap);
159 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
160 if (ret < 0) {
161 return ret;
162 } else if (ret < UCX_PRINTF_BUFSIZE) {
163 return (int)wfc(buf, 1, ret, stream);
164 } else {
165 if (ret == INT_MAX) {
166 errno = ENOMEM;
167 return -1;
168 }
169
170 int len = ret + 1;
171 char *newbuf = (char*)malloc(len);
172 if (!newbuf) {
173 return -1;
174 }
175
176 ret = vsnprintf(newbuf, len, fmt, ap2);
177 if (ret > 0) {
178 ret = (int)wfc(newbuf, 1, ret, stream);
179 }
180 free(newbuf);
181 }
182 return ret;
183 #else
184 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
185 if (ret < 0) {
186 return ret;
187 } else if (ret < UCX_PRINTF_BUFSIZE) {
188 return (int)wfc(buf, 1, ret, stream);
189 } else {
190 errno = ENOMEM;
191 return -1;
192 }
193 #endif
194 }
195
196 sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...) {
197 va_list ap;
198 sstr_t ret;
199 va_start(ap, fmt);
200 ret = ucx_vasprintf(allocator, fmt, ap);
201 va_end(ap);
202 return ret;
203 }
204
205 sstr_t ucx_vasprintf(UcxAllocator *a, const char *fmt, va_list ap) {
206 sstr_t s;
207 s.ptr = NULL;
208 s.length = 0;
209 char buf[UCX_PRINTF_BUFSIZE];
210 #ifdef va_copy
211 va_list ap2;
212 va_copy(ap2, ap);
213 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
214 if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
215 s.ptr = (char*)a->malloc(a->pool, ret + 1);
216 s.length = (size_t)ret;
217 memcpy(s.ptr, buf, ret);
218 s.ptr[s.length] = '\0';
219 } else if (ret == INT_MAX) {
220 errno = ENOMEM;
221 } else {
222 int len = ret + 1;
223 s.ptr = (char*)a->malloc(a->pool, len);
224 ret = vsnprintf(s.ptr, len, fmt, ap2);
225 if (ret < 0) {
226 free(s.ptr);
227 s.ptr = NULL;
228 } else {
229 s.length = (size_t)ret;
230 }
231 }
232 #else
233 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
234 if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
235 s.ptr = (char*)a->malloc(a->pool, ret + 1);
236 s.length = (size_t)ret;
237 memcpy(s.ptr, buf, ret);
238 s.ptr[s.length] = '\0';
239 } else {
240 errno = ENOMEM;
241 }
242 #endif
243 return s;
244 }

mercurial