git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@3847 35acf78f-673a-0410-8e92-d51de3d6d3f4

master
gdisirio 2012-01-21 19:43:40 +00:00
parent de5dcbba85
commit f231e6dbdd
1 changed files with 44 additions and 44 deletions

View File

@ -49,7 +49,7 @@ static char *ltoa(char *p, long num, unsigned radix) {
* @brief System formatted output function. * @brief System formatted output function.
* @details This function implements a minimal @p printf() like functionality * @details This function implements a minimal @p printf() like functionality
* with output on a @p BaseChannel. * 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: * The following parameter types (p) are supported:
* - <b>x</b> hexadecimal integer. * - <b>x</b> hexadecimal integer.
* - <b>X</b> hexadecimal long. * - <b>X</b> hexadecimal long.
@ -72,8 +72,8 @@ void chprintf(BaseChannel *chp, const char *fmt, ...) {
va_list ap; va_list ap;
char tmpbuf[MAX_FILLER + 1]; char tmpbuf[MAX_FILLER + 1];
char *p, *s, c, filler; char *p, *s, c, filler;
int i, n, width; int i, precision, width;
bool_t is_long, left_just; bool_t is_long, left_align;
long l; long l;
va_start(ap, fmt); va_start(ap, fmt);
@ -89,10 +89,10 @@ void chprintf(BaseChannel *chp, const char *fmt, ...) {
} }
p = tmpbuf; p = tmpbuf;
s = tmpbuf; s = tmpbuf;
left_just = FALSE; left_align = FALSE;
if (*fmt == '-') { if (*fmt == '-') {
fmt++; fmt++;
left_just = TRUE; left_align = TRUE;
} }
filler = ' '; filler = ' ';
if (*fmt == '.') { if (*fmt == '.') {
@ -110,7 +110,7 @@ void chprintf(BaseChannel *chp, const char *fmt, ...) {
break; break;
width = width * 10 + c; width = width * 10 + c;
} }
n = 0; precision = 0;
if (c == '.') { if (c == '.') {
while (TRUE) { while (TRUE) {
c = *fmt++; c = *fmt++;
@ -120,40 +120,35 @@ void chprintf(BaseChannel *chp, const char *fmt, ...) {
c = va_arg(ap, int); c = va_arg(ap, int);
else else
break; break;
n *= 10; precision *= 10;
n += c; precision += c;
} }
} }
is_long = FALSE; /* Long modifier.*/
if (c == 'l' || c == 'L') { if (c == 'l' || c == 'L') {
is_long++; is_long = TRUE;
if (*fmt) if (*fmt)
c = *fmt++; c = *fmt++;
} }
else
is_long = (c >= 'A') && (c <= 'Z');
/* Command decoding.*/
switch (c) { switch (c) {
case 'X': case 'c':
is_long = TRUE; filler = ' ';
case 'x': *p++ = va_arg(ap, int);
c = 16; break;
goto skip; case 's':
case 'U': filler = ' ';
is_long = TRUE; if ((s = va_arg(ap, char *)) == 0)
case 'u': s = "(null)";
c = 10; if (precision == 0)
goto skip; precision = 32767;
case 'O': for (p = s; *p && (--precision >= 0); p++)
is_long = TRUE; ;
case 'o':
c = 8;
skip:
if (is_long)
l = va_arg(ap, long);
else
l = va_arg(ap, int);
p = ltoa(p, l, c);
break; break;
case 'D': case 'D':
is_long = TRUE;
case 'd': case 'd':
if (is_long) if (is_long)
l = va_arg(ap, long); l = va_arg(ap, long);
@ -165,18 +160,23 @@ skip:
} }
p = ltoa(p, l, 10); p = ltoa(p, l, 10);
break; break;
case 'c': case 'X':
filler = ' '; case 'x':
*p++ = va_arg(ap, int); c = 16;
break; goto unsigned_common;
case 's': case 'U':
filler = ' '; case 'u':
if ((s = va_arg(ap, char *)) == 0) c = 10;
s = "(null)"; goto unsigned_common;
if (n == 0) case 'O':
n = 32767; case 'o':
for (p = s; *p && --n >= 0; p++) c = 8;
; unsigned_common:
if (is_long)
l = va_arg(ap, long);
else
l = va_arg(ap, int);
p = ltoa(p, l, c);
break; break;
default: default:
*p++ = c; *p++ = c;
@ -185,7 +185,7 @@ skip:
i = (int)(p - s); i = (int)(p - s);
if ((width -= i) < 0) if ((width -= i) < 0)
width = 0; width = 0;
if (left_just == FALSE) if (left_align == FALSE)
width = -width; width = -width;
if (width < 0) { if (width < 0) {
if (*s == '-' && filler == '0') { if (*s == '-' && filler == '0') {