ucx/utils.h

Wed, 07 Sep 2016 12:26:01 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 07 Sep 2016 12:26:01 +0200
changeset 222
e0f850709a5c
parent 218
b20d6088795c
child 223
e18884bbad48
permissions
-rw-r--r--

changes ucx_stream_Xcopy API

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

mercurial