Tue, 13 Aug 2013 14:20:12 +0200
completed documentation + changed API for buffer/stream generic copy functions
universe@103 | 1 | /* |
universe@103 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@103 | 3 | * |
universe@103 | 4 | * Copyright 2013 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 | * |
universe@135 | 32 | * Common utilities like compare and copy 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 |
olaf@120 | 39 | #define UCX_UTILS_H |
universe@89 | 40 | |
universe@89 | 41 | #ifdef __cplusplus |
universe@89 | 42 | extern "C" { |
universe@89 | 43 | #endif |
universe@89 | 44 | |
universe@89 | 45 | #include "ucx.h" |
universe@140 | 46 | #include <stdint.h> |
universe@89 | 47 | #include <string.h> |
universe@89 | 48 | |
universe@89 | 49 | /** |
universe@94 | 50 | * Copies a string. |
universe@94 | 51 | * @param s the string to copy |
universe@94 | 52 | * @param data omitted |
universe@94 | 53 | * @return a pointer to a copy of s1 that can be passed to free(void*) |
universe@94 | 54 | */ |
universe@94 | 55 | void *ucx_strcpy(void *s, void *data); |
universe@94 | 56 | |
universe@94 | 57 | /** |
universe@94 | 58 | * Copies a memory area. |
universe@94 | 59 | * @param m a pointer to the memory area |
universe@94 | 60 | * @param n a pointer to the size_t containing the size of the memory area |
universe@94 | 61 | * @return a pointer to a copy of the specified memory area that can |
universe@94 | 62 | * be passed to free(void*) |
universe@94 | 63 | */ |
universe@94 | 64 | void *ucx_memcpy(void *m, void *n); |
universe@94 | 65 | |
universe@140 | 66 | |
universe@140 | 67 | /** |
universe@140 | 68 | * Reads data from a stream and writes it to another stream. |
universe@140 | 69 | * |
universe@140 | 70 | * @param src the source stream |
universe@140 | 71 | * @param dest the destination stream |
universe@140 | 72 | * @param rfnc the read function |
universe@140 | 73 | * @param wfnc the write function |
universe@140 | 74 | * @param buf a pointer to the copy buffer or <code>NULL</code> if a buffer |
universe@140 | 75 | * shall be implicitly created on the heap |
universe@140 | 76 | * @param bufsize the size of the copy buffer - if <code>NULL</code> was |
universe@140 | 77 | * provided for <code>buf</code>, this is the size of the buffer that shall be |
universe@140 | 78 | * implicitly created |
universe@140 | 79 | * @param n the maximum number of bytes that shall be copied |
universe@140 | 80 | * @return the total number of bytes copied |
universe@140 | 81 | */ |
universe@140 | 82 | size_t ucx_stream_copy(void *src, void *dest, read_func rfnc, write_func wfnc, |
universe@140 | 83 | char* buf, size_t bufsize, size_t n); |
universe@140 | 84 | |
universe@140 | 85 | /** |
universe@140 | 86 | * Shorthand for ucx_stream_copy using the default copy buffer. |
universe@140 | 87 | * |
universe@140 | 88 | * @param src the source stream |
universe@140 | 89 | * @param dest the destination stream |
universe@140 | 90 | * @param rfnc the read function |
universe@140 | 91 | * @param wfnc the write function |
universe@140 | 92 | * @return total number of bytes copied |
universe@140 | 93 | */ |
universe@140 | 94 | #define ucx_stream_hcopy(src,dest,rfnc,wfnc) ucx_stream_copy(\ |
universe@140 | 95 | src, dest, (read_func)rfnc, (write_func)wfnc, NULL, 0x100, SIZE_MAX) |
universe@140 | 96 | |
universe@140 | 97 | /** |
universe@140 | 98 | * Shorthand for ucx_stream_copy using the default copy buffer and a copy limit. |
universe@140 | 99 | * |
universe@140 | 100 | * @param src the source stream |
universe@140 | 101 | * @param dest the destination stream |
universe@140 | 102 | * @param rfnc the read function |
universe@140 | 103 | * @param wfnc the write function |
universe@140 | 104 | * @param n maximum number of bytes that shall be copied |
universe@140 | 105 | * @return total number of bytes copied |
universe@140 | 106 | */ |
universe@140 | 107 | #define ucx_stream_ncopy(src,dest,rfnc,wfnc, n) ucx_stream_copy(\ |
universe@140 | 108 | src, dest, (read_func)rfnc, (write_func)wfnc, NULL, 0x100, n) |
universe@140 | 109 | |
universe@94 | 110 | /** |
universe@89 | 111 | * Wraps the strcmp function. |
universe@89 | 112 | * @param s1 string one |
universe@89 | 113 | * @param s2 string two |
universe@89 | 114 | * @param data omitted |
universe@89 | 115 | * @return the result of strcmp(s1, s2) |
universe@89 | 116 | */ |
universe@89 | 117 | int ucx_strcmp(void *s1, void *s2, void *data); |
universe@89 | 118 | |
universe@89 | 119 | /** |
universe@89 | 120 | * Wraps the strncmp function. |
universe@89 | 121 | * @param s1 string one |
universe@89 | 122 | * @param s2 string two |
universe@89 | 123 | * @param n a pointer to the size_t containing the third strncmp parameter |
universe@89 | 124 | * @return the result of strncmp(s1, s2, *n) |
universe@89 | 125 | */ |
universe@89 | 126 | int ucx_strncmp(void *s1, void *s2, void *n); |
universe@89 | 127 | |
universe@89 | 128 | /** |
universe@89 | 129 | * Compares two integers of type int. |
universe@89 | 130 | * @param i1 pointer to integer one |
universe@89 | 131 | * @param i2 pointer to integer two |
universe@89 | 132 | * @param data omitted |
universe@89 | 133 | * @return -1, if *i1 is less than *i2, 0 if both are equal, |
universe@89 | 134 | * 1 if *i1 is greater than *i2 |
universe@89 | 135 | */ |
universe@89 | 136 | |
universe@89 | 137 | int ucx_intcmp(void *i1, void *i2, void *data); |
universe@89 | 138 | |
universe@89 | 139 | /** |
universe@92 | 140 | * Compares two real numbers of type float. |
universe@92 | 141 | * @param f1 pointer to float one |
universe@92 | 142 | * @param f2 pointer to float two |
universe@138 | 143 | * @param data if provided: a pointer to precision (default: 1e-6f) |
universe@92 | 144 | * @return -1, if *f1 is less than *f2, 0 if both are equal, |
universe@92 | 145 | * 1 if *f1 is greater than *f2 |
universe@92 | 146 | */ |
universe@92 | 147 | |
universe@92 | 148 | int ucx_floatcmp(void *f1, void *f2, void *data); |
universe@92 | 149 | |
universe@92 | 150 | /** |
universe@92 | 151 | * Compares two real numbers of type double. |
universe@138 | 152 | * @param d1 pointer to double one |
universe@138 | 153 | * @param d2 pointer to double two |
universe@138 | 154 | * @param data if provided: a pointer to precision (default: 1e-14) |
universe@92 | 155 | * @return -1, if *d1 is less than *d2, 0 if both are equal, |
universe@92 | 156 | * 1 if *d1 is greater than *d2 |
universe@92 | 157 | */ |
universe@92 | 158 | |
universe@92 | 159 | int ucx_doublecmp(void *d1, void *d2, void *data); |
universe@92 | 160 | |
universe@92 | 161 | /** |
universe@89 | 162 | * Compares two pointers. |
universe@89 | 163 | * @param ptr1 pointer one |
universe@89 | 164 | * @param ptr2 pointer two |
universe@89 | 165 | * @param data omitted |
universe@89 | 166 | * @return -1 if ptr1 is less than ptr2, 0 if both are equal, |
universe@89 | 167 | * 1 if ptr1 is greater than ptr2 |
universe@89 | 168 | */ |
universe@89 | 169 | int ucx_ptrcmp(void *ptr1, void *ptr2, void *data); |
universe@89 | 170 | |
universe@91 | 171 | /** |
universe@91 | 172 | * Compares two memory areas. |
universe@91 | 173 | * @param ptr1 pointer one |
universe@91 | 174 | * @param ptr2 pointer two |
universe@91 | 175 | * @param n a pointer to the size_t containing the third parameter for memcmp |
universe@91 | 176 | * @return the result of memcmp(ptr1, ptr2, *n) |
universe@91 | 177 | */ |
universe@91 | 178 | int ucx_memcmp(void *ptr1, void *ptr2, void *n); |
universe@91 | 179 | |
universe@89 | 180 | #ifdef __cplusplus |
universe@89 | 181 | } |
universe@89 | 182 | #endif |
universe@89 | 183 | |
olaf@120 | 184 | #endif /* UCX_UTILS_H */ |
universe@89 | 185 |