2011-06-27 15:00:05 +00:00
|
|
|
/*
|
2014-07-26 09:06:28 +00:00
|
|
|
ChibiOS - Copyright (C) 2006-2014 Giovanni Di Sirio
|
2011-06-27 15:00:05 +00:00
|
|
|
|
2013-05-11 06:08:17 +00:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
2011-06-27 15:00:05 +00:00
|
|
|
|
2013-05-11 06:08:17 +00:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2011-06-27 15:00:05 +00:00
|
|
|
|
2013-05-11 06:08:17 +00:00
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
2012-01-21 14:29:42 +00:00
|
|
|
*/
|
2013-11-12 13:44:26 +00:00
|
|
|
|
2012-03-11 17:02:06 +00:00
|
|
|
/*
|
2013-11-12 13:44:26 +00:00
|
|
|
Concepts and parts of this file have been contributed by Fabio Utzig,
|
|
|
|
chvprintf() added by Brent Roman.
|
2012-03-11 17:02:06 +00:00
|
|
|
*/
|
2011-06-27 15:00:05 +00:00
|
|
|
|
2012-05-18 15:37:58 +00:00
|
|
|
/**
|
|
|
|
* @file chprintf.c
|
|
|
|
* @brief Mini printf-like functionality.
|
|
|
|
*
|
|
|
|
* @addtogroup chprintf
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2011-06-27 15:00:05 +00:00
|
|
|
#include "ch.h"
|
2012-03-11 17:02:06 +00:00
|
|
|
#include "chprintf.h"
|
2013-12-03 09:36:46 +00:00
|
|
|
#include "memstreams.h"
|
2011-06-27 15:00:05 +00:00
|
|
|
|
2011-06-27 19:57:11 +00:00
|
|
|
#define MAX_FILLER 11
|
2014-10-04 07:47:23 +00:00
|
|
|
#define FLOAT_PRECISION 5
|
2011-06-27 15:00:05 +00:00
|
|
|
|
2012-03-11 17:02:06 +00:00
|
|
|
static char *long_to_string_with_divisor(char *p,
|
|
|
|
long num,
|
|
|
|
unsigned radix,
|
|
|
|
long divisor) {
|
2011-06-27 15:00:05 +00:00
|
|
|
int i;
|
|
|
|
char *q;
|
2012-03-11 17:02:06 +00:00
|
|
|
long l, ll;
|
|
|
|
|
|
|
|
l = num;
|
|
|
|
if (divisor == 0) {
|
|
|
|
ll = num;
|
|
|
|
} else {
|
|
|
|
ll = divisor;
|
|
|
|
}
|
2011-06-27 15:00:05 +00:00
|
|
|
|
2011-06-27 19:57:11 +00:00
|
|
|
q = p + MAX_FILLER;
|
2011-06-27 15:00:05 +00:00
|
|
|
do {
|
2012-03-11 17:02:06 +00:00
|
|
|
i = (int)(l % radix);
|
2011-06-27 15:00:05 +00:00
|
|
|
i += '0';
|
|
|
|
if (i > '9')
|
2011-06-27 19:57:11 +00:00
|
|
|
i += 'A' - '0' - 10;
|
2011-06-27 15:00:05 +00:00
|
|
|
*--q = i;
|
2012-03-11 17:02:06 +00:00
|
|
|
l /= radix;
|
|
|
|
} while ((ll /= radix) != 0);
|
2011-06-27 15:00:05 +00:00
|
|
|
|
2011-06-27 19:57:11 +00:00
|
|
|
i = (int)(p + MAX_FILLER - q);
|
2011-06-27 15:00:05 +00:00
|
|
|
do
|
|
|
|
*p++ = *q++;
|
|
|
|
while (--i);
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2012-03-11 17:02:06 +00:00
|
|
|
static char *ltoa(char *p, long num, unsigned radix) {
|
|
|
|
|
|
|
|
return long_to_string_with_divisor(p, num, radix, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if CHPRINTF_USE_FLOAT
|
2014-10-04 07:47:23 +00:00
|
|
|
static long pow10[] = {10, 100, 1000, 10000, 100000, 1000000, 10000000,
|
|
|
|
100000000, 1000000000, 10000000000, 100000000000,
|
|
|
|
1000000000000, 10000000000000};
|
|
|
|
|
|
|
|
static char *ftoa(char *p, double num, unsigned long precision) {
|
2012-03-11 17:02:06 +00:00
|
|
|
long l;
|
2014-10-04 07:47:23 +00:00
|
|
|
|
|
|
|
if (precision == 0)
|
|
|
|
precision = FLOAT_PRECISION;
|
|
|
|
precision = pow10[precision - 1];
|
2012-03-11 17:02:06 +00:00
|
|
|
|
2014-02-09 08:44:55 +00:00
|
|
|
l = (long)num;
|
2012-03-11 17:02:06 +00:00
|
|
|
p = long_to_string_with_divisor(p, l, 10, 0);
|
|
|
|
*p++ = '.';
|
2014-02-09 08:44:55 +00:00
|
|
|
l = (long)((num - l) * precision);
|
2012-03-11 17:02:06 +00:00
|
|
|
return long_to_string_with_divisor(p, l, 10, precision / 10);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-06-28 06:29:04 +00:00
|
|
|
/**
|
|
|
|
* @brief System formatted output function.
|
2013-11-12 13:44:26 +00:00
|
|
|
* @details This function implements a minimal @p vprintf()-like functionality
|
2012-05-08 17:09:20 +00:00
|
|
|
* with output on a @p BaseSequentialStream.
|
2012-01-21 19:43:40 +00:00
|
|
|
* The general parameters format is: %[-][width|*][.precision|*][l|L]p.
|
2011-06-28 06:29:04 +00:00
|
|
|
* 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.
|
|
|
|
* .
|
|
|
|
*
|
2012-05-08 17:09:20 +00:00
|
|
|
* @param[in] chp pointer to a @p BaseSequentialStream implementing object
|
2011-06-28 06:29:04 +00:00
|
|
|
* @param[in] fmt formatting string
|
2013-11-12 13:44:26 +00:00
|
|
|
* @param[in] ap list of parameters
|
2014-10-26 10:17:14 +00:00
|
|
|
* @return The number of bytes that would have been
|
|
|
|
* written to @p chp if no stream error occurs
|
2013-11-12 13:44:26 +00:00
|
|
|
*
|
|
|
|
* @api
|
2011-06-28 06:29:04 +00:00
|
|
|
*/
|
2014-10-26 10:17:14 +00:00
|
|
|
int chvprintf(BaseSequentialStream *chp, const char *fmt, va_list ap) {
|
2011-06-27 19:57:11 +00:00
|
|
|
char *p, *s, c, filler;
|
2012-01-21 19:43:40 +00:00
|
|
|
int i, precision, width;
|
2014-10-26 10:17:14 +00:00
|
|
|
int n = 0;
|
2013-09-05 09:01:21 +00:00
|
|
|
bool is_long, left_align;
|
2011-06-27 15:00:05 +00:00
|
|
|
long l;
|
2012-03-11 17:02:06 +00:00
|
|
|
#if CHPRINTF_USE_FLOAT
|
|
|
|
float f;
|
2012-07-22 08:45:48 +00:00
|
|
|
char tmpbuf[2*MAX_FILLER + 1];
|
|
|
|
#else
|
|
|
|
char tmpbuf[MAX_FILLER + 1];
|
2012-03-11 17:02:06 +00:00
|
|
|
#endif
|
2011-06-27 15:00:05 +00:00
|
|
|
|
|
|
|
while (TRUE) {
|
|
|
|
c = *fmt++;
|
2013-11-12 13:44:26 +00:00
|
|
|
if (c == 0)
|
2014-10-26 10:17:14 +00:00
|
|
|
return n;
|
2011-06-27 15:00:05 +00:00
|
|
|
if (c != '%') {
|
2012-06-18 16:22:34 +00:00
|
|
|
chSequentialStreamPut(chp, (uint8_t)c);
|
2014-10-26 10:17:14 +00:00
|
|
|
n++;
|
2011-06-27 15:00:05 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-01-16 11:41:04 +00:00
|
|
|
p = tmpbuf;
|
|
|
|
s = tmpbuf;
|
2012-01-21 19:43:40 +00:00
|
|
|
left_align = FALSE;
|
2011-06-27 15:00:05 +00:00
|
|
|
if (*fmt == '-') {
|
|
|
|
fmt++;
|
2012-01-21 19:43:40 +00:00
|
|
|
left_align = TRUE;
|
2011-06-27 15:00:05 +00:00
|
|
|
}
|
2011-06-27 19:57:11 +00:00
|
|
|
filler = ' ';
|
2014-10-04 07:47:23 +00:00
|
|
|
if (*fmt == '0') {
|
2011-06-27 15:00:05 +00:00
|
|
|
fmt++;
|
2011-06-27 19:57:11 +00:00
|
|
|
filler = '0';
|
2011-06-27 15:00:05 +00:00
|
|
|
}
|
2012-01-16 11:41:04 +00:00
|
|
|
width = 0;
|
|
|
|
while (TRUE) {
|
2011-06-27 15:00:05 +00:00
|
|
|
c = *fmt++;
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
c -= '0';
|
|
|
|
else if (c == '*')
|
|
|
|
c = va_arg(ap, int);
|
|
|
|
else
|
|
|
|
break;
|
2011-06-28 06:29:04 +00:00
|
|
|
width = width * 10 + c;
|
2011-06-27 15:00:05 +00:00
|
|
|
}
|
2012-01-21 19:43:40 +00:00
|
|
|
precision = 0;
|
2011-06-27 15:00:05 +00:00
|
|
|
if (c == '.') {
|
2012-01-16 11:41:04 +00:00
|
|
|
while (TRUE) {
|
2011-06-27 15:00:05 +00:00
|
|
|
c = *fmt++;
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
c -= '0';
|
|
|
|
else if (c == '*')
|
|
|
|
c = va_arg(ap, int);
|
|
|
|
else
|
|
|
|
break;
|
2012-01-21 19:43:40 +00:00
|
|
|
precision *= 10;
|
|
|
|
precision += c;
|
2011-06-27 15:00:05 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-21 19:43:40 +00:00
|
|
|
/* Long modifier.*/
|
2011-06-27 15:00:05 +00:00
|
|
|
if (c == 'l' || c == 'L') {
|
2012-01-21 19:43:40 +00:00
|
|
|
is_long = TRUE;
|
2011-06-27 15:00:05 +00:00
|
|
|
if (*fmt)
|
|
|
|
c = *fmt++;
|
|
|
|
}
|
2012-01-21 19:43:40 +00:00
|
|
|
else
|
|
|
|
is_long = (c >= 'A') && (c <= 'Z');
|
|
|
|
|
|
|
|
/* Command decoding.*/
|
2011-06-27 15:00:05 +00:00
|
|
|
switch (c) {
|
2012-01-21 19:43:40 +00:00
|
|
|
case 'c':
|
|
|
|
filler = ' ';
|
|
|
|
*p++ = va_arg(ap, int);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
filler = ' ';
|
|
|
|
if ((s = va_arg(ap, char *)) == 0)
|
|
|
|
s = "(null)";
|
|
|
|
if (precision == 0)
|
|
|
|
precision = 32767;
|
|
|
|
for (p = s; *p && (--precision >= 0); p++)
|
|
|
|
;
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
case 'd':
|
2013-04-14 09:12:49 +00:00
|
|
|
case 'I':
|
|
|
|
case 'i':
|
2012-01-21 19:43:40 +00:00
|
|
|
if (is_long)
|
|
|
|
l = va_arg(ap, long);
|
|
|
|
else
|
|
|
|
l = va_arg(ap, int);
|
|
|
|
if (l < 0) {
|
|
|
|
*p++ = '-';
|
|
|
|
l = -l;
|
|
|
|
}
|
|
|
|
p = ltoa(p, l, 10);
|
|
|
|
break;
|
2012-03-11 17:02:06 +00:00
|
|
|
#if CHPRINTF_USE_FLOAT
|
|
|
|
case 'f':
|
|
|
|
f = (float) va_arg(ap, double);
|
|
|
|
if (f < 0) {
|
|
|
|
*p++ = '-';
|
|
|
|
f = -f;
|
|
|
|
}
|
2014-10-04 07:47:23 +00:00
|
|
|
p = ftoa(p, f, precision);
|
2012-03-11 17:02:06 +00:00
|
|
|
break;
|
|
|
|
#endif
|
2011-06-27 15:00:05 +00:00
|
|
|
case 'X':
|
|
|
|
case 'x':
|
|
|
|
c = 16;
|
2012-01-21 19:43:40 +00:00
|
|
|
goto unsigned_common;
|
2011-06-27 15:00:05 +00:00
|
|
|
case 'U':
|
|
|
|
case 'u':
|
|
|
|
c = 10;
|
2012-01-21 19:43:40 +00:00
|
|
|
goto unsigned_common;
|
2011-06-27 15:00:05 +00:00
|
|
|
case 'O':
|
|
|
|
case 'o':
|
|
|
|
c = 8;
|
2012-01-21 19:43:40 +00:00
|
|
|
unsigned_common:
|
2012-01-16 11:41:04 +00:00
|
|
|
if (is_long)
|
2013-04-07 08:02:48 +00:00
|
|
|
l = va_arg(ap, unsigned long);
|
2011-06-27 19:57:11 +00:00
|
|
|
else
|
2013-04-07 08:02:48 +00:00
|
|
|
l = va_arg(ap, unsigned int);
|
2011-06-27 19:57:11 +00:00
|
|
|
p = ltoa(p, l, c);
|
2011-06-27 15:00:05 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*p++ = c;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i = (int)(p - s);
|
|
|
|
if ((width -= i) < 0)
|
|
|
|
width = 0;
|
2012-01-21 19:43:40 +00:00
|
|
|
if (left_align == FALSE)
|
2011-06-27 15:00:05 +00:00
|
|
|
width = -width;
|
|
|
|
if (width < 0) {
|
2011-06-27 19:57:11 +00:00
|
|
|
if (*s == '-' && filler == '0') {
|
2012-06-18 16:22:34 +00:00
|
|
|
chSequentialStreamPut(chp, (uint8_t)*s++);
|
2014-10-26 10:17:14 +00:00
|
|
|
n++;
|
2011-06-27 15:00:05 +00:00
|
|
|
i--;
|
|
|
|
}
|
2014-04-22 11:22:06 +00:00
|
|
|
do {
|
2012-06-18 16:22:34 +00:00
|
|
|
chSequentialStreamPut(chp, (uint8_t)filler);
|
2014-10-26 10:17:14 +00:00
|
|
|
n++;
|
2014-04-22 11:22:06 +00:00
|
|
|
} while (++width != 0);
|
2011-06-27 15:00:05 +00:00
|
|
|
}
|
2014-10-26 10:17:14 +00:00
|
|
|
while (--i >= 0) {
|
2014-04-22 11:22:06 +00:00
|
|
|
chSequentialStreamPut(chp, (uint8_t)*s++);
|
2014-10-26 10:17:14 +00:00
|
|
|
n++;
|
|
|
|
}
|
2011-06-27 15:00:05 +00:00
|
|
|
|
|
|
|
while (width) {
|
2012-06-18 16:22:34 +00:00
|
|
|
chSequentialStreamPut(chp, (uint8_t)filler);
|
2014-10-26 10:17:14 +00:00
|
|
|
n++;
|
2011-06-27 15:00:05 +00:00
|
|
|
width--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-14 16:45:57 +00:00
|
|
|
|
2013-12-03 09:36:46 +00:00
|
|
|
/**
|
|
|
|
* @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:
|
|
|
|
* - <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.
|
|
|
|
* .
|
2014-10-26 10:17:14 +00:00
|
|
|
* @post @p str is NUL-terminated, unless @p size is 0.
|
2013-12-03 09:36:46 +00:00
|
|
|
*
|
|
|
|
* @param[in] str pointer to a buffer
|
|
|
|
* @param[in] size maximum size of the buffer
|
|
|
|
* @param[in] fmt formatting string
|
2014-10-26 10:17:14 +00:00
|
|
|
* @return The number of characters (excluding the
|
|
|
|
* terminating NUL byte) that would have been
|
|
|
|
* stored in @p str if there was room.
|
2013-12-03 09:36:46 +00:00
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
|
|
|
int chsnprintf(char *str, size_t size, const char *fmt, ...) {
|
|
|
|
va_list ap;
|
|
|
|
MemoryStream ms;
|
|
|
|
BaseSequentialStream *chp;
|
2014-10-26 10:17:14 +00:00
|
|
|
size_t size_wo_nul;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (size > 0)
|
|
|
|
size_wo_nul = size - 1;
|
|
|
|
else
|
|
|
|
size_wo_nul = 0;
|
2013-12-03 09:36:46 +00:00
|
|
|
|
2014-10-04 07:47:23 +00:00
|
|
|
/* Memory stream object to be used as a string writer, reserving one
|
|
|
|
byte for the final zero.*/
|
2014-10-26 10:17:14 +00:00
|
|
|
msObjectInit(&ms, (uint8_t *)str, size_wo_nul, 0);
|
2013-12-03 09:36:46 +00:00
|
|
|
|
|
|
|
/* Performing the print operation using the common code.*/
|
|
|
|
chp = (BaseSequentialStream *)&ms;
|
|
|
|
va_start(ap, fmt);
|
2014-10-26 10:17:14 +00:00
|
|
|
retval = chvprintf(chp, fmt, ap);
|
2013-12-03 09:36:46 +00:00
|
|
|
va_end(ap);
|
|
|
|
|
2014-10-26 10:17:14 +00:00
|
|
|
/* Terminate with a zero, unless size==0.*/
|
|
|
|
if (ms.eos < size)
|
|
|
|
str[ms.eos] = 0;
|
|
|
|
|
|
|
|
/* Return number of bytes that would have been written.*/
|
|
|
|
return retval;
|
2013-12-03 09:36:46 +00:00
|
|
|
}
|
|
|
|
|
2012-06-14 16:45:57 +00:00
|
|
|
/** @} */
|