static CONTEXTCALL __print_string(char * c, context_p o) { if(c) { for(; *c; c++) __print_char(*c, o); } else { debug(DEBUG_ERR, "NULL pointer"); __print_char('N', o); __print_char('U', o); __print_char('L', o); __print_char('L', o); } return 0; }
/** * Print a number to the given string, with the given base. */ static int __print_int( _print_ctx_t* ctx, int i, int b, int sg, int width, int pad, int letbase, int print_limit ) { char print_buf[_PRINTFMT_INT_BUF_LEN]; register char *s; register int t, neg = 0, pc = 0; register unsigned int u = i; if( i == 0 ) { print_buf[0] = '0'; print_buf[1] = '\0'; return __print_str( ctx, print_buf, width, pad, print_limit, true ); } if( sg && b == 10 && i < 0 ) { neg = 1; u = -i; } s = print_buf + _PRINTFMT_INT_BUF_LEN - 1; *s = '\0'; while( u ) { t = u % b; if( t >= 10 ) t += letbase - '0' - 10; *--s = t + '0'; u /= b; } if( neg ) { if( width && ( pad & _PRINTFMT_PAD_ZERO ) ) { __print_char( ctx, '-' ); ++pc; --width; } else { *--s = '-'; } } return pc + __print_str( ctx, s, width, pad, print_limit, true ); }
/** * Print a string to a given string. */ static int __print_str( _print_ctx_t* ctx, const char *string, int width, int pad, int print_limit, bool is_number ) { int pc = 0; int padchar = ' '; int i, len; if( width > 0 ) { register int len = 0; register const char *ptr; for( ptr = string; *ptr; ++ptr ) ++len; if( len >= width ) width = 0; else width -= len; if( pad & _PRINTFMT_PAD_ZERO ) padchar = '0'; } if( !( pad & _PRINTFMT_PAD_RIGHT ) ) { for( ; width > 0; --width ) { __print_char( ctx, padchar ); ++pc; } } // The string to print is not the result of a number conversion to ascii. if( false == is_number ) { // For a string, printlimit is the max number of characters to display. for( ; print_limit && *string; ++string, --print_limit ) { __print_char( ctx, *string ); ++pc; } } // The string to print represents an integer number. if( true == is_number ) { // In this case, printlimit is the min number of digits to print. // If the length of the number to print is less than the min nb of i // digits to display, we add 0 before printing the number. len = strlen( string ); if( len < print_limit ) { i = print_limit - len; for( ; i; i-- ) { __print_char( ctx, '0' ); ++pc; } } } /* * Else: The string to print is not the result of a number conversion to ascii. * For a string, printlimit is the max number of characters to display. */ for( ; print_limit && *string; ++string, --print_limit ) { __print_char( ctx, *string ); ++pc; } for( ; width > 0; --width ) { __print_char( ctx, padchar ); ++pc; } return pc; }