aboutsummaryrefslogtreecommitdiffstats
path: root/os/various
diff options
context:
space:
mode:
authorgdisirio <gdisirio@35acf78f-673a-0410-8e92-d51de3d6d3f4>2012-01-21 19:43:40 +0000
committergdisirio <gdisirio@35acf78f-673a-0410-8e92-d51de3d6d3f4>2012-01-21 19:43:40 +0000
commitf231e6dbddd39a9f41f83cb2b3b0e5ec6d88875d (patch)
treee8df84345122de8153b1af22750e782432fb2e3e /os/various
parentde5dcbba856524599a8f06d3a9bdbf1b01db44c2 (diff)
downloadChibiOS-f231e6dbddd39a9f41f83cb2b3b0e5ec6d88875d.tar.gz
ChibiOS-f231e6dbddd39a9f41f83cb2b3b0e5ec6d88875d.tar.bz2
ChibiOS-f231e6dbddd39a9f41f83cb2b3b0e5ec6d88875d.zip
git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@3847 35acf78f-673a-0410-8e92-d51de3d6d3f4
Diffstat (limited to 'os/various')
-rw-r--r--os/various/chprintf.c86
1 files changed, 43 insertions, 43 deletions
diff --git a/os/various/chprintf.c b/os/various/chprintf.c
index da0390cc4..3936226c1 100644
--- a/os/various/chprintf.c
+++ b/os/various/chprintf.c
@@ -49,7 +49,7 @@ static char *ltoa(char *p, long num, unsigned radix) {
* @brief System formatted output function.
* @details This function implements a minimal @p printf() like functionality
* with output on a @p BaseChannel.
- * The general parameters format is: %[.][width|*][l|L]p.
+ * 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.
@@ -72,8 +72,8 @@ void chprintf(BaseChannel *chp, const char *fmt, ...) {
va_list ap;
char tmpbuf[MAX_FILLER + 1];
char *p, *s, c, filler;
- int i, n, width;
- bool_t is_long, left_just;
+ int i, precision, width;
+ bool_t is_long, left_align;
long l;
va_start(ap, fmt);
@@ -89,10 +89,10 @@ void chprintf(BaseChannel *chp, const char *fmt, ...) {
}
p = tmpbuf;
s = tmpbuf;
- left_just = FALSE;
+ left_align = FALSE;
if (*fmt == '-') {
fmt++;
- left_just = TRUE;
+ left_align = TRUE;
}
filler = ' ';
if (*fmt == '.') {
@@ -110,7 +110,7 @@ void chprintf(BaseChannel *chp, const char *fmt, ...) {
break;
width = width * 10 + c;
}
- n = 0;
+ precision = 0;
if (c == '.') {
while (TRUE) {
c = *fmt++;
@@ -120,64 +120,64 @@ void chprintf(BaseChannel *chp, const char *fmt, ...) {
c = va_arg(ap, int);
else
break;
- n *= 10;
- n += c;
+ precision *= 10;
+ precision += c;
}
}
- is_long = FALSE;
+ /* Long modifier.*/
if (c == 'l' || c == 'L') {
- is_long++;
+ is_long = TRUE;
if (*fmt)
c = *fmt++;
}
+ else
+ is_long = (c >= 'A') && (c <= 'Z');
+
+ /* Command decoding.*/
switch (c) {
+ 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':
+ 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;
case 'X':
- is_long = TRUE;
case 'x':
c = 16;
- goto skip;
+ goto unsigned_common;
case 'U':
- is_long = TRUE;
case 'u':
c = 10;
- goto skip;
+ goto unsigned_common;
case 'O':
- is_long = TRUE;
case 'o':
c = 8;
-skip:
+unsigned_common:
if (is_long)
l = va_arg(ap, long);
else
l = va_arg(ap, int);
p = ltoa(p, l, c);
break;
- case 'D':
- is_long = TRUE;
- case 'd':
- 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;
- case 'c':
- filler = ' ';
- *p++ = va_arg(ap, int);
- break;
- case 's':
- filler = ' ';
- if ((s = va_arg(ap, char *)) == 0)
- s = "(null)";
- if (n == 0)
- n = 32767;
- for (p = s; *p && --n >= 0; p++)
- ;
- break;
default:
*p++ = c;
break;
@@ -185,7 +185,7 @@ skip:
i = (int)(p - s);
if ((width -= i) < 0)
width = 0;
- if (left_just == FALSE)
+ if (left_align == FALSE)
width = -width;
if (width < 0) {
if (*s == '-' && filler == '0') {