ucx
UAP Common Extensions
Macros | Functions
utils.h File Reference

Compare, copy and printf functions. More...

#include "ucx.h"
#include "string.h"
#include "allocator.h"
#include <inttypes.h>
#include <stdarg.h>

Go to the source code of this file.

Macros

#define UCX_STREAM_COPY_BUFSIZE   4096
 Default buffer size for ucx_stream_copy() and ucx_stream_ncopy().
 
#define ucx_stream_copy(src, dest, rfnc, wfnc)
 Shorthand for an unbounded ucx_stream_bncopy call using a default buffer. More...
 
#define ucx_stream_ncopy(src, dest, rfnc, wfnc, n)
 Shorthand for ucx_stream_bncopy using a default copy buffer. More...
 
#define ucx_stream_bcopy(src, dest, rfnc, wfnc, buf, bufsize)
 Shorthand for an unbounded ucx_stream_bncopy call using the specified buffer. More...
 
#define ucx_sprintf(...)   ucx_asprintf(ucx_default_allocator(), __VA_ARGS__)
 Shortcut for ucx_asprintf() with default allocator. More...
 
#define ucx_bprintf(buffer, ...)
 A printf() like function which writes the output to a UcxBuffer. More...
 

Functions

void * ucx_strcpy (const void *s, void *data)
 Copies a string. More...
 
void * ucx_memcpy (const void *m, void *n)
 Copies a memory area. More...
 
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. More...
 
int ucx_cmp_str (const void *s1, const void *s2, void *data)
 Wraps the strcmp function. More...
 
int ucx_cmp_strn (const void *s1, const void *s2, void *n)
 Wraps the strncmp function. More...
 
int ucx_cmp_sstr (const void *s1, const void *s2, void *data)
 Wraps the sstrcmp function. More...
 
int ucx_cmp_int (const void *i1, const void *i2, void *data)
 Compares two integers of type int. More...
 
int ucx_cmp_longint (const void *i1, const void *i2, void *data)
 Compares two integers of type long int. More...
 
int ucx_cmp_longlong (const void *i1, const void *i2, void *data)
 Compares two integers of type long long. More...
 
int ucx_cmp_int16 (const void *i1, const void *i2, void *data)
 Compares two integers of type int16_t. More...
 
int ucx_cmp_int32 (const void *i1, const void *i2, void *data)
 Compares two integers of type int32_t. More...
 
int ucx_cmp_int64 (const void *i1, const void *i2, void *data)
 Compares two integers of type int64_t. More...
 
int ucx_cmp_uint (const void *i1, const void *i2, void *data)
 Compares two integers of type unsigned int. More...
 
int ucx_cmp_ulongint (const void *i1, const void *i2, void *data)
 Compares two integers of type unsigned long int. More...
 
int ucx_cmp_ulonglong (const void *i1, const void *i2, void *data)
 Compares two integers of type unsigned long long. More...
 
int ucx_cmp_uint16 (const void *i1, const void *i2, void *data)
 Compares two integers of type uint16_t. More...
 
int ucx_cmp_uint32 (const void *i1, const void *i2, void *data)
 Compares two integers of type uint32_t. More...
 
int ucx_cmp_uint64 (const void *i1, const void *i2, void *data)
 Compares two integers of type uint64_t. More...
 
intmax_t ucx_dist_int (const void *i1, const void *i2, void *data)
 Distance function for integers of type int. More...
 
intmax_t ucx_dist_longint (const void *i1, const void *i2, void *data)
 Distance function for integers of type long int. More...
 
intmax_t ucx_dist_longlong (const void *i1, const void *i2, void *data)
 Distance function for integers of type long long. More...
 
intmax_t ucx_dist_int16 (const void *i1, const void *i2, void *data)
 Distance function for integers of type int16_t. More...
 
intmax_t ucx_dist_int32 (const void *i1, const void *i2, void *data)
 Distance function for integers of type int32_t. More...
 
intmax_t ucx_dist_int64 (const void *i1, const void *i2, void *data)
 Distance function for integers of type int64_t. More...
 
intmax_t ucx_dist_uint (const void *i1, const void *i2, void *data)
 Distance function for integers of type unsigned int. More...
 
intmax_t ucx_dist_ulongint (const void *i1, const void *i2, void *data)
 Distance function for integers of type unsigned long int. More...
 
intmax_t ucx_dist_ulonglong (const void *i1, const void *i2, void *data)
 Distance function for integers of type unsigned long long. More...
 
