ucx
UAP Common Extensions
Loading...
Searching...
No Matches
Macros | Functions | Variables
printf.h File Reference

Wrapper for write functions with a printf-like interface. More...

#include "common.h"
#include "string.h"
#include <stdarg.h>

Go to the source code of this file.

Macros

#define cx_attr_printf(fmt_idx, arg_idx)    __attribute__((__format__(printf, fmt_idx, arg_idx)))
 Attribute for printf-like functions.
 
#define cx_asprintf(fmt, ...)    cx_asprintf_a(cxDefaultAllocator, fmt, __VA_ARGS__)
 A asprintf like function which allocates space for a string the result is written to.
 
#define cx_vasprintf(fmt, ap)   cx_vasprintf_a(cxDefaultAllocator, fmt, ap)
 A vasprintf like function which allocates space for a string the result is written to.
 
#define cx_bprintf(buffer, fmt, ...)
 A printf like function which writes the output to a CxBuffer.
 
#define cx_sprintf(str, len, fmt, ...)   cx_sprintf_a(cxDefaultAllocator, str, len, fmt, __VA_ARGS__)
 An sprintf like function which reallocates the string when the buffer is not large enough.
 
#define cx_vsprintf(str, len, fmt, ap)   cx_vsprintf_a(cxDefaultAllocator, str, len, fmt, ap)
 An sprintf like function which reallocates the string when the buffer is not large enough.
 
#define cx_sprintf_s(buf, len, str, fmt, ...)   cx_sprintf_sa(cxDefaultAllocator, buf, len, str, fmt, __VA_ARGS__)
 An sprintf like function which allocates a new string when the buffer is not large enough.
 
#define cx_vsprintf_s(buf, len, str, fmt, ap)   cx_vsprintf_sa(cxDefaultAllocator, buf, len, str, fmt, ap)
 An sprintf like function which allocates a new string when the buffer is not large enough.
 

Functions

int cx_fprintf (void *stream, cx_write_func wfc, const char *fmt,...)
 A fprintf like function which writes the output to a stream by using a write_func.
 
int cx_vfprintf (void *stream, cx_write_func wfc, const char *fmt, va_list ap)
 A vfprintf like function which writes the output to a stream by using a write_func.
 
cxmutstr cx_asprintf_a (const CxAllocator *allocator, const char *fmt,...)
 A asprintf like function which allocates space for a string the result is written to.
 
cxmutstr cx_vasprintf_a (const CxAllocator *allocator, const char *fmt, va_list ap)
 A vasprintf like function which allocates space for a string the result is written to.
 
int cx_sprintf_a (CxAllocator *alloc, char **str, size_t *len, const char *fmt,...)
 An sprintf like function which reallocates the string when the buffer is not large enough.
 
int cx_vsprintf_a (CxAllocator *alloc, char **str, size_t *len, const char *fmt, va_list ap)
 An sprintf like function which reallocates the string when the buffer is not large enough.
 
int cx_sprintf_sa (CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt,...)
 An sprintf like function which allocates a new string when the buffer is not large enough.
 
int cx_vsprintf_sa (CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt, va_list ap)
 An sprintf like function which allocates a new string when the buffer is not large enough.
 

Variables

const unsigned cx_printf_sbo_size
 The maximum string length that fits into stack memory.
 

Detailed Description

Wrapper for write functions with a printf-like interface.

Author
Mike Becker
Olaf Wintermann

Macro Definition Documentation

◆ cx_asprintf

#define cx_asprintf ( fmt,
... )    cx_asprintf_a(cxDefaultAllocator, fmt, __VA_ARGS__)

A asprintf like function which allocates space for a string the result is written to.

Note
The resulting string is guaranteed to be zero-terminated, unless there was an error, in which case the string's pointer will be NULL.
Parameters
fmt(char*) format string
...additional arguments
Returns
(cxmutstr) the formatted string
See also
cx_strfree()

◆ cx_attr_printf

#define cx_attr_printf ( fmt_idx,
arg_idx )    __attribute__((__format__(printf, fmt_idx, arg_idx)))

Attribute for printf-like functions.

Parameters
fmt_idxindex of the format string parameter
arg_idxindex of the first formatting argument

◆ cx_bprintf

