Sat, 05 Dec 2020 10:32:49 +0100
Motif: enable xft text rendering by default
0 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
157 | 4 | * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. |
0 | 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 | ||
157 | 29 | #include "ucx/utils.h" |
30 | ||
0 | 31 | #include <math.h> |
32 | #include <stdio.h> | |
33 | #include <limits.h> | |
34 | #include <errno.h> | |
35 | ||
36 | /* COPY FUCNTIONS */ | |
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
37 | void* ucx_strcpy(const void* s, void* data) { |
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
38 | const char *str = (const char*) s; |
0 | 39 | size_t n = 1+strlen(str); |
40 | char *cpy = (char*) malloc(n); | |
41 | memcpy(cpy, str, n); | |
42 | return cpy; | |
43 | } | |
44 | ||
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
45 | void* ucx_memcpy(const void* m, void* n) { |
0 | 46 | size_t k = *((size_t*)n); |
47 | void *cpy = malloc(k); | |
48 | memcpy(cpy, m, k); | |
49 | return cpy; | |
50 | } | |
51 | ||
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
52 | size_t ucx_stream_bncopy(void *src, void *dest, read_func readfnc, |
0 | 53 | write_func writefnc, char* buf, size_t bufsize, size_t n) { |
54 | if(n == 0 || bufsize == 0) { | |
55 | return 0; | |
56 | } | |
57 | ||
124
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
58 | char *lbuf; |
0 | 59 | size_t ncp = 0; |
124
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
60 | |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
61 | if(buf) { |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
62 | lbuf = buf; |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
63 | } else { |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
64 | lbuf = (char*)malloc(bufsize); |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
65 | if(lbuf == NULL) { |
0 | 66 | return 0; |
67 | } | |
68 | } | |
69 | ||
70 | size_t r; | |
71 | size_t rn = bufsize > n ? n : bufsize; | |
124
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
72 | while((r = readfnc(lbuf, 1, rn, src)) != 0) { |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
73 | r = writefnc(lbuf, 1, r, dest); |
0 | 74 | ncp += r; |
75 | n -= r; | |
76 | rn = bufsize > n ? n : bufsize; | |
77 | if(r == 0 || n == 0) { | |
78 | break; | |
79 | } | |
80 | } | |
81 | ||
124
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
82 | if (lbuf != buf) { |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
83 | free(lbuf); |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
84 | } |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
85 | |
0 | 86 | return ncp; |
87 | } | |
88 | ||
89 | /* COMPARE FUNCTIONS */ | |
90 | ||
157 | 91 | int ucx_cmp_str(const void *s1, const void *s2, void *data) { |
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
92 | return strcmp((const char*)s1, (const char*)s2); |
0 | 93 | } |
94 | ||
157 | 95 | int ucx_cmp_strn(const void *s1, const void *s2, void *n) { |
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
96 | return strncmp((const char*)s1, (const char*)s2, *((size_t*) n)); |
0 | 97 | } |
98 | ||
157 | 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); | |
103 | } | |
104 | ||
105 | int ucx_cmp_int(const void *i1, const void *i2, void *data) { | |
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
106 | int a = *((const int*) i1); |
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
107 | int b = *((const int*) i2); |
0 | 108 | if (a == b) { |
109 | return 0; | |
110 | } else { | |
111 | return a < b ? -1 : 1; | |
112 | } | |
113 | } | |
114 | ||
157 | 115 | int ucx_cmp_longint(const void *i1, const void *i2, void *data) { |
116 | int a = *((const long int*) i1); | |
117 | int b = *((const long int*) i2); | |
118 | if (a == b) { | |
119 | return 0; | |
120 | } else { | |
121 | return a < b ? -1 : 1; | |
122 | } | |
123 | } | |
124 | ||
125 | intmax_t ucx_dist_int(const void *i1, const void *i2, void *data) { | |
126 | intmax_t a = *((const int*) i1); | |
127 | intmax_t b = *((const int*) i2); | |
128 | return a - b; | |
129 | } | |
130 | ||
131 | intmax_t ucx_dist_longint(const void *i1, const void *i2, void *data) { | |
132 | intmax_t a = *((const long int*) i1); | |
133 | intmax_t b = *((const long int*) i2); | |
134 | return a - b; | |
135 | } | |
136 | ||
137 | int ucx_cmp_float(const void *f1, const void *f2, void *epsilon) { | |
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
138 | float a = *((const float*) f1); |
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
139 | float b = *((const float*) f2); |
0 | 140 | float e = !epsilon ? 1e-6f : *((float*)epsilon); |
141 | if (fabsf(a - b) < e) { | |
142 | return 0; | |
143 | } else { | |
144 | return a < b ? -1 : 1; | |
145 | } | |
146 | } | |
147 | ||
157 | 148 | int ucx_cmp_double(const void *d1, const void *d2, void *epsilon) { |
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
149 | double a = *((const double*) d1); |
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
150 | double b = *((const double*) d2); |
0 | 151 | double e = !epsilon ? 1e-14 : *((double*)epsilon); |
152 | if (fabs(a - b) < e) { | |
153 | return 0; | |
154 | } else { | |
155 | return a < b ? -1 : 1; | |
156 | } | |
157 | } | |
158 | ||
157 | 159 | int ucx_cmp_ptr(const void *ptr1, const void *ptr2, void *data) { |
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
160 | const intptr_t p1 = (const intptr_t) ptr1; |
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
161 | const intptr_t p2 = (const intptr_t) ptr2; |
124
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
162 | if (p1 == p2) { |
0 | 163 | return 0; |
164 | } else { | |
124
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
165 | return p1 < p2 ? -1 : 1; |
0 | 166 | } |
167 | } | |
168 | ||
157 | 169 | int ucx_cmp_mem(const void *ptr1, const void *ptr2, void *n) { |
0 | 170 | return memcmp(ptr1, ptr2, *((size_t*)n)); |
171 | } | |
172 | ||
173 | /* PRINTF FUNCTIONS */ | |
174 | ||
175 | #ifdef va_copy | |
176 | #define UCX_PRINTF_BUFSIZE 256 | |
177 | #else | |
178 | #pragma message("WARNING: C99 va_copy macro not supported by this platform" \ | |
179 | " - limiting ucx_*printf to 2 KiB") | |
180 | #define UCX_PRINTF_BUFSIZE 0x800 | |
181 | #endif | |
182 | ||
183 | int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...) { | |
184 | int ret; | |
185 | va_list ap; | |
186 | va_start(ap, fmt); | |
187 | ret = ucx_vfprintf(stream, wfc, fmt, ap); | |
188 | va_end(ap); | |
189 | return ret; | |
190 | } | |
191 | ||
192 | int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap) { | |
193 | char buf[UCX_PRINTF_BUFSIZE]; | |
194 | #ifdef va_copy | |
195 | va_list ap2; | |
196 | va_copy(ap2, ap); | |
197 | int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); | |
198 | if (ret < 0) { | |
199 | return ret; | |
200 | } else if (ret < UCX_PRINTF_BUFSIZE) { | |
201 | return (int)wfc(buf, 1, ret, stream); | |
202 | } else { | |
203 | if (ret == INT_MAX) { | |
204 | errno = ENOMEM; | |
205 | return -1; | |
206 | } | |
207 | ||
208 | int len = ret + 1; | |
209 | char *newbuf = (char*)malloc(len); | |
210 | if (!newbuf) { | |
211 | return -1; | |
212 | } | |
213 | ||
214 | ret = vsnprintf(newbuf, len, fmt, ap2); | |
215 | if (ret > 0) { | |
216 | ret = (int)wfc(newbuf, 1, ret, stream); | |
217 | } | |
218 | free(newbuf); | |
219 | } | |
220 | return ret; | |
221 | #else | |
222 | int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); | |
223 | if (ret < 0) { | |
224 | return ret; | |
225 | } else if (ret < UCX_PRINTF_BUFSIZE) { | |
226 | return (int)wfc(buf, 1, ret, stream); | |
227 | } else { | |
228 | errno = ENOMEM; | |
229 | return -1; | |
230 | } | |
231 | #endif | |
232 | } | |
233 | ||
234 | sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...) { | |
235 | va_list ap; | |
236 | sstr_t ret; | |
237 | va_start(ap, fmt); | |
238 | ret = ucx_vasprintf(allocator, fmt, ap); | |
239 | va_end(ap); | |
240 | return ret; | |
241 | } | |
242 | ||
243 | sstr_t ucx_vasprintf(UcxAllocator *a, const char *fmt, va_list ap) { | |
244 | sstr_t s; | |
245 | s.ptr = NULL; | |
246 | s.length = 0; | |
247 | char buf[UCX_PRINTF_BUFSIZE]; | |
248 | #ifdef va_copy | |
249 | va_list ap2; | |
250 | va_copy(ap2, ap); | |
251 | int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); | |
252 | if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) { | |
124
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
253 | s.ptr = (char*)almalloc(a, ret + 1); |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
254 | if (s.ptr) { |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
255 | s.length = (size_t)ret; |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
256 | memcpy(s.ptr, buf, ret); |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
257 | s.ptr[s.length] = '\0'; |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
258 | } |
0 | 259 | } else if (ret == INT_MAX) { |
260 | errno = ENOMEM; | |
261 | } else { | |
262 | int len = ret + 1; | |
124
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
263 | s.ptr = (char*)almalloc(a, len); |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
264 | if (s.ptr) { |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
265 | ret = vsnprintf(s.ptr, len, fmt, ap2); |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
266 | if (ret < 0) { |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
267 | free(s.ptr); |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
268 | s.ptr = NULL; |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
269 | } else { |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
270 | s.length = (size_t)ret; |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
271 | } |
0 | 272 | } |
273 | } | |
274 | #else | |
275 | int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); | |
276 | if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) { | |
124
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
277 | s.ptr = (char*)almalloc(a, ret + 1); |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
278 | if (s.ptr) { |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
279 | s.length = (size_t)ret; |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
280 | memcpy(s.ptr, buf, ret); |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
281 | s.ptr[s.length] = '\0'; |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
282 | } |
0 | 283 | } else { |
284 | errno = ENOMEM; | |
285 | } | |
286 | #endif | |
287 | return s; | |
288 | } |