Sat, 21 Dec 2013 12:31:31 +0100
Added allocator to mempool struct + fixed suncc.mk
103
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
1 | /* |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
3 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
4 | * Copyright 2013 Olaf Wintermann. All rights reserved. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
5 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
8 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
11 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
15 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
27 | */ |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
94
diff
changeset
|
28 | |
94
57ea041df22f
renamed comparator to utils module and added copy functions
Mike Becker <universe@uap-core.de>
parents:
92
diff
changeset
|
29 | #include "utils.h" |
140
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
30 | #include <math.h> |
142
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
31 | #include <stdio.h> |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
32 | #include <limits.h> |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
33 | #include <errno.h> |
89 | 34 | |
94
57ea041df22f
renamed comparator to utils module and added copy functions
Mike Becker <universe@uap-core.de>
parents:
92
diff
changeset
|
35 | /* COPY FUCNTIONS */ |
57ea041df22f
renamed comparator to utils module and added copy functions
Mike Becker <universe@uap-core.de>
parents:
92
diff
changeset
|
36 | void* ucx_strcpy(void* s, void* data) { |
57ea041df22f
renamed comparator to utils module and added copy functions
Mike Becker <universe@uap-core.de>
parents:
92
diff
changeset
|
37 | char *str = (char*) s; |
57ea041df22f
renamed comparator to utils module and added copy functions
Mike Becker <universe@uap-core.de>
parents:
92
diff
changeset
|
38 | size_t n = 1+strlen(str); |
57ea041df22f
renamed comparator to utils module and added copy functions
Mike Becker <universe@uap-core.de>
parents:
92
diff
changeset
|
39 | char *cpy = (char*) malloc(n); |
57ea041df22f
renamed comparator to utils module and added copy functions
Mike Becker <universe@uap-core.de>
parents:
92
diff
changeset
|
40 | memcpy(cpy, str, n); |
57ea041df22f
renamed comparator to utils module and added copy functions
Mike Becker <universe@uap-core.de>
parents:
92
diff
changeset
|
41 | return cpy; |
57ea041df22f
renamed comparator to utils module and added copy functions
Mike Becker <universe@uap-core.de>
parents:
92
diff
changeset
|
42 | } |
57ea041df22f
renamed comparator to utils module and added copy functions
Mike Becker <universe@uap-core.de>
parents:
92
diff
changeset
|
43 | |
57ea041df22f
renamed comparator to utils module and added copy functions
Mike Becker <universe@uap-core.de>
parents:
92
diff
changeset
|
44 | void* ucx_memcpy(void* m, void* n) { |
57ea041df22f
renamed comparator to utils module and added copy functions
Mike Becker <universe@uap-core.de>
parents:
92
diff
changeset
|
45 | size_t k = *((size_t*)n); |
57ea041df22f
renamed comparator to utils module and added copy functions
Mike Becker <universe@uap-core.de>
parents:
92
diff
changeset
|
46 | void *cpy = malloc(k); |
57ea041df22f
renamed comparator to utils module and added copy functions
Mike Becker <universe@uap-core.de>
parents:
92
diff
changeset
|
47 | memcpy(cpy, m, k); |
57ea041df22f
renamed comparator to utils module and added copy functions
Mike Becker <universe@uap-core.de>
parents:
92
diff
changeset
|
48 | return cpy; |
57ea041df22f
renamed comparator to utils module and added copy functions
Mike Becker <universe@uap-core.de>
parents:
92
diff
changeset
|
49 | } |
57ea041df22f
renamed comparator to utils module and added copy functions
Mike Becker <universe@uap-core.de>
parents:
92
diff
changeset
|
50 | |
140
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
51 | size_t ucx_stream_copy(void *src, void *dest, read_func readfnc, |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
52 | write_func writefnc, char* buf, size_t bufsize, size_t n) { |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
53 | if(n == 0 || bufsize == 0) { |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
54 | return 0; |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
55 | } |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
56 | |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
57 | size_t ncp = 0; |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
58 | if (!buf) { |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
59 | buf = (char*)malloc(bufsize); |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
60 | if(buf == NULL) { |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
61 | return 0; |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
62 | } |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
63 | } |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
64 | |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
65 | size_t r; |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
66 | size_t rn = bufsize > n ? n : bufsize; |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
67 | while((r = readfnc(buf, 1, rn, src)) != 0) { |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
68 | r = writefnc(buf, 1, r, dest); |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
69 | ncp += r; |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
70 | n -= r; |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
71 | rn = bufsize > n ? n : bufsize; |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
72 | if(r == 0 || n == 0) { |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
73 | break; |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
74 | } |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
75 | } |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
76 | |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
77 | free(buf); |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
78 | return ncp; |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
79 | } |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
80 | |
142
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
81 | /* COMPARE FUNCTIONS */ |
94
57ea041df22f
renamed comparator to utils module and added copy functions
Mike Becker <universe@uap-core.de>
parents:
92
diff
changeset
|
82 | |
89 | 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 | ||
92
7625a8efcc97
added floating point comparators
Mike Becker <universe@uap-core.de>
parents:
91
diff
changeset
|
101 | int ucx_floatcmp(void *f1, void *f2, void *epsilon) { |
7625a8efcc97
added floating point comparators
Mike Becker <universe@uap-core.de>
parents:
91
diff
changeset
|
102 | float a = *((float*) f1); |
7625a8efcc97
added floating point comparators
Mike Becker <universe@uap-core.de>
parents:
91
diff
changeset
|
103 | float b = *((float*) f2); |
7625a8efcc97
added floating point comparators
Mike Becker <universe@uap-core.de>
parents:
91
diff
changeset
|
104 | float e = !epsilon ? 1e-6f : *((float*)epsilon); |
7625a8efcc97
added floating point comparators
Mike Becker <universe@uap-core.de>
parents:
91
diff
changeset
|
105 | if (fabsf(a - b) < e) { |
7625a8efcc97
added floating point comparators
Mike Becker <universe@uap-core.de>
parents:
91
diff
changeset
|
106 | return 0; |
7625a8efcc97
added floating point comparators
Mike Becker <universe@uap-core.de>
parents:
91
diff
changeset
|
107 | } else { |
7625a8efcc97
added floating point comparators
Mike Becker <universe@uap-core.de>
parents:
91
diff
changeset
|
108 | return a < b ? -1 : 1; |
7625a8efcc97
added floating point comparators
Mike Becker <universe@uap-core.de>
parents:
91
diff
changeset
|
109 | } |
7625a8efcc97
added floating point comparators
Mike Becker <universe@uap-core.de>
parents:
91
diff
changeset
|
110 | } |
7625a8efcc97
added floating point comparators
Mike Becker <universe@uap-core.de>
parents:
91
diff
changeset
|
111 | |
7625a8efcc97
added floating point comparators
Mike Becker <universe@uap-core.de>
parents:
91
diff
changeset
|
112 | int ucx_doublecmp(void *d1, void *d2, void *epsilon) { |
7625a8efcc97
added floating point comparators
Mike Becker <universe@uap-core.de>
parents:
91
diff
changeset
|
113 | double a = *((float*) d1); |
7625a8efcc97
added floating point comparators
Mike Becker <universe@uap-core.de>
parents:
91
diff
changeset
|
114 | double b = *((float*) d2); |
7625a8efcc97
added floating point comparators
Mike Becker <universe@uap-core.de>
parents:
91
diff
changeset
|
115 | double e = !epsilon ? 1e-14 : *((double*)epsilon); |
7625a8efcc97
added floating point comparators
Mike Becker <universe@uap-core.de>
parents:
91
diff
changeset
|
116 | if (fabs(a - b) < e) { |
7625a8efcc97
added floating point comparators
Mike Becker <universe@uap-core.de>
parents:
91
diff
changeset
|
117 | return 0; |
7625a8efcc97
added floating point comparators
Mike Becker <universe@uap-core.de>
parents:
91
diff
changeset
|
118 | } else { |
7625a8efcc97
added floating point comparators
Mike Becker <universe@uap-core.de>
parents:
91
diff
changeset
|
119 | return a < b ? -1 : 1; |
7625a8efcc97
added floating point comparators
Mike Becker <universe@uap-core.de>
parents:
91
diff
changeset
|
120 | } |
7625a8efcc97
added floating point comparators
Mike Becker <universe@uap-core.de>
parents:
91
diff
changeset
|
121 | } |
7625a8efcc97
added floating point comparators
Mike Becker <universe@uap-core.de>
parents:
91
diff
changeset
|
122 | |
89 | 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 | } | |
91
91595a45fad6
added memcmp to the comparator module
Mike Becker <universe@uap-core.de>
parents:
89
diff
changeset
|
130 | |
91595a45fad6
added memcmp to the comparator module
Mike Becker <universe@uap-core.de>
parents:
89
diff
changeset
|
131 | int ucx_memcmp(void *ptr1, void *ptr2, void *n) { |
91595a45fad6
added memcmp to the comparator module
Mike Becker <universe@uap-core.de>
parents:
89
diff
changeset
|
132 | return memcmp(ptr1, ptr2, *((size_t*)n)); |
91595a45fad6
added memcmp to the comparator module
Mike Becker <universe@uap-core.de>
parents:
89
diff
changeset
|
133 | } |
142
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
134 | |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
135 | /* PRINTF FUNCTIONS */ |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
136 | |
150
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
137 | #ifdef va_copy |
142
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
138 | #define UCX_PRINTF_BUFSIZE 256 |
150
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
139 | #else |
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
140 | #pragma message("WARNING: C99 va_copy macro not supported by this platform" \ |
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
141 | " - limiting ucx_*printf to 2 KiB") |
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
142 | #define UCX_PRINTF_BUFSIZE 0x800 |
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
143 | #endif |
142
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
144 | |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
145 | int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...) { |
150
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
146 | int ret; |
142
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
147 | va_list ap; |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
148 | va_start(ap, fmt); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
149 | ret = ucx_vfprintf(stream, wfc, fmt, ap); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
150 | va_end(ap); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
151 | return ret; |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
152 | } |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
153 | |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
154 | int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap) { |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
155 | char buf[UCX_PRINTF_BUFSIZE]; |
150
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
156 | #ifdef va_copy |
144
b6dcc9d112eb
fixed printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
157 | va_list ap2; |
b6dcc9d112eb
fixed printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
158 | va_copy(ap2, ap); |
142
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
159 | int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
160 | if (ret < 0) { |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
161 | return ret; |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
162 | } else if (ret < UCX_PRINTF_BUFSIZE) { |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
163 | return (int)wfc(buf, 1, ret, stream); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
164 | } else { |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
165 | if (ret == INT_MAX) { |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
166 | errno = ENOMEM; |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
167 | return -1; |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
168 | } |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
169 | |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
170 | int len = ret + 1; |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
171 | char *newbuf = (char*)malloc(len); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
172 | if (!newbuf) { |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
173 | return -1; |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
174 | } |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
175 | |
144
b6dcc9d112eb
fixed printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
176 | ret = vsnprintf(newbuf, len, fmt, ap2); |
142
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
177 | if (ret > 0) { |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
178 | ret = (int)wfc(newbuf, 1, ret, stream); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
179 | } |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
180 | free(newbuf); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
181 | } |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
182 | return ret; |
150
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
183 | #else |
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
184 | int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); |
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
185 | if (ret < 0) { |
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
186 | return ret; |
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
187 | } else if (ret < UCX_PRINTF_BUFSIZE) { |
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
188 | return (int)wfc(buf, 1, ret, stream); |
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
189 | } else { |
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
190 | errno = ENOMEM; |
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
191 | return -1; |
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
192 | } |
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
193 | #endif |
142
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
194 | } |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
195 | |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
196 | sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...) { |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
197 | va_list ap; |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
198 | sstr_t ret; |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
199 | va_start(ap, fmt); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
200 | ret = ucx_vasprintf(allocator, fmt, ap); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
201 | va_end(ap); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
202 | return ret; |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
203 | } |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
204 | |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
205 | sstr_t ucx_vasprintf(UcxAllocator *a, const char *fmt, va_list ap) { |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
206 | sstr_t s; |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
207 | s.ptr = NULL; |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
208 | s.length = 0; |
150
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
209 | char buf[UCX_PRINTF_BUFSIZE]; |
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
210 | #ifdef va_copy |
144
b6dcc9d112eb
fixed printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
211 | va_list ap2; |
b6dcc9d112eb
fixed printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
212 | va_copy(ap2, ap); |
142
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
213 | int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
214 | if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) { |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
215 | s.ptr = (char*)a->malloc(a->pool, ret + 1); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
216 | s.length = (size_t)ret; |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
217 | memcpy(s.ptr, buf, ret); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
218 | s.ptr[s.length] = '\0'; |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
219 | } else if (ret == INT_MAX) { |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
220 | errno = ENOMEM; |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
221 | } else { |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
222 | int len = ret + 1; |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
223 | s.ptr = (char*)a->malloc(a->pool, len); |
144
b6dcc9d112eb
fixed printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
224 | ret = vsnprintf(s.ptr, len, fmt, ap2); |
142
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
225 | if (ret < 0) { |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
226 | free(s.ptr); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
227 | s.ptr = NULL; |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
228 | } else { |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
229 | s.length = (size_t)ret; |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
230 | } |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
231 | } |
150
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
232 | #else |
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
233 | int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); |
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
234 | if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) { |
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
235 | s.ptr = (char*)a->malloc(a->pool, ret + 1); |
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
236 | s.length = (size_t)ret; |
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
237 | memcpy(s.ptr, buf, ret); |
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
238 | s.ptr[s.length] = '\0'; |
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
239 | } else { |
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
240 | errno = ENOMEM; |
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
241 | } |
1cf2eabf94ed
fixes for ultra fail not C99 supporting VC wannebe compiler
Mike Becker <universe@uap-core.de>
parents:
144
diff
changeset
|
242 | #endif |
142
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
243 | return s; |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
244 | } |