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 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 #include "ucx.h"
46 #include "string.h"
47 #include "allocator.h"
48 #include <inttypes.h>
49 #include <string.h>
50 #include <stdarg.h>
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_strcmp(const void *s1, const void *s2, void *data);
148 
156 int ucx_strncmp(const void *s1, const void *s2, void *n);
157 
166 int ucx_intcmp(const void *i1, const void *i2, void *data);
167 
177 int ucx_floatcmp(const void *f1, const void *f2, void *data);
178 
187 int ucx_doublecmp(const void *d1, const void *d2, void *data);
188 
197 int ucx_ptrcmp(const void *ptr1, const void *ptr2, void *data);
198 
206 int ucx_memcmp(const void *ptr1, const void *ptr2, void *n);
207 
217 int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...);
218 
228 int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap);
229 
247 sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...);
248 
258 sstr_t ucx_vasprintf(UcxAllocator *allocator, const char *fmt, va_list ap);
259 
261 #define ucx_sprintf(...) \
262  ucx_asprintf(ucx_default_allocator(), __VA_ARGS__)
263 
273 #define ucx_bprintf(buffer, ...) ucx_fprintf((UcxBuffer*)buffer, \
274  (write_func)ucx_buffer_write, __VA_ARGS__)
275 
276 #ifdef __cplusplus
277 }
278 #endif
279 
280 #endif /* UCX_UTILS_H */
281 
The UCX string structure.
Definition: string.h:68
Bounded string implementation.
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
Main UCX Header providing most common definitions.
int ucx_ptrcmp(const void *ptr1, const void *ptr2, void *data)
Compares two pointers.
Definition: utils.c:131
int ucx_doublecmp(const void *d1, const void *d2, void *data)
Compares two real numbers of type double.
Definition: utils.c:120
UCX allocator data structure containing memory management functions.
Definition: allocator.h:88
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:155
int ucx_memcmp(const void *ptr1, const void *ptr2, void *n)
Compares two memory areas.
Definition: utils.c:141
int ucx_strncmp(const void *s1, const void *s2, void *n)
Wraps the strncmp function.
Definition: utils.c:95
size_t(* read_func)(void *, size_t, size_t, void *)
Function pointer to a read function.
Definition: ucx.h:132
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:206
void * ucx_strcpy(const void *s, void *data)
Copies a string.
Definition: utils.c:37
int ucx_floatcmp(const void *f1, const void *f2, void *data)
Compares two real numbers of type float.
Definition: utils.c:109
int ucx_strcmp(const void *s1, const void *s2, void *data)
Wraps the strcmp function.
Definition: utils.c:91
Allocator for custom memory management.
size_t(* write_func)(const void *, size_t, size_t, void *)
Function pointer to a write function.
Definition: ucx.h:119
void * ucx_memcpy(const void *m, void *n)
Copies a memory area.
Definition: utils.c:45
int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap)
va_list version of ucx_fprintf().
Definition: utils.c:164
sstr_t ucx_vasprintf(UcxAllocator *allocator, const char *fmt, va_list ap)
va_list version of ucx_asprintf().
Definition: utils.c:215
int ucx_intcmp(const void *i1, const void *i2, void *data)
Compares two integers of type int.
Definition: utils.c:99