ucx/utils.h

changeset 17
11dffb40cd91
parent 5
88625853ae74
child 39
3e55bed345f9
equal deleted inserted replaced
16:5dbef9e07376 17:11dffb40cd91
27 */ 27 */
28 28
29 /** 29 /**
30 * @file utils.h 30 * @file utils.h
31 * 31 *
32 * Common utilities like compare and copy functions. 32 * Compare, copy and printf functions.
33 * 33 *
34 * @author Mike Becker 34 * @author Mike Becker
35 * @author Olaf Wintermann 35 * @author Olaf Wintermann
36 */ 36 */
37 37
41 #ifdef __cplusplus 41 #ifdef __cplusplus
42 extern "C" { 42 extern "C" {
43 #endif 43 #endif
44 44
45 #include "ucx.h" 45 #include "ucx.h"
46 #include "string.h"
47 #include "allocator.h"
48 #include <stdint.h>
46 #include <string.h> 49 #include <string.h>
50 #include <stdarg.h>
47 51
48 /** 52 /**
49 * Copies a string. 53 * Copies a string.
50 * @param s the string to copy 54 * @param s the string to copy
51 * @param data omitted 55 * @param data omitted
60 * @return a pointer to a copy of the specified memory area that can 64 * @return a pointer to a copy of the specified memory area that can
61 * be passed to free(void*) 65 * be passed to free(void*)
62 */ 66 */
63 void *ucx_memcpy(void *m, void *n); 67 void *ucx_memcpy(void *m, void *n);
64 68
69
70 /**
71 * Reads data from a stream and writes it to another stream.
72 *
73 * @param src the source stream
74 * @param dest the destination stream
75 * @param rfnc the read function
76 * @param wfnc the write function
77 * @param buf a pointer to the copy buffer or <code>NULL</code> if a buffer
78 * shall be implicitly created on the heap
79 * @param bufsize the size of the copy buffer - if <code>NULL</code> was
80 * provided for <code>buf</code>, this is the size of the buffer that shall be
81 * implicitly created
82 * @param n the maximum number of bytes that shall be copied
83 * @return the total number of bytes copied
84 */
85 size_t ucx_stream_copy(void *src, void *dest, read_func rfnc, write_func wfnc,
86 char* buf, size_t bufsize, size_t n);
87
88 /**
89 * Shorthand for ucx_stream_copy using the default copy buffer.
90 *
91 * @param src the source stream
92 * @param dest the destination stream
93 * @param rfnc the read function
94 * @param wfnc the write function
95 * @return total number of bytes copied
96 */
97 #define ucx_stream_hcopy(src,dest,rfnc,wfnc) ucx_stream_copy(\
98 src, dest, (read_func)rfnc, (write_func)wfnc, NULL, 0x100, SIZE_MAX)
99
100 /**
101 * Shorthand for ucx_stream_copy using the default copy buffer and a copy limit.
102 *
103 * @param src the source stream
104 * @param dest the destination stream
105 * @param rfnc the read function
106 * @param wfnc the write function
107 * @param n maximum number of bytes that shall be copied
108 * @return total number of bytes copied
109 */
110 #define ucx_stream_ncopy(src,dest,rfnc,wfnc, n) ucx_stream_copy(\
111 src, dest, (read_func)rfnc, (write_func)wfnc, NULL, 0x100, n)
112
65 /** 113 /**
66 * Wraps the strcmp function. 114 * Wraps the strcmp function.
67 * @param s1 string one 115 * @param s1 string one
68 * @param s2 string two 116 * @param s2 string two
69 * @param data omitted 117 * @param data omitted
86 * @param i2 pointer to integer two 134 * @param i2 pointer to integer two
87 * @param data omitted 135 * @param data omitted
88 * @return -1, if *i1 is less than *i2, 0 if both are equal, 136 * @return -1, if *i1 is less than *i2, 0 if both are equal,
89 * 1 if *i1 is greater than *i2 137 * 1 if *i1 is greater than *i2
90 */ 138 */
91
92 int ucx_intcmp(void *i1, void *i2, void *data); 139 int ucx_intcmp(void *i1, void *i2, void *data);
93 140
94 /** 141 /**
95 * Compares two real numbers of type float. 142 * Compares two real numbers of type float.
96 * @param f1 pointer to float one 143 * @param f1 pointer to float one
97 * @param f2 pointer to float two 144 * @param f2 pointer to float two
98 * @param if provided: a pointer to precision (default: 1e-6f) 145 * @param data if provided: a pointer to precision (default: 1e-6f)
99 * @return -1, if *f1 is less than *f2, 0 if both are equal, 146 * @return -1, if *f1 is less than *f2, 0 if both are equal,
100 * 1 if *f1 is greater than *f2 147 * 1 if *f1 is greater than *f2
101 */ 148 */
102 149
103 int ucx_floatcmp(void *f1, void *f2, void *data); 150 int ucx_floatcmp(void *f1, void *f2, void *data);
104 151
105 /** 152 /**
106 * Compares two real numbers of type double. 153 * Compares two real numbers of type double.
107 * @param f1 pointer to double one 154 * @param d1 pointer to double one
108 * @param f2 pointer to double two 155 * @param d2 pointer to double two
109 * @param if provided: a pointer to precision (default: 1e-14) 156 * @param data if provided: a pointer to precision (default: 1e-14)
110 * @return -1, if *d1 is less than *d2, 0 if both are equal, 157 * @return -1, if *d1 is less than *d2, 0 if both are equal,
111 * 1 if *d1 is greater than *d2 158 * 1 if *d1 is greater than *d2
112 */ 159 */
113
114 int ucx_doublecmp(void *d1, void *d2, void *data); 160 int ucx_doublecmp(void *d1, void *d2, void *data);
115 161
116 /** 162 /**
117 * Compares two pointers. 163 * Compares two pointers.
118 * @param ptr1 pointer one 164 * @param ptr1 pointer one
130 * @param n a pointer to the size_t containing the third parameter for memcmp 176 * @param n a pointer to the size_t containing the third parameter for memcmp
131 * @return the result of memcmp(ptr1, ptr2, *n) 177 * @return the result of memcmp(ptr1, ptr2, *n)
132 */ 178 */
133 int ucx_memcmp(void *ptr1, void *ptr2, void *n); 179 int ucx_memcmp(void *ptr1, void *ptr2, void *n);
134 180
181 /**
182 * A <code>printf()</code> like function which writes the output to a stream by
183 * using a write_func().
184 * @param stream the stream the data is written to
185 * @param wfc the write function
186 * @param fmt format string
187 * @param ... additional arguments
188 * @return the total number of bytes written
189 */
190 int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...);
191
192 /**
193 * <code>va_list</code> version of ucx_fprintf().
194 * @param stream the stream the data is written to
195 * @param wfc the write function
196 * @param fmt format string
197 * @param ap argument list
198 * @return the total number of bytes written
199 * @see ucx_fprintf()
200 */
201 int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap);
202
203 /**
204 * A <code>printf()</code> like function which allocates space for a sstr_t
205 * the result is written to.
206 *
207 * <b>Attention</b>: The sstr_t data is allocated with the allocators
208 * ucx_allocator_malloc() function. So it is implementation dependent, if
209 * the returned sstr_t.ptr pointer must be passed to the allocators
210 * ucx_allocator_free() function manually.
211 *
212 * <b>Note</b>: The sstr_t.ptr of the return value will <i>always</i> be
213 * <code>NULL</code>-terminated.
214 *
215 * @param allocator the UcxAllocator used for allocating the result sstr_t
216 * @param fmt format string
217 * @param ... additional arguments
218 * @return a sstr_t containing the formatted string
219 */
220 sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...);
221
222 /**
223 * <code>va_list</code> version of ucx_asprintf().
224 *
225 * @param allocator the UcxAllocator used for allocating the result sstr_t
226 * @param fmt format string
227 * @param ap argument list
228 * @return a sstr_t containing the formatted string
229 * @see ucx_asprintf()
230 */
231 sstr_t ucx_vasprintf(UcxAllocator *allocator, const char *fmt, va_list ap);
232
233 /**
234 * A <code>printf()</code> like function which writes the output to an
235 * UcxBuffer.
236 *
237 * @param buffer the buffer the data is written to
238 * @param ... format string and additional arguments
239 * @return the total number of bytes written
240 * @see ucx_fprintf()
241 */
242 #define ucx_bprintf(buffer, ...) ucx_fprintf((UcxBuffer*)buffer, \
243 (write_func)ucx_buffer_write, __VA_ARGS__)
244
135 #ifdef __cplusplus 245 #ifdef __cplusplus
136 } 246 }
137 #endif 247 #endif
138 248
139 #endif /* UCX_UTILS_H */ 249 #endif /* UCX_UTILS_H */

mercurial