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
112 if( c ==
'\r' || c ==
'\n' ) {
113 if( ctx->_max_len > 1 ) {
126 if( ctx->_max_len ) {
133 __putc( (uint8_t)c );
152 register int len = 0;
153 register const char *ptr;
154 for( ptr =
string; *ptr; ++ptr )
164 for( ; width > 0; --width ) {
171 if(
false == is_number ) {
173 for( ; print_limit && *string; ++string, --print_limit ) {
180 if(
true == is_number ) {
185 len = strlen(
string );
186 if( len < print_limit ) {
187 i = print_limit - len;
199 for( ; print_limit && *string; ++string, --print_limit ) {
204 for( ; width > 0; --width ) {
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 ) {
246 t += letbase -
'0' - 10;
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 ==
'-' ) {
301 while( *format ==
'0' ) {
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 );
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 );
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 );
397 int printf(
const char *format, ... )
426 va_start( args, format );
#define _PRINTFMT_PAD_RIGHT
Pad string to right.
static int __print_int(_print_ctx_t *ctx, int i, int b, int sg, int width, int pad, int letbase, int print_limit)
Print a number to the given string, with the given base.
#define _PRINTFMT_INT_BUF_LEN
The following should be enough for 32 bit int.
static void __print_char(_print_ctx_t *ctx, char c)
Print a character to stdout (if string is null) otherwise, put the character at the end of the provid...
#define _PRINTFMT_PAD_ZERO
Pad the number with zeroes.
#define NULL
The null pointer.
static int __print_str(_print_ctx_t *ctx, const char *string, int width, int pad, int print_limit, bool is_number)
Print a string to a given string.
static int __print_fmt(_print_ctx_t *ctx, const char *format, va_list args)
Print the given arguments, with given format onto string out.