From 130e3be57409f06dd395ef8626bf848097ea1b79 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Tue, 3 Dec 2013 09:36:46 +0000 Subject: chsnprintf() implementation added. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/branches/kernel_3_dev@6528 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/various/chprintf.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'os/various/chprintf.c') diff --git a/os/various/chprintf.c b/os/various/chprintf.c index ffb6650f8..190a7defa 100644 --- a/os/various/chprintf.c +++ b/os/various/chprintf.c @@ -29,6 +29,7 @@ #include "ch.h" #include "chprintf.h" +#include "memstreams.h" #define MAX_FILLER 11 #define FLOAT_PRECISION 100000 @@ -259,4 +260,48 @@ unsigned_common: } } +/** + * @brief System formatted output function. + * @details This function implements a minimal @p vprintf()-like functionality + * with output on a @p BaseSequentialStream. + * 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. + * . + * + * @param[in] str pointer to a buffer + * @param[in] size maximum size of the buffer + * @param[in] fmt formatting string + * @param[in] ap list of parameters + * + * @api + */ +int chsnprintf(char *str, size_t size, const char *fmt, ...) { + va_list ap; + MemoryStream ms; + BaseSequentialStream *chp; + + /* Memory stream object to be used as a string writer.*/ + msObjectInit(&ms, (uint8_t *)str, size, 0); + + /* Performing the print operation using the common code.*/ + chp = (BaseSequentialStream *)&ms; + va_start(ap, fmt); + chvprintf(chp, fmt, ap); + va_end(ap); + + /* Final zero and size return.*/ + chSequentialStreamPut(chp, 0); + return ms.eos - 1; +} + /** @} */ -- cgit v1.2.3