?? _scanfi.c
字號:
}
}
/*****************************************************************************/
/* _SGET_CONV - Read the format flags into the _SFIELD pointer sfield */
/* */
/* This function reads the characters directly after the '%' character, */
/* and stores them as flags in sfield, a pointer to a _SFIELD structure. */
/* These flags will later be used to process the conversion. */
/* */
/*****************************************************************************/
static int _sget_conv(char **_format, _SFIELD *sfield)
{
/*------------------------------------------------------------------------*/
/* Local variables */
/*------------------------------------------------------------------------*/
char tmpbuf[8],
*tmpptr = tmpbuf,
*strend = (*_format) + strlen(*_format);
(*_format)++; /* Go to the character after the '%' */
/*------------------------------------------------------------------------*/
/* If the next character in the format statement is a '*', set the */
/* _SFSTAR flag in sfield */
/*------------------------------------------------------------------------*/
if (**_format == '*')
{
_SET(sfield, _SFSTAR);
(*_format)++;
}
/*------------------------------------------------------------------------*/
/* If numerical characters follow, read them into a temporary string, */
/* convert it into a number, and store it as the field width in sfield */
/*------------------------------------------------------------------------*/
for(;**_format >= '0' && **_format <= '9'; *(tmpptr++) = *((*_format)++));
*tmpptr = '\0';
if (strlen(tmpbuf)) sfield->fwidth = atoi(tmpbuf);
/*------------------------------------------------------------------------*/
/* Set the h, l, or L flags if they were specified */
/*------------------------------------------------------------------------*/
switch(**_format)
{
case 'L': _SET(sfield, _MFLD); (*_format)++; break;
case 'h': _SET(sfield, _MFH); (*_format)++; break;
case 'l':
{
(*_format)++;
if (**_format == 'l') { _SET(sfield, _MFLL); (*_format)++; }
else _SET(sfield, _MFL);
}
}
/*------------------------------------------------------------------------*/
/* Read in the last character as the conversion specifier */
/*------------------------------------------------------------------------*/
sfield->conv = *((*_format)++);
/*------------------------------------------------------------------------*/
/* For the '[' conversion, read in the scanset. Return an EOF if */
/* _SGET_SCANSET fails. */
/*------------------------------------------------------------------------*/
if ((sfield->conv == '[') && _sget_scanset(sfield, _format)) return (EOF);
/*------------------------------------------------------------------------*/
/* If we read past the end of the format string, return an error */
/*------------------------------------------------------------------------*/
if (*_format > strend) return (EOF);
else return (0);
}
/*****************************************************************************/
/* _SGET_SCANSET - Read in the scanset from the format statement */
/* */
/* This function is called when the '[' conversion specifier has been */
/* encountered. It reads in the scanset from the format statement, */
/* and stores it in sfield for later reference during the conversion. */
/* */
/*****************************************************************************/
static int _sget_scanset(_SFIELD *sfield, char **_format)
{
/*------------------------------------------------------------------------*/
/* Local variables */
/*------------------------------------------------------------------------*/
char *tmpptr = sfield->scanset;
if (**_format == '^')
{
_SET(sfield, _SFCIRC);
(*_format)++;
}
if (**_format == ']') *(tmpptr++) = *((*_format)++);
while(**_format != ']' && **_format != '\0') *(tmpptr++) = *((*_format)++);
*tmpptr = '\0';
if (**_format == ']') (*_format)++;
if (**_format == '\0') return (EOF);
return(0);
}
/*****************************************************************************/
/* _SPROC_INT - Read an integer string into a temporary string */
/* */
/* This function takes the next integer in character form from the */
/* current input source, and copies it into a temporary string, where */
/* it can later be converted into a numerical value. */
/* */
/*****************************************************************************/
static int _sproc_int(int w_counter, int (*_inpchar)(void **inp),
void (*_uninpchar)(void **inp, char outchar),
char *tmpptr, char conv, void **inp, int *num_read)
{
/*------------------------------------------------------------------------*/
/* Note: w_counter is a parameter that holds the field width. When */
/* the number of digits specified by w_counter has been read from */
/* input, the function finishes. w_counter is checked before each */
/* read to make sure that it is not equal to zero, and it is */
/* decremented after each read. If no field width was specified, */
/* w_counter will be equal to -1, in which case it will never */
/* equal zero, and the function will read from input until it */
/* encounters the first invalid character. */
/*------------------------------------------------------------------------*/
/*------------------------------------------------------------------------*/
/* Local variables */
/*------------------------------------------------------------------------*/
signed char inchar;
int bnum_read = *num_read;
/*------------------------------------------------------------------------*/
/* Read in the next character */
/*------------------------------------------------------------------------*/
inchar = _inpchar(inp);
(*num_read)++;
/*------------------------------------------------------------------------*/
/* Accept the next character if it is a sign for the number */
/*------------------------------------------------------------------------*/
if ((inchar == '+' || inchar == '-') && w_counter != 0)
{
*(tmpptr++) = inchar;
inchar = _inpchar(inp);
(*num_read)++;
w_counter--;
}
/*------------------------------------------------------------------------*/
/* Accept a leading '0' for an octal number, or a '0x' or '0X' for a */
/* hexadecimal number. */
/*------------------------------------------------------------------------*/
if ((conv == 'o' || conv == 'i' || conv =='x' || conv == 'p') &&
w_counter != 0 && inchar == '0')
{
*(tmpptr++) = inchar;
inchar = _inpchar(inp);
(*num_read)++;
w_counter--;
}
if ((conv == 'x' || conv == 'p' || conv == 'i') && w_counter != 0 &&
(inchar == 'x' || inchar == 'X'))
{
*(tmpptr++) = inchar;
inchar = _inpchar(inp);
(*num_read)++;
w_counter--;
}
/*------------------------------------------------------------------------*/
/* Accept digits 0-9 for decimal numbers, or 0-F for hexadecimal numbers */
/*------------------------------------------------------------------------*/
while(((inchar >= '0' && inchar <= '9') ||
((conv == 'x' || conv == 'p' || conv == 'i') &&
((inchar >= 'A' && inchar <= 'F') ||
(inchar >= 'a' && inchar <= 'f')))) && w_counter != 0)
{
*(tmpptr++) = inchar;
inchar = _inpchar(inp);
(*num_read)++;
w_counter--;
}
_uninpchar(inp, inchar);
(*num_read)--;
*tmpptr = '\0';
if (bnum_read == *num_read) return(EOF);
return(1);
}
/*****************************************************************************/
/* _SPROC_FLOAT - Read a float string into a temporary string */
/* */
/* This function takes the next float in character form from the */
/* current input source, and copies it into a temporary string, where */
/* it can later be converted into a numerical value. */
/* */
/*****************************************************************************/
static int _sproc_float(int w_counter, int (*_inpchar)(void **inp),
void (*_uninpchar)(void **inp, char outchar),
char *tmpptr, char conv, void **inp, int *num_read)
{
/*------------------------------------------------------------------------*/
/* Note: w_counter is a parameter that holds the field width. When */
/* the number of digits specified by w_counter has been read from */
/* input, the function finishes. w_counter is checked before each */
/* read to make sure that it is not equal to zero, and it is */
/* decremented after each read. If no field width was specified, */
/* w_counter will be equal to -1, in which case it will never */
/* equal zero, and the function will read from input until it */
/* encounters the first invalid character. */
/*------------------------------------------------------------------------*/
/*------------------------------------------------------------------------*/
/* Local variables */
/*------------------------------------------------------------------------*/
signed char inchar;
int invalid = 0;
int bnum_read = *num_read;
int dec_flag = 0;
int e_flag = 0;
/*------------------------------------------------------------------------*/
/* Read in the next character */
/*------------------------------------------------------------------------*/
inchar = _inpchar(inp);
(*num_read)++;
/*------------------------------------------------------------------------*/
/* Accept the next character if it is a sign */
/*------------------------------------------------------------------------*/
if ((inchar == '+' || inchar == '-') && w_counter != 0)
{
*(tmpptr++) = inchar;
inchar = _inpchar(inp);
(*num_read)++;
w_counter--;
}
/*------------------------------------------------------------------------*/
/* Accept the next character if it is a numerical digit. */
/* The characters '.', 'e', 'E', '+', and '-' are accepted under the */
/* following conditions: */
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -