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, va_list ap)
57 #define _FUNCTION_ static int vcwscanf_l(const wchar_t *format, _locale_t locale, va_list ap)
59 #else /* WIDE_SCANF */
61 #define _FUNCTION_ static int vcscanf_s_l(const char *format, _locale_t locale, va_list ap)
63 #define _FUNCTION_ static int vcscanf_l(const char *format, _locale_t locale, 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, va_list ap)
89 #define _FUNCTION_ static int vsnwscanf_l(const wchar_t *file, size_t length, const wchar_t *format, _locale_t locale, 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, va_list ap)
95 #define _FUNCTION_ static int vsnscanf_l(const char *file, size_t length, const char *format, _locale_t locale, 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, va_list ap)
111 #define _FUNCTION_ static int vswscanf_l(const wchar_t *file, const wchar_t *format, _locale_t locale, va_list ap)
113 #else /* WIDE_SCANF */
115 #define _FUNCTION_ static int vsscanf_s_l(const char *file, const char *format, _locale_t locale, va_list ap)
117 #define _FUNCTION_ static int vsscanf_l(const char *file, const char *format, _locale_t locale, 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, va_list ap)
132 #define _FUNCTION_ static int vfwscanf_l(FILE* file, const wchar_t *format, _locale_t locale, 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, va_list ap)
144 #define _FUNCTION_ static int vfscanf_l(FILE* file, const char *format, _locale_t locale, 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') {
284 else if (*(format
+ 1) == '3' &&
285 *(format
+ 2) == '2') {
290 #if _MSVCR_VER == 0 || _MSVCR_VER >= 140
293 if (sizeof(void *) == sizeof(LONGLONG
)) I64_prefix
= 1;
298 if (!prefix_finished
) format
++;
300 if (width
==0) width
=-1; /* no width spec seen */
304 case 'P': /* pointer. */
305 if (sizeof(void *) == sizeof(LONGLONG
)) I64_prefix
= 1;
308 case 'X': /* hexadecimal integer. */
311 case 'o': /* octal integer */
314 case 'u': /* unsigned decimal integer */
317 case 'd': /* signed decimal integer */
320 case 'i': /* generic integer */
323 /* read an integer */
327 /* skip initial whitespace */
328 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
331 if (nch
== '-' || nch
== '+') {
332 negative
= (nch
=='-');
334 if (width
>0) width
--;
336 /* look for leading indication of base */
337 if (width
!=0 && nch
== '0' && *format
!= 'p' && *format
!= 'P') {
339 if (width
>0) width
--;
341 if (width
!=0 && (nch
=='x' || nch
=='X')) {
346 if (width
>0) width
--;
352 /* format %i without indication of base */
355 /* throw away leading zeros */
356 while (width
!=0 && nch
=='0') {
358 if (width
>0) width
--;
361 if (width
!=0 && _CHAR2DIGIT_(nch
, base
)!=-1) {
362 cur
= _CHAR2DIGIT_(nch
, base
);
364 if (width
>0) width
--;
367 /* read until no more digits */
368 while (width
!=0 && (nch
!=_EOF_
) && _CHAR2DIGIT_(nch
, base
)!=-1) {
369 cur
= cur
*base
+ _CHAR2DIGIT_(nch
, base
);
371 if (width
>0) width
--;
375 if (!seendigit
) break; /* not a valid number */
378 #define _SET_NUMBER_(type) *va_arg(ap, type*) = negative ? -cur : cur
379 if (I64_prefix
) _SET_NUMBER_(LONGLONG
);
380 else if (l_prefix
) _SET_NUMBER_(LONG
);
381 else if (h_prefix
== 1) _SET_NUMBER_(short int);
382 #if _MSVCR_VER >= 140
383 else if (h_prefix
== 2) _SET_NUMBER_(char);
385 else _SET_NUMBER_(int);
393 case 'G': { /* read a float */
395 struct _STRTOD_NAME_(strtod_scanf_ctx
) ctx
= {locinfo
, 0, width
};
397 struct _STRTOD_NAME_(strtod_scanf_ctx
) ctx
= {locinfo
, file
, width
};
403 /* skip initial whitespace */
404 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
413 if(ctx
.length
> length
-consumed
+1) ctx
.length
= length
-consumed
+1;
416 fp
= fpnum_parse(_STRTOD_NAME_(strtod_scanf_get
),
417 _STRTOD_NAME_(strtod_scanf_unget
), &ctx
, locinfo
, FALSE
);
418 fpnum_double(&fp
, &cur
);
423 if(ctx
.err
|| !ctx
.read
)
425 consumed
+= ctx
.read
;
433 if (L_prefix
|| l_prefix
) _SET_NUMBER_(double);
434 else _SET_NUMBER_(float);
438 /* According to msdn,
439 * 's' reads a character string in a call to fscanf
440 * and 'S' a wide character string and vice versa in a
441 * call to fwscanf. The 'h', 'w' and 'l' prefixes override
442 * this behaviour. 'h' forces reading char * but 'l' and 'w'
443 * force reading WCHAR. */
445 if (w_prefix
|| l_prefix
) goto widecharstring
;
446 else if (h_prefix
) goto charstring
;
448 else goto widecharstring
;
449 #else /* WIDE_SCANF */
450 else goto charstring
;
451 #endif /* WIDE_SCANF */
453 if (w_prefix
|| l_prefix
) goto widecharstring
;
454 else if (h_prefix
) goto charstring
;
456 else goto charstring
;
457 #else /* WIDE_SCANF */
458 else goto widecharstring
;
459 #endif /* WIDE_SCANF */
460 charstring
: { /* read a word into a char */
461 char *sptr
= suppress
? NULL
: va_arg(ap
, char*);
462 char *sptr_beg
= sptr
;
464 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned);
466 unsigned size
= UINT_MAX
;
468 /* skip initial whitespace */
469 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
471 /* read until whitespace */
472 while (width
!=0 && (nch
!=_EOF_
) && !_ISSPACE_(nch
)) {
474 *sptr
++ = _CHAR2SUPPORTED_(nch
);
484 if (width
>0) width
--;
486 /* if we have reached the EOF and output nothing then report EOF */
487 if (nch
==_EOF_
&& rd
==0 && st
==0) {
492 if (st
&& !suppress
) *sptr
= 0;
495 widecharstring
: { /* read a word into a wchar_t* */
496 wchar_t *sptr
= suppress
? NULL
: va_arg(ap
, wchar_t*);
497 wchar_t *sptr_beg
= sptr
;
499 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned);
501 unsigned size
= UINT_MAX
;
503 /* skip initial whitespace */
504 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
506 /* read until whitespace */
507 while (width
!=0 && (nch
!=_EOF_
) && !_ISSPACE_(nch
)) {
509 *sptr
++ = _WIDE2SUPPORTED_(nch
);
519 if (width
>0) width
--;
522 /* if we have reached the EOF and output nothing then report EOF */
523 if (nch
==_EOF_
&& rd
==0 && st
==0) {
529 if (st
&& !suppress
) *sptr
= 0;
532 /* 'c' and 'C work analogously to 's' and 'S' as described
535 if (w_prefix
|| l_prefix
) goto widecharacter
;
536 else if (h_prefix
) goto character
;
538 else goto widecharacter
;
539 #else /* WIDE_SCANF */
541 #endif /* WIDE_SCANF */
543 if (w_prefix
|| l_prefix
) goto widecharacter
;
544 else if (h_prefix
) goto character
;
547 #else /* WIDE_SCANF */
548 else goto widecharacter
;
549 #endif /* WIDE_SCANF */
550 character
: { /* read single character into char */
551 char *str
= suppress
? NULL
: va_arg(ap
, char*);
554 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned);
556 unsigned size
= UINT_MAX
;
558 if (width
== -1) width
= 1;
559 while (width
&& (nch
!= _EOF_
))
568 *str
++ = _CHAR2SUPPORTED_(nch
);
576 widecharacter
: { /* read single character into a wchar_t */
577 wchar_t *str
= suppress
? NULL
: va_arg(ap
, wchar_t*);
580 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned);
582 unsigned size
= UINT_MAX
;
584 if (width
== -1) width
= 1;
585 while (width
&& (nch
!= _EOF_
))
594 *str
++ = _WIDE2SUPPORTED_(nch
);
604 int*n
= va_arg(ap
, int*);
607 /* This is an odd one: according to the standard,
608 * "Execution of a %n directive does not increment the
609 * assignment count returned at the completion of
610 * execution" even if it wasn't suppressed with the
611 * '*' flag. The Corrigendum to the standard seems
612 * to contradict this (comment out the assignment to
613 * suppress below if you want to implement these
614 * alternate semantics) but the windows program I'm
615 * looking at expects the behavior I've coded here
616 * (which happens to be what glibc does as well).
623 _CHAR_
*str
= suppress
? NULL
: va_arg(ap
, _CHAR_
*);
627 int invert
= 0; /* Set if we are NOT to find the chars */
629 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned);
631 unsigned size
= UINT_MAX
;
634 /* Init our bitmap */
635 Mask
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, _BITMAPSIZE_
/8);
636 RtlInitializeBitMap(&bitMask
, Mask
, _BITMAPSIZE_
);
638 /* Read the format */
645 RtlSetBits(&bitMask
, ']', 1);
648 while(*format
&& (*format
!= ']')) {
649 /* According to msdn:
650 * "Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]." */
651 if(format
[1] == '-' && format
[2] && format
[2] != ']') {
652 if (format
[0] < format
[2])
653 RtlSetBits(&bitMask
, format
[0], format
[2] - format
[0] + 1);
655 RtlSetBits(&bitMask
, format
[2], format
[0] - format
[2] + 1);
658 RtlSetBits(&bitMask
, *format
, 1);
661 /* read until char is not suitable */
662 while ((width
!= 0) && (nch
!= _EOF_
)) {
664 if(RtlAreBitsSet(&bitMask
, nch
, 1)) {
665 if (!suppress
) *sptr
++ = _CHAR2SUPPORTED_(nch
);
669 if(RtlAreBitsClear(&bitMask
, nch
, 1)) {
670 if (!suppress
) *sptr
++ = _CHAR2SUPPORTED_(nch
);
676 if (width
>0) width
--;
681 HeapFree(GetProcessHeap(), 0, Mask
);
686 if (!suppress
) *sptr
= 0;
687 HeapFree(GetProcessHeap(), 0, Mask
);
691 /* From spec: "if a percent sign is followed by a character
692 * that has no meaning as a format-control character, that
693 * character and the following characters are treated as
694 * an ordinary sequence of characters, that is, a sequence
695 * of characters that must match the input. For example,
696 * to specify that a percent-sign character is to be input,
698 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
700 if ((_CHAR_
)nch
== *format
) {
701 suppress
= 1; /* whoops no field to be read */
702 st
= 1; /* but we got what we expected */
707 if (st
&& !suppress
) rd
++;
710 /* a non-white-space character causes scanf to read, but not store,
711 * a matching non-white-space character. */
713 /* check for character match */
714 if ((_CHAR_
)nch
== *format
) {
724 TRACE("returning %d\n", rd
);
733 #undef _CHAR2SUPPORTED_
734 #undef _WIDE2SUPPORTED_