rework #4

Merged
Steins7 merged 88 commits from rework into dev 2024-04-20 18:20:23 +00:00
2 changed files with 50 additions and 0 deletions
Showing only changes of commit 26f2b4d545 - Show all commits

View File

@ -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;
} }

View File

@ -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);