1
0
mirror of https://github.com/rfivet/stm32bringup.git synced 2024-12-24 01:26:39 -05:00
stm32bringup/printf.c

197 lines
4.9 KiB
C
Raw Normal View History

2021-06-13 22:41:15 -04:00
/* printf.c -- format and print data
** Copyright (c) 2020-2021 Renaud Fivet
** v3: %[flags][width]type
** v2: zero padding and uppercase/lowercase for hexadecimal
** v1: type=c,u,x,X,i,d,s,%
*/
#include <stdarg.h>
#include <stdio.h>
2021-06-13 22:41:15 -04:00
#include <string.h>
#include "system.h" /* kputc(), kputs() */
2021-06-13 22:41:15 -04:00
size_t strlen( const char *s) {
const char *end = s - 1 ;
while( *++end) ;
return end - s ;
}
2021-06-14 03:51:46 -04:00
#define LEFTALIGN (1 << 3)
#define ZEROPAD (1 << 2)
2021-06-13 22:41:15 -04:00
#define PLUSSIGN (1 << 1)
#define BLANKSIGN (1 << 0)
static const unsigned char base[] = {
16, /* Xx 0x58 0x78 */
10, /* iu 0x69 0x75 */
2, /* b 0x62 unused */
8, /* o 0x6F */
} ;
static const char signs[] = { 0, ' ', '+', '+'} ;
static int kputpad( char c, int len) {
int cnt ;
if( len > 0) {
cnt = len ;
while( len--)
kputc( c) ;
} else
cnt = 0 ;
2021-06-14 03:51:46 -04:00
return cnt ;
}
static int kputwidth( char *s, int left_aligned, int width) {
int cnt = left_aligned ? 0 : kputpad( ' ', width - strlen( s)) ;
int size = kputs( s) ;
cnt += size ;
if( left_aligned)
cnt += kputpad( ' ', width - size) ;
2021-06-13 22:41:15 -04:00
return cnt ;
}
2021-06-14 03:51:46 -04:00
static int kputu( unsigned u, int flags, int width, char fmt) {
#define MAXDIGITS 11
char s[ MAXDIGITS + 1] ; /* room for 11 octal digit + EOS */
/* octal 37777777777, decimal -2147483647 */
char *p = &s[ sizeof s - 1] ; /* point to last byte */
2021-06-13 22:41:15 -04:00
char signprefix ;
*p = 0 ; /* null terminated string */
2021-06-14 03:51:46 -04:00
if( fmt != 'i')
/* ouxX (d has been converted to i) */
signprefix = 0 ;
else if( 0 > (int) u) {
2021-06-13 22:41:15 -04:00
u = - (int) u ;
signprefix = '-' ;
} else
signprefix = signs[ flags & 3] ;
unsigned d = base[ fmt & 3] ;
fmt = fmt & 0x20 ; /* set uppercase bit */
do {
2021-06-13 22:41:15 -04:00
*--p = "0123456789ABCDEF"[ u % d] | fmt ;
u /= d ;
} while( u) ;
2021-06-14 03:51:46 -04:00
if( signprefix)
*--p = signprefix ;
int size = MAXDIGITS - (p - s) ; /* string length */
/* reuse u to return output length */
if( width <= size)
u = kputs( p) ;
else if( (flags & 0x0C) == ZEROPAD) { /* LEFTALIGN precedence over ZEROPAD */
/* handle zero padding */
if( signprefix)
kputc( *p++) ;
kputpad( '0', width - size) ;
kputs( p) ;
u = width ;
} else
/* handle left and right alignment */
u = kputwidth( p, flags >> 3, width) ;
2021-06-13 22:41:15 -04:00
return u ;
}
int printf( const char *fmt, ...) {
va_list ap ;
int cnt = 0 ;
int c ; /* current char in format string */
2021-06-14 03:51:46 -04:00
char *sp ;
va_start( ap, fmt) ;
while( ( c = *fmt++) != 0)
if( c != '%') {
cnt += 1 ; kputc( c) ;
} else {
2021-06-13 22:41:15 -04:00
/* %[flags][width]type */
int flags = 0 ;
int width = 0 ;
2021-06-13 22:41:15 -04:00
/* flags and width */
for( ;;) {
c = *fmt++ ;
2021-06-13 22:41:15 -04:00
switch( c) {
case ' ':
flags |= BLANKSIGN ;
continue ;
case '+':
flags |= PLUSSIGN ;
continue ;
case '-':
flags |= LEFTALIGN ;
continue ;
case '0':
flags |= ZEROPAD ;
continue ;
case '*':
width = va_arg( ap, int) ;
c = *fmt++ ;
break ;
default:
while( c >= '0' && c <= '9') {
width = width * 10 + ( c - '0') ;
c = *fmt++ ;
}
}
break ;
}
2021-06-13 22:41:15 -04:00
/* type */
if( c == 0)
break ;
switch( c) {
case 'c':
2021-06-14 03:51:46 -04:00
c = va_arg( ap, int) ;
if( width > 1) {
/* handle left and right alignment */
sp = (char *) &c ;
goto s_width ;
} else {
cnt += 1 ; kputc( c) ;
}
2021-06-13 22:41:15 -04:00
break ;
2021-06-13 22:41:15 -04:00
case 'd':
c = 'i' ;
/* fallthrough */
case 'i':
case 'o':
case 'u':
case 'x':
case 'X':
2021-06-14 03:51:46 -04:00
cnt += kputu( va_arg( ap, unsigned), flags, width, c) ;
break ;
2021-06-14 03:51:46 -04:00
case 's':
sp = va_arg( ap, char *) ;
if( width)
/* handle left and right alignment */
s_width:
cnt += kputwidth( sp, flags >> 3, width) ;
else
cnt += kputs( sp) ;
2021-06-13 22:41:15 -04:00
break ;
default:
cnt += 1 ; kputc( '%') ;
/* fallthrough */
case '%':
cnt += 1 ; kputc( c) ;
}
}
va_end( ap) ;
return cnt ;
}
/* end of printf.c */