Wed, 14 Aug 2013 15:22:35 +0200
added printf 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@94 | 29 | #include "utils.h" |
universe@140 | 30 | #include <math.h> |
olaf@142 | 31 | #include <stdio.h> |
olaf@142 | 32 | #include <limits.h> |
olaf@142 | 33 | #include <errno.h> |
universe@89 | 34 | |
universe@94 | 35 | /* COPY FUCNTIONS */ |
universe@94 | 36 | void* ucx_strcpy(void* s, void* data) { |
universe@94 | 37 | char *str = (char*) s; |
universe@94 | 38 | size_t n = 1+strlen(str); |
universe@94 | 39 | char *cpy = (char*) malloc(n); |
universe@94 | 40 | memcpy(cpy, str, n); |
universe@94 | 41 | return cpy; |
universe@94 | 42 | } |
universe@94 | 43 | |
universe@94 | 44 | void* ucx_memcpy(void* m, void* n) { |
universe@94 | 45 | size_t k = *((size_t*)n); |
universe@94 | 46 | void *cpy = malloc(k); |
universe@94 | 47 | memcpy(cpy, m, k); |
universe@94 | 48 | return cpy; |
universe@94 | 49 | } |
universe@94 | 50 | |
universe@140 | 51 | size_t ucx_stream_copy(void *src, void *dest, read_func readfnc, |
universe@140 | 52 | write_func writefnc, char* buf, size_t bufsize, size_t n) { |
universe@140 | 53 | if(n == 0 || bufsize == 0) { |
universe@140 | 54 | return 0; |
universe@140 | 55 | } |
universe@140 | 56 | |
universe@140 | 57 | size_t ncp = 0; |
universe@140 | 58 | if (!buf) { |
universe@140 | 59 | buf = (char*)malloc(bufsize); |
universe@140 | 60 | if(buf == NULL) { |
universe@140 | 61 | return 0; |
universe@140 | 62 | } |
universe@140 | 63 | } |
universe@140 | 64 | |
universe@140 | 65 | size_t r; |
universe@140 | 66 | size_t rn = bufsize > n ? n : bufsize; |
universe@140 | 67 | while((r = readfnc(buf, 1, rn, src)) != 0) { |
universe@140 | 68 | r = writefnc(buf, 1, r, dest); |
universe@140 | 69 | ncp += r; |
universe@140 | 70 | n -= r; |
universe@140 | 71 | rn = bufsize > n ? n : bufsize; |
universe@140 | 72 | if(r == 0 || n == 0) { |
universe@140 | 73 | break; |
universe@140 | 74 | } |
universe@140 | 75 | } |
universe@140 | 76 | |
universe@140 | 77 | free(buf); |
universe@140 | 78 | return ncp; |
universe@140 | 79 | } |
universe@140 | 80 | |
olaf@142 | 81 | /* COMPARE FUNCTIONS */ |
universe@94 | 82 | |
universe@89 | 83 | int ucx_strcmp(void *s1, void *s2, void *data) { |
universe@89 | 84 | return strcmp((char*)s1, (char*)s2); |
universe@89 | 85 | } |
universe@89 | 86 | |
universe@89 | 87 | int ucx_strncmp(void *s1, void *s2, void *n) { |
universe@89 | 88 | return strncmp((char*)s1, (char*)s2, *((size_t*) n)); |
universe@89 | 89 | } |
universe@89 | 90 | |
universe@89 | 91 | int ucx_intcmp(void *i1, void *i2, void *data) { |
universe@89 | 92 | int a = *((int*) i1); |
universe@89 | 93 | int b = *((int*) i2); |
universe@89 | 94 | if (a == b) { |
universe@89 | 95 | return 0; |
universe@89 | 96 | } else { |
universe@89 | 97 | return a < b ? -1 : 1; |
universe@89 | 98 | } |
universe@89 | 99 | } |
universe@89 | 100 | |
universe@92 | 101 | int ucx_floatcmp(void *f1, void *f2, void *epsilon) { |
universe@92 | 102 | float a = *((float*) f1); |
universe@92 | 103 | float b = *((float*) f2); |
universe@92 | 104 | float e = !epsilon ? 1e-6f : *((float*)epsilon); |
universe@92 | 105 | if (fabsf(a - b) < e) { |
universe@92 | 106 | return 0; |
universe@92 | 107 | } else { |
universe@92 | 108 | return a < b ? -1 : 1; |
universe@92 | 109 | } |
universe@92 | 110 | } |
universe@92 | 111 | |
universe@92 | 112 | int ucx_doublecmp(void *d1, void *d2, void *epsilon) { |
universe@92 | 113 | double a = *((float*) d1); |
universe@92 | 114 | double b = *((float*) d2); |
universe@92 | 115 | double e = !epsilon ? 1e-14 : *((double*)epsilon); |
universe@92 | 116 | if (fabs(a - b) < e) { |
universe@92 | 117 | return 0; |
universe@92 | 118 | } else { |
universe@92 | 119 | return a < b ? -1 : 1; |
universe@92 | 120 | } |
universe@92 | 121 | } |
universe@92 | 122 | |
universe@89 | 123 | int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) { |
universe@89 | 124 | if (ptr1 == ptr2) { |
universe@89 | 125 | return 0; |
universe@89 | 126 | } else { |
universe@89 | 127 | return ptr1 < ptr2 ? -1 : 1; |
universe@89 | 128 | } |
universe@89 | 129 | } |
universe@91 | 130 | |
universe@91 | 131 | int ucx_memcmp(void *ptr1, void *ptr2, void *n) { |
universe@91 | 132 | return memcmp(ptr1, ptr2, *((size_t*)n)); |
universe@91 | 133 | } |
olaf@142 | 134 | |
olaf@142 | 135 | /* PRINTF FUNCTIONS */ |
olaf@142 | 136 | |
olaf@142 | 137 | #define UCX_PRINTF_BUFSIZE 256 |
olaf@142 | 138 | |
olaf@142 | 139 | int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...) { |
olaf@142 | 140 | va_list ap; |
olaf@142 | 141 | int ret; |
olaf@142 | 142 | va_start(ap, fmt); |
olaf@142 | 143 | ret = ucx_vfprintf(stream, wfc, fmt, ap); |
olaf@142 | 144 | va_end(ap); |
olaf@142 | 145 | return ret; |
olaf@142 | 146 | } |
olaf@142 | 147 | |
olaf@142 | 148 | int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap) { |
olaf@142 | 149 | char buf[UCX_PRINTF_BUFSIZE]; |
olaf@142 | 150 | int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); |
olaf@142 | 151 | if (ret < 0) { |
olaf@142 | 152 | return ret; |
olaf@142 | 153 | } else if (ret < UCX_PRINTF_BUFSIZE) { |
olaf@142 | 154 | return (int)wfc(buf, 1, ret, stream); |
olaf@142 | 155 | } else { |
olaf@142 | 156 | if (ret == INT_MAX) { |
olaf@142 | 157 | errno = ENOMEM; |
olaf@142 | 158 | return -1; |
olaf@142 | 159 | } |
olaf@142 | 160 | |
olaf@142 | 161 | int len = ret + 1; |
olaf@142 | 162 | char *newbuf = (char*)malloc(len); |
olaf@142 | 163 | if (!newbuf) { |
olaf@142 | 164 | return -1; |
olaf@142 | 165 | } |
olaf@142 | 166 | |
olaf@142 | 167 | ret = vsnprintf(newbuf, len, fmt, ap); |
olaf@142 | 168 | if (ret > 0) { |
olaf@142 | 169 | ret = (int)wfc(newbuf, 1, ret, stream); |
olaf@142 | 170 | } |
olaf@142 | 171 | free(newbuf); |
olaf@142 | 172 | } |
olaf@142 | 173 | return ret; |
olaf@142 | 174 | } |
olaf@142 | 175 | |
olaf@142 | 176 | sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...) { |
olaf@142 | 177 | va_list ap; |
olaf@142 | 178 | sstr_t ret; |
olaf@142 | 179 | va_start(ap, fmt); |
olaf@142 | 180 | ret = ucx_vasprintf(allocator, fmt, ap); |
olaf@142 | 181 | va_end(ap); |
olaf@142 | 182 | return ret; |
olaf@142 | 183 | } |
olaf@142 | 184 | |
olaf@142 | 185 | sstr_t ucx_vasprintf(UcxAllocator *a, const char *fmt, va_list ap) { |
olaf@142 | 186 | sstr_t s; |
olaf@142 | 187 | s.ptr = NULL; |
olaf@142 | 188 | s.length = 0; |
olaf@142 | 189 | char buf[UCX_PRINTF_BUFSIZE]; |
olaf@142 | 190 | int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); |
olaf@142 | 191 | if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) { |
olaf@142 | 192 | s.ptr = (char*)a->malloc(a->pool, ret + 1); |
olaf@142 | 193 | s.length = (size_t)ret; |
olaf@142 | 194 | memcpy(s.ptr, buf, ret); |
olaf@142 | 195 | s.ptr[s.length] = '\0'; |
olaf@142 | 196 | } else if (ret == INT_MAX) { |
olaf@142 | 197 | errno = ENOMEM; |
olaf@142 | 198 | } else { |
olaf@142 | 199 | int len = ret + 1; |
olaf@142 | 200 | s.ptr = (char*)a->malloc(a->pool, len); |
olaf@142 | 201 | ret = vsnprintf(s.ptr, len, fmt, ap); |
olaf@142 | 202 | if (ret < 0) { |
olaf@142 | 203 | free(s.ptr); |
olaf@142 | 204 | s.ptr = NULL; |
olaf@142 | 205 | } else { |
olaf@142 | 206 | s.length = (size_t)ret; |
olaf@142 | 207 | } |
olaf@142 | 208 | } |
olaf@142 | 209 | return s; |
olaf@142 | 210 | } |