intmax_t ucx_dist_uint16 (const void *i1, const void *i2, void *data)
 Distance function for integers of type uint16_t. More...
 
intmax_t ucx_dist_uint32 (const void *i1, const void *i2, void *data)
 Distance function for integers of type uint32_t. More...
 
intmax_t ucx_dist_uint64 (const void *i1, const void *i2, void *data)
 Distance function for integers of type uint64_t. More...
 
int ucx_cmp_float (const void *f1, const void *f2, void *data)
 Compares two real numbers of type float. More...
 
int ucx_cmp_double (const void *d1, const void *d2, void *data)
 Compares two real numbers of type double. More...
 
int ucx_cmp_ptr (const void *ptr1, const void *ptr2, void *data)
 Compares two pointers. More...
 
int ucx_cmp_mem (const void *ptr1, const void *ptr2, void *n)
 Compares two memory areas. More...
 
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(). More...
 
int ucx_vfprintf (void *stream, write_func wfc, const char *fmt, va_list ap)
 va_list version of ucx_fprintf(). More...
 
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. More...
 
sstr_t ucx_vasprintf (UcxAllocator *allocator, const char *fmt, va_list ap)
 va_list version of ucx_asprintf(). More...
 

Detailed Description

Compare, copy and printf functions.

Author
Mike Becker
Olaf Wintermann

Macro Definition Documentation

◆ ucx_bprintf

#define ucx_bprintf (   buffer,
  ... 
)
Value:
size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems, UcxBuffer *buffer)
Writes data to a UcxBuffer.
Definition: buffer.c:152
UCX Buffer.
Definition: buffer.h:73
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
size_t(* write_func)(const void *, size_t, size_t, void *)
Function pointer to a write function.
Definition: ucx.h:119

A printf() like function which writes the output to a UcxBuffer.

Parameters
bufferthe buffer the data is written to
...format string and additional arguments
Returns
the total number of bytes written
See also
ucx_fprintf()

◆ ucx_sprintf

#define ucx_sprintf (   ...)    ucx_asprintf(ucx_default_allocator(), __VA_ARGS__)

Shortcut for ucx_asprintf() with default allocator.

◆ ucx_stream_bcopy

#define ucx_stream_bcopy (   src,
  dest,
  rfnc,
  wfnc,
  buf,
  bufsize 
)
Value:
src, dest, (read_func)rfnc, (write_func)wfnc, \
buf, bufsize, (size_t)-1)
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
size_t(* read_func)(void *, size_t, size_t, void *)
Function pointer to a read function.
Definition: ucx.h:132
size_t(* write_func)(const void *, size_t, size_t, void *)
Function pointer to a write function.
Definition: ucx.h:119

Shorthand for an unbounded ucx_stream_bncopy call using the specified buffer.

Parameters
srcthe source stream
destthe destination stream
rfncthe read function
wfncthe write function
bufa pointer to the copy buffer or NULL if a buffer shall be implicitly created on the heap
bufsizethe size of the copy buffer - if NULL was provided for buf, this is the size of the buffer that shall be implicitly created
Returns
total number of bytes copied

◆ ucx_stream_copy

#define ucx_stream_copy (   src,
  dest,
  rfnc,
  wfnc 
)
Value:
src, dest, (read_func)rfnc, (write_func)wfnc, \
NULL, UCX_STREAM_COPY_BUFSIZE, (size_t)-1)
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
#define UCX_STREAM_COPY_BUFSIZE
Default buffer size for ucx_stream_copy() and ucx_stream_ncopy().
Definition: utils.h:55
size_t(* read_func)(void *, size_t, size_t, void *)
Function pointer to a read function.
Definition: ucx.h:132
size_t(* write_func)(const void *, size_t, size_t, void *)
Function pointer to a write function.
Definition: ucx.h:119

Shorthand for an unbounded ucx_stream_bncopy call using a default buffer.

Parameters
srcthe source stream
destthe destination stream
rfncthe read function
wfncthe write function
Returns
total number of bytes copied
See also
UCX_STREAM_COPY_BUFSIZE

◆ ucx_stream_ncopy