#define cx_bprintf ( buffer,
fmt,
... )
Value:
cx_fprintf((void*)buffer, \
cxBufferWriteFunc, fmt, __VA_ARGS__)
#define cxBufferWriteFunc
Function pointer for cxBufferWrite that is compatible with cx_write_func.
Definition buffer.h:95
int cx_fprintf(void *stream, cx_write_func wfc, const char *fmt,...)
A fprintf like function which writes the output to a stream by using a write_func.

A printf like function which writes the output to a CxBuffer.

Parameters
buffer(CxBuffer*) a pointer to the buffer the data is written to
fmt(char*) the format string
...additional arguments
Returns
(int) the total number of bytes written or an error code from stdlib printf implementation
See also
cx_fprintf()
cxBufferWrite()

◆ cx_sprintf

#define cx_sprintf ( str,
len,
fmt,
... )   cx_sprintf_a(cxDefaultAllocator, str, len, fmt, __VA_ARGS__)

An sprintf like function which reallocates the string when the buffer is not large enough.

The size of the buffer will be updated in len when necessary.

Note
The resulting string, if successful, is guaranteed to be zero-terminated.
Parameters
str(char**) a pointer to the string buffer
len(size_t*) a pointer to the length of the buffer
fmt(char*) the format string
...additional arguments
Returns
(int) the length of produced string or an error code from stdlib printf implementation

◆ cx_sprintf_s

#define cx_sprintf_s ( buf,
len,
str,
fmt,
... )   cx_sprintf_sa(cxDefaultAllocator, buf, len, str, fmt, __VA_ARGS__)

An sprintf like function which allocates a new string when the buffer is not large enough.

The size of the buffer will be updated in len when necessary.

The location of the resulting string will always be stored to str. When the buffer was sufficiently large, buf itself will be stored to the location of str.

Note
The resulting string, if successful, is guaranteed to be zero-terminated.
Remarks
When a new string needed to be allocated, the contents of buf will be poisoned after the call, because this function tries to produce the string in buf, first.
Parameters
buf(char*) a pointer to the buffer
len(size_t*) a pointer to the length of the buffer
str(char**) a pointer where the location of the result shall be stored
fmt(char*) the format string
...additional arguments
Returns
(int) the length of produced string or an error code from stdlib printf implementation

◆ cx_vasprintf

#define cx_vasprintf ( fmt,
ap )   cx_vasprintf_a(cxDefaultAllocator, fmt, ap)

A vasprintf like function which allocates space for a string the result is written to.

Note
The resulting string is guaranteed to be zero-terminated, unless there was in error, in which case the string's pointer will be NULL.
Parameters
fmt(char*) format string
ap(va_list) argument list
Returns
(cxmutstr) the formatted string
See also
cx_asprintf()

◆ cx_vsprintf

#define cx_vsprintf ( str,
len,
fmt,
ap )   cx_vsprintf_a(cxDefaultAllocator, str, len, fmt, ap)

An sprintf like function which reallocates the string when the buffer is not large enough.

The size of the buffer will be updated in len when necessary.

Note
The resulting string, if successful, is guaranteed to be zero-terminated.
Parameters
str(char**) a pointer to the string buffer
len(size_t*) a pointer to the length of the buffer
fmt(char*) the format string
ap(va_list) argument list
Returns
(int) the length of produced string or an error code from stdlib printf implementation

◆ cx_vsprintf_s

#define cx_vsprintf_s ( buf,
len,
str,
fmt,
ap )   cx_vsprintf_sa(cxDefaultAllocator, buf, len, str, fmt, ap)

An sprintf like function which allocates a new string when the buffer is not large enough.

The size of the buffer will be updated in len when necessary.

The location of the resulting string will always be stored to str. When the buffer was sufficiently large, buf itself will be stored to the location of str.

Note
The resulting string is guaranteed to be zero-terminated.
Remarks
When a new string needed to be allocated, the contents of buf will be poisoned after the call, because this function tries to produce the string in buf, first.
Parameters
buf(char*) a pointer to the buffer
len(size_t*) a pointer to the length of the buffer
str(char**) a pointer where the location of the result shall be stored
fmt(char*) the format string
ap(va_list) argument list
Returns
(int) the length of produced string or an error code from stdlib printf implementation

Function Documentation

◆ cx_asprintf_a()

