75 #define __putc(x) uart1_putc(x)
88 typedef struct __print_ctx_t _print_ctx_t;
93 #define _PRINTFMT_PAD_RIGHT 1
98 #define _PRINTFMT_PAD_ZERO 2
103 #define _PRINTFMT_INT_BUF_LEN 12
109 static void __print_char( _print_ctx_t* ctx,
char c )
112 if( c ==
'\r' || c ==
'\n' ) {
113 if( ctx->_max_len > 1 ) {
126 if( ctx->_max_len ) {
133 __putc( (uint8_t)c );
140 static int __print_str( _print_ctx_t* ctx,
152 register int len = 0;
153 register const char *ptr;
154 for( ptr =
string; *ptr; ++ptr )
160 if( pad & _PRINTFMT_PAD_ZERO )
163 if( !( pad & _PRINTFMT_PAD_RIGHT ) ) {
164 for( ; width > 0; --width ) {
165 __print_char( ctx, padchar );
171 if(
false == is_number ) {
173 for( ; print_limit && *string; ++string, --print_limit ) {
174 __print_char( ctx, *
string );
180 if(
true == is_number ) {
185 len = strlen(
string );
186 if( len < print_limit ) {
187 i = print_limit - len;
189 __print_char( ctx,
'0' );
199 for( ; print_limit && *string; ++string, --print_limit ) {
200 __print_char( ctx, *
string );
204 for( ; width > 0; --width ) {
205 __print_char( ctx, padchar );
215 static int __print_int( _print_ctx_t* ctx,
224 char print_buf[_PRINTFMT_INT_BUF_LEN];
226 register int t, neg = 0, pc = 0;
227 register unsigned int u = i;
232 return __print_str( ctx, print_buf, width, pad, print_limit,
true );
235 if( sg && b == 10 && i < 0 ) {
240 s = print_buf + _PRINTFMT_INT_BUF_LEN - 1;
246 t += letbase -
'0' - 10;
252 if( width && ( pad & _PRINTFMT_PAD_ZERO ) ) {
253 __print_char( ctx,
'-' );
261 return pc + __print_str( ctx, s, width, pad, print_limit,
true );
275 static int __print_fmt( _print_ctx_t* ctx,
const char *format, va_list args )
283 for( ; *format != 0; ++format ) {
284 if( *format ==
'%' ) {
286 width = pad = print_limit = 0;
288 if( *format ==
'\0' ) {
292 if( *format ==
'%' ) {
296 if( *format ==
'-' ) {
298 pad = _PRINTFMT_PAD_RIGHT;
301 while( *format ==
'0' ) {
303 pad |= _PRINTFMT_PAD_ZERO;
306 for( ; *format >=
'0' && *format <=
'9'; ++format ) {
308 width += *format -
'0';
311 if( *format ==
'.' ) {
313 for( ; *format >=
'0' && *format <=
'9'; ++format ) {
315 print_limit += *format -
'0';
319 if( 0 == print_limit ) {
323 if( *format ==
'l' ) {
327 if( *format ==
's' ) {
328 register char *s = (
char *) va_arg( args,
int );
329 pc += __print_str( ctx,
338 if( *format ==
'd' ) {
339 pc += __print_int( ctx, va_arg( args,
int ), 10, 1, width, pad,
'a', print_limit );
343 if( ( *format ==
'x' ) || ( *format ==
'p' ) ) {
344 pc += __print_int( ctx, va_arg( args,
int ), 16, 0, width, pad,
'a', print_limit );
348 if( *format ==
'X' ) {
349 pc += __print_int( ctx, va_arg( args,
int ), 16, 0, width, pad,
'A', print_limit );
353 if( *format ==
'u' ) {
354 pc += __print_int( ctx, va_arg( args,
int ), 10, 0, width, pad,
'a', print_limit );
358 if( *format ==
'c' ) {
360 scr[0] = (char) va_arg( args,
int );
362 pc += __print_str( ctx, scr, width, pad, print_limit,
false );
367 __print_char( ctx, *format );
372 if( ctx && ctx->_max_len ) {
379 #define BLOCK_MEM_SIZE 1024
381 int sprintf(
char *out,
const char *format, ... )
388 ctx._max_len = BLOCK_MEM_SIZE;
390 va_start( args, format );
391 retval = __print_fmt( &ctx, format, args );
397 int printf(
const char *format, ... )
426 va_start( args, format );
427 retval = __print_fmt(
NULL, format, args );