ucx
UAP Common Extensions
utils.h
Go to the documentation of this file.
1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3  *
4  * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
38 #ifndef UCX_UTILS_H
39 #define UCX_UTILS_H
40 
41 #include "ucx.h"
42 #include "string.h"
43 #include "allocator.h"
44 #include <inttypes.h>
45 #include <string.h>
46 #include <stdarg.h>
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
55 #define UCX_STREAM_COPY_BUFSIZE 4096
56 
63 void *ucx_strcpy(const void *s, void *data);
64 
72 void *ucx_memcpy(const void *m, void *n);
73 
74 
90 size_t ucx_stream_bncopy(void *src, void *dest, read_func rfnc, write_func wfnc,
91  char* buf, size_t bufsize, size_t n);
92 
104 #define ucx_stream_copy(src,dest,rfnc,wfnc) ucx_stream_bncopy(\
105  src, dest, (read_func)rfnc, (write_func)wfnc, \
106  NULL, UCX_STREAM_COPY_BUFSIZE, (size_t)-1)
107 
118 #define ucx_stream_ncopy(src,dest,rfnc,wfnc, n) ucx_stream_bncopy(\
119  src, dest, (read_func)rfnc, (write_func)wfnc, \
120  NULL, UCX_STREAM_COPY_BUFSIZE, n)
121 
136 #define ucx_stream_bcopy(src,dest,rfnc,wfnc, buf, bufsize) ucx_stream_bncopy(\
137  src, dest, (read_func)rfnc, (write_func)wfnc, \
138  buf, bufsize, (size_t)-1)
139 
147 int ucx_cmp_str(const void *s1, const void *s2, void *data);
148 
156 int ucx_cmp_strn(const void *s1, const void *s2, void *n);
157 
165 int ucx_cmp_sstr(const void *s1, const void *s2, void *data);
166 
175 int ucx_cmp_int(const void *i1, const void *i2, void *data);
176 
185 int ucx_cmp_longint(const void *i1, const void *i2, void *data);
186 
195 int ucx_cmp_longlong(const void *i1, const void *i2, void *data);
196 
205 int ucx_cmp_int16(const void *i1, const void *i2, void *data);
206 
215 int ucx_cmp_int32(const void *i1, const void *i2, void *data);
216 
225 int ucx_cmp_int64(const void *i1, const void *i2, void *data);
226 
235 int ucx_cmp_uint(const void *i1, const void *i2, void *data);
236 
245 int ucx_cmp_ulongint(const void *i1, const void *i2, void *data);
246 
255 int ucx_cmp_ulonglong(const void *i1, const void *i2, void *data);
256 
265 int ucx_cmp_uint16(const void *i1, const void *i2, void *data);
266 
275 int ucx_cmp_uint32(const void *i1, const void *i2, void *data);
276 
285 int ucx_cmp_uint64(const void *i1, const void *i2, void *data);
286 
294 intmax_t ucx_dist_int(const void *i1, const void *i2, void *data);
295 
303 intmax_t ucx_dist_longint(const void *i1, const void *i2, void *data);
304 
312 intmax_t ucx_dist_longlong(const void *i1, const void *i2, void *data);
313 
321 intmax_t ucx_dist_int16(const void *i1, const void *i2, void *data);
322 
330 intmax_t ucx_dist_int32(const void *i1, const void *i2, void *data);
331 
339 intmax_t ucx_dist_int64(const void *i1, const void *i2, void *data);
340 
348 intmax_t ucx_dist_uint(const void *i1, const void *i2, void *data);
349 
357 intmax_t ucx_dist_ulongint(const void *i1, const void *i2, void *data);
358 
366 intmax_t ucx_dist_ulonglong(const void *i1, const void *i2, void *data);
367 
375 intmax_t ucx_dist_uint16(const void *i1, const void *i2, void *data);
376 
384 intmax_t ucx_dist_uint32(const void *i1, const void *i2, void *data);
385 
393 intmax_t ucx_dist_uint64(const void *i1, const void *i2, void *data);
394 
404 int ucx_cmp_float(const void *f1, const void *f2, void *data);
405 
414 int ucx_cmp_double(const void *d1, const void *d2, void *data);
415 
424 int ucx_cmp_ptr(const void *ptr1, const void *ptr2, void *data);
425 
433 int ucx_cmp_mem(const void *ptr1, const void *ptr2, void *n);
434 
444 int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...);
445 
455 int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap);
456 
474 sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...);
475 
485 sstr_t ucx_vasprintf(UcxAllocator *allocator, const char *fmt, va_list ap);
486 
488 #define ucx_sprintf(...) \
489  ucx_asprintf(ucx_default_allocator(), __VA_ARGS__)
490 
500 #define ucx_bprintf(buffer, ...) ucx_fprintf((UcxBuffer*)buffer, \
501  (write_func)ucx_buffer_write, __VA_ARGS__)
502 
503 #ifdef __cplusplus
504 }
505 #endif
506 
507 #endif /* UCX_UTILS_H */
508 
int ucx_cmp_uint32(const void *i1, const void *i2, void *data)
Compares two integers of type uint32_t.
Definition: utils.c:205
The UCX string structure.
Definition: string.h:90
int ucx_cmp_longint(const void *i1, const void *i2, void *data)
Compares two integers of type long int.
Definition: utils.c:115
intmax_t ucx_dist_int(const void *i1, const void *i2, void *data)
Distance function for integers of type int.
Definition: utils.c:225
Bounded string implementation.
int ucx_cmp_ulonglong(const void *i1, const void *i2, void *data)
Compares two integers of type unsigned long long.
Definition: utils.c:185
size_t ucx_stream_bncopy(void *src, void *dest, read_func rfnc, write_func wfnc, char *buf, size_t bufsize, size_t n)
Reads data from a stream and writes it to another stream.
Definition: utils.c:52
int ucx_cmp_sstr(const void *s1, const void *s2, void *data)
Wraps the sstrcmp function.
Definition: utils.c:99
Main UCX Header providing most common definitions.
int ucx_cmp_float(const void *f1, const void *f2, void *data)
Compares two real numbers of type float.
Definition: utils.c:297
int ucx_cmp_longlong(const void *i1, const void *i2, void *data)
Compares two integers of type long long.
Definition: utils.c:125
int ucx_cmp_int32(const void *i1, const void *i2, void *data)
Compares two integers of type int32_t.
Definition: utils.c:145
int ucx_cmp_ulongint(const void *i1, const void *i2, void *data)
Compares two integers of type unsigned long int.
Definition: utils.c:175
int ucx_cmp_uint64(const void *i1, const void *i2, void *data)
Compares two integers of type uint64_t.
Definition: utils.c:215
intmax_t ucx_dist_longlong(const void *i1, const void *i2, void *data)
Distance function for integers of type long long.
Definition: utils.c:237
int ucx_cmp_strn(const void *s1, const void *s2, void *n)
Wraps the strncmp function.
Definition: utils.c:95
int ucx_cmp_str(const void *s1, const void *s2, void *data)
Wraps the strcmp function.
Definition: utils.c:91
int ucx_cmp_double(const void *d1, const void *d2, void *data)
Compares two real numbers of type double.
Definition: utils.c:308
intmax_t ucx_dist_uint64(const void *i1, const void *i2, void *data)
Distance function for integers of type uint64_t.
Definition: utils.c:291
intmax_t ucx_dist_uint32(const void *i1, const void *i2, void *data)
Distance function for integers of type uint32_t.
Definition: utils.c:285
UCX allocator data structure containing memory management functions.
Definition: allocator.h:88
int ucx_cmp_ptr(const void *ptr1, const void *ptr2, void *data)
Compares two pointers.
Definition: utils.c:319
int ucx_cmp_mem(const void *ptr1, const void *ptr2, void *n)
Compares two memory areas.
Definition: utils.c:329
intmax_t ucx_dist_ulonglong(const void *i1, const void *i2, void *data)
Distance function for integers of type unsigned long long.
Definition: utils.c:273
int ucx_fprintf(void *stream, write_func wfc, const char *fmt,...)
A printf() like function which writes the output to a stream by using a write_func().
Definition: utils.c:343
int ucx_cmp_uint16(const void *i1, const void *i2, void *data)
Compares two integers of type uint16_t.
Definition: utils.c:195
int ucx_cmp_int(const void *i1, const void *i2, void *data)
Compares two integers of type int.
Definition: utils.c:105
intmax_t ucx_dist_int64(const void *i1, const void *i2, void *data)
Distance function for integers of type int64_t.
Definition: utils.c:255
size_t(* read_func)(void *, size_t, size_t, void *)
Function pointer to a read function.
Definition: ucx.h:132
int ucx_cmp_uint(const void *i1, const void *i2, void *data)
Compares two integers of type unsigned int.
Definition: utils.c:165
sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt,...)
A printf() like function which allocates space for a sstr_t the result is written to...
Definition: utils.c:394
intmax_t ucx_dist_uint(const void *i1, const void *i2, void *data)
Distance function for integers of type unsigned int.
Definition: utils.c:261
intmax_t ucx_dist_int16(const void *i1, const void *i2, void *data)
Distance function for integers of type int16_t.
Definition: utils.c:243
void * ucx_strcpy(const void *s, void *data)
Copies a string.
Definition: utils.c:37
intmax_t ucx_dist_longint(const void *i1, const void *i2, void *data)
Distance function for integers of type long int.
Definition: utils.c:231
intmax_t ucx_dist_uint16(const void *i1, const void *i2, void *data)
Distance function for integers of type uint16_t.
Definition: utils.c:279
intmax_t ucx_dist_int32(const void *i1, const void *i2, void *data)
Distance function for integers of type int32_t.
Definition: utils.c:249
Allocator for custom memory management.
intmax_t ucx_dist_ulongint(const void *i1, const void *i2, void *data)
Distance function for integers of type unsigned long int.
Definition: utils.c:267
size_t(* write_func)(const void *, size_t, size_t, void *)
Function pointer to a write function.
Definition: ucx.h:119
int ucx_cmp_int16(const void *i1, const void *i2, void *data)
Compares two integers of type int16_t.
Definition: utils.c:135
void * ucx_memcpy(const void *m, void *n)
Copies a memory area.
Definition: utils.c:45
int ucx_cmp_int64(const void *i1, const void *i2, void *data)
Compares two integers of type int64_t.
Definition: utils.c:155
int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap)
va_list version of ucx_fprintf().
Definition: utils.c:352
sstr_t ucx_vasprintf(UcxAllocator *allocator, const char *fmt, va_list ap)
va_list version of ucx_asprintf().
Definition: utils.c:403