Tue, 13 Aug 2013 14:20:12 +0200
completed documentation + changed API for buffer/stream generic copy functions
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 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 * Common utilities like compare and copy 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 <stdint.h>
47 #include <string.h>
49 /**
50 * Copies a string.
51 * @param s the string to copy
52 * @param data omitted
53 * @return a pointer to a copy of s1 that can be passed to free(void*)
54 */
55 void *ucx_strcpy(void *s, void *data);
57 /**
58 * Copies a memory area.
59 * @param m a pointer to the memory area
60 * @param n a pointer to the size_t containing the size of the memory area
61 * @return a pointer to a copy of the specified memory area that can
62 * be passed to free(void*)
63 */
64 void *ucx_memcpy(void *m, void *n);
67 /**
68 * Reads data from a stream and writes it to another stream.
69 *
70 * @param src the source stream
71 * @param dest the destination stream
72 * @param rfnc the read function
73 * @param wfnc the write function
74 * @param buf a pointer to the copy buffer or <code>NULL</code> if a buffer
75 * shall be implicitly created on the heap
76 * @param bufsize the size of the copy buffer - if <code>NULL</code> was
77 * provided for <code>buf</code>, this is the size of the buffer that shall be
78 * implicitly created
79 * @param n the maximum number of bytes that shall be copied
80 * @return the total number of bytes copied
81 */
82 size_t ucx_stream_copy(void *src, void *dest, read_func rfnc, write_func wfnc,
83 char* buf, size_t bufsize, size_t n);
85 /**
86 * Shorthand for ucx_stream_copy using the default copy buffer.
87 *
88 * @param src the source stream
89 * @param dest the destination stream
90 * @param rfnc the read function
91 * @param wfnc the write function
92 * @return total number of bytes copied
93 */
94 #define ucx_stream_hcopy(src,dest,rfnc,wfnc) ucx_stream_copy(\
95 src, dest, (read_func)rfnc, (write_func)wfnc, NULL, 0x100, SIZE_MAX)
97 /**
98 * Shorthand for ucx_stream_copy using the default copy buffer and a copy limit.
99 *
100 * @param src the source stream
101 * @param dest the destination stream
102 * @param rfnc the read function
103 * @param wfnc the write function
104 * @param n maximum number of bytes that shall be copied
105 * @return total number of bytes copied
106 */
107 #define ucx_stream_ncopy(src,dest,rfnc,wfnc, n) ucx_stream_copy(\
108 src, dest, (read_func)rfnc, (write_func)wfnc, NULL, 0x100, n)
110 /**
111 * Wraps the strcmp function.
112 * @param s1 string one
113 * @param s2 string two
114 * @param data omitted
115 * @return the result of strcmp(s1, s2)
116 */
117 int ucx_strcmp(void *s1, void *s2, void *data);
119 /**
120 * Wraps the strncmp function.
121 * @param s1 string one
122 * @param s2 string two
123 * @param n a pointer to the size_t containing the third strncmp parameter
124 * @return the result of strncmp(s1, s2, *n)
125 */
126 int ucx_strncmp(void *s1, void *s2, void *n);
128 /**
129 * Compares two integers of type int.
130 * @param i1 pointer to integer one
131 * @param i2 pointer to integer two
132 * @param data omitted
133 * @return -1, if *i1 is less than *i2, 0 if both are equal,
134 * 1 if *i1 is greater than *i2
135 */
137 int ucx_intcmp(void *i1, void *i2, void *data);
139 /**
140 * Compares two real numbers of type float.
141 * @param f1 pointer to float one
142 * @param f2 pointer to float two
143 * @param data if provided: a pointer to precision (default: 1e-6f)
144 * @return -1, if *f1 is less than *f2, 0 if both are equal,
145 * 1 if *f1 is greater than *f2
146 */
148 int ucx_floatcmp(void *f1, void *f2, void *data);
150 /**
151 * Compares two real numbers of type double.
152 * @param d1 pointer to double one
153 * @param d2 pointer to double two
154 * @param data if provided: a pointer to precision (default: 1e-14)
155 * @return -1, if *d1 is less than *d2, 0 if both are equal,
156 * 1 if *d1 is greater than *d2
157 */
159 int ucx_doublecmp(void *d1, void *d2, void *data);
161 /**
162 * Compares two pointers.
163 * @param ptr1 pointer one
164 * @param ptr2 pointer two
165 * @param data omitted
166 * @return -1 if ptr1 is less than ptr2, 0 if both are equal,
167 * 1 if ptr1 is greater than ptr2
168 */
169 int ucx_ptrcmp(void *ptr1, void *ptr2, void *data);
171 /**
172 * Compares two memory areas.
173 * @param ptr1 pointer one
174 * @param ptr2 pointer two
175 * @param n a pointer to the size_t containing the third parameter for memcmp
176 * @return the result of memcmp(ptr1, ptr2, *n)
177 */
178 int ucx_memcmp(void *ptr1, void *ptr2, void *n);
180 #ifdef __cplusplus
181 }
182 #endif
184 #endif /* UCX_UTILS_H */