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_ wchar_t
29 #define _EOF_RET (short)WEOF
30 #define _ISSPACE_(c) iswspace(c)
31 #define _WIDE2SUPPORTED_(c) c /* No conversion needed (wide to wide) */
32 #define _CHAR2SUPPORTED_(c) c /* FIXME: convert char to wide char */
33 #define _CHAR2DIGIT_(c, base) wchar2digit((c), (base))
34 #define _BITMAPSIZE_ 256*256
35 #else /* WIDE_SCANF */
39 #define _ISSPACE_(c) isspace(c)
40 #define _WIDE2SUPPORTED_(c) c /* FIXME: convert wide char to char */
41 #define _CHAR2SUPPORTED_(c) c /* No conversion needed (char to char) */
42 #define _CHAR2DIGIT_(c, base) char2digit((c), (base))
43 #define _BITMAPSIZE_ 256
44 #endif /* WIDE_SCANF */
47 #define _GETC_FUNC_(file) _getch()
48 #define _STRTOD_NAME_(func) console_ ## func
49 #define _GETC_(file) (consumed++, _getch())
50 #define _UNGETC_(nch, file) do { _ungetch(nch); consumed--; } while(0)
51 #define _LOCK_FILE_(file) _lock_file(MSVCRT_stdin)
52 #define _UNLOCK_FILE_(file) _unlock_file(MSVCRT_stdin)
55 #define _FUNCTION_ static int vcwscanf_s_l(const wchar_t *format, _locale_t locale, __ms_va_list ap)
57 #define _FUNCTION_ static int vcwscanf_l(const wchar_t *format, _locale_t locale, __ms_va_list ap)
59 #else /* WIDE_SCANF */
61 #define _FUNCTION_ static int vcscanf_s_l(const char *format, _locale_t locale, __ms_va_list ap)
63 #define _FUNCTION_ static int vcscanf_l(const char *format, _locale_t locale, __ms_va_list ap)
65 #endif /* WIDE_SCANF */
70 #define _GETC_FUNC_(file) (*file++)
72 #define _STRTOD_NAME_(func) wstr_ ## func
74 #define _STRTOD_NAME_(func) str_ ## func
78 #define _GETC_(file) (consumed++, consumed>length ? '\0' : *file++)
79 #else /* WIDE_SCANF */
80 #define _GETC_(file) (consumed++, consumed>length ? '\0' : (unsigned char)*file++)
81 #endif /* WIDE_SCANF */
82 #define _UNGETC_(nch, file) do { file--; consumed--; } while(0)
83 #define _LOCK_FILE_(file) do {} while(0)
84 #define _UNLOCK_FILE_(file) do {} while(0)
87 #define _FUNCTION_ static int vsnwscanf_s_l(const wchar_t *file, size_t length, const wchar_t *format, _locale_t locale, __ms_va_list ap)
89 #define _FUNCTION_ static int vsnwscanf_l(const wchar_t *file, size_t length, const wchar_t *format, _locale_t locale, __ms_va_list ap)
91 #else /* WIDE_SCANF */
93 #define _FUNCTION_ static int vsnscanf_s_l(const char *file, size_t length, const char *format, _locale_t locale, __ms_va_list ap)
95 #define _FUNCTION_ static int vsnscanf_l(const char *file, size_t length, const char *format, _locale_t locale, __ms_va_list ap)
97 #endif /* WIDE_SCANF */
98 #else /* STRING_LEN */
100 #define _GETC_(file) (consumed++, *file++)
101 #else /* WIDE_SCANF */
102 #define _GETC_(file) (consumed++, (unsigned char)*file++)
103 #endif /* WIDE_SCANF */
104 #define _UNGETC_(nch, file) do { file--; consumed--; } while(0)
105 #define _LOCK_FILE_(file) do {} while(0)
106 #define _UNLOCK_FILE_(file) do {} while(0)
109 #define _FUNCTION_ static int vswscanf_s_l(const wchar_t *file, const wchar_t *format, _locale_t locale, __ms_va_list ap)
111 #define _FUNCTION_ static int vswscanf_l(const wchar_t *file, const wchar_t *format, _locale_t locale, __ms_va_list ap)
113 #else /* WIDE_SCANF */
115 #define _FUNCTION_ static int vsscanf_s_l(const char *file, const char *format, _locale_t locale, __ms_va_list ap)
117 #define _FUNCTION_ static int vsscanf_l(const char *file, const char *format, _locale_t locale, __ms_va_list ap)
119 #endif /* WIDE_SCANF */
120 #endif /* STRING_LEN */
123 #define _GETC_FUNC_(file) fgetwc(file)
124 #define _STRTOD_NAME_(func) filew_ ## func
125 #define _GETC_(file) (consumed++, fgetwc(file))
126 #define _UNGETC_(nch, file) do { ungetwc(nch, file); consumed--; } while(0)
127 #define _LOCK_FILE_(file) _lock_file(file)
128 #define _UNLOCK_FILE_(file) _unlock_file(file)
130 #define _FUNCTION_ static int vfwscanf_s_l(FILE* file, const wchar_t *format, _locale_t locale, __ms_va_list ap)
132 #define _FUNCTION_ static int vfwscanf_l(FILE* file, const wchar_t *format, _locale_t locale, __ms_va_list ap)
134 #else /* WIDE_SCANF */
135 #define _GETC_FUNC_(file) fgetc(file)
136 #define _STRTOD_NAME_(func) file_ ## func
137 #define _GETC_(file) (consumed++, fgetc(file))
138 #define _UNGETC_(nch, file) do { ungetc(nch, file); consumed--; } while(0)
139 #define _LOCK_FILE_(file) _lock_file(file)
140 #define _UNLOCK_FILE_(file) _unlock_file(file)
142 #define _FUNCTION_ static int vfscanf_s_l(FILE* file, const char *format, _locale_t locale, __ms_va_list ap)
144 #define _FUNCTION_ static int vfscanf_l(FILE* file, const char *format, _locale_t locale, __ms_va_list ap)
146 #endif /* WIDE_SCANF */
150 #if (!defined(SECURE) && !defined(STRING_LEN) && (!defined(CONSOLE) || !defined(WIDE_SCANF)))
151 struct _STRTOD_NAME_(strtod_scanf_ctx
) {
152 pthreadlocinfo locinfo
;
165 static wchar_t _STRTOD_NAME_(strtod_scanf_get
)(void *ctx
)
167 struct _STRTOD_NAME_(strtod_scanf_ctx
) *context
= ctx
;
169 context
->cur
= _EOF_
;
170 if (!context
->length
) return WEOF
;
171 if (context
->unget
!= _EOF_
) {
172 context
->cur
= context
->unget
;
173 context
->unget
= _EOF_
;
175 context
->cur
= _GETC_FUNC_(context
->file
);
176 if (context
->cur
== _EOF_
) return WEOF
;
179 if (context
->length
> 0) context
->length
--;
184 static void _STRTOD_NAME_(strtod_scanf_unget
)(void *ctx
)
186 struct _STRTOD_NAME_(strtod_scanf_ctx
) *context
= ctx
;
188 if (context
->length
>= 0) context
->length
++;
190 if (context
->unget
!= _EOF_
|| context
->cur
== _EOF_
) {
194 context
->unget
= context
->cur
;
199 pthreadlocinfo locinfo
;
200 int rd
= 0, consumed
= 0;
202 if (!*format
) return 0;
205 TRACE("(%s):\n", debugstr_a(format
));
208 TRACE("%s (%s)\n", debugstr_a(file
), debugstr_a(format
));
210 TRACE("%p (%s)\n", file
, debugstr_a(format
));
213 #endif /* WIDE_SCANF */
223 locinfo
= get_locinfo();
225 locinfo
= locale
->locinfo
;
228 /* a whitespace character in the format string causes scanf to read,
229 * but not store, all consecutive white-space characters in the input
230 * up to the next non-white-space character. One white space character
231 * in the input matches any number (including zero) and combination of
232 * white-space characters in the input. */
233 if (_ISSPACE_(*format
)) {
234 /* skip whitespace */
235 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
238 /* a format specification causes scanf to read and convert characters
239 * in the input into values of a specified type. The value is assigned
240 * to an argument in the argument list. Format specifications have
241 * the form %[*][width][{h | l | I64 | L}]type */
242 else if (*format
== '%') {
243 int st
= 0; int suppress
= 0; int width
= 0;
249 int prefix_finished
= 0;
252 /* look for leading asterisk, which means 'suppress assignment of
258 /* read prefix (if any) */
259 while (!prefix_finished
) {
260 /* look for width specification */
261 while (*format
>= '0' && *format
<= '9') {
263 width
+= *format
++ - '0';
267 case 'h': h_prefix
++; break;
269 if(*(format
+1) == 'l') {
275 case 'w': w_prefix
= 1; break;
276 case 'L': L_prefix
= 1; break;
278 if (*(format
+ 1) == '6' &&
279 *(format
+ 2) == '4') {
287 if (!prefix_finished
) format
++;
289 if (width
==0) width
=-1; /* no width spec seen */
293 case 'P': /* pointer. */
294 if (sizeof(void *) == sizeof(LONGLONG
)) I64_prefix
= 1;
297 case 'X': /* hexadecimal integer. */
300 case 'o': /* octal integer */
303 case 'u': /* unsigned decimal integer */
306 case 'd': /* signed decimal integer */
309 case 'i': /* generic integer */
312 /* read an integer */
316 /* skip initial whitespace */
317 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
320 if (nch
== '-' || nch
== '+') {
321 negative
= (nch
=='-');
323 if (width
>0) width
--;
325 /* look for leading indication of base */
326 if (width
!=0 && nch
== '0' && *format
!= 'p' && *format
!= 'P') {
328 if (width
>0) width
--;
330 if (width
!=0 && (nch
=='x' || nch
=='X')) {
335 if (width
>0) width
--;
341 /* format %i without indication of base */
344 /* throw away leading zeros */
345 while (width
!=0 && nch
=='0') {
347 if (width
>0) width
--;
350 if (width
!=0 && _CHAR2DIGIT_(nch
, base
)!=-1) {
351 cur
= _CHAR2DIGIT_(nch
, base
);
353 if (width
>0) width
--;
356 /* read until no more digits */
357 while (width
!=0 && (nch
!=_EOF_
) && _CHAR2DIGIT_(nch
, base
)!=-1) {
358 cur
= cur
*base
+ _CHAR2DIGIT_(nch
, base
);
360 if (width
>0) width
--;
364 if (!seendigit
) break; /* not a valid number */
367 #define _SET_NUMBER_(type) *va_arg(ap, type*) = negative ? -cur : cur
368 if (I64_prefix
) _SET_NUMBER_(LONGLONG
);
369 else if (l_prefix
) _SET_NUMBER_(LONG
);
370 else if (h_prefix
== 1) _SET_NUMBER_(short int);
371 #if _MSVCR_VER >= 140
372 else if (h_prefix
== 2) _SET_NUMBER_(char);
374 else _SET_NUMBER_(int);
382 case 'G': { /* read a float */
384 struct _STRTOD_NAME_(strtod_scanf_ctx
) ctx
= {locinfo
, 0, width
};
386 struct _STRTOD_NAME_(strtod_scanf_ctx
) ctx
= {locinfo
, file
, width
};
392 /* skip initial whitespace */
393 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
402 if(ctx
.length
> length
-consumed
+1) ctx
.length
= length
-consumed
+1;
405 fp
= fpnum_parse(_STRTOD_NAME_(strtod_scanf_get
),
406 _STRTOD_NAME_(strtod_scanf_unget
), &ctx
, locinfo
, FALSE
);
407 fpnum_double(&fp
, &cur
);
412 if(ctx
.err
|| !ctx
.read
)
414 consumed
+= ctx
.read
;
422 if (L_prefix
|| l_prefix
) _SET_NUMBER_(double);
423 else _SET_NUMBER_(float);
427 /* According to msdn,
428 * 's' reads a character string in a call to fscanf
429 * and 'S' a wide character string and vice versa in a
430 * call to fwscanf. The 'h', 'w' and 'l' prefixes override
431 * this behaviour. 'h' forces reading char * but 'l' and 'w'
432 * force reading WCHAR. */
434 if (w_prefix
|| l_prefix
) goto widecharstring
;
435 else if (h_prefix
) goto charstring
;
437 else goto widecharstring
;
438 #else /* WIDE_SCANF */
439 else goto charstring
;
440 #endif /* WIDE_SCANF */
442 if (w_prefix
|| l_prefix
) goto widecharstring
;
443 else if (h_prefix
) goto charstring
;
445 else goto charstring
;
446 #else /* WIDE_SCANF */
447 else goto widecharstring
;
448 #endif /* WIDE_SCANF */
449 charstring
: { /* read a word into a char */
450 char *sptr
= suppress
? NULL
: va_arg(ap
, char*);
451 char *sptr_beg
= sptr
;
453 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned);
455 unsigned size
= UINT_MAX
;
457 /* skip initial whitespace */
458 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
460 /* read until whitespace */
461 while (width
!=0 && (nch
!=_EOF_
) && !_ISSPACE_(nch
)) {
463 *sptr
++ = _CHAR2SUPPORTED_(nch
);
473 if (width
>0) width
--;
475 /* if we have reached the EOF and output nothing then report EOF */
476 if (nch
==_EOF_
&& rd
==0 && st
==0) {
481 if (st
&& !suppress
) *sptr
= 0;
484 widecharstring
: { /* read a word into a wchar_t* */
485 wchar_t *sptr
= suppress
? NULL
: va_arg(ap
, wchar_t*);
486 wchar_t *sptr_beg
= sptr
;
488 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned);
490 unsigned size
= UINT_MAX
;
492 /* skip initial whitespace */
493 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
495 /* read until whitespace */
496 while (width
!=0 && (nch
!=_EOF_
) && !_ISSPACE_(nch
)) {
498 *sptr
++ = _WIDE2SUPPORTED_(nch
);
508 if (width
>0) width
--;
511 /* if we have reached the EOF and output nothing then report EOF */
512 if (nch
==_EOF_
&& rd
==0 && st
==0) {
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 wchar_t *str
= suppress
? NULL
: va_arg(ap
, wchar_t*);
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
[1] == '-' && format
[2] && format
[2] != ']') {
641 if (format
[0] < format
[2])
642 RtlSetBits(&bitMask
, format
[0], format
[2] - format
[0] + 1);
644 RtlSetBits(&bitMask
, format
[2], format
[0] - format
[2] + 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
);
722 #undef _CHAR2SUPPORTED_
723 #undef _WIDE2SUPPORTED_