Wed, 07 Sep 2016 12:26:01 +0200
changes ucx_stream_Xcopy API
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2015 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
29 /**
30 * @file utils.h
31 *
32 * Compare, copy and printf functions.
33 *
34 * @author Mike Becker
35 * @author Olaf Wintermann
36 */
38 #ifndef UCX_UTILS_H
39 #define UCX_UTILS_H
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
45 #include "ucx.h"
46 #include "string.h"
47 #include "allocator.h"
48 #include <inttypes.h>
49 #include <string.h>
50 #include <stdarg.h>
52 #define UCX_STREAM_COPY_BUFSIZE 4096
54 /**
55 * Copies a string.
56 * @param s the string to copy
57 * @param data omitted
58 * @return a pointer to a copy of s1 that can be passed to free(void*)
59 */
60 void *ucx_strcpy(void *s, void *data);
62 /**
63 * Copies a memory area.
64 * @param m a pointer to the memory area
65 * @param n a pointer to the size_t containing the size of the memory area
66 * @return a pointer to a copy of the specified memory area that can
67 * be passed to free(void*)
68 */
69 void *ucx_memcpy(void *m, void *n);
72 /**
73 * Reads data from a stream and writes it to another stream.
74 *
75 * @param src the source stream
76 * @param dest the destination stream
77 * @param rfnc the read function
78 * @param wfnc the write function
79 * @param buf a pointer to the copy buffer or <code>NULL</code> if a buffer
80 * shall be implicitly created on the heap
81 * @param bufsize the size of the copy buffer - if <code>NULL</code> was
82 * provided for <code>buf</code>, this is the size of the buffer that shall be
83 * implicitly created
84 * @param n the maximum number of bytes that shall be copied
85 * @return the total number of bytes copied
86 */
87 size_t ucx_stream_bncopy(void *src, void *dest, read_func rfnc, write_func wfnc,
88 char* buf, size_t bufsize, size_t n);
90 /**
91 * Shorthand for an unbounded ucx_stream_bncopy call using a default buffer.
92 *
93 * @param src the source stream
94 * @param dest the destination stream
95 * @param rfnc the read function
96 * @param wfnc the write function
97 * @return total number of bytes copied
98 *
99 * @see #UCX_STREAM_COPY_BUFSIZE
100 */
101 #define ucx_stream_copy(src,dest,rfnc,wfnc) ucx_stream_bncopy(\
102 src, dest, (read_func)rfnc, (write_func)wfnc, \
103 NULL, UCX_STREAM_COPY_BUFSIZE, (size_t)-1)
105 /**
106 * Shorthand for ucx_stream_bncopy using a default copy buffer.
107 *
108 * @param src the source stream
109 * @param dest the destination stream
110 * @param rfnc the read function
111 * @param wfnc the write function
112 * @param n maximum number of bytes that shall be copied
113 * @return total number of bytes copied
114 */
115 #define ucx_stream_ncopy(src,dest,rfnc,wfnc, n) ucx_stream_bncopy(\
116 src, dest, (read_func)rfnc, (write_func)wfnc, \
117 NULL, UCX_STREAM_COPY_BUFSIZE, n)
119 /**
120 * Shorthand for an unbounded ucx_stream_bncopy call using the specified buffer.
121 *
122 * @param src the source stream
123 * @param dest the destination stream
124 * @param rfnc the read function
125 * @param wfnc the write function
126 * @param buf a pointer to the copy buffer or <code>NULL</code> if a buffer
127 * shall be implicitly created on the heap
128 * @param bufsize the size of the copy buffer - if <code>NULL</code> was
129 * provided for <code>buf</code>, this is the size of the buffer that shall be
130 * implicitly created
131 * @return total number of bytes copied
132 */
133 #define ucx_stream_bcopy(src,dest,rfnc,wfnc, buf, bufsize) ucx_stream_bncopy(\
134 src, dest, (read_func)rfnc, (write_func)wfnc, \
135 buf, bufsize, (size_t)-1)
137 /**
138 * Wraps the strcmp function.
139 * @param s1 string one
140 * @param s2 string two
141 * @param data omitted
142 * @return the result of strcmp(s1, s2)
143 */
144 int ucx_strcmp(void *s1, void *s2, void *data);
146 /**
147 * Wraps the strncmp function.
148 * @param s1 string one
149 * @param s2 string two
150 * @param n a pointer to the size_t containing the third strncmp parameter
151 * @return the result of strncmp(s1, s2, *n)
152 */
153 int ucx_strncmp(void *s1, void *s2, void *n);
155 /**
156 * Compares two integers of type int.
157 * @param i1 pointer to integer one
158 * @param i2 pointer to integer two
159 * @param data omitted
160 * @return -1, if *i1 is less than *i2, 0 if both are equal,
161 * 1 if *i1 is greater than *i2
162 */
163 int ucx_intcmp(void *i1, void *i2, void *data);
165 /**
166 * Compares two real numbers of type float.
167 * @param f1 pointer to float one
168 * @param f2 pointer to float two
169 * @param data if provided: a pointer to precision (default: 1e-6f)
170 * @return -1, if *f1 is less than *f2, 0 if both are equal,
171 * 1 if *f1 is greater than *f2
172 */
174 int ucx_floatcmp(void *f1, void *f2, void *data);
176 /**
177 * Compares two real numbers of type double.
178 * @param d1 pointer to double one
179 * @param d2 pointer to double two
180 * @param data if provided: a pointer to precision (default: 1e-14)
181 * @return -1, if *d1 is less than *d2, 0 if both are equal,
182 * 1 if *d1 is greater than *d2
183 */
184 int ucx_doublecmp(void *d1, void *d2, void *data);
186 /**
187 * Compares two pointers.
188 * @param ptr1 pointer one
189 * @param ptr2 pointer two
190 * @param data omitted
191 * @return -1 if ptr1 is less than ptr2, 0 if both are equal,
192 * 1 if ptr1 is greater than ptr2
193 */
194 int ucx_ptrcmp(void *ptr1, void *ptr2, void *data);
196 /**
197 * Compares two memory areas.
198 * @param ptr1 pointer one
199 * @param ptr2 pointer two
200 * @param n a pointer to the size_t containing the third parameter for memcmp
201 * @return the result of memcmp(ptr1, ptr2, *n)
202 */
203 int ucx_memcmp(void *ptr1, void *ptr2, void *n);
205 /**
206 * A <code>printf()</code> like function which writes the output to a stream by
207 * using a write_func().
208 * @param stream the stream the data is written to
209 * @param wfc the write function
210 * @param fmt format string
211 * @param ... additional arguments
212 * @return the total number of bytes written
213 */
214 int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...);
216 /**
217 * <code>va_list</code> version of ucx_fprintf().
218 * @param stream the stream the data is written to
219 * @param wfc the write function
220 * @param fmt format string
221 * @param ap argument list
222 * @return the total number of bytes written
223 * @see ucx_fprintf()
224 */
225 int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap);
227 /**
228 * A <code>printf()</code> like function which allocates space for a sstr_t
229 * the result is written to.
230 *
231 * <b>Attention</b>: The sstr_t data is allocated with the allocators
232 * ucx_allocator_malloc() function. So it is implementation dependent, if
233 * the returned sstr_t.ptr pointer must be passed to the allocators
234 * ucx_allocator_free() function manually.
235 *
236 * <b>Note</b>: The sstr_t.ptr of the return value will <i>always</i> be
237 * <code>NULL</code>-terminated.
238 *
239 * @param allocator the UcxAllocator used for allocating the result sstr_t
240 * @param fmt format string
241 * @param ... additional arguments
242 * @return a sstr_t containing the formatted string
243 */
244 sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...);
246 /** Shortcut for ucx_asprintf() with default allocator. */
247 #define ucx_sprintf(fmt, ...) \
248 ucx_asprintf(ucx_default_allocator(), fmt, __VA_ARGS__)
250 /**
251 * <code>va_list</code> version of ucx_asprintf().
252 *
253 * @param allocator the UcxAllocator used for allocating the result sstr_t
254 * @param fmt format string
255 * @param ap argument list
256 * @return a sstr_t containing the formatted string
257 * @see ucx_asprintf()
258 */
259 sstr_t ucx_vasprintf(UcxAllocator *allocator, const char *fmt, va_list ap);
261 /**
262 * A <code>printf()</code> like function which writes the output to an
263 * UcxBuffer.
264 *
265 * @param buffer the buffer the data is written to
266 * @param ... format string and additional arguments
267 * @return the total number of bytes written
268 * @see ucx_fprintf()
269 */
270 #define ucx_bprintf(buffer, ...) ucx_fprintf((UcxBuffer*)buffer, \
271 (write_func)ucx_buffer_write, __VA_ARGS__)
273 #ifdef __cplusplus
274 }
275 #endif
277 #endif /* UCX_UTILS_H */