From 6421004d21f26d809d6b7dcd18d5b0d6ee02eb8c Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Sun, 6 Jan 2019 09:14:06 +0000 Subject: Added chvsnprintf(). git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@12534 110e8d01-0319-4d1e-a829-52ad28d1bb01 --- os/hal/lib/streams/chprintf.c | 46 +++++++++++++++++++++++++++++++++++++++---- os/hal/lib/streams/chprintf.h | 1 + 2 files changed, 43 insertions(+), 4 deletions(-) (limited to 'os') diff --git a/os/hal/lib/streams/chprintf.c b/os/hal/lib/streams/chprintf.c index 50b1ed75e..ba622446c 100644 --- a/os/hal/lib/streams/chprintf.c +++ b/os/hal/lib/streams/chprintf.c @@ -339,6 +339,45 @@ int chprintf(BaseSequentialStream *chp, const char *fmt, ...) { */ int chsnprintf(char *str, size_t size, const char *fmt, ...) { va_list ap; + int retval; + + /* Performing the print operation.*/ + va_start(ap, fmt); + retval = chvsnprintf(str, size, fmt, ap); + va_end(ap); + + /* Return number of bytes that would have been written.*/ + return retval; +} + +/** + * @brief System formatted output function. + * @details This function implements a minimal @p vsnprintf()-like functionality. + * The general parameters format is: %[-][width|*][.precision|*][l|L]p. + * The following parameter types (p) are supported: + * - x hexadecimal integer. + * - X hexadecimal long. + * - o octal integer. + * - O octal long. + * - d decimal signed integer. + * - D decimal signed long. + * - u decimal unsigned integer. + * - U decimal unsigned long. + * - c character. + * - s string. + * . + * @post @p str is NUL-terminated, unless @p size is 0. + * + * @param[in] str pointer to a buffer + * @param[in] size maximum size of the buffer + * @param[in] ap list of parameters + * @return The number of characters (excluding the + * terminating NUL byte) that would have been + * stored in @p str if there was room. + * + * @api + */ +int chvsnprintf(char *str, size_t size, const char *fmt, va_list ap) { MemoryStream ms; BaseSequentialStream *chp; size_t size_wo_nul; @@ -355,13 +394,12 @@ int chsnprintf(char *str, size_t size, const char *fmt, ...) { /* Performing the print operation using the common code.*/ chp = (BaseSequentialStream *)(void *)&ms; - va_start(ap, fmt); retval = chvprintf(chp, fmt, ap); - va_end(ap); /* Terminate with a zero, unless size==0.*/ - if (ms.eos < size) - str[ms.eos] = 0; + if (ms.eos < size) { + str[ms.eos] = 0; + } /* Return number of bytes that would have been written.*/ return retval; diff --git a/os/hal/lib/streams/chprintf.h b/os/hal/lib/streams/chprintf.h index 19774eaab..833ac27f4 100644 --- a/os/hal/lib/streams/chprintf.h +++ b/os/hal/lib/streams/chprintf.h @@ -40,6 +40,7 @@ extern "C" { int chvprintf(BaseSequentialStream *chp, const char *fmt, va_list ap); int chprintf(BaseSequentialStream *chp, const char *fmt, ...); int chsnprintf(char *str, size_t size, const char *fmt, ...); + int chvsnprintf(char *str, size_t size, const char *fmt, va_list ap); #ifdef __cplusplus } #endif -- cgit v1.2.3