#define ucx_stream_ncopy (   src,
  dest,
  rfnc,
  wfnc,
 
)
Value:
src, dest, (read_func)rfnc, (write_func)wfnc, \
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
#define UCX_STREAM_COPY_BUFSIZE
Default buffer size for ucx_stream_copy() and ucx_stream_ncopy().
Definition: utils.h:55
size_t(* read_func)(void *, size_t, size_t, void *)
Function pointer to a read function.
Definition: ucx.h:132
size_t(* write_func)(const void *, size_t, size_t, void *)
Function pointer to a write function.
Definition: ucx.h:119

Shorthand for ucx_stream_bncopy using a default copy buffer.

Parameters
srcthe source stream
destthe destination stream
rfncthe read function
wfncthe write function
nmaximum number of bytes that shall be copied
Returns
total number of bytes copied

Function Documentation

◆ ucx_asprintf()

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.

Attention: The sstr_t data is allocated with the allocators ucx_allocator_malloc() function. So it is implementation dependent, if the returned sstr_t.ptr pointer must be passed to the allocators ucx_allocator_free() function manually.

Note: The sstr_t.ptr of the return value will always be NULL-terminated.

Parameters
allocatorthe UcxAllocator used for allocating the result sstr_t
fmtformat string
...additional arguments
Returns
a sstr_t containing the formatted string

◆ ucx_cmp_double()

int ucx_cmp_double ( const void *  d1,
const void *  d2,
void *  data 
)

Compares two real numbers of type double.

Parameters
d1pointer to double one
d2pointer to double two
dataif provided: a pointer to precision (default: 1e-14)
Returns
-1, if *d1 is less than *d2, 0 if both are equal, 1 if *d1 is greater than *d2

◆ ucx_cmp_float()

int ucx_cmp_float ( const void *  f1,
const void *  f2,
void *  data 
)

Compares two real numbers of type float.

Parameters
f1pointer to float one
f2pointer to float two
dataif provided: a pointer to precision (default: 1e-6f)
Returns
-1, if *f1 is less than *f2, 0 if both are equal, 1 if *f1 is greater than *f2

◆ ucx_cmp_int()

int ucx_cmp_int ( const void *  i1,
const void *  i2,
void *  data 
)

Compares two integers of type int.

Parameters
i1pointer to integer one
i2pointer to integer two
dataomitted
Returns
-1, if *i1 is less than *i2, 0 if both are equal, 1 if *i1 is greater than *i2

◆ ucx_cmp_int16()

int ucx_cmp_int16 ( const void *  i1,
const void *  i2,
void *  data 
)

Compares two integers of type int16_t.

Parameters
i1pointer to int16_t one
i2pointer to int16_t two
dataomitted
Returns
-1, if *i1 is less than *i2, 0 if both are equal, 1 if *i1 is greater than *i2

◆ ucx_cmp_int32()

int ucx_cmp_int32 ( const void *  i1,
const void *  i2,
void *  data 
)

Compares two integers of type int32_t.

Parameters
i1pointer to int32_t one
i2pointer to int32_t two
dataomitted
Returns
-1, if *i1 is less than *i2, 0 if both are equal, 1 if *i1 is greater than *i2

◆ ucx_cmp_int64()

int ucx_cmp_int64 ( const void *  i1,
const void *  i2,
void *  data 
)

Compares two integers of type int64_t.

Parameters
i1pointer to int64_t one
i2pointer to int64_t two
dataomitted
Returns
-1, if *i1 is less than *i2, 0 if both are equal, 1 if *i1 is greater than *i2

◆ ucx_cmp_longint()

int ucx_cmp_longint ( const void *  i1,
const void *  i2,
void *  data 
)

Compares two integers of type long int.

Parameters
i1pointer to long integer one
i2pointer to long integer two
dataomitted
Returns
-1, if *i1 is less than *i2, 0 if both are equal, 1 if *i1 is greater than *i2

◆ ucx_cmp_longlong()

int ucx_cmp_longlong ( const void *  i1,
const void *  i2,
void *  data 
)

Compares two integers of type long long.

Parameters
i1pointer to long long one
i2pointer to long long two
dataomitted
Returns
-1, if *i1 is less than *i2, 0 if both are equal, 1 if *i1 is greater than *i2

◆ ucx_cmp_mem()

int ucx_cmp_mem ( const void *  ptr1,
const void *  ptr2,
void *  n 
)

Compares two memory areas.

Parameters
ptr1pointer one
ptr2pointer two
na pointer to the size_t containing the third parameter for memcmp
Returns
the result of memcmp(ptr1, ptr2, *n)

◆ ucx_cmp_ptr()

int ucx_cmp_ptr ( const void *  ptr1,
const void *  ptr2,
void *  data 
)

