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