src/ucx/utils.h

Sat, 28 Oct 2017 15:43:51 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 28 Oct 2017 15:43:51 +0200
changeset 259
2f5dea574a75
parent 251
fae240d633fc
child 285
7be3ae7ffb58
permissions
-rw-r--r--

modules documentation

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@244 147 int ucx_strcmp(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@244 156 int ucx_strncmp(const void *s1, const void *s2, void *n);
universe@89 157
universe@89 158 /**
universe@89 159 * Compares two integers of type int.
universe@89 160 * @param i1 pointer to integer one
universe@89 161 * @param i2 pointer to integer two
universe@89 162 * @param data omitted
universe@89 163 * @return -1, if *i1 is less than *i2, 0 if both are equal,
universe@89 164 * 1 if *i1 is greater than *i2
universe@89 165 */
universe@244 166 int ucx_intcmp(const void *i1, const void *i2, void *data);
universe@89 167
universe@89 168 /**
universe@92 169 * Compares two real numbers of type float.
universe@92 170 * @param f1 pointer to float one
universe@92 171 * @param f2 pointer to float two
universe@138 172 * @param data if provided: a pointer to precision (default: 1e-6f)
universe@92 173 * @return -1, if *f1 is less than *f2, 0 if both are equal,
universe@92 174 * 1 if *f1 is greater than *f2
universe@92 175 */
universe@92 176
universe@244 177 int ucx_floatcmp(const void *f1, const void *f2, void *data);
universe@92 178
universe@92 179 /**
universe@92 180 * Compares two real numbers of type double.
universe@138 181 * @param d1 pointer to double one
universe@138 182 * @param d2 pointer to double two
universe@138 183 * @param data if provided: a pointer to precision (default: 1e-14)
universe@92 184 * @return -1, if *d1 is less than *d2, 0 if both are equal,
universe@92 185 * 1 if *d1 is greater than *d2
universe@92 186 */
universe@244 187 int ucx_doublecmp(const void *d1, const void *d2, void *data);
universe@92 188
universe@92 189 /**
universe@89 190 * Compares two pointers.
universe@89 191 * @param ptr1 pointer one
universe@89 192 * @param ptr2 pointer two
universe@89 193 * @param data omitted
universe@89 194 * @return -1 if ptr1 is less than ptr2, 0 if both are equal,
universe@89 195 * 1 if ptr1 is greater than ptr2
universe@89 196 */
universe@244 197 int ucx_ptrcmp(const void *ptr1, const void *ptr2, void *data);
universe@89 198
universe@91 199 /**
universe@91 200 * Compares two memory areas.
universe@91 201 * @param ptr1 pointer one
universe@91 202 * @param ptr2 pointer two
universe@91 203 * @param n a pointer to the size_t containing the third parameter for memcmp
universe@91 204 * @return the result of memcmp(ptr1, ptr2, *n)
universe@91 205 */
universe@244 206 int ucx_memcmp(const void *ptr1, const void *ptr2, void *n);
universe@91 207
olaf@142 208 /**
universe@146 209 * A <code>printf()</code> like function which writes the output to a stream by
universe@146 210 * using a write_func().
universe@146 211 * @param stream the stream the data is written to
universe@146 212 * @param wfc the write function
olaf@142 213 * @param fmt format string
olaf@142 214 * @param ... additional arguments
olaf@142 215 * @return the total number of bytes written
olaf@142 216 */
olaf@142 217 int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...);
olaf@142 218
olaf@142 219 /**
universe@146 220 * <code>va_list</code> version of ucx_fprintf().
universe@146 221 * @param stream the stream the data is written to
universe@146 222 * @param wfc the write function
olaf@142 223 * @param fmt format string
olaf@142 224 * @param ap argument list
olaf@142 225 * @return the total number of bytes written
olaf@142 226 * @see ucx_fprintf()
olaf@142 227 */
olaf@142 228 int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap);
olaf@142 229
olaf@142 230 /**
universe@146 231 * A <code>printf()</code> like function which allocates space for a sstr_t
universe@146 232 * the result is written to.
olaf@142 233 *
universe@146 234 * <b>Attention</b>: The sstr_t data is allocated with the allocators
universe@146 235 * ucx_allocator_malloc() function. So it is implementation dependent, if
universe@146 236 * the returned sstr_t.ptr pointer must be passed to the allocators
universe@146 237 * ucx_allocator_free() function manually.
olaf@142 238 *
universe@146 239 * <b>Note</b>: The sstr_t.ptr of the return value will <i>always</i> be
universe@146 240 * <code>NULL</code>-terminated.
olaf@142 241 *
universe@146 242 * @param allocator the UcxAllocator used for allocating the result sstr_t
olaf@142 243 * @param fmt format string
olaf@142 244 * @param ... additional arguments
universe@146 245 * @return a sstr_t containing the formatted string
olaf@142 246 */
olaf@142 247 sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...);
olaf@142 248
olaf@142 249 /**
universe@146 250 * <code>va_list</code> version of ucx_asprintf().
universe@146 251 *
universe@146 252 * @param allocator the UcxAllocator used for allocating the result sstr_t
olaf@142 253 * @param fmt format string
olaf@142 254 * @param ap argument list
universe@146 255 * @return a sstr_t containing the formatted string
universe@146 256 * @see ucx_asprintf()
olaf@142 257 */
olaf@142 258 sstr_t ucx_vasprintf(UcxAllocator *allocator, const char *fmt, va_list ap);
olaf@142 259
universe@223 260 /** Shortcut for ucx_asprintf() with default allocator. */
universe@223 261 #define ucx_sprintf(...) \
universe@223 262 ucx_asprintf(ucx_default_allocator(), __VA_ARGS__)
universe@223 263
olaf@147 264 /**
universe@225 265 * A <code>printf()</code> like function which writes the output to a
olaf@147 266 * UcxBuffer.
olaf@147 267 *
olaf@147 268 * @param buffer the buffer the data is written to
olaf@147 269 * @param ... format string and additional arguments
olaf@147 270 * @return the total number of bytes written
olaf@147 271 * @see ucx_fprintf()
olaf@147 272 */
olaf@147 273 #define ucx_bprintf(buffer, ...) ucx_fprintf((UcxBuffer*)buffer, \
olaf@147 274 (write_func)ucx_buffer_write, __VA_ARGS__)
olaf@147 275
universe@150 276 #ifdef __cplusplus
universe@89 277 }
universe@89 278 #endif
universe@89 279
universe@150 280 #endif /* UCX_UTILS_H */
universe@89 281

mercurial