📄 stdiop_f.c
字号:
flags = flags | MINUS_FLAG;
width = 0 - width;
}
/*---------------------------------------------------------------*/
/* If c is '.', a precision field is specified. */
/*---------------------------------------------------------------*/
if (c == '.')
{
++format;
state = 5;
}
/*---------------------------------------------------------------*/
/* Else no special character found, continue. */
/*---------------------------------------------------------------*/
else
state = 8;
break;
/*-----------------------------------------------------------------*/
/* State 5 looks for the precision field. */
/*-----------------------------------------------------------------*/
case 5:
++format;
/*---------------------------------------------------------------*/
/* If c is digit, keep looking for digits until no more found. */
/*---------------------------------------------------------------*/
if (c >= '0' && c <= '9')
{
precision = c - '0';
precision_flag = 1;
state = 6;
}
/*---------------------------------------------------------------*/
/* Else if c is '*', get integer value from list of arguments. */
/*---------------------------------------------------------------*/
else if (c == '*')
state = 7;
/*---------------------------------------------------------------*/
/* Else no special character found, put it back and continue. */
/*---------------------------------------------------------------*/
else
{
state = 8;
--format;
}
break;
/*-----------------------------------------------------------------*/
/* State 6 keeps looking for digits until no more found. */
/*-----------------------------------------------------------------*/
case 6:
/*---------------------------------------------------------------*/
/* If c is a digit, add it to the precision field. */
/*---------------------------------------------------------------*/
if (isdigit(c))
{
precision = 10 * precision + c - '0';
++format;
}
/*---------------------------------------------------------------*/
/* Else no special character found, put it back and continue. */
/*---------------------------------------------------------------*/
else
state = 8;
break;
/*-----------------------------------------------------------------*/
/* State 7 fetches an int from list of args to be the precision. */
/*-----------------------------------------------------------------*/
case 7:
precision = va_arg(list_of_args, int);
/*---------------------------------------------------------------*/
/* If precision is negative, set it to 0. */
/*---------------------------------------------------------------*/
if (precision < 0)
precision = 0;
state = 8;
break;
/*-----------------------------------------------------------------*/
/* State 8 checks for 'l', 'L', 'h'. */
/*-----------------------------------------------------------------*/
case 8:
++format;
if (c == 'l')
special_value = l_VALUE;
else if (c == 'L')
special_value = L_VALUE;
else if (c == 'h')
special_value = h_VALUE;
else
--format;
state = 9;
break;
/*-----------------------------------------------------------------*/
/* State 9 checks for conversion specifiers ('x', 'd', etc). */
/*-----------------------------------------------------------------*/
case 9:
++format;
if (c == 'c')
state = 10;
else if (c == 'i' || c == 'd')
state = 11;
else if (c == 'n')
state = 12;
else if (c == 'o')
state = 13;
else if (c == 'p')
state = 14;
else if (c == 'x' || c == 'X')
{
/*-------------------------------------------------------------*/
/* Sets x to distinguish later between 'x' and 'X'. */
/*-------------------------------------------------------------*/
x = (c == 'x') ? 1 : 0;
state = 15;
}
else if (c == 's')
state = 16;
else if (c == 'u')
state = 17;
else if (c == 'f' || c == 'e' || c == 'E' || c == 'g' || c == 'G')
{
/*-------------------------------------------------------------*/
/* Sets e to distinguish later between 'e'('g') and 'E'('G'). */
/*-------------------------------------------------------------*/
state = 18;
e = ((c == 'e') || (c == 'g')) ? 1 : 0;
spec = c;
}
else
{
/*-------------------------------------------------------------*/
/* No valid specifiers following %, go back to initial state. */
/*-------------------------------------------------------------*/
state = 0;
--format;
}
break;
/*-----------------------------------------------------------------*/
/* State 10 handles the 'c' specifier. */
/*-----------------------------------------------------------------*/
case 10:
buf[BUF_BYTES - 1] = (char)va_arg(list_of_args, int);
/*---------------------------------------------------------------*/
/* Send buf to be printed. */
/*---------------------------------------------------------------*/
bytes = print_buf(stream, buf, BUF_BYTES, BUF_BYTES - 1, flags,
S_SPEC, width, precision);
if (bytes == -1)
return -1;
count += bytes;
state = 0;
break;
/*-----------------------------------------------------------------*/
/* State 11 handles the 'i' and 'd' specifiers */
/*-----------------------------------------------------------------*/
case 11:
/*---------------------------------------------------------------*/
/* Based on the special value get the correct argument */
/*---------------------------------------------------------------*/
if (special_value == h_VALUE)
val = (i16)va_arg(list_of_args, int);
else if (special_value == l_VALUE)
val = va_arg(list_of_args, long);
else
val = va_arg(list_of_args, int);
/*---------------------------------------------------------------*/
/* If val is negative, put '-' in front and make it positive. */
/*---------------------------------------------------------------*/
if (val < 0)
{
sign = 1;
val = -val;
}
else
sign = 0;
start = trnsfrm_val(buf, (ui32)val, 10, 0, precision);
if (sign)
{
if (start > 0) --start;
buf[start] = '-';
}
/*---------------------------------------------------------------*/
/* Send buf to be printed. */
/*---------------------------------------------------------------*/
bytes = print_buf(stream, buf, BUF_BYTES, start, flags, DI_SPEC,
width, precision);
if (bytes == -1)
return -1;
count += bytes;
/*---------------------------------------------------------------*/
/* Go back to initial state. */
/*---------------------------------------------------------------*/
state = 0;
break;
/*-----------------------------------------------------------------*/
/* State 12 handles the 'n' specifier. */
/*-----------------------------------------------------------------*/
case 12:
/*---------------------------------------------------------------*/
/* Based on special_value get correct ptr, and store count. */
/*---------------------------------------------------------------*/
if (special_value == h_VALUE)
*va_arg(list_of_args, i16 *) = (i16)count;
else if (special_value == l_VALUE)
*va_arg(list_of_args, i32 *) = count;
else
*va_arg(list_of_args, int *) = count;
/*---------------------------------------------------------------*/
/* Go back to initial state. */
/*---------------------------------------------------------------*/
state = 0;
break;
/*-----------------------------------------------------------------*/
/* State 13 handles the 'o' specifier. */
/*-----------------------------------------------------------------*/
case 13:
/*---------------------------------------------------------------*/
/* Based on special_value get the correct argument. */
/*---------------------------------------------------------------*/
if (special_value == h_VALUE)
unsigned_val = (ui16)va_arg(list_of_args, int);
else if (special_value == l_VALUE)
unsigned_val = (ui32)va_arg(list_of_args, long);
else
unsigned_val = (uint)va_arg(list_of_args, int);
start = trnsfrm_val(buf, unsigned_val, 8, 0, precision);
/*---------------------------------------------------------------*/
/* If '#' flag is set, add a '0' in front of the number. */
/*---------------------------------------------------------------*/
if (flags & NUM_FLAG)
{
if (start > 0) --start;
buf[start] = '0';
}
/*---------------------------------------------------------------*/
/* Send buf to be printed. */
/*---------------------------------------------------------------*/
bytes = print_buf(stream, buf, BUF_BYTES, start, flags, UO_SPEC,
width, precision);
if (bytes == -1)
return -1;
count += bytes;
/*---------------------------------------------------------------*/
/* Go back to initial state. */
/*---------------------------------------------------------------*/
state = 0;
break;
/*-----------------------------------------------------------------*/
/* State 14 handles the 'p' specifier. */
/*-----------------------------------------------------------------*/
case 14:
p = (int)va_arg(list_of_args, void *);
start = trnsfrm_val(buf, (ui32)p, 16, 0, precision);
/*---------------------------------------------------------------*/
/* Send buf to be printed. */
/*---------------------------------------------------------------*/
bytes = print_buf(stream, buf, BUF_BYTES, start, 0, P_SPEC, width,
precision);
if (bytes == -1)
return -1;
count += bytes;
/*---------------------------------------------------------------*/
/* Go back to initial state. */
/*---------------------------------------------------------------*/
state = 0;
break;
/*-----------------------------------------------------------------*/
/* State 15 handles the 'x' and 'X' specifiers. */
/*-----------------------------------------------------------------*/
case 15:
/*---------------------------------------------------------------*/
/* Based on special_value get the correct argument. */
/*---------------------------------------------------------------*/
if (special_value == h_VALUE)
unsigned_val = (ui16)va_arg(list_of_args, int);
else if (special_value == l_VALUE)
unsigned_val = (ui32)va_arg(list_of_args, long);
else
unsigned_val = (uint)va_arg(list_of_args, int);
start = trnsfrm_val(buf, unsigned_val, 16, x, precision);
/*---------------------------------------------------------------*/
/* If '#' flag is set, add '0x' or '0X' on front of the number. */
/*---------------------------------------------------------------*/
if (flags & NUM_FLAG)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -