src/ucx/utils.h

Tue, 23 Aug 2016 13:49:38 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Aug 2016 13:49:38 +0200
changeset 39
ac35daceb24c
permissions
-rw-r--r--

adds UCX + changes how the input file is read (uses an consecutive memory area now)

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

mercurial