Compares two pointers.

Parameters
ptr1pointer one
ptr2pointer two
dataomitted
Returns
-1 if ptr1 is less than ptr2, 0 if both are equal, 1 if ptr1 is greater than ptr2

◆ ucx_cmp_sstr()

int ucx_cmp_sstr ( const void *  s1,
const void *  s2,
void *  data 
)

Wraps the sstrcmp function.

Parameters
s1sstr one
s2sstr two
dataignored
Returns
the result of sstrcmp(s1, s2)

◆ ucx_cmp_str()

int ucx_cmp_str ( const void *  s1,
const void *  s2,
void *  data 
)

Wraps the strcmp function.

Parameters
s1string one
s2string two
dataomitted
Returns
the result of strcmp(s1, s2)

◆ ucx_cmp_strn()

int ucx_cmp_strn ( const void *  s1,
const void *  s2,
void *  n 
)

Wraps the strncmp function.

Parameters
s1string one
s2string two
na pointer to the size_t containing the third strncmp parameter
Returns
the result of strncmp(s1, s2, *n)

◆ ucx_cmp_uint()

int ucx_cmp_uint ( const void *  i1,
const void *  i2,
void *  data 
)

Compares two integers of type unsigned int.

Parameters
i1pointer to unsigned integer one
i2pointer to unsigned integer two
dataomitted
Returns
-1, if *i1 is less than *i2, 0 if both are equal, 1 if *i1 is greater than *i2

◆ ucx_cmp_uint16()

int ucx_cmp_uint16 ( const void *  i1,
const void *  i2,
void *  data 
)

Compares two integers of type uint16_t.

Parameters
i1pointer to uint16_t one
i2pointer to uint16_t two
dataomitted
Returns
-1, if *i1 is less than *i2, 0 if both are equal, 1 if *i1 is greater than *i2

◆ ucx_cmp_uint32()

int ucx_cmp_uint32 ( const void *  i1,
const void *  i2,
void *  data 
)

Compares two integers of type uint32_t.

Parameters
i1pointer to uint32_t one
i2pointer to uint32_t two
dataomitted
Returns
-1, if *i1 is less than *i2, 0 if both are equal, 1 if *i1 is greater than *i2

◆ ucx_cmp_uint64()

int ucx_cmp_uint64 ( const void *  i1,
const void *  i2,
void *  data 
)

Compares two integers of type uint64_t.

Parameters
i1pointer to uint64_t one
i2pointer to uint64_t two
dataomitted
Returns
-1, if *i1 is less than *i2, 0 if both are equal, 1 if *i1 is greater than *i2

◆ ucx_cmp_ulongint()

int ucx_cmp_ulongint ( const void *  i1,
const void *  i2,
void *  data 
)

Compares two integers of type unsigned long int.

Parameters
i1pointer to unsigned long integer one
i2pointer to unsigned long integer two
dataomitted
Returns
-1, if *i1 is less than *i2, 0 if both are equal, 1 if *i1 is greater than *i2

◆ ucx_cmp_ulonglong()

int ucx_cmp_ulonglong ( const void *  i1,
const void *  i2,
void *  data 
)

Compares two integers of type unsigned long long.

Parameters
i1pointer to unsigned long long one
i2pointer to unsigned long long two
dataomitted
Returns
-1, if *i1 is less than *i2, 0 if both are equal, 1 if *i1 is greater than *i2

◆ ucx_dist_int()

intmax_t ucx_dist_int ( const void *  i1,
const void *  i2,
void *  data 
)

Distance function for integers of type int.

Parameters
i1pointer to integer one
i2pointer to integer two
dataomitted
Returns
i1 minus i2

◆ ucx_dist_int16()

intmax_t ucx_dist_int16 ( const void *  i1,
const void *  i2,
void *  data 
)

Distance function for integers of type int16_t.

Parameters
i1pointer to int16_t one
i2pointer to int16_t two
dataomitted
Returns
i1 minus i2

◆ ucx_dist_int32()

intmax_t ucx_dist_int32 ( const void *  i1,
const void *  i2,
void *  data 
)

Distance function for integers of type int32_t.

Parameters
i1pointer to int32_t one
i2pointer to int32_t two
dataomitted
Returns
i1 minus i2

◆ ucx_dist_int64()

intmax_t ucx_dist_int64 ( const void *  i1,
const void *  i2,
void *  data 
)

Distance function for integers of type int64_t.

Parameters
i1pointer to int64_t one
i2pointer to int64_t two
dataomitted
Returns
i1 minus i2

◆ ucx_dist_longint()

intmax_t ucx_dist_longint ( const void *  i1,
const void *  i2,
void *  data 
)

Distance function for integers of type long int.

Parameters
i1pointer to long integer one
i2pointer to long integer two
dataomitted
Returns
i1 minus i2

◆ ucx_dist_longlong()

intmax_t ucx_dist_longlong ( const void *  i1,
const void *  i2,
void *  data 
)

Distance function for integers of type long long.

Parameters
i1pointer to long long one
i2pointer to long long two
dataomitted
Returns
i1 minus i2

◆ ucx_dist_uint()

intmax_t ucx_dist_uint ( const void *  i1,
const void *  i2,
void *  data 
)

Distance function for integers of type unsigned int.

Parameters
i1pointer to unsigned integer one
i2pointer to unsigned integer two
dataomitted
Returns
i1 minus i2

◆ ucx_dist_uint16()

intmax_t ucx_dist_uint16 ( const void *  i1,
const void *  i2,
void *  data 
)

Distance function for integers of type uint16_t.

Parameters
i1pointer to uint16_t one
i2pointer to uint16_t two
dataomitted
Returns
i1 minus i2

◆ ucx_dist_uint32()

intmax_t ucx_dist_uint32 ( const void *  i1,
const void *  i2,
void *  data 
)

Distance function for integers of type uint32_t.

Parameters
i1pointer to uint32_t one
i2pointer to uint32_t two
dataomitted
Returns
i1 minus i2

◆ ucx_dist_uint64()

intmax_t ucx_dist_uint64 ( const void *  i1,
const void *  i2,
void *  data 
)

Distance function for integers of type uint64_t.

Parameters
i1pointer to uint64_t one
i2pointer to uint64_t two
dataomitted
Returns
i1 minus i2

◆ ucx_dist_ulongint()

intmax_t ucx_dist_ulongint ( const void *  i1,
const void *  i2,
void *  data 
)

Distance function for integers of type unsigned long int.

Parameters
i1pointer to unsigned long integer one
i2pointer to unsigned long integer two
dataomitted
Returns
i1 minus i2

◆ ucx_dist_ulonglong()

intmax_t ucx_dist_ulonglong ( const void *  i1,
const void *  i2,
void *  data 
)

Distance function for integers of type unsigned long long.

Parameters
i1pointer to unsigned long long one
i2pointer to unsigned long long two
dataomitted
Returns
i1 minus i2

◆ ucx_fprintf()

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().

Parameters
streamthe stream the data is written to
wfcthe write function
fmtformat string
...additional arguments
Returns
the total number of bytes written

◆ ucx_memcpy()

void* ucx_memcpy ( const void *  m,
void *  n 
)

Copies a memory area.

Parameters
ma pointer to the memory area
na pointer to the size_t containing the size of the memory area
Returns
a pointer to a copy of the specified memory area that can be passed to free(void*)

◆ ucx_strcpy()

void* ucx_strcpy ( const void *  s,
void *  data 
)

Copies a string.

Parameters
sthe string to copy
dataomitted
Returns
a pointer to a copy of s1 that can be passed to free(void*)

◆ ucx_stream_bncopy()

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.

Parameters
srcthe source stream
destthe destination stream
rfncthe read function
wfncthe write function
bufa pointer to the copy buffer or NULL if a buffer shall be implicitly created on the heap
bufsizethe size of the copy buffer - if NULL was provided for buf, this is the size of the buffer that shall be implicitly created
nthe maximum number of bytes that shall be copied
Returns
the total number of bytes copied

◆ ucx_vasprintf()

sstr_t ucx_vasprintf ( UcxAllocator allocator,
const char *  fmt,
va_list  ap 
)

va_list version of ucx_asprintf().

Parameters
allocatorthe UcxAllocator used for allocating the result sstr_t
fmtformat string
apargument list
Returns
a sstr_t containing the formatted string
See also
ucx_asprintf()

◆ ucx_vfprintf()

int ucx_vfprintf ( void *  stream,
write_func  wfc,
const char *  fmt,
va_list  ap 
)

va_list version of ucx_fprintf().

Parameters
streamthe stream the data is written to
wfcthe write function
fmtformat string
apargument list
Returns
the total number of bytes written
See also
ucx_fprintf()