ucx/utils.c

Fri, 11 Dec 2020 18:32:54 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 11 Dec 2020 18:32:54 +0100
changeset 4
d8e8f34e65ee
parent 0
804d8803eade
permissions
-rw-r--r--

add sqlite dependency

0
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include "ucx/utils.h"
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <math.h>
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include <stdio.h>
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include <limits.h>
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include <errno.h>
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 /* COPY FUCNTIONS */
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 void* ucx_strcpy(const void* s, void* data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 const char *str = (const char*) s;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 size_t n = 1+strlen(str);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 char *cpy = (char*) malloc(n);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 memcpy(cpy, str, n);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 return cpy;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 void* ucx_memcpy(const void* m, void* n) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 size_t k = *((size_t*)n);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 void *cpy = malloc(k);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 memcpy(cpy, m, k);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 return cpy;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 size_t ucx_stream_bncopy(void *src, void *dest, read_func readfnc,
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 write_func writefnc, char* buf, size_t bufsize, size_t n) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 if(n == 0 || bufsize == 0) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 return 0;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 char *lbuf;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 size_t ncp = 0;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 if(buf) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 lbuf = buf;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 } else {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 lbuf = (char*)malloc(bufsize);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 if(lbuf == NULL) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 return 0;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 size_t r;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 size_t rn = bufsize > n ? n : bufsize;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 while((r = readfnc(lbuf, 1, rn, src)) != 0) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 r = writefnc(lbuf, 1, r, dest);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 ncp += r;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 n -= r;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 rn = bufsize > n ? n : bufsize;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 if(r == 0 || n == 0) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 break;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 if (lbuf != buf) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 free(lbuf);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 return ncp;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 /* COMPARE FUNCTIONS */
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 int ucx_cmp_str(const void *s1, const void *s2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 return strcmp((const char*)s1, (const char*)s2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 int ucx_cmp_strn(const void *s1, const void *s2, void *n) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 return strncmp((const char*)s1, (const char*)s2, *((size_t*) n));
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 int ucx_cmp_sstr(const void *s1, const void *s2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 sstr_t a = *(const sstr_t*) s1;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 sstr_t b = *(const sstr_t*) s2;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 return sstrcmp(a, b);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 int ucx_cmp_int(const void *i1, const void *i2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 int a = *((const int*) i1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 int b = *((const int*) i2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 if (a == b) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 return 0;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 } else {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 return a < b ? -1 : 1;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 int ucx_cmp_longint(const void *i1, const void *i2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 long int a = *((const long int*) i1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 long int b = *((const long int*) i2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 if (a == b) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 return 0;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 } else {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 return a < b ? -1 : 1;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 int ucx_cmp_longlong(const void *i1, const void *i2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 long long a = *((const long long*) i1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 long long b = *((const long long*) i2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 if (a == b) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 return 0;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 } else {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 return a < b ? -1 : 1;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 int ucx_cmp_int16(const void *i1, const void *i2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 int16_t a = *((const int16_t*) i1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 int16_t b = *((const int16_t*) i2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 if (a == b) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 return 0;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 } else {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 return a < b ? -1 : 1;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 int ucx_cmp_int32(const void *i1, const void *i2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 int32_t a = *((const int32_t*) i1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 int32_t b = *((const int32_t*) i2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 if (a == b) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 return 0;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 } else {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 return a < b ? -1 : 1;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 int ucx_cmp_int64(const void *i1, const void *i2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 int64_t a = *((const int64_t*) i1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 int64_t b = *((const int64_t*) i2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 if (a == b) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 return 0;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 } else {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 return a < b ? -1 : 1;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 int ucx_cmp_uint(const void *i1, const void *i2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 unsigned int a = *((const unsigned int*) i1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 unsigned int b = *((const unsigned int*) i2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 if (a == b) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 return 0;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 } else {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 return a < b ? -1 : 1;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 int ucx_cmp_ulongint(const void *i1, const void *i2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 unsigned long int a = *((const unsigned long int*) i1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 unsigned long int b = *((const unsigned long int*) i2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 if (a == b) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 return 0;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 } else {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 return a < b ? -1 : 1;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 int ucx_cmp_ulonglong(const void *i1, const void *i2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 unsigned long long a = *((const unsigned long long*) i1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 unsigned long long b = *((const unsigned long long*) i2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 if (a == b) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 return 0;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190 } else {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 return a < b ? -1 : 1;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 int ucx_cmp_uint16(const void *i1, const void *i2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 uint16_t a = *((const uint16_t*) i1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 uint16_t b = *((const uint16_t*) i2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 if (a == b) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 return 0;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 } else {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 return a < b ? -1 : 1;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 int ucx_cmp_uint32(const void *i1, const void *i2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 uint32_t a = *((const uint32_t*) i1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 uint32_t b = *((const uint32_t*) i2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 if (a == b) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 return 0;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 } else {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 return a < b ? -1 : 1;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 int ucx_cmp_uint64(const void *i1, const void *i2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 uint64_t a = *((const uint64_t*) i1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 uint64_t b = *((const uint64_t*) i2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 if (a == b) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 return 0;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 } else {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 return a < b ? -1 : 1;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 intmax_t ucx_dist_int(const void *i1, const void *i2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 intmax_t a = *((const int*) i1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227 intmax_t b = *((const int*) i2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 return a - b;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 intmax_t ucx_dist_longint(const void *i1, const void *i2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 intmax_t a = *((const long int*) i1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 intmax_t b = *((const long int*) i2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 return a - b;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 intmax_t ucx_dist_longlong(const void *i1, const void *i2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238 intmax_t a = *((const long long*) i1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 intmax_t b = *((const long long*) i2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
240 return a - b;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
241 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243 intmax_t ucx_dist_int16(const void *i1, const void *i2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
244 intmax_t a = *((const int16_t*) i1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245 intmax_t b = *((const int16_t*) i2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
246 return a - b;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
247 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
248
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
249 intmax_t ucx_dist_int32(const void *i1, const void *i2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
250 intmax_t a = *((const int32_t*) i1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
251 intmax_t b = *((const int32_t*) i2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
252 return a - b;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
253 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
254
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
255 intmax_t ucx_dist_int64(const void *i1, const void *i2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 intmax_t a = *((const int64_t*) i1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
257 intmax_t b = *((const int64_t*) i2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
258 return a - b;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
259 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
260
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
261 intmax_t ucx_dist_uint(const void *i1, const void *i2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
262 uintmax_t a = *((const unsigned int*) i1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
263 uintmax_t b = *((const unsigned int*) i2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
264 return a > b ? (intmax_t)(a - b) : -(intmax_t)(b - a);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
265 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
266
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
267 intmax_t ucx_dist_ulongint(const void *i1, const void *i2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
268 uintmax_t a = *((const unsigned long int*) i1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
269 uintmax_t b = *((const unsigned long int*) i2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
270 return a > b ? (intmax_t)(a - b) : -(intmax_t)(b - a);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
271 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
272
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
273 intmax_t ucx_dist_ulonglong(const void *i1, const void *i2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
274 uintmax_t a = *((const unsigned long long*) i1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
275 uintmax_t b = *((const unsigned long long*) i2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
276 return a > b ? (intmax_t)(a - b) : -(intmax_t)(b - a);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
277 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
278
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
279 intmax_t ucx_dist_uint16(const void *i1, const void *i2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
280 uintmax_t a = *((const uint16_t*) i1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
281 uintmax_t b = *((const uint16_t*) i2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
282 return a > b ? (intmax_t)(a - b) : -(intmax_t)(b - a);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
283 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
284
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
285 intmax_t ucx_dist_uint32(const void *i1, const void *i2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
286 uintmax_t a = *((const uint32_t*) i1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
287 uintmax_t b = *((const uint32_t*) i2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
288 return a > b ? (intmax_t)(a - b) : -(intmax_t)(b - a);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
289 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
290
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
291 intmax_t ucx_dist_uint64(const void *i1, const void *i2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
292 uintmax_t a = *((const uint64_t*) i1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
293 uintmax_t b = *((const uint64_t*) i2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
294 return a > b ? (intmax_t)(a - b) : -(intmax_t)(b - a);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
295 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
296
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297 int ucx_cmp_float(const void *f1, const void *f2, void *epsilon) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
298 float a = *((const float*) f1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
299 float b = *((const float*) f2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
300 float e = !epsilon ? 1e-6f : *((float*)epsilon);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
301 if (fabsf(a - b) < e) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
302 return 0;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
303 } else {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
304 return a < b ? -1 : 1;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
305 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
306 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
307
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
308 int ucx_cmp_double(const void *d1, const void *d2, void *epsilon) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
309 double a = *((const double*) d1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
310 double b = *((const double*) d2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
311 double e = !epsilon ? 1e-14 : *((double*)epsilon);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
312 if (fabs(a - b) < e) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
313 return 0;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
314 } else {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
315 return a < b ? -1 : 1;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
316 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
317 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
318
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
319 int ucx_cmp_ptr(const void *ptr1, const void *ptr2, void *data) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
320 const intptr_t p1 = (const intptr_t) ptr1;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
321 const intptr_t p2 = (const intptr_t) ptr2;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
322 if (p1 == p2) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
323 return 0;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
324 } else {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
325 return p1 < p2 ? -1 : 1;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
326 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
327 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
328
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
329 int ucx_cmp_mem(const void *ptr1, const void *ptr2, void *n) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
330 return memcmp(ptr1, ptr2, *((size_t*)n));
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
331 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
332
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
333 /* PRINTF FUNCTIONS */
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
334
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
335 #ifdef va_copy
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
336 #define UCX_PRINTF_BUFSIZE 256
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
337 #else
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
338 #pragma message("WARNING: C99 va_copy macro not supported by this platform" \
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
339 " - limiting ucx_*printf to 2 KiB")
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
340 #define UCX_PRINTF_BUFSIZE 0x800
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
341 #endif
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
342
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
343 int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
344 int ret;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
345 va_list ap;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
346 va_start(ap, fmt);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
347 ret = ucx_vfprintf(stream, wfc, fmt, ap);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
348 va_end(ap);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
349 return ret;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
350 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
351
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
352 int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
353 char buf[UCX_PRINTF_BUFSIZE];
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
354 #ifdef va_copy
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
355 va_list ap2;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
356 va_copy(ap2, ap);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
357 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
358 if (ret < 0) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
359 return ret;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
360 } else if (ret < UCX_PRINTF_BUFSIZE) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
361 return (int)wfc(buf, 1, ret, stream);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
362 } else {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
363 if (ret == INT_MAX) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
364 errno = ENOMEM;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
365 return -1;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
366 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
367
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
368 int len = ret + 1;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
369 char *newbuf = (char*)malloc(len);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
370 if (!newbuf) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
371 return -1;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
372 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
373
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
374 ret = vsnprintf(newbuf, len, fmt, ap2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
375 if (ret > 0) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
376 ret = (int)wfc(newbuf, 1, ret, stream);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
377 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
378 free(newbuf);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
379 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
380 return ret;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
381 #else
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
382 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
383 if (ret < 0) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
384 return ret;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
385 } else if (ret < UCX_PRINTF_BUFSIZE) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
386 return (int)wfc(buf, 1, ret, stream);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
387 } else {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
388 errno = ENOMEM;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
389 return -1;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
390 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
391 #endif
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
392 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
393
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
394 sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
395 va_list ap;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
396 sstr_t ret;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
397 va_start(ap, fmt);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
398 ret = ucx_vasprintf(allocator, fmt, ap);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
399 va_end(ap);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
400 return ret;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
401 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
402
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
403 sstr_t ucx_vasprintf(UcxAllocator *a, const char *fmt, va_list ap) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
404 sstr_t s;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
405 s.ptr = NULL;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
406 s.length = 0;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
407 char buf[UCX_PRINTF_BUFSIZE];
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
408 #ifdef va_copy
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
409 va_list ap2;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
410 va_copy(ap2, ap);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
411 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
412 if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
413 s.ptr = (char*)almalloc(a, ret + 1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
414 if (s.ptr) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
415 s.length = (size_t)ret;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
416 memcpy(s.ptr, buf, ret);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
417 s.ptr[s.length] = '\0';
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
418 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
419 } else if (ret == INT_MAX) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
420 errno = ENOMEM;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
421 } else {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
422 int len = ret + 1;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
423 s.ptr = (char*)almalloc(a, len);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
424 if (s.ptr) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
425 ret = vsnprintf(s.ptr, len, fmt, ap2);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
426 if (ret < 0) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
427 free(s.ptr);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
428 s.ptr = NULL;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
429 } else {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
430 s.length = (size_t)ret;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
431 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
432 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
433 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
434 #else
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
435 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
436 if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
437 s.ptr = (char*)almalloc(a, ret + 1);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
438 if (s.ptr) {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
439 s.length = (size_t)ret;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
440 memcpy(s.ptr, buf, ret);
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
441 s.ptr[s.length] = '\0';
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
442 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
443 } else {
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
444 errno = ENOMEM;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
445 }
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
446 #endif
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
447 return s;
804d8803eade add existing toolkit source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
448 }

mercurial