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_strcmp (const void *s1, const void *s2, void *data)
 Wraps the strcmp function. More...
 
int ucx_strncmp (const void *s1, const void *s2, void *n)
 Wraps the strncmp function. More...
 
int ucx_intcmp (const void *i1, const void *i2, void *data)
 Compares two integers of type int. More...
 
int ucx_floatcmp (const void *f1, const void *f2, void *data)
 Compares two real numbers of type float. More...
 
int ucx_doublecmp (const void *d1, const void *d2, void *data)
 Compares two real numbers of type double. More...
 
int ucx_ptrcmp (const void *ptr1, const void *ptr2, void *data)
 Compares two pointers. More...
 
int ucx_memcmp (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

#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:155
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()
#define ucx_sprintf (   ...)    ucx_asprintf(ucx_default_allocator(), __VA_ARGS__)

Shortcut for ucx_asprintf() with default allocator.

#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
#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
#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

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
int ucx_doublecmp ( 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
int ucx_floatcmp ( 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
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
int ucx_intcmp ( 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
int ucx_memcmp ( 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)
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*)
int ucx_ptrcmp ( 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
int ucx_strcmp ( 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)
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*)
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
int ucx_strncmp ( 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)
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()
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()