📄 stdiop_f.c
字号:
}
else
buf[index] = '.';
}
for (index = 17; index < 23; ++index)
{
/*-----------------------------------------------------------------*/
/* Fill the rest of the mantissa with 0s (x.xxx...000). */
/*-----------------------------------------------------------------*/
buf[index] = '0';
}
/*-------------------------------------------------------------------*/
/* Store the exponent as E+/-xxxx. */
/*-------------------------------------------------------------------*/
buf[23] = 'E';
/*-------------------------------------------------------------------*/
/* Store the sign of the exponent (required); either '+' or '-'. */
/*-------------------------------------------------------------------*/
if (exponent < 0)
{
exponent = 0 - exponent;
buf[24] = '-';
}
else
buf[24] = '+';
for (index = 0; index < 4; ++index)
{
/*-----------------------------------------------------------------*/
/* If exponent < 10, store it and set it to 0, else store it and */
/* divide it by 10. */
/*-----------------------------------------------------------------*/
if (exponent < 10)
{
temp_buf[index] = (char)(exponent + '0');
if (exponent)
exponent = 0;
}
else
{
temp_buf[index] = (char)(exponent % 10 + '0');
exponent = exponent / 10;
}
}
for (index = 3; index >= 0; index--)
{
/*-----------------------------------------------------------------*/
/* Store the value of the exponent (temp_buf) in buf. */
/*-----------------------------------------------------------------*/
buf[28 - index] = temp_buf[index];
}
}
/***********************************************************************/
/* Global Function Definitions */
/***********************************************************************/
/***********************************************************************/
/* StringWrite: String write function used by file control block */
/* */
/* Inputs: stream = pointer to file control block */
/* buf = buffer to read data from */
/* len = number of bytes to read */
/* */
/* Returns: Number of bytes successfully written, -1 on error */
/* */
/***********************************************************************/
int StringWrite(FILE *stream, const ui8 *buf, ui32 len)
{
ui32 pos = (ui32)stream->pos;
memcpy(&((ui8 *)stream->handle)[pos], buf, len);
stream->pos = (void *)(pos + len);
return (int)len;
}
/***********************************************************************/
/* StringWriteN: String write function used by file control block when */
/* there is a limit on string size (snprintf, etc.) */
/* */
/* Inputs: stream = pointer to file control block */
/* buf = buffer to read data from */
/* len = number of bytes to read */
/* */
/* Returns: Number of bytes successfully written, -1 when string */
/* limit has been reached */
/* */
/***********************************************************************/
int StringWriteN(FILE *stream, const ui8 *buf, ui32 len)
{
ui32 pos = (ui32)stream->pos;
ui32 n = (ui32)stream->volume;
/*-------------------------------------------------------------------*/
/* Write to string only when n is positive. */
/*-------------------------------------------------------------------*/
if (n)
{
/*-----------------------------------------------------------------*/
/* If not enough space to write, write as much as possible and */
/* stop processing. */
/*-----------------------------------------------------------------*/
if (pos + len > n)
{
memcpy(&((ui8 *)stream->handle)[pos], buf, n - pos);
stream->pos = (void *)n;
return -1;
}
else
{
memcpy(&((ui8 *)stream->handle)[pos], buf, len);
stream->pos = (void *)(pos + len);
}
}
/*-------------------------------------------------------------------*/
/* For n == 0, process the print function without writing to string. */
/*-------------------------------------------------------------------*/
else
stream->pos = (void *)(pos + len);
return (int)len;
}
/***********************************************************************/
/* ProcessPrintf: Print format and list_of_args to stream */
/* */
/* Inputs: stream = pointer to file control block */
/* format = string to be printed */
/* list_of_args = arguments from the print functions */
/* */
/* Returns: number of characters written to stream or s */
/* */
/***********************************************************************/
int ProcessPrintf(FILE *stream, const char *format, va_list list_of_args)
{
int special_value, start, x, flags, count = 0, state = 0, bytes;
int width, precision, sign, e, p, length, normal_chars = 0;
int precision_flag = 0, exponent;
char buf[BUF_BYTES + 1], real_buf[29];
i8 *str, c, spec;
ui32 unsigned_val;
i32 val;
long double real_num;
buf[BUF_BYTES] = '\0';
for (;;)
{
/*-----------------------------------------------------------------*/
/* Read in a char at a time and go to right state. */
/*-----------------------------------------------------------------*/
c = *format;
switch (state)
{
/*-----------------------------------------------------------------*/
/* State 0 reads in chars until it finds '%' or '\0'. */
/*-----------------------------------------------------------------*/
case 0:
flags = special_value = DEFAULT_FLAG;
width = precision_flag = 0;
precision = 0;
++format;
/*---------------------------------------------------------------*/
/* If either a '%' or '\0' encountered, move to different state */
/* or quit based on which one found. */
/*---------------------------------------------------------------*/
if (c == '\0' || c == '%')
{
/*-------------------------------------------------------------*/
/* First, print all the normal chars that have accumulated. */
/*-------------------------------------------------------------*/
if (normal_chars)
{
if (send_buf(stream, (char *)(format - normal_chars - 1),
normal_chars))
return -1;
count += normal_chars;
}
/*-------------------------------------------------------------*/
/* If '\0', return and if sprintf, terminate the string. */
/*-------------------------------------------------------------*/
if (c == '\0')
return count;
/*-------------------------------------------------------------*/
/* Else, '%', move to different state and reset normal chars */
/* counter. */
/*-------------------------------------------------------------*/
else
{
state = 1;
normal_chars = 0;
}
}
/*---------------------------------------------------------------*/
/* Else accumulate normal characters. */
/*---------------------------------------------------------------*/
else
++normal_chars;
break;
/*-----------------------------------------------------------------*/
/* State 1 looks for special flags ('0', '-', '#', or ' '). */
/*-----------------------------------------------------------------*/
case 1:
++format;
if (c == '0')
flags = flags | ZERO_FLAG;
else if (c == '-')
flags = flags | MINUS_FLAG;
else if (c == '+')
flags = flags | PLUS_FLAG;
else if (c == '#')
flags = flags | NUM_FLAG;
else if (c == ' ')
flags = flags | SPACE_FLAG;
else if (c == '%')
{
/*-------------------------------------------------------------*/
/* Write '%' and return back to initial state. */
/*-------------------------------------------------------------*/
if (send_char(stream, c))
return -1;
++count;
state = 0;
}
else
{
/*-------------------------------------------------------------*/
/* No special flags found anymore, so go to next state. */
/*-------------------------------------------------------------*/
state = 2;
--format;
}
break;
/*-----------------------------------------------------------------*/
/* State 2 looks for the width field. */
/*-----------------------------------------------------------------*/
case 2:
++format;
/*---------------------------------------------------------------*/
/* If c is a digit, keep looking for digits until no more found. */
/*---------------------------------------------------------------*/
if (c >= '0' && c <= '9')
{
state = 3;
width = c - '0';
}
/*---------------------------------------------------------------*/
/* Else if c is '*', get integer value from list of arguments. */
/*---------------------------------------------------------------*/
else if (c == '*')
state = 4;
/*---------------------------------------------------------------*/
/* Else if c is '.', a precision field is specified. */
/*---------------------------------------------------------------*/
else if (c == '.')
state = 5;
/*---------------------------------------------------------------*/
/* Else no special character found, put it back and continue. */
/*---------------------------------------------------------------*/
else
{
state = 8;
--format;
}
break;
/*-----------------------------------------------------------------*/
/* State 3 keeps looking for digits until no more found. */
/*-----------------------------------------------------------------*/
case 3:
++format;
/*---------------------------------------------------------------*/
/* If c is a digit, add it to the width field. */
/*---------------------------------------------------------------*/
if (isdigit(c))
width = 10 * width + c - '0';
/*---------------------------------------------------------------*/
/* Else if c is '.', a precision field is specified. */
/*---------------------------------------------------------------*/
else if (c == '.')
state = 5;
/*---------------------------------------------------------------*/
/* Else no special character found, put it back and continue. */
/*---------------------------------------------------------------*/
else
{
state = 8;
--format;
}
break;
/*-----------------------------------------------------------------*/
/* State 4 fetches an int from list of args to be the width. */
/*-----------------------------------------------------------------*/
case 4:
width = va_arg(list_of_args, int);
/*---------------------------------------------------------------*/
/* If width is negative, same as having a minus sign in flags. */
/*---------------------------------------------------------------*/
if (width < 0)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -