2 * general implementation of scanf used by scanf, sscanf, fscanf,
3 * _cscanf, wscanf, swscanf and fwscanf
5 * Copyright 1996,1998 Marcus Meissner
6 * Copyright 1996 Jukka Iivonen
7 * Copyright 1997,2000, 2003 Uwe Bonnes
8 * Copyright 2000 Jon Griffiths
9 * Copyright 2002 Daniel Gudbjartsson
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #define _CHAR_ MSVCRT_wchar_t
28 #define _EOF_ MSVCRT_WEOF
29 #define _EOF_RET (short)MSVCRT_WEOF
30 #define _ISSPACE_(c) MSVCRT_iswspace(c)
31 #define _ISDIGIT_(c) MSVCRT_iswdigit(c)
32 #define _WIDE2SUPPORTED_(c) c /* No conversion needed (wide to wide) */
33 #define _CHAR2SUPPORTED_(c) c /* FIXME: convert char to wide char */
34 #define _CHAR2DIGIT_(c, base) wchar2digit((c), (base))
35 #define _BITMAPSIZE_ 256*256
36 #else /* WIDE_SCANF */
38 #define _EOF_ MSVCRT_EOF
39 #define _EOF_RET MSVCRT_EOF
40 #define _ISSPACE_(c) isspace(c)
41 #define _ISDIGIT_(c) isdigit(c)
42 #define _WIDE2SUPPORTED_(c) c /* FIXME: convert wide char to char */
43 #define _CHAR2SUPPORTED_(c) c /* No conversion needed (char to char) */
44 #define _CHAR2DIGIT_(c, base) char2digit((c), (base))
45 #define _BITMAPSIZE_ 256
46 #endif /* WIDE_SCANF */
49 #define _GETC_(file) (consumed++, _getch())
50 #define _UNGETC_(nch, file) do { _ungetch(nch); consumed--; } while(0)
51 #define _LOCK_FILE_(file) MSVCRT__lock_file(MSVCRT_stdin)
52 #define _UNLOCK_FILE_(file) MSVCRT__unlock_file(MSVCRT_stdin)
55 #define _FUNCTION_ static int MSVCRT_vcwscanf_s_l(const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
57 #define _FUNCTION_ static int MSVCRT_vcwscanf_l(const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
59 #else /* WIDE_SCANF */
61 #define _FUNCTION_ static int MSVCRT_vcscanf_s_l(const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
63 #define _FUNCTION_ static int MSVCRT_vcscanf_l(const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
65 #endif /* WIDE_SCANF */
72 #define _GETC_(file) (consumed==length ? '\0' : (consumed++, *file++))
73 #else /* WIDE_SCANF */
74 #define _GETC_(file) (consumed==length ? '\0' : (consumed++, (unsigned char)*file++))
75 #endif /* WIDE_SCANF */
76 #define _UNGETC_(nch, file) do { file--; consumed--; } while(0)
77 #define _LOCK_FILE_(file) do {} while(0)
78 #define _UNLOCK_FILE_(file) do {} while(0)
81 #define _FUNCTION_ static int MSVCRT_vsnwscanf_s_l(const MSVCRT_wchar_t *file, MSVCRT_size_t length, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
83 #define _FUNCTION_ static int MSVCRT_vsnwscanf_l(const MSVCRT_wchar_t *file, MSVCRT_size_t length, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
85 #else /* WIDE_SCANF */
87 #define _FUNCTION_ static int MSVCRT_vsnscanf_s_l(const char *file, MSVCRT_size_t length, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
89 #define _FUNCTION_ static int MSVCRT_vsnscanf_l(const char *file, MSVCRT_size_t length, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
91 #endif /* WIDE_SCANF */
92 #else /* STRING_LEN */
94 #define _GETC_(file) (consumed++, *file++)
95 #else /* WIDE_SCANF */
96 #define _GETC_(file) (consumed++, (unsigned char)*file++)
97 #endif /* WIDE_SCANF */
98 #define _UNGETC_(nch, file) do { file--; consumed--; } while(0)
99 #define _LOCK_FILE_(file) do {} while(0)
100 #define _UNLOCK_FILE_(file) do {} while(0)
103 #define _FUNCTION_ static int MSVCRT_vswscanf_s_l(const MSVCRT_wchar_t *file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
105 #define _FUNCTION_ static int MSVCRT_vswscanf_l(const MSVCRT_wchar_t *file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
107 #else /* WIDE_SCANF */
109 #define _FUNCTION_ static int MSVCRT_vsscanf_s_l(const char *file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
111 #define _FUNCTION_ static int MSVCRT_vsscanf_l(const char *file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
113 #endif /* WIDE_SCANF */
114 #endif /* STRING_LEN */
117 #define _GETC_(file) (consumed++, MSVCRT_fgetwc(file))
118 #define _UNGETC_(nch, file) do { MSVCRT_ungetwc(nch, file); consumed--; } while(0)
119 #define _LOCK_FILE_(file) MSVCRT__lock_file(file)
120 #define _UNLOCK_FILE_(file) MSVCRT__unlock_file(file)
122 #define _FUNCTION_ static int MSVCRT_vfwscanf_s_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
124 #define _FUNCTION_ static int MSVCRT_vfwscanf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
126 #else /* WIDE_SCANF */
127 #define _GETC_(file) (consumed++, MSVCRT_fgetc(file))
128 #define _UNGETC_(nch, file) do { MSVCRT_ungetc(nch, file); consumed--; } while(0)
129 #define _LOCK_FILE_(file) MSVCRT__lock_file(file)
130 #define _UNLOCK_FILE_(file) MSVCRT__unlock_file(file)
132 #define _FUNCTION_ static int MSVCRT_vfscanf_s_l(MSVCRT_FILE* file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
134 #define _FUNCTION_ static int MSVCRT_vfscanf_l(MSVCRT_FILE* file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
136 #endif /* WIDE_SCANF */
141 MSVCRT_pthreadlocinfo locinfo
;
142 int rd
= 0, consumed
= 0;
144 if (!*format
) return 0;
147 TRACE("(%s):\n", debugstr_a(format
));
150 TRACE("%s (%s)\n", debugstr_a(file
), debugstr_a(format
));
152 TRACE("%p (%s)\n", file
, debugstr_a(format
));
155 #endif /* WIDE_SCANF */
165 locinfo
= get_locinfo();
167 locinfo
= locale
->locinfo
;
170 /* a whitespace character in the format string causes scanf to read,
171 * but not store, all consecutive white-space characters in the input
172 * up to the next non-white-space character. One white space character
173 * in the input matches any number (including zero) and combination of
174 * white-space characters in the input. */
175 if (_ISSPACE_(*format
)) {
176 /* skip whitespace */
177 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
180 /* a format specification causes scanf to read and convert characters
181 * in the input into values of a specified type. The value is assigned
182 * to an argument in the argument list. Format specifications have
183 * the form %[*][width][{h | l | I64 | L}]type */
184 else if (*format
== '%') {
185 int st
= 0; int suppress
= 0; int width
= 0;
191 int prefix_finished
= 0;
194 /* look for leading asterisk, which means 'suppress assignment of
200 /* look for width specification */
201 while (_ISDIGIT_(*format
)) {
203 width
+=*format
++ - '0';
205 if (width
==0) width
=-1; /* no width spec seen */
206 /* read prefix (if any) */
207 while (!prefix_finished
) {
209 case 'h': h_prefix
++; break;
211 if(*(format
+1) == 'l') {
217 case 'w': w_prefix
= 1; break;
218 case 'L': L_prefix
= 1; break;
220 if (*(format
+ 1) == '6' &&
221 *(format
+ 2) == '4') {
229 if (!prefix_finished
) format
++;
234 case 'P': /* pointer. */
235 if (sizeof(void *) == sizeof(LONGLONG
)) I64_prefix
= 1;
238 case 'X': /* hexadecimal integer. */
241 case 'o': /* octal integer */
244 case 'u': /* unsigned decimal integer */
247 case 'd': /* signed decimal integer */
250 case 'i': /* generic integer */
253 /* read an integer */
257 /* skip initial whitespace */
258 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
261 if (nch
== '-' || nch
== '+') {
262 negative
= (nch
=='-');
264 if (width
>0) width
--;
266 /* look for leading indication of base */
267 if (width
!=0 && nch
== '0' && *format
!= 'p' && *format
!= 'P') {
269 if (width
>0) width
--;
271 if (width
!=0 && (nch
=='x' || nch
=='X')) {
276 if (width
>0) width
--;
282 /* format %i without indication of base */
285 /* throw away leading zeros */
286 while (width
!=0 && nch
=='0') {
288 if (width
>0) width
--;
291 if (width
!=0 && _CHAR2DIGIT_(nch
, base
)!=-1) {
292 cur
= _CHAR2DIGIT_(nch
, base
);
294 if (width
>0) width
--;
297 /* read until no more digits */
298 while (width
!=0 && (nch
!=_EOF_
) && _CHAR2DIGIT_(nch
, base
)!=-1) {
299 cur
= cur
*base
+ _CHAR2DIGIT_(nch
, base
);
301 if (width
>0) width
--;
305 if (!seendigit
) break; /* not a valid number */
308 #define _SET_NUMBER_(type) *va_arg(ap, type*) = negative ? -cur : cur
309 if (I64_prefix
) _SET_NUMBER_(LONGLONG
);
310 else if (l_prefix
) _SET_NUMBER_(LONG
);
311 else if (h_prefix
== 1) _SET_NUMBER_(short int);
312 else _SET_NUMBER_(int);
320 case 'G': { /* read a float */
321 long double cur
= 1, expcnt
= 10;
323 int exp
= 0, negative
= 0;
327 /* skip initial whitespace */
328 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
332 if (nch
== '-' || nch
== '+') {
333 negative
= (nch
=='-');
334 if (width
>0) width
--;
339 /* get first digit. */
340 if (*locinfo
->lconv
->decimal_point
!= nch
) {
341 if (!_ISDIGIT_(nch
)) break;
344 if (width
>0) width
--;
345 /* read until no more digits */
346 while (width
!=0 && (nch
!=_EOF_
) && _ISDIGIT_(nch
)) {
347 hlp
= d
*10 + nch
- '0';
349 if (width
>0) width
--;
350 if(d
> (ULONGLONG
)-1/10 || hlp
<d
) {
357 while (width
!=0 && (nch
!=_EOF_
) && _ISDIGIT_(nch
)) {
360 if (width
>0) width
--;
363 d
= 0; /* Fix: .8 -> 0.8 */
366 /* handle decimals */
367 if (width
!=0 && nch
== *locinfo
->lconv
->decimal_point
) {
369 if (width
>0) width
--;
371 while (width
!=0 && (nch
!=_EOF_
) && _ISDIGIT_(nch
)) {
372 hlp
= d
*10 + nch
- '0';
374 if (width
>0) width
--;
375 if(d
> (ULONGLONG
)-1/10 || hlp
<d
)
381 while (width
!=0 && (nch
!=_EOF_
) && _ISDIGIT_(nch
)) {
383 if (width
>0) width
--;
387 /* handle exponent */
388 if (width
!=0 && (nch
== 'e' || nch
== 'E')) {
392 if (width
>0) width
--;
393 if (width
!=0 && (nch
=='+' || nch
=='-')) {
397 if (width
>0) width
--;
400 /* exponent digits */
401 while (width
!=0 && (nch
!=_EOF_
) && _ISDIGIT_(nch
)) {
402 if(e
> INT_MAX
/10 || (e
= e
*10 + nch
- '0')<0)
405 if (width
>0) width
--;
409 if(exp
<0 && e
<0 && e
+exp
>0) exp
= INT_MIN
;
410 else if(exp
>0 && e
>0 && e
+exp
<0) exp
= INT_MAX
;
414 fpcontrol
= _control87(0, 0);
415 _control87(MSVCRT__EM_DENORMAL
|MSVCRT__EM_INVALID
|MSVCRT__EM_ZERODIVIDE
416 |MSVCRT__EM_OVERFLOW
|MSVCRT__EM_UNDERFLOW
|MSVCRT__EM_INEXACT
, 0xffffffff);
421 /* update 'cur' with this exponent. */
426 expcnt
= expcnt
*expcnt
;
428 cur
= (negexp
? d
/cur
: d
*cur
);
430 _control87(fpcontrol
, 0xffffffff);
434 if (L_prefix
|| l_prefix
) _SET_NUMBER_(double);
435 else _SET_NUMBER_(float);
439 /* According to msdn,
440 * 's' reads a character string in a call to fscanf
441 * and 'S' a wide character string and vice versa in a
442 * call to fwscanf. The 'h', 'w' and 'l' prefixes override
443 * this behaviour. 'h' forces reading char * but 'l' and 'w'
444 * force reading WCHAR. */
446 if (w_prefix
|| l_prefix
) goto widecharstring
;
447 else if (h_prefix
) goto charstring
;
449 else goto widecharstring
;
450 #else /* WIDE_SCANF */
451 else goto charstring
;
452 #endif /* WIDE_SCANF */
454 if (w_prefix
|| l_prefix
) goto widecharstring
;
455 else if (h_prefix
) goto charstring
;
457 else goto charstring
;
458 #else /* WIDE_SCANF */
459 else goto widecharstring
;
460 #endif /* WIDE_SCANF */
461 charstring
: { /* read a word into a char */
462 char *sptr
= suppress
? NULL
: va_arg(ap
, char*);
463 char *sptr_beg
= sptr
;
465 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned);
467 unsigned size
= UINT_MAX
;
469 /* skip initial whitespace */
470 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
472 /* read until whitespace */
473 while (width
!=0 && (nch
!=_EOF_
) && !_ISSPACE_(nch
)) {
475 *sptr
++ = _CHAR2SUPPORTED_(nch
);
485 if (width
>0) width
--;
488 if (st
&& !suppress
) *sptr
= 0;
491 widecharstring
: { /* read a word into a wchar_t* */
492 MSVCRT_wchar_t
*sptr
= suppress
? NULL
: va_arg(ap
, MSVCRT_wchar_t
*);
493 MSVCRT_wchar_t
*sptr_beg
= sptr
;
495 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned);
497 unsigned size
= UINT_MAX
;
499 /* skip initial whitespace */
500 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
502 /* read until whitespace */
503 while (width
!=0 && (nch
!=_EOF_
) && !_ISSPACE_(nch
)) {
505 *sptr
++ = _WIDE2SUPPORTED_(nch
);
515 if (width
>0) width
--;
518 if (st
&& !suppress
) *sptr
= 0;
521 /* 'c' and 'C work analogously to 's' and 'S' as described
524 if (w_prefix
|| l_prefix
) goto widecharacter
;
525 else if (h_prefix
) goto character
;
527 else goto widecharacter
;
528 #else /* WIDE_SCANF */
530 #endif /* WIDE_SCANF */
532 if (w_prefix
|| l_prefix
) goto widecharacter
;
533 else if (h_prefix
) goto character
;
536 #else /* WIDE_SCANF */
537 else goto widecharacter
;
538 #endif /* WIDE_SCANF */
539 character
: { /* read single character into char */
540 char *str
= suppress
? NULL
: va_arg(ap
, char*);
543 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned);
545 unsigned size
= UINT_MAX
;
547 if (width
== -1) width
= 1;
548 while (width
&& (nch
!= _EOF_
))
557 *str
++ = _CHAR2SUPPORTED_(nch
);
565 widecharacter
: { /* read single character into a wchar_t */
566 MSVCRT_wchar_t
*str
= suppress
? NULL
: va_arg(ap
, MSVCRT_wchar_t
*);
567 MSVCRT_wchar_t
*pstr
= str
;
569 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned);
571 unsigned size
= UINT_MAX
;
573 if (width
== -1) width
= 1;
574 while (width
&& (nch
!= _EOF_
))
583 *str
++ = _WIDE2SUPPORTED_(nch
);
593 int*n
= va_arg(ap
, int*);
596 /* This is an odd one: according to the standard,
597 * "Execution of a %n directive does not increment the
598 * assignment count returned at the completion of
599 * execution" even if it wasn't suppressed with the
600 * '*' flag. The Corrigendum to the standard seems
601 * to contradict this (comment out the assignment to
602 * suppress below if you want to implement these
603 * alternate semantics) but the windows program I'm
604 * looking at expects the behavior I've coded here
605 * (which happens to be what glibc does as well).
612 _CHAR_
*str
= suppress
? NULL
: va_arg(ap
, _CHAR_
*);
616 int invert
= 0; /* Set if we are NOT to find the chars */
618 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned);
620 unsigned size
= UINT_MAX
;
623 /* Init our bitmap */
624 Mask
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, _BITMAPSIZE_
/8);
625 RtlInitializeBitMap(&bitMask
, Mask
, _BITMAPSIZE_
);
627 /* Read the format */
634 RtlSetBits(&bitMask
, ']', 1);
637 while(*format
&& (*format
!= ']')) {
638 /* According to msdn:
639 * "Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]." */
640 if((*format
== '-') && (*(format
+ 1) != ']')) {
641 if ((*(format
- 1)) < *(format
+ 1))
642 RtlSetBits(&bitMask
, *(format
- 1) +1 , *(format
+ 1) - *(format
- 1));
644 RtlSetBits(&bitMask
, *(format
+ 1) , *(format
- 1) - *(format
+ 1));
647 RtlSetBits(&bitMask
, *format
, 1);
650 /* read until char is not suitable */
651 while ((width
!= 0) && (nch
!= _EOF_
)) {
653 if(RtlAreBitsSet(&bitMask
, nch
, 1)) {
654 if (!suppress
) *sptr
++ = _CHAR2SUPPORTED_(nch
);
658 if(RtlAreBitsClear(&bitMask
, nch
, 1)) {
659 if (!suppress
) *sptr
++ = _CHAR2SUPPORTED_(nch
);
665 if (width
>0) width
--;
670 HeapFree(GetProcessHeap(), 0, Mask
);
675 if (!suppress
) *sptr
= 0;
676 HeapFree(GetProcessHeap(), 0, Mask
);
680 /* From spec: "if a percent sign is followed by a character
681 * that has no meaning as a format-control character, that
682 * character and the following characters are treated as
683 * an ordinary sequence of characters, that is, a sequence
684 * of characters that must match the input. For example,
685 * to specify that a percent-sign character is to be input,
687 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
689 if ((_CHAR_
)nch
== *format
) {
690 suppress
= 1; /* whoops no field to be read */
691 st
= 1; /* but we got what we expected */
696 if (st
&& !suppress
) rd
++;
699 /* a non-white-space character causes scanf to read, but not store,
700 * a matching non-white-space character. */
702 /* check for character match */
703 if ((_CHAR_
)nch
== *format
) {
713 TRACE("returning %d\n", rd
);
723 #undef _CHAR2SUPPORTED_
724 #undef _WIDE2SUPPORTED_