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_FUNC_(file) _getch()
50 #define _STRTOD_NAME_(func) console_ ## func
51 #define _GETC_(file) (consumed++, _getch())
52 #define _UNGETC_(nch, file) do { _ungetch(nch); consumed--; } while(0)
53 #define _LOCK_FILE_(file) MSVCRT__lock_file(MSVCRT_stdin)
54 #define _UNLOCK_FILE_(file) MSVCRT__unlock_file(MSVCRT_stdin)
57 #define _FUNCTION_ static int MSVCRT_vcwscanf_s_l(const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
59 #define _FUNCTION_ static int MSVCRT_vcwscanf_l(const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
61 #else /* WIDE_SCANF */
63 #define _FUNCTION_ static int MSVCRT_vcscanf_s_l(const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
65 #define _FUNCTION_ static int MSVCRT_vcscanf_l(const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
67 #endif /* WIDE_SCANF */
72 #define _GETC_FUNC_(file) (*file++)
74 #define _STRTOD_NAME_(func) wstr_ ## func
76 #define _STRTOD_NAME_(func) str_ ## func
80 #define _GETC_(file) (consumed++, consumed>length ? '\0' : *file++)
81 #else /* WIDE_SCANF */
82 #define _GETC_(file) (consumed++, consumed>length ? '\0' : (unsigned char)*file++)
83 #endif /* WIDE_SCANF */
84 #define _UNGETC_(nch, file) do { file--; consumed--; } while(0)
85 #define _LOCK_FILE_(file) do {} while(0)
86 #define _UNLOCK_FILE_(file) do {} while(0)
89 #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)
91 #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)
93 #else /* WIDE_SCANF */
95 #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)
97 #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)
99 #endif /* WIDE_SCANF */
100 #else /* STRING_LEN */
102 #define _GETC_(file) (consumed++, *file++)
103 #else /* WIDE_SCANF */
104 #define _GETC_(file) (consumed++, (unsigned char)*file++)
105 #endif /* WIDE_SCANF */
106 #define _UNGETC_(nch, file) do { file--; consumed--; } while(0)
107 #define _LOCK_FILE_(file) do {} while(0)
108 #define _UNLOCK_FILE_(file) do {} while(0)
111 #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)
113 #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)
115 #else /* WIDE_SCANF */
117 #define _FUNCTION_ static int MSVCRT_vsscanf_s_l(const char *file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
119 #define _FUNCTION_ static int MSVCRT_vsscanf_l(const char *file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
121 #endif /* WIDE_SCANF */
122 #endif /* STRING_LEN */
125 #define _GETC_FUNC_(file) MSVCRT_fgetwc(file)
126 #define _STRTOD_NAME_(func) filew_ ## func
127 #define _GETC_(file) (consumed++, MSVCRT_fgetwc(file))
128 #define _UNGETC_(nch, file) do { MSVCRT_ungetwc(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_vfwscanf_s_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
134 #define _FUNCTION_ static int MSVCRT_vfwscanf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
136 #else /* WIDE_SCANF */
137 #define _GETC_FUNC_(file) MSVCRT_fgetc(file)
138 #define _STRTOD_NAME_(func) file_ ## func
139 #define _GETC_(file) (consumed++, MSVCRT_fgetc(file))
140 #define _UNGETC_(nch, file) do { MSVCRT_ungetc(nch, file); consumed--; } while(0)
141 #define _LOCK_FILE_(file) MSVCRT__lock_file(file)
142 #define _UNLOCK_FILE_(file) MSVCRT__unlock_file(file)
144 #define _FUNCTION_ static int MSVCRT_vfscanf_s_l(MSVCRT_FILE* file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
146 #define _FUNCTION_ static int MSVCRT_vfscanf_l(MSVCRT_FILE* file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
148 #endif /* WIDE_SCANF */
152 #if (!defined(SECURE) && !defined(STRING_LEN) && (!defined(CONSOLE) || !defined(WIDE_SCANF)))
153 struct _STRTOD_NAME_(strtod_scanf_ctx
) {
154 MSVCRT_pthreadlocinfo locinfo
;
167 static MSVCRT_wchar_t
_STRTOD_NAME_(strtod_scanf_get
)(void *ctx
)
169 struct _STRTOD_NAME_(strtod_scanf_ctx
) *context
= ctx
;
171 context
->cur
= _EOF_
;
172 if (!context
->length
) return MSVCRT_WEOF
;
173 if (context
->unget
!= _EOF_
) {
174 context
->cur
= context
->unget
;
175 context
->unget
= _EOF_
;
177 context
->cur
= _GETC_FUNC_(context
->file
);
178 if (context
->cur
== _EOF_
) return MSVCRT_WEOF
;
181 if (context
->length
> 0) context
->length
--;
186 static void _STRTOD_NAME_(strtod_scanf_unget
)(void *ctx
)
188 struct _STRTOD_NAME_(strtod_scanf_ctx
) *context
= ctx
;
190 if (context
->length
>= 0) context
->length
++;
192 if (context
->unget
!= _EOF_
|| context
->cur
== _EOF_
) {
196 context
->unget
= context
->cur
;
201 MSVCRT_pthreadlocinfo locinfo
;
202 int rd
= 0, consumed
= 0;
204 if (!*format
) return 0;
207 TRACE("(%s):\n", debugstr_a(format
));
210 TRACE("%s (%s)\n", debugstr_a(file
), debugstr_a(format
));
212 TRACE("%p (%s)\n", file
, debugstr_a(format
));
215 #endif /* WIDE_SCANF */
225 locinfo
= get_locinfo();
227 locinfo
= locale
->locinfo
;
230 /* a whitespace character in the format string causes scanf to read,
231 * but not store, all consecutive white-space characters in the input
232 * up to the next non-white-space character. One white space character
233 * in the input matches any number (including zero) and combination of
234 * white-space characters in the input. */
235 if (_ISSPACE_(*format
)) {
236 /* skip whitespace */
237 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
240 /* a format specification causes scanf to read and convert characters
241 * in the input into values of a specified type. The value is assigned
242 * to an argument in the argument list. Format specifications have
243 * the form %[*][width][{h | l | I64 | L}]type */
244 else if (*format
== '%') {
245 int st
= 0; int suppress
= 0; int width
= 0;
251 int prefix_finished
= 0;
254 /* look for leading asterisk, which means 'suppress assignment of
260 /* read prefix (if any) */
261 while (!prefix_finished
) {
262 /* look for width specification */
263 while (_ISDIGIT_(*format
)) {
265 width
+= *format
++ - '0';
269 case 'h': h_prefix
++; break;
271 if(*(format
+1) == 'l') {
277 case 'w': w_prefix
= 1; break;
278 case 'L': L_prefix
= 1; break;
280 if (*(format
+ 1) == '6' &&
281 *(format
+ 2) == '4') {
289 if (!prefix_finished
) format
++;
291 if (width
==0) width
=-1; /* no width spec seen */
295 case 'P': /* pointer. */
296 if (sizeof(void *) == sizeof(LONGLONG
)) I64_prefix
= 1;
299 case 'X': /* hexadecimal integer. */
302 case 'o': /* octal integer */
305 case 'u': /* unsigned decimal integer */
308 case 'd': /* signed decimal integer */
311 case 'i': /* generic integer */
314 /* read an integer */
318 /* skip initial whitespace */
319 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
322 if (nch
== '-' || nch
== '+') {
323 negative
= (nch
=='-');
325 if (width
>0) width
--;
327 /* look for leading indication of base */
328 if (width
!=0 && nch
== '0' && *format
!= 'p' && *format
!= 'P') {
330 if (width
>0) width
--;
332 if (width
!=0 && (nch
=='x' || nch
=='X')) {
337 if (width
>0) width
--;
343 /* format %i without indication of base */
346 /* throw away leading zeros */
347 while (width
!=0 && nch
=='0') {
349 if (width
>0) width
--;
352 if (width
!=0 && _CHAR2DIGIT_(nch
, base
)!=-1) {
353 cur
= _CHAR2DIGIT_(nch
, base
);
355 if (width
>0) width
--;
358 /* read until no more digits */
359 while (width
!=0 && (nch
!=_EOF_
) && _CHAR2DIGIT_(nch
, base
)!=-1) {
360 cur
= cur
*base
+ _CHAR2DIGIT_(nch
, base
);
362 if (width
>0) width
--;
366 if (!seendigit
) break; /* not a valid number */
369 #define _SET_NUMBER_(type) *va_arg(ap, type*) = negative ? -cur : cur
370 if (I64_prefix
) _SET_NUMBER_(LONGLONG
);
371 else if (l_prefix
) _SET_NUMBER_(LONG
);
372 else if (h_prefix
== 1) _SET_NUMBER_(short int);
373 #if _MSVCR_VER >= 140
374 else if (h_prefix
== 2) _SET_NUMBER_(char);
376 else _SET_NUMBER_(int);
384 case 'G': { /* read a float */
386 struct _STRTOD_NAME_(strtod_scanf_ctx
) ctx
= {locinfo
, 0, width
};
388 struct _STRTOD_NAME_(strtod_scanf_ctx
) ctx
= {locinfo
, file
, width
};
394 /* skip initial whitespace */
395 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
404 if(ctx
.length
> length
-consumed
+1) ctx
.length
= length
-consumed
+1;
407 fp
= fpnum_parse(_STRTOD_NAME_(strtod_scanf_get
),
408 _STRTOD_NAME_(strtod_scanf_unget
), &ctx
, locinfo
, FALSE
);
409 fpnum_double(&fp
, &cur
);
414 if(ctx
.err
|| !ctx
.read
)
416 consumed
+= ctx
.read
;
424 if (L_prefix
|| l_prefix
) _SET_NUMBER_(double);
425 else _SET_NUMBER_(float);
429 /* According to msdn,
430 * 's' reads a character string in a call to fscanf
431 * and 'S' a wide character string and vice versa in a
432 * call to fwscanf. The 'h', 'w' and 'l' prefixes override
433 * this behaviour. 'h' forces reading char * but 'l' and 'w'
434 * force reading WCHAR. */
436 if (w_prefix
|| l_prefix
) goto widecharstring
;
437 else if (h_prefix
) goto charstring
;
439 else goto widecharstring
;
440 #else /* WIDE_SCANF */
441 else goto charstring
;
442 #endif /* WIDE_SCANF */
444 if (w_prefix
|| l_prefix
) goto widecharstring
;
445 else if (h_prefix
) goto charstring
;
447 else goto charstring
;
448 #else /* WIDE_SCANF */
449 else goto widecharstring
;
450 #endif /* WIDE_SCANF */
451 charstring
: { /* read a word into a char */
452 char *sptr
= suppress
? NULL
: va_arg(ap
, char*);
453 char *sptr_beg
= sptr
;
455 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned);
457 unsigned size
= UINT_MAX
;
459 /* skip initial whitespace */
460 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
462 /* read until whitespace */
463 while (width
!=0 && (nch
!=_EOF_
) && !_ISSPACE_(nch
)) {
465 *sptr
++ = _CHAR2SUPPORTED_(nch
);
475 if (width
>0) width
--;
477 /* if we have reached the EOF and output nothing then report EOF */
478 if (nch
==_EOF_
&& rd
==0 && st
==0) {
483 if (st
&& !suppress
) *sptr
= 0;
486 widecharstring
: { /* read a word into a wchar_t* */
487 MSVCRT_wchar_t
*sptr
= suppress
? NULL
: va_arg(ap
, MSVCRT_wchar_t
*);
488 MSVCRT_wchar_t
*sptr_beg
= sptr
;
490 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned);
492 unsigned size
= UINT_MAX
;
494 /* skip initial whitespace */
495 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
497 /* read until whitespace */
498 while (width
!=0 && (nch
!=_EOF_
) && !_ISSPACE_(nch
)) {
500 *sptr
++ = _WIDE2SUPPORTED_(nch
);
510 if (width
>0) width
--;
513 /* if we have reached the EOF and output nothing then report EOF */
514 if (nch
==_EOF_
&& rd
==0 && st
==0) {
520 if (st
&& !suppress
) *sptr
= 0;
523 /* 'c' and 'C work analogously to 's' and 'S' as described
526 if (w_prefix
|| l_prefix
) goto widecharacter
;
527 else if (h_prefix
) goto character
;
529 else goto widecharacter
;
530 #else /* WIDE_SCANF */
532 #endif /* WIDE_SCANF */
534 if (w_prefix
|| l_prefix
) goto widecharacter
;
535 else if (h_prefix
) goto character
;
538 #else /* WIDE_SCANF */
539 else goto widecharacter
;
540 #endif /* WIDE_SCANF */
541 character
: { /* read single character into char */
542 char *str
= suppress
? NULL
: va_arg(ap
, char*);
545 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned);
547 unsigned size
= UINT_MAX
;
549 if (width
== -1) width
= 1;
550 while (width
&& (nch
!= _EOF_
))
559 *str
++ = _CHAR2SUPPORTED_(nch
);
567 widecharacter
: { /* read single character into a wchar_t */
568 MSVCRT_wchar_t
*str
= suppress
? NULL
: va_arg(ap
, MSVCRT_wchar_t
*);
569 MSVCRT_wchar_t
*pstr
= str
;
571 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned);
573 unsigned size
= UINT_MAX
;
575 if (width
== -1) width
= 1;
576 while (width
&& (nch
!= _EOF_
))
585 *str
++ = _WIDE2SUPPORTED_(nch
);
595 int*n
= va_arg(ap
, int*);
598 /* This is an odd one: according to the standard,
599 * "Execution of a %n directive does not increment the
600 * assignment count returned at the completion of
601 * execution" even if it wasn't suppressed with the
602 * '*' flag. The Corrigendum to the standard seems
603 * to contradict this (comment out the assignment to
604 * suppress below if you want to implement these
605 * alternate semantics) but the windows program I'm
606 * looking at expects the behavior I've coded here
607 * (which happens to be what glibc does as well).
614 _CHAR_
*str
= suppress
? NULL
: va_arg(ap
, _CHAR_
*);
618 int invert
= 0; /* Set if we are NOT to find the chars */
620 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned);
622 unsigned size
= UINT_MAX
;
625 /* Init our bitmap */
626 Mask
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, _BITMAPSIZE_
/8);
627 RtlInitializeBitMap(&bitMask
, Mask
, _BITMAPSIZE_
);
629 /* Read the format */
636 RtlSetBits(&bitMask
, ']', 1);
639 while(*format
&& (*format
!= ']')) {
640 /* According to msdn:
641 * "Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]." */
642 if((*format
== '-') && (*(format
+ 1) != ']')) {
643 if ((*(format
- 1)) < *(format
+ 1))
644 RtlSetBits(&bitMask
, *(format
- 1) +1 , *(format
+ 1) - *(format
- 1));
646 RtlSetBits(&bitMask
, *(format
+ 1) , *(format
- 1) - *(format
+ 1));
649 RtlSetBits(&bitMask
, *format
, 1);
652 /* read until char is not suitable */
653 while ((width
!= 0) && (nch
!= _EOF_
)) {
655 if(RtlAreBitsSet(&bitMask
, nch
, 1)) {
656 if (!suppress
) *sptr
++ = _CHAR2SUPPORTED_(nch
);
660 if(RtlAreBitsClear(&bitMask
, nch
, 1)) {
661 if (!suppress
) *sptr
++ = _CHAR2SUPPORTED_(nch
);
667 if (width
>0) width
--;
672 HeapFree(GetProcessHeap(), 0, Mask
);
677 if (!suppress
) *sptr
= 0;
678 HeapFree(GetProcessHeap(), 0, Mask
);
682 /* From spec: "if a percent sign is followed by a character
683 * that has no meaning as a format-control character, that
684 * character and the following characters are treated as
685 * an ordinary sequence of characters, that is, a sequence
686 * of characters that must match the input. For example,
687 * to specify that a percent-sign character is to be input,
689 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
691 if ((_CHAR_
)nch
== *format
) {
692 suppress
= 1; /* whoops no field to be read */
693 st
= 1; /* but we got what we expected */
698 if (st
&& !suppress
) rd
++;
701 /* a non-white-space character causes scanf to read, but not store,
702 * a matching non-white-space character. */
704 /* check for character match */
705 if ((_CHAR_
)nch
== *format
) {
715 TRACE("returning %d\n", rd
);
725 #undef _CHAR2SUPPORTED_
726 #undef _WIDE2SUPPORTED_