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 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)
53 #define _FUNCTION_ static int MSVCRT_vcwscanf_s_l(const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
55 #define _FUNCTION_ static int MSVCRT_vcwscanf_l(const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
57 #else /* WIDE_SCANF */
59 #define _FUNCTION_ static int MSVCRT_vcscanf_s_l(const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
61 #define _FUNCTION_ static int MSVCRT_vcscanf_l(const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
63 #endif /* WIDE_SCANF */
68 #define _GETC_(file) (consumed++, *file++)
69 #define _UNGETC_(nch, file) do { file--; consumed--; } while(0)
72 #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)
74 #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)
76 #else /* WIDE_SCANF */
78 #define _FUNCTION_ static int MSVCRT_vsscanf_s_l(const char *file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
80 #define _FUNCTION_ static int MSVCRT_vsscanf_l(const char *file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
82 #endif /* WIDE_SCANF */
85 #define _GETC_(file) (consumed++, MSVCRT_fgetwc(file))
86 #define _UNGETC_(nch, file) do { MSVCRT_ungetwc(nch, file); consumed--; } while(0)
88 #define _FUNCTION_ static int MSVCRT_vfwscanf_s_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
90 #define _FUNCTION_ static int MSVCRT_vfwscanf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
92 #else /* WIDE_SCANF */
93 #define _GETC_(file) (consumed++, MSVCRT_fgetc(file))
94 #define _UNGETC_(nch, file) do { MSVCRT_ungetc(nch, file); consumed--; } while(0)
96 #define _FUNCTION_ static int MSVCRT_vfscanf_s_l(MSVCRT_FILE* file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
98 #define _FUNCTION_ static int MSVCRT_vfscanf_l(MSVCRT_FILE* file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
100 #endif /* WIDE_SCANF */
105 int rd
= 0, consumed
= 0;
107 if (!*format
) return 0;
110 TRACE("(%s):\n", debugstr_a(format
));
113 TRACE("%s (%s)\n", debugstr_a(file
), debugstr_a(format
));
115 TRACE("%p (%s)\n", file
, debugstr_a(format
));
118 #endif /* WIDE_SCANF */
120 if (nch
== _EOF_
) return _EOF_RET
;
123 locale
= get_locale();
126 /* a whitespace character in the format string causes scanf to read,
127 * but not store, all consecutive white-space characters in the input
128 * up to the next non-white-space character. One white space character
129 * in the input matches any number (including zero) and combination of
130 * white-space characters in the input. */
131 if (_ISSPACE_(*format
)) {
132 /* skip whitespace */
133 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
136 /* a format specification causes scanf to read and convert characters
137 * in the input into values of a specified type. The value is assigned
138 * to an argument in the argument list. Format specifications have
139 * the form %[*][width][{h | l | I64 | L}]type */
140 else if (*format
== '%') {
141 int st
= 0; int suppress
= 0; int width
= 0;
147 int prefix_finished
= 0;
150 /* look for leading asterisk, which means 'suppress assignment of
156 /* look for width specification */
157 while (_ISDIGIT_(*format
)) {
159 width
+=*format
++ - '0';
161 if (width
==0) width
=-1; /* no width spec seen */
162 /* read prefix (if any) */
163 while (!prefix_finished
) {
165 case 'h': h_prefix
= 1; break;
166 case 'l': l_prefix
= 1; break;
167 case 'w': w_prefix
= 1; break;
168 case 'L': L_prefix
= 1; break;
170 if (*(format
+ 1) == '6' &&
171 *(format
+ 2) == '4') {
179 if (!prefix_finished
) format
++;
184 case 'P': /* pointer. */
185 if (sizeof(void *) == sizeof(LONGLONG
)) I64_prefix
= 1;
188 case 'X': /* hexadecimal integer. */
191 case 'o': /* octal integer */
194 case 'u': /* unsigned decimal integer */
197 case 'd': /* signed decimal integer */
200 case 'i': /* generic integer */
203 /* read an integer */
207 /* skip initial whitespace */
208 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
211 if (nch
== '-' || nch
== '+') {
212 negative
= (nch
=='-');
214 if (width
>0) width
--;
216 /* look for leading indication of base */
217 if (width
!=0 && nch
== '0' && *format
!= 'p' && *format
!= 'P') {
219 if (width
>0) width
--;
221 if (width
!=0 && (nch
=='x' || nch
=='X')) {
226 if (width
>0) width
--;
232 /* format %i without indication of base */
235 /* throw away leading zeros */
236 while (width
!=0 && nch
=='0') {
238 if (width
>0) width
--;
241 if (width
!=0 && _CHAR2DIGIT_(nch
, base
)!=-1) {
242 cur
= _CHAR2DIGIT_(nch
, base
);
244 if (width
>0) width
--;
247 /* read until no more digits */
248 while (width
!=0 && (nch
!=_EOF_
) && _CHAR2DIGIT_(nch
, base
)!=-1) {
249 cur
= cur
*base
+ _CHAR2DIGIT_(nch
, base
);
251 if (width
>0) width
--;
255 if (!seendigit
) break; /* not a valid number */
258 #define _SET_NUMBER_(type) *va_arg(ap, type*) = negative ? -cur : cur
259 if (I64_prefix
) _SET_NUMBER_(LONGLONG
);
260 else if (l_prefix
) _SET_NUMBER_(LONG
);
261 else if (h_prefix
) _SET_NUMBER_(short int);
262 else _SET_NUMBER_(int);
270 case 'G': { /* read a float */
273 /* skip initial whitespace */
274 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
277 if (nch
== '-' || nch
== '+') {
278 negative
= (nch
=='-');
279 if (width
>0) width
--;
283 /* get first digit. */
284 if (*locale
->locinfo
->lconv
->decimal_point
!= nch
) {
285 if (!_ISDIGIT_(nch
)) break;
288 if (width
>0) width
--;
289 /* read until no more digits */
290 while (width
!=0 && (nch
!=_EOF_
) && _ISDIGIT_(nch
)) {
291 cur
= cur
*10 + (nch
- '0');
293 if (width
>0) width
--;
296 cur
= 0; /* Fix: .8 -> 0.8 */
298 /* handle decimals */
299 if (width
!=0 && nch
== *locale
->locinfo
->lconv
->decimal_point
) {
302 if (width
>0) width
--;
303 while (width
!=0 && (nch
!=_EOF_
) && _ISDIGIT_(nch
)) {
305 cur
+= dec
* (nch
- '0');
307 if (width
>0) width
--;
310 /* handle exponent */
311 if (width
!=0 && (nch
== 'e' || nch
== 'E')) {
312 int exponent
= 0, negexp
= 0;
315 if (width
>0) width
--;
316 /* possible sign on the exponent */
317 if (width
!=0 && (nch
=='+' || nch
=='-')) {
320 if (width
>0) width
--;
322 /* exponent digits */
323 while (width
!=0 && (nch
!=_EOF_
) && _ISDIGIT_(nch
)) {
325 exponent
+= (nch
- '0');
327 if (width
>0) width
--;
329 /* update 'cur' with this exponent. */
330 expcnt
= negexp
? .1 : 10;
331 while (exponent
!=0) {
335 expcnt
=expcnt
*expcnt
;
340 if (L_prefix
) _SET_NUMBER_(long double);
341 else if (l_prefix
) _SET_NUMBER_(double);
342 else _SET_NUMBER_(float);
346 /* According to msdn,
347 * 's' reads a character string in a call to fscanf
348 * and 'S' a wide character string and vice versa in a
349 * call to fwscanf. The 'h', 'w' and 'l' prefixes override
350 * this behaviour. 'h' forces reading char * but 'l' and 'w'
351 * force reading WCHAR. */
353 if (w_prefix
|| l_prefix
) goto widecharstring
;
354 else if (h_prefix
) goto charstring
;
356 else goto widecharstring
;
357 #else /* WIDE_SCANF */
358 else goto charstring
;
359 #endif /* WIDE_SCANF */
361 if (w_prefix
|| l_prefix
) goto widecharstring
;
362 else if (h_prefix
) goto charstring
;
364 else goto charstring
;
365 #else /* WIDE_SCANF */
366 else goto widecharstring
;
367 #endif /* WIDE_SCANF */
368 charstring
: { /* read a word into a char */
369 char *sptr
= suppress
? NULL
: va_arg(ap
, char*);
370 char *sptr_beg
= sptr
;
372 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned);
374 unsigned size
= UINT_MAX
;
376 /* skip initial whitespace */
377 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
379 /* read until whitespace */
380 while (width
!=0 && (nch
!=_EOF_
) && !_ISSPACE_(nch
)) {
382 *sptr
++ = _CHAR2SUPPORTED_(nch
);
391 if (width
>0) width
--;
394 if (!suppress
) *sptr
= 0;
397 widecharstring
: { /* read a word into a wchar_t* */
398 MSVCRT_wchar_t
*sptr
= suppress
? NULL
: va_arg(ap
, MSVCRT_wchar_t
*);
399 MSVCRT_wchar_t
*sptr_beg
= sptr
;
401 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned);
403 unsigned size
= UINT_MAX
;
405 /* skip initial whitespace */
406 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
408 /* read until whitespace */
409 while (width
!=0 && (nch
!=_EOF_
) && !_ISSPACE_(nch
)) {
411 *sptr
++ = _WIDE2SUPPORTED_(nch
);
420 if (width
>0) width
--;
423 if (!suppress
) *sptr
= 0;
426 /* 'c' and 'C work analogously to 's' and 'S' as described
429 if (w_prefix
|| l_prefix
) goto widecharacter
;
430 else if (h_prefix
) goto character
;
432 else goto widecharacter
;
433 #else /* WIDE_SCANF */
435 #endif /* WIDE_SCANF */
437 if (w_prefix
|| l_prefix
) goto widecharacter
;
438 else if (h_prefix
) goto character
;
441 #else /* WIDE_SCANF */
442 else goto widecharacter
;
443 #endif /* WIDE_SCANF */
444 character
: { /* read single character into char */
445 char *str
= suppress
? NULL
: va_arg(ap
, char*);
448 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned)/sizeof(char);
450 unsigned size
= UINT_MAX
;
452 if (width
== -1) width
= 1;
453 while (width
&& (nch
!= _EOF_
))
456 *str
++ = _CHAR2SUPPORTED_(nch
);
469 widecharacter
: { /* read single character into a wchar_t */
470 MSVCRT_wchar_t
*str
= suppress
? NULL
: va_arg(ap
, MSVCRT_wchar_t
*);
471 MSVCRT_wchar_t
*pstr
= str
;
473 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned)/sizeof(MSVCRT_wchar_t
);
475 unsigned size
= UINT_MAX
;
477 if (width
== -1) width
= 1;
478 while (width
&& (nch
!= _EOF_
))
481 *str
++ = _WIDE2SUPPORTED_(nch
);
496 int*n
= va_arg(ap
, int*);
499 /* This is an odd one: according to the standard,
500 * "Execution of a %n directive does not increment the
501 * assignment count returned at the completion of
502 * execution" even if it wasn't suppressed with the
503 * '*' flag. The Corrigendum to the standard seems
504 * to contradict this (comment out the assignment to
505 * suppress below if you want to implement these
506 * alternate semantics) but the windows program I'm
507 * looking at expects the behavior I've coded here
508 * (which happens to be what glibc does as well).
515 _CHAR_
*str
= suppress
? NULL
: va_arg(ap
, _CHAR_
*);
519 int invert
= 0; /* Set if we are NOT to find the chars */
521 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned)/sizeof(_CHAR_
);
523 unsigned size
= UINT_MAX
;
526 /* Init our bitmap */
527 Mask
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, _BITMAPSIZE_
/8);
528 RtlInitializeBitMap(&bitMask
, Mask
, _BITMAPSIZE_
);
530 /* Read the format */
537 RtlSetBits(&bitMask
, ']', 1);
540 while(*format
&& (*format
!= ']')) {
541 /* According to msdn:
542 * "Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]." */
543 if((*format
== '-') && (*(format
+ 1) != ']')) {
544 if ((*(format
- 1)) < *(format
+ 1))
545 RtlSetBits(&bitMask
, *(format
- 1) +1 , *(format
+ 1) - *(format
- 1));
547 RtlSetBits(&bitMask
, *(format
+ 1) , *(format
- 1) - *(format
+ 1));
550 RtlSetBits(&bitMask
, *format
, 1);
553 /* read until char is not suitable */
554 while ((width
!= 0) && (nch
!= _EOF_
)) {
556 if(RtlAreBitsSet(&bitMask
, nch
, 1)) {
557 if (!suppress
) *sptr
++ = _CHAR2SUPPORTED_(nch
);
561 if(RtlAreBitsClear(&bitMask
, nch
, 1)) {
562 if (!suppress
) *sptr
++ = _CHAR2SUPPORTED_(nch
);
568 if (width
>0) width
--;
576 if (!suppress
) *sptr
= 0;
577 HeapFree(GetProcessHeap(), 0, Mask
);
581 /* From spec: "if a percent sign is followed by a character
582 * that has no meaning as a format-control character, that
583 * character and the following characters are treated as
584 * an ordinary sequence of characters, that is, a sequence
585 * of characters that must match the input. For example,
586 * to specify that a percent-sign character is to be input,
588 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
591 suppress
= 1; /* whoops no field to be read */
592 st
= 1; /* but we got what we expected */
597 if (st
&& !suppress
) rd
++;
600 /* a non-white-space character causes scanf to read, but not store,
601 * a matching non-white-space character. */
603 /* check for character match */
604 if (nch
== *format
) {
613 TRACE("returning %d\n", rd
);
622 #undef _CHAR2SUPPORTED_
623 #undef _WIDE2SUPPORTED_