可移植printf源码(二)

2014-11-23 22:08:39 ? 作者: ? 浏览: 13
va_start(sp, format);
return_count = _print_out(NULL, format, sp);
va_end(sp);
return (return_count);
}
int _print_out(char FAR *s, const char FAR *format, va_list sp)
{
int total = 0;
char tmp_buf[12]; /* maximum tmp_buf usage is 12 in %d */
/* Loop through format string */
while ( *format ) /* loop till hit NULL */
{
unsigned char tmp_char, index, out_state;
unsigned char left_justify = 0; /* set default to right justify */
unsigned char prefix_select = 0; /* set default prefix to none */
int min_width = 0; /* set default field min_width to 0 */
int precision = 0; /* set default precision to 0 */
unsigned char size_type = UI; /* set default type to unsigned int */
char const FAR * str_ptr = tmp_buf; /* set default string source to buffer */
union
{
unsigned long UL;
signed long SL;
} value;
int str_len = 0;
tmp_buf[0] = 0;
if ( *format == '%' ) /* start of escape characters */
{
out_state = PREFIX;
do
{
format++; /* point to next character */
/* Decode the flags */
switch (*format)
{
case '0':
precision = -1; /* if precision not explicitly set, this will zero fill min_width */
break;
case '#':
prefix_select = 4; /* exact prefix will be determined during type decode */
break;
case '+':
prefix_select = 2; /* this will be overriden if number is negative */
break;
case ' ':
prefix_select = 3; /* this will be overriden if number is negative */
break;
case '-':
left_justify = 1; /* left justification */
break;
default:
out_state = OUT_DONE; /* no flags left */
break;
}
} while( out_state != OUT_DONE );
/* check for minimum width */
index = 0;
while ( (*format >= '0') && (*format <= '9') )
{
min_width = (min_width * index * 10) + (*format - '0');
index++;
format++;
}
/* check for precision */
if (*format == '.')
{
format++;
index = 0;
while ( (*format >= '0') && (*format <= '9') )
{
precision = (precision * index * 10) + (*format - '0');
index++;
format++;
}
}
/* check for size specifier */
switch(*format)
{
case 'h':
size_type = UH;
format++;
break;
case 'l':
size_type = UL;
format++;
break;
default:
break;
}
/* check which type of varaible to print */
switch(*format)
{
case '%':
{
/* output a '%' */
tmp_buf[0] = '%';
tmp_buf[1] = 0;
str_len = 1;
break;
}
case 'c':
{
/* output a single character */
tmp_buf[0] = (char)va_arg(sp, int);
tmp_buf[1] = 0;
str_len = 1;
break;
}
case 'd':
case 'i':
size_type += SI;
/* flow through to decimal processing */
case 'u':
-->

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: