📄 scanf.h
字号:
/*
* general implementation of scanf used by scanf, sscanf, fscanf,
* _cscanf, wscanf, swscanf and fwscanf
*
* Copyright 1996,1998 Marcus Meissner
* Copyright 1996 Jukka Iivonen
* Copyright 1997,2000, 2003 Uwe Bonnes
* Copyright 2000 Jon Griffiths
* Copyright 2002 Daniel Gudbjartsson
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef WIDE_SCANF
#define _CHAR_ wchar_t
#define _EOF_ WEOF
#define _EOF_RET WEOF
#define _ISSPACE_(c) iswspace(c)
#define _ISDIGIT_(c) iswdigit(c)
#define _WIDE2SUPPORTED_(c) c /* No conversion needed (wide to wide) */
#define _CHAR2SUPPORTED_(c) c /* FIXME: convert char to wide char */
#define _CHAR2DIGIT_(c, base) wchar2digit((c), (base))
#define _BITMAPSIZE_ 256*256
#else /* WIDE_SCANF */
#define _CHAR_ char
#define _EOF_ EOF
#define _EOF_RET EOF
#define _ISSPACE_(c) isspace(c)
#define _ISDIGIT_(c) isdigit(c)
#define _WIDE2SUPPORTED_(c) c /* FIXME: convert wide char to char */
#define _CHAR2SUPPORTED_(c) c /* No conversion needed (char to char) */
#define _CHAR2DIGIT_(c, base) char2digit((c), (base))
#define _BITMAPSIZE_ 256
#endif /* WIDE_SCANF */
#ifdef CONSOLE
#define _GETC_(file) (consumed++, _getch())
#define _UNGETC_(nch, file) do { _ungetch(nch); consumed--; } while(0)
#define _FUNCTION_ int vcscanf(const char *format, va_list ap)
#else
#ifdef STRING
#undef _EOF_
#define _EOF_ 0
#define _GETC_(file) (consumed++, *file++)
#define _UNGETC_(nch, file) do { file--; consumed--; } while(0)
#ifdef WIDE_SCANF
#define _FUNCTION_ int vswscanf(const wchar_t *file, const wchar_t *format, va_list ap)
#else /* WIDE_SCANF */
#define _FUNCTION_ int vsscanf(const char *file, const char *format, va_list ap)
#endif /* WIDE_SCANF */
#else /* STRING */
#ifdef WIDE_SCANF
#define _GETC_(file) (consumed++, fgetwc(file))
#define _UNGETC_(nch, file) do { ungetwc(nch, file); consumed--; } while(0)
#define _FUNCTION_ int vfwscanf(FILE* file, const wchar_t *format, va_list ap)
#else /* WIDE_SCANF */
#define _GETC_(file) (consumed++, fgetc(file))
#define _UNGETC_(nch, file) do { ungetc(nch, file); consumed--; } while(0)
#define _FUNCTION_ int vfscanf(FILE* file, const char *format, va_list ap)
#endif /* WIDE_SCANF */
#endif /* STRING */
#endif /* CONSOLE */
/*********************************************************************
* Implemented based on
* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt_format_specification_fields_.2d_.scanf_and_wscanf_functions.asp
* Extended by C. Scott Ananian <cananian@alumni.princeton.edu> to handle
* more types of format spec.
*/
_FUNCTION_ {
int rd = 0, consumed = 0;
int nch;
if (!*format) return 0;
#ifndef WIDE_SCANF
#ifdef CONSOLE
TRACE("(%s): \n", debugstr_a(format));
#else /* CONSOLE */
#ifdef STRING
TRACE("%s (%s)\n", file, debugstr_a(format));
#else /* STRING */
TRACE("%p (%s)\n", file, debugstr_a(format));
#endif /* STRING */
#endif /* CONSOLE */
#endif /* WIDE_SCANF */
nch = _GETC_(file);
if (nch == _EOF_) return _EOF_RET;
while (*format) {
/* a whitespace character in the format string causes scanf to read,
* but not store, all consecutive white-space characters in the input
* up to the next non-white-space character. One white space character
* in the input matches any number (including zero) and combination of
* white-space characters in the input. */
if (_ISSPACE_(*format)) {
/* skip whitespace */
while ((nch!=_EOF_) && _ISSPACE_(nch))
nch = _GETC_(file);
}
/* a format specification causes scanf to read and convert characters
* in the input into values of a specified type. The value is assigned
* to an argument in the argument list. Format specifications have
* the form %[*][width][{h | l | I64 | L}]type */
else if (*format == '%') {
int st = 0; int suppress = 0; int width = 0;
int base, number_signed;
int h_prefix = 0;
int l_prefix = 0;
int L_prefix = 0;
int w_prefix = 0;
int prefix_finished = 0;
int I64_prefix = 0;
format++;
/* look for leading asterisk, which means 'suppress assignment of
* this field'. */
if (*format=='*') {
format++;
suppress=1;
}
/* look for width specification */
while (_ISDIGIT_(*format)) {
width*=10;
width+=*format++ - '0';
}
if (width==0) width=-1; /* no width spec seen */
/* read prefix (if any) */
while (!prefix_finished) {
switch(*format) {
case 'h': h_prefix = 1; break;
case 'l': l_prefix = 1; break;
case 'w': w_prefix = 1; break;
case 'L': L_prefix = 1; break;
case 'I':
if (*(format + 1) == '6' &&
*(format + 2) == '4') {
I64_prefix = 1;
format += 2;
}
break;
default:
prefix_finished = 1;
}
if (!prefix_finished) format++;
}
/* read type */
switch(*format) {
case 'x':
case 'X': /* hexadecimal integer. */
base = 16; number_signed = 0;
goto number;
case 'o': /* octal integer */
base = 8; number_signed = 0;
goto number;
case 'u': /* unsigned decimal integer */
base = 10; number_signed = 0;
goto number;
case 'd': /* signed decimal integer */
base = 10; number_signed = 1;
goto number;
case 'i': /* generic integer */
base = 10; number_signed = 1;
number: {
/* read an integer */
ULONGLONG cur = 0;
int negative = 0;
int seendigit=0;
/* skip initial whitespace */
while ((nch!=_EOF_) && _ISSPACE_(nch))
nch = _GETC_(file);
/* get sign */
if (number_signed && (nch == '-' ||
nch == '+')) {
negative = (nch=='-');
nch = _GETC_(file);
if (width>0) width--;
}
/* look for leading indication of base */
if (width!=0 && nch == '0') {
nch = _GETC_(file);
if (width>0) width--;
seendigit=1;
if (width!=0 && (nch=='x' || nch=='X')) {
if (base==0)
base=16;
if (base==16) {
nch = _GETC_(file);
if (width>0) width--;
seendigit=0;
}
} else if (base==0)
base = 8;
}
/* throw away leading zeros */
while (width!=0 && nch=='0') {
nch = _GETC_(file);
if (width>0) width--;
seendigit=1;
}
if (width!=0 && _CHAR2DIGIT_(nch, base)!=-1) {
cur = _CHAR2DIGIT_(nch, base);
nch = _GETC_(file);
if (width>0) width--;
seendigit=1;
}
/* read until no more digits */
while (width!=0 && (nch!=_EOF_) && _CHAR2DIGIT_(nch, base)!=-1) {
cur = cur*base + _CHAR2DIGIT_(nch, base);
nch = _GETC_(file);
if (width>0) width--;
seendigit=1;
}
/* okay, done! */
if (!seendigit) break; /* not a valid number */
st = 1;
if (!suppress) {
#define _SET_NUMBER_(type) *va_arg(ap, type*) = negative ? -cur : cur
if (number_signed) {
if (I64_prefix) _SET_NUMBER_(LONGLONG);
else if (l_prefix) _SET_NUMBER_(long int);
else if (h_prefix) _SET_NUMBER_(short int);
else _SET_NUMBER_(int);
} else {
if (negative) {
WARN("Dropping sign in reading a negative number into an unsigned value");
negative = 0;
}
if (I64_prefix) _SET_NUMBER_(ULONGLONG);
else if (l_prefix) _SET_NUMBER_(unsigned long int);
else if (h_prefix)
_SET_NUMBER_(unsigned short int);
else _SET_NUMBER_(unsigned int);
}
}
}
break;
case 'e':
case 'E':
case 'f':
case 'g':
case 'G': { /* read a float */
long double cur = 0;
int negative = 0;
/* skip initial whitespace */
while ((nch!=_EOF_) && _ISSPACE_(nch))
nch = _GETC_(file);
/* get sign. */
if (nch == '-' || nch == '+') {
negative = (nch=='-');
if (width>0) width--;
if (width==0) break;
nch = _GETC_(file);
}
/* get first digit. */
if ('.' != nch) {
if (!_ISDIGIT_(nch)) break;
cur = (nch - '0');
nch = _GETC_(file);
if (width>0) width--;
/* read until no more digits */
while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
cur = cur*10 + (nch - '0');
nch = _GETC_(file);
if (width>0) width--;
}
} else {
cur = 0; /* MaxPayneDemo Fix: .8 -> 0.8 */
}
/* handle decimals */
if (width!=0 && nch == '.') {
float dec = 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -