ucx/utils.c

changeset 152
62921b370c60
parent 124
80609f9675f1
child 157
0b33b9396851
equal deleted inserted replaced
151:11f3bb408051 152:62921b370c60
1 /* 1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * 3 *
4 * Copyright 2015 Olaf Wintermann. All rights reserved. 4 * Copyright 2016 Olaf Wintermann. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met: 7 * modification, are permitted provided that the following conditions are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
31 #include <stdio.h> 31 #include <stdio.h>
32 #include <limits.h> 32 #include <limits.h>
33 #include <errno.h> 33 #include <errno.h>
34 34
35 /* COPY FUCNTIONS */ 35 /* COPY FUCNTIONS */
36 void* ucx_strcpy(void* s, void* data) { 36 void* ucx_strcpy(const void* s, void* data) {
37 char *str = (char*) s; 37 const char *str = (const char*) s;
38 size_t n = 1+strlen(str); 38 size_t n = 1+strlen(str);
39 char *cpy = (char*) malloc(n); 39 char *cpy = (char*) malloc(n);
40 memcpy(cpy, str, n); 40 memcpy(cpy, str, n);
41 return cpy; 41 return cpy;
42 } 42 }
43 43
44 void* ucx_memcpy(void* m, void* n) { 44 void* ucx_memcpy(const void* m, void* n) {
45 size_t k = *((size_t*)n); 45 size_t k = *((size_t*)n);
46 void *cpy = malloc(k); 46 void *cpy = malloc(k);
47 memcpy(cpy, m, k); 47 memcpy(cpy, m, k);
48 return cpy; 48 return cpy;
49 } 49 }
50 50
51 size_t ucx_stream_copy(void *src, void *dest, read_func readfnc, 51 size_t ucx_stream_bncopy(void *src, void *dest, read_func readfnc,
52 write_func writefnc, char* buf, size_t bufsize, size_t n) { 52 write_func writefnc, char* buf, size_t bufsize, size_t n) {
53 if(n == 0 || bufsize == 0) { 53 if(n == 0 || bufsize == 0) {
54 return 0; 54 return 0;
55 } 55 }
56 56
85 return ncp; 85 return ncp;
86 } 86 }
87 87
88 /* COMPARE FUNCTIONS */ 88 /* COMPARE FUNCTIONS */
89 89
90 int ucx_strcmp(void *s1, void *s2, void *data) { 90 int ucx_strcmp(const void *s1, const void *s2, void *data) {
91 return strcmp((char*)s1, (char*)s2); 91 return strcmp((const char*)s1, (const char*)s2);
92 } 92 }
93 93
94 int ucx_strncmp(void *s1, void *s2, void *n) { 94 int ucx_strncmp(const void *s1, const void *s2, void *n) {
95 return strncmp((char*)s1, (char*)s2, *((size_t*) n)); 95 return strncmp((const char*)s1, (const char*)s2, *((size_t*) n));
96 } 96 }
97 97
98 int ucx_intcmp(void *i1, void *i2, void *data) { 98 int ucx_intcmp(const void *i1, const void *i2, void *data) {
99 int a = *((int*) i1); 99 int a = *((const int*) i1);
100 int b = *((int*) i2); 100 int b = *((const int*) i2);
101 if (a == b) { 101 if (a == b) {
102 return 0; 102 return 0;
103 } else { 103 } else {
104 return a < b ? -1 : 1; 104 return a < b ? -1 : 1;
105 } 105 }
106 } 106 }
107 107
108 int ucx_floatcmp(void *f1, void *f2, void *epsilon) { 108 int ucx_floatcmp(const void *f1, const void *f2, void *epsilon) {
109 float a = *((float*) f1); 109 float a = *((const float*) f1);
110 float b = *((float*) f2); 110 float b = *((const float*) f2);
111 float e = !epsilon ? 1e-6f : *((float*)epsilon); 111 float e = !epsilon ? 1e-6f : *((float*)epsilon);
112 if (fabsf(a - b) < e) { 112 if (fabsf(a - b) < e) {
113 return 0; 113 return 0;
114 } else { 114 } else {
115 return a < b ? -1 : 1; 115 return a < b ? -1 : 1;
116 } 116 }
117 } 117 }
118 118
119 int ucx_doublecmp(void *d1, void *d2, void *epsilon) { 119 int ucx_doublecmp(const void *d1, const void *d2, void *epsilon) {
120 double a = *((float*) d1); 120 double a = *((const double*) d1);
121 double b = *((float*) d2); 121 double b = *((const double*) d2);
122 double e = !epsilon ? 1e-14 : *((double*)epsilon); 122 double e = !epsilon ? 1e-14 : *((double*)epsilon);
123 if (fabs(a - b) < e) { 123 if (fabs(a - b) < e) {
124 return 0; 124 return 0;
125 } else { 125 } else {
126 return a < b ? -1 : 1; 126 return a < b ? -1 : 1;
127 } 127 }
128 } 128 }
129 129
130 int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) { 130 int ucx_ptrcmp(const void *ptr1, const void *ptr2, void *data) {
131 intptr_t p1 = (intptr_t) ptr1; 131 const intptr_t p1 = (const intptr_t) ptr1;
132 intptr_t p2 = (intptr_t) ptr2; 132 const intptr_t p2 = (const intptr_t) ptr2;
133 if (p1 == p2) { 133 if (p1 == p2) {
134 return 0; 134 return 0;
135 } else { 135 } else {
136 return p1 < p2 ? -1 : 1; 136 return p1 < p2 ? -1 : 1;
137 } 137 }
138 } 138 }
139 139
140 int ucx_memcmp(void *ptr1, void *ptr2, void *n) { 140 int ucx_memcmp(const void *ptr1, const void *ptr2, void *n) {
141 return memcmp(ptr1, ptr2, *((size_t*)n)); 141 return memcmp(ptr1, ptr2, *((size_t*)n));
142 } 142 }
143 143
144 /* PRINTF FUNCTIONS */ 144 /* PRINTF FUNCTIONS */
145 145

mercurial