Add chvsnprintf(...) to streams library Topic is solved

Use this forum for requesting small changes in ChibiOS. Large changes should be discussed in the development forum. This forum is NOT for support.
Post Reply
User avatar
FXCoder
Posts: 403
Joined: Sun Jun 12, 2016 4:10 am
Has thanked: 188 times
Been thanked: 134 times

Add chvsnprintf(...) to streams library

Post by FXCoder »

For convenience when printing to a char buffer with a va_list passed as a parameter...

Code: Select all

Index: chprintf.c
===================================================================
--- chprintf.c   (revision 12211)
+++ chprintf.c   (working copy)
@@ -339,6 +339,45 @@
  */
 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:
+ *          - <b>x</b> hexadecimal integer.
+ *          - <b>X</b> hexadecimal long.
+ *          - <b>o</b> octal integer.
+ *          - <b>O</b> octal long.
+ *          - <b>d</b> decimal signed integer.
+ *          - <b>D</b> decimal signed long.
+ *          - <b>u</b> decimal unsigned integer.
+ *          - <b>U</b> decimal unsigned long.
+ *          - <b>c</b> character.
+ *          - <b>s</b> 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,9 +394,7 @@
 
   /* 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)
Index: chprintf.h
===================================================================
--- chprintf.h   (revision 12211)
+++ chprintf.h   (working copy)
@@ -40,6 +40,7 @@
   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
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Add chvsnprintf(...) to streams library

Post by Giovanni »

Merged thanks.

Giovanni
Post Reply