src/ucx/utils.h

Mon, 14 May 2018 18:25:20 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 14 May 2018 18:25:20 +0200
changeset 314
5d28dc8f0765
parent 313
b7753273f0fd
child 328
2bf1da3c411e
permissions
-rw-r--r--

renames int and longint distance and compare functions according to the new scheme

universe@103 1 /*
universe@103 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@103 3 *
universe@259 4 * Copyright 2017 Mike Becker, 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@259 45 #include "ucx.h"
universe@259 46 #include "string.h"
universe@259 47 #include "allocator.h"
universe@218 48 #include <inttypes.h>
universe@89 49 #include <string.h>
olaf@142 50 #include <stdarg.h>
universe@224 51
universe@224 52 /**
universe@224 53 * Default buffer size for ucx_stream_copy() and ucx_stream_ncopy().
universe@224 54 */
universe@222 55 #define UCX_STREAM_COPY_BUFSIZE 4096
universe@89 56
universe@89 57 /**
universe@94 58 * Copies a string.
universe@94 59 * @param s the string to copy
universe@94 60 * @param data omitted
universe@94 61 * @return a pointer to a copy of s1 that can be passed to free(void*)
universe@94 62 */
universe@244 63 void *ucx_strcpy(const void *s, void *data);
universe@94 64
universe@94 65 /**
universe@94 66 * Copies a memory area.
universe@94 67 * @param m a pointer to the memory area
universe@94 68 * @param n a pointer to the size_t containing the size of the memory area
universe@94 69 * @return a pointer to a copy of the specified memory area that can
universe@94 70 * be passed to free(void*)
universe@94 71 */
universe@244 72 void *ucx_memcpy(const void *m, void *n);
universe@94 73
universe@140 74
universe@140 75 /**
universe@140 76 * Reads data from a stream and writes it to another stream.
universe@140 77 *
universe@140 78 * @param src the source stream
universe@140 79 * @param dest the destination stream
universe@140 80 * @param rfnc the read function
universe@140 81 * @param wfnc the write function
universe@140 82 * @param buf a pointer to the copy buffer or <code>NULL</code> if a buffer
universe@140 83 * shall be implicitly created on the heap
universe@140 84 * @param bufsize the size of the copy buffer - if <code>NULL</code> was
universe@140 85 * provided for <code>buf</code>, this is the size of the buffer that shall be
universe@140 86 * implicitly created
universe@140 87 * @param n the maximum number of bytes that shall be copied
universe@140 88 * @return the total number of bytes copied
universe@140 89 */
universe@222 90 size_t ucx_stream_bncopy(void *src, void *dest, read_func rfnc, write_func wfnc,
universe@140 91 char* buf, size_t bufsize, size_t n);
universe@140 92
universe@140 93 /**
universe@222 94 * Shorthand for an unbounded ucx_stream_bncopy call using a default buffer.
universe@140 95 *
universe@140 96 * @param src the source stream
universe@140 97 * @param dest the destination stream
universe@140 98 * @param rfnc the read function
universe@140 99 * @param wfnc the write function
universe@140 100 * @return total number of bytes copied
universe@222 101 *
universe@222 102 * @see #UCX_STREAM_COPY_BUFSIZE
universe@140 103 */
universe@222 104 #define ucx_stream_copy(src,dest,rfnc,wfnc) ucx_stream_bncopy(\
universe@222 105 src, dest, (read_func)rfnc, (write_func)wfnc, \
universe@222 106 NULL, UCX_STREAM_COPY_BUFSIZE, (size_t)-1)
universe@140 107
universe@140 108 /**
universe@222 109 * Shorthand for ucx_stream_bncopy using a default copy buffer.
universe@140 110 *
universe@140 111 * @param src the source stream
universe@140 112 * @param dest the destination stream
universe@140 113 * @param rfnc the read function
universe@140 114 * @param wfnc the write function
universe@140 115 * @param n maximum number of bytes that shall be copied
universe@140 116 * @return total number of bytes copied
universe@140 117 */
universe@222 118 #define ucx_stream_ncopy(src,dest,rfnc,wfnc, n) ucx_stream_bncopy(\
universe@222 119 src, dest, (read_func)rfnc, (write_func)wfnc, \
universe@222 120 NULL, UCX_STREAM_COPY_BUFSIZE, n)
universe@222 121
universe@222 122 /**
universe@222 123 * Shorthand for an unbounded ucx_stream_bncopy call using the specified buffer.
universe@222 124 *
universe@222 125 * @param src the source stream
universe@222 126 * @param dest the destination stream
universe@222 127 * @param rfnc the read function
universe@222 128 * @param wfnc the write function
universe@222 129 * @param buf a pointer to the copy buffer or <code>NULL</code> if a buffer
universe@222 130 * shall be implicitly created on the heap
universe@222 131 * @param bufsize the size of the copy buffer - if <code>NULL</code> was
universe@222 132 * provided for <code>buf</code>, this is the size of the buffer that shall be
universe@222 133 * implicitly created
universe@222 134 * @return total number of bytes copied
universe@222 135 */
universe@222 136 #define ucx_stream_bcopy(src,dest,rfnc,wfnc, buf, bufsize) ucx_stream_bncopy(\
universe@222 137 src, dest, (read_func)rfnc, (write_func)wfnc, \
universe@222 138 buf, bufsize, (size_t)-1)
universe@140 139
universe@94 140 /**
universe@89 141 * Wraps the strcmp function.
universe@89 142 * @param s1 string one
universe@89 143 * @param s2 string two
universe@89 144 * @param data omitted
universe@89 145 * @return the result of strcmp(s1, s2)
universe@89 146 */
universe@308 147 int ucx_cmp_str(const void *s1, const void *s2, void *data);
universe@89 148
universe@89 149 /**
universe@89 150 * Wraps the strncmp function.
universe@89 151 * @param s1 string one
universe@89 152 * @param s2 string two
universe@89 153 * @param n a pointer to the size_t containing the third strncmp parameter
universe@89 154 * @return the result of strncmp(s1, s2, *n)
universe@89 155 */
universe@309 156 int ucx_cmp_strn(const void *s1, const void *s2, void *n);
universe@89 157
universe@89 158 /**
universe@292 159 * Wraps the sstrcmp function.
universe@292 160 * @param s1 sstr one
universe@292 161 * @param s2 sstr two
universe@292 162 * @param data ignored
universe@292 163 * @return the result of sstrcmp(s1, s2)
universe@292 164 */
universe@310 165 int ucx_cmp_sstr(const void *s1, const void *s2, void *data);
universe@292 166
universe@292 167 /**
universe@89 168 * Compares two integers of type int.
universe@89 169 * @param i1 pointer to integer one
universe@89 170 * @param i2 pointer to integer two
universe@89 171 * @param data omitted
universe@89 172 * @return -1, if *i1 is less than *i2, 0 if both are equal,
universe@89 173 * 1 if *i1 is greater than *i2
universe@89 174 */
universe@314 175 int ucx_cmp_int(const void *i1, const void *i2, void *data);
universe@89 176
universe@89 177 /**
universe@285 178 * Compares two integers of type long int.
universe@285 179 * @param i1 pointer to long integer one
universe@285 180 * @param i2 pointer to long integer two
universe@285 181 * @param data omitted
universe@285 182 * @return -1, if *i1 is less than *i2, 0 if both are equal,
universe@285 183 * 1 if *i1 is greater than *i2
universe@285 184 */
universe@314 185 int ucx_cmp_longint(const void *i1, const void *i2, void *data);
universe@285 186
universe@286 187
universe@286 188 /**
universe@286 189 * Distance function for integers of type int.
universe@286 190 * @param i1 pointer to integer one
universe@286 191 * @param i2 pointer to integer two
universe@286 192 * @param data omitted
universe@286 193 * @return i1 minus i2
universe@286 194 */
universe@314 195 intmax_t ucx_dist_int(const void *i1, const void *i2, void *data);
universe@286 196
universe@286 197 /**
universe@286 198 * Distance function for integers of type long int.
universe@286 199 * @param i1 pointer to long integer one
universe@286 200 * @param i2 pointer to long integer two
universe@286 201 * @param data omitted
universe@286 202 * @return i1 minus i2
universe@286 203 */
universe@314 204 intmax_t ucx_dist_longint(const void *i1, const void *i2, void *data);
universe@286 205
universe@285 206 /**
universe@92 207 * Compares two real numbers of type float.
universe@92 208 * @param f1 pointer to float one
universe@92 209 * @param f2 pointer to float two
universe@138 210 * @param data if provided: a pointer to precision (default: 1e-6f)
universe@92 211 * @return -1, if *f1 is less than *f2, 0 if both are equal,
universe@92 212 * 1 if *f1 is greater than *f2
universe@92 213 */
universe@92 214
universe@313 215 int ucx_cmp_float(const void *f1, const void *f2, void *data);
universe@92 216
universe@92 217 /**
universe@92 218 * Compares two real numbers of type double.
universe@138 219 * @param d1 pointer to double one
universe@138 220 * @param d2 pointer to double two
universe@138 221 * @param data if provided: a pointer to precision (default: 1e-14)
universe@92 222 * @return -1, if *d1 is less than *d2, 0 if both are equal,
universe@92 223 * 1 if *d1 is greater than *d2
universe@92 224 */
universe@313 225 int ucx_cmp_double(const void *d1, const void *d2, void *data);
universe@92 226
universe@92 227 /**
universe@89 228 * Compares two pointers.
universe@89 229 * @param ptr1 pointer one
universe@89 230 * @param ptr2 pointer two
universe@89 231 * @param data omitted
universe@89 232 * @return -1 if ptr1 is less than ptr2, 0 if both are equal,
universe@89 233 * 1 if ptr1 is greater than ptr2
universe@89 234 */
universe@312 235 int ucx_cmp_ptr(const void *ptr1, const void *ptr2, void *data);
universe@89 236
universe@91 237 /**
universe@91 238 * Compares two memory areas.
universe@91 239 * @param ptr1 pointer one
universe@91 240 * @param ptr2 pointer two
universe@91 241 * @param n a pointer to the size_t containing the third parameter for memcmp
universe@91 242 * @return the result of memcmp(ptr1, ptr2, *n)
universe@91 243 */
universe@311 244 int ucx_cmp_mem(const void *ptr1, const void *ptr2, void *n);
universe@91 245
olaf@142 246 /**
universe@146 247 * A <code>printf()</code> like function which writes the output to a stream by
universe@146 248 * using a write_func().
universe@146 249 * @param stream the stream the data is written to
universe@146 250 * @param wfc the write function
olaf@142 251 * @param fmt format string
olaf@142 252 * @param ... additional arguments
olaf@142 253 * @return the total number of bytes written
olaf@142 254 */
olaf@142 255 int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...);
olaf@142 256
olaf@142 257 /**
universe@146 258 * <code>va_list</code> version of ucx_fprintf().
universe@146 259 * @param stream the stream the data is written to
universe@146 260 * @param wfc the write function
olaf@142 261 * @param fmt format string
olaf@142 262 * @param ap argument list
olaf@142 263 * @return the total number of bytes written
olaf@142 264 * @see ucx_fprintf()
olaf@142 265 */
olaf@142 266 int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap);
olaf@142 267
olaf@142 268 /**
universe@146 269 * A <code>printf()</code> like function which allocates space for a sstr_t
universe@146 270 * the result is written to.
olaf@142 271 *
universe@146 272 * <b>Attention</b>: The sstr_t data is allocated with the allocators
universe@146 273 * ucx_allocator_malloc() function. So it is implementation dependent, if
universe@146 274 * the returned sstr_t.ptr pointer must be passed to the allocators
universe@146 275 * ucx_allocator_free() function manually.
olaf@142 276 *
universe@146 277 * <b>Note</b>: The sstr_t.ptr of the return value will <i>always</i> be
universe@146 278 * <code>NULL</code>-terminated.
olaf@142 279 *
universe@146 280 * @param allocator the UcxAllocator used for allocating the result sstr_t
olaf@142 281 * @param fmt format string
olaf@142 282 * @param ... additional arguments
universe@146 283 * @return a sstr_t containing the formatted string
olaf@142 284 */
olaf@142 285 sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...);
olaf@142 286
olaf@142 287 /**
universe@146 288 * <code>va_list</code> version of ucx_asprintf().
universe@146 289 *
universe@146 290 * @param allocator the UcxAllocator used for allocating the result sstr_t
olaf@142 291 * @param fmt format string
olaf@142 292 * @param ap argument list
universe@146 293 * @return a sstr_t containing the formatted string
universe@146 294 * @see ucx_asprintf()
olaf@142 295 */
olaf@142 296 sstr_t ucx_vasprintf(UcxAllocator *allocator, const char *fmt, va_list ap);
olaf@142 297
universe@223 298 /** Shortcut for ucx_asprintf() with default allocator. */
universe@223 299 #define ucx_sprintf(...) \
universe@223 300 ucx_asprintf(ucx_default_allocator(), __VA_ARGS__)
universe@223 301
olaf@147 302 /**
universe@225 303 * A <code>printf()</code> like function which writes the output to a
olaf@147 304 * UcxBuffer.
olaf@147 305 *
olaf@147 306 * @param buffer the buffer the data is written to
olaf@147 307 * @param ... format string and additional arguments
olaf@147 308 * @return the total number of bytes written
olaf@147 309 * @see ucx_fprintf()
olaf@147 310 */
olaf@147 311 #define ucx_bprintf(buffer, ...) ucx_fprintf((UcxBuffer*)buffer, \
olaf@147 312 (write_func)ucx_buffer_write, __VA_ARGS__)
olaf@147 313
universe@150 314 #ifdef __cplusplus
universe@89 315 }
universe@89 316 #endif
universe@89 317
universe@150 318 #endif /* UCX_UTILS_H */
universe@89 319

mercurial