cxmutstr cx_asprintf_a ( const CxAllocator * allocator,
const char * fmt,
... )

A asprintf like function which allocates space for a string the result is written to.

Note
The resulting string is guaranteed to be zero-terminated, unless there was an error, in which case the string's pointer will be NULL.
Parameters
allocatorthe CxAllocator used for allocating the string
fmtformat string
...additional arguments
Returns
the formatted string
See also
cx_strfree_a()

◆ cx_fprintf()

int cx_fprintf ( void * stream,
cx_write_func wfc,
const char * fmt,
... )

A fprintf 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 or an error code from stdlib printf implementation

◆ cx_sprintf_a()

int cx_sprintf_a ( CxAllocator * alloc,
char ** str,
size_t * len,
const char * fmt,
... )

An sprintf like function which reallocates the string when the buffer is not large enough.

The size of the buffer will be updated in len when necessary.

Note
The resulting string, if successful, is guaranteed to be zero-terminated.
Attention
The original buffer MUST have been allocated with the same allocator!
Parameters
allocthe allocator to use
stra pointer to the string buffer
lena pointer to the length of the buffer
fmtthe format string
...additional arguments
Returns
the length of produced string or an error code from stdlib printf implementation

◆ cx_sprintf_sa()

int cx_sprintf_sa ( CxAllocator * alloc,
char * buf,
size_t * len,
char ** str,
const char * fmt,
... )

An sprintf like function which allocates a new string when the buffer is not large enough.

The size of the buffer will be updated in len when necessary.

The location of the resulting string will always be stored to str. When the buffer was sufficiently large, buf itself will be stored to the location of str.

Note
The resulting string, if successful, is guaranteed to be zero-terminated.
Remarks
When a new string needed to be allocated, the contents of buf will be poisoned after the call, because this function tries to produce the string in buf, first.
Parameters
allocthe allocator to use
bufa pointer to the buffer
lena pointer to the length of the buffer
stra pointer where the location of the result shall be stored
fmtthe format string
...additional arguments
Returns
the length of produced string or an error code from stdlib printf implementation

◆ cx_vasprintf_a()

cxmutstr cx_vasprintf_a ( const CxAllocator * allocator,
const char * fmt,
va_list ap )

A vasprintf like function which allocates space for a string the result is written to.

Note
The resulting string is guaranteed to be zero-terminated, unless there was an error, in which case the string's pointer will be NULL.
Parameters
allocatorthe CxAllocator used for allocating the string
fmtformat string
apargument list
Returns
the formatted string
See also
cx_asprintf_a()

◆ cx_vfprintf()

int cx_vfprintf ( void * stream,
cx_write_func wfc,
const char * fmt,
va_list ap )

A vfprintf 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
apargument list
Returns
the total number of bytes written or an error code from stdlib printf implementation
See also
cx_fprintf()

◆ cx_vsprintf_a()

int cx_vsprintf_a ( CxAllocator * alloc,
char ** str,
size_t * len,
const char * fmt,
va_list ap )

An sprintf like function which reallocates the string when the buffer is not large enough.

The size of the buffer will be updated in len when necessary.

Note
The resulting string is guaranteed to be zero-terminated.
Attention
The original buffer MUST have been allocated with the same allocator!
Parameters
allocthe allocator to use
stra pointer to the string buffer
lena pointer to the length of the buffer
fmtthe format string
apargument list
Returns
the length of produced string or an error code from stdlib printf implementation

◆ cx_vsprintf_sa()

int cx_vsprintf_sa ( CxAllocator * alloc,
char * buf,
size_t * len,
char ** str,
const char * fmt,
va_list ap )

An sprintf like function which allocates a new string when the buffer is not large enough.

The size of the buffer will be updated in len when necessary.

The location of the resulting string will always be stored to str. When the buffer was sufficiently large, buf itself will be stored to the location of str.

Note
The resulting string is guaranteed to be zero-terminated.
Remarks
When a new string needed to be allocated, the contents of buf will be poisoned after the call, because this function tries to produce the string in buf, first.
Parameters
allocthe allocator to use
bufa pointer to the buffer
lena pointer to the length of the buffer
stra pointer where the location of the result shall be stored
fmtthe format string
apargument list
Returns
the length of produced string or an error code from stdlib printf implementation