rework #4
26
srv/format.c
26
srv/format.c
@ -57,8 +57,15 @@ uint32_t format_vfctprintf(FormatCallback callback, void* arg,
|
|||||||
return render_format(callback, arg, format, va);
|
return render_format(callback, arg, format, va);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//--local functions-------------------------------------------------------------
|
//--local functions-------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FormatCallback to write into a regular buffer. A pointer to BufferArg shall
|
||||||
|
* be passed as arg.
|
||||||
|
*
|
||||||
|
* Returns 0 if everything went as expected, 1 otherwise
|
||||||
|
*/
|
||||||
static uint32_t buffer_write(uint8_t byte, void* arg)
|
static uint32_t buffer_write(uint8_t byte, void* arg)
|
||||||
{
|
{
|
||||||
struct BufferArg* buffer_arg = (struct BufferArg*)arg;
|
struct BufferArg* buffer_arg = (struct BufferArg*)arg;
|
||||||
@ -71,6 +78,12 @@ static uint32_t buffer_write(uint8_t byte, void* arg)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Renders the given format to the output defined by the provided callback,
|
||||||
|
* formatting accordingly the provided arguments
|
||||||
|
*
|
||||||
|
* Returns 0 if everything went as expected, 1 otherwise
|
||||||
|
*/
|
||||||
static uint32_t render_format(FormatCallback callback, void* arg,
|
static uint32_t render_format(FormatCallback callback, void* arg,
|
||||||
const char* restrict format, va_list va)
|
const char* restrict format, va_list va)
|
||||||
{
|
{
|
||||||
@ -157,6 +170,12 @@ static uint32_t render_format(FormatCallback callback, void* arg,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Formats a signed integer and renders it to the output defined by the provided
|
||||||
|
* callback
|
||||||
|
*
|
||||||
|
* Returns 0 if everything went as expected, 1 otherwise
|
||||||
|
*/
|
||||||
static uint32_t render_signed(FormatCallback callback, void* arg,
|
static uint32_t render_signed(FormatCallback callback, void* arg,
|
||||||
int32_t number, uint8_t base, uint8_t width)
|
int32_t number, uint8_t base, uint8_t width)
|
||||||
{
|
{
|
||||||
@ -195,6 +214,12 @@ static uint32_t render_signed(FormatCallback callback, void* arg,
|
|||||||
return render_unsigned(callback, arg, (uint32_t)number, base, 0, false);
|
return render_unsigned(callback, arg, (uint32_t)number, base, 0, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Formats an unsigned integer and renders it to the output defined by the
|
||||||
|
* provided callback
|
||||||
|
*
|
||||||
|
* Returns 0 if everything went as expected, 1 otherwise
|
||||||
|
*/
|
||||||
static uint32_t render_unsigned(FormatCallback callback, void* arg,
|
static uint32_t render_unsigned(FormatCallback callback, void* arg,
|
||||||
uint32_t number, uint8_t base, uint8_t width, bool zero_padding)
|
uint32_t number, uint8_t base, uint8_t width, bool zero_padding)
|
||||||
{
|
{
|
||||||
@ -237,3 +262,4 @@ static uint32_t render_unsigned(FormatCallback callback, void* arg,
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
24
srv/format.h
24
srv/format.h
@ -20,12 +20,36 @@ typedef uint32_t (*FormatCallback)(uint8_t byte, void* arg);
|
|||||||
|
|
||||||
//--functions-------------------------------------------------------------------
|
//--functions-------------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom implementation of snprintf. Print the given format into the given
|
||||||
|
* buffer with the attached arguments properly formated.
|
||||||
|
*
|
||||||
|
* This function only provided a minimal subset of the standard library's
|
||||||
|
* snprintf : %c, %s, %i, %d, %x as well as right-justified padding for %d and
|
||||||
|
* %x
|
||||||
|
*
|
||||||
|
* Returns 0 if everything went as expected, 1 otherwise
|
||||||
|
*/
|
||||||
uint32_t format_snprintf(char* restrict buffer, uint32_t buffer_size,
|
uint32_t format_snprintf(char* restrict buffer, uint32_t buffer_size,
|
||||||
const char* restrict format, ...);
|
const char* restrict format, ...);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom implementation of fprintf using a callback instead of a file pointer.
|
||||||
|
* Print the given format using the given callback with the attached arguments
|
||||||
|
* properly formated.
|
||||||
|
*
|
||||||
|
* This function only provided a minimal subset of the standard library's
|
||||||
|
* snprintf : %c, %s, %i, %d, %x as well as right-justified padding for %d and
|
||||||
|
* %x
|
||||||
|
*
|
||||||
|
* Returns 0 if everything went as expected, 1 otherwise
|
||||||
|
*/
|
||||||
uint32_t format_fctprintf(FormatCallback callback, void* arg,
|
uint32_t format_fctprintf(FormatCallback callback, void* arg,
|
||||||
const char* restrict format, ...);
|
const char* restrict format, ...);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Variadic variation of format_fctprintf(), see the later for more information
|
||||||
|
*/
|
||||||
uint32_t format_vfctprintf(FormatCallback callback, void* arg,
|
uint32_t format_vfctprintf(FormatCallback callback, void* arg,
|
||||||
const char* restrict format, va_list va);
|
const char* restrict format, va_list va);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user