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 */
69 #define _GETC_(file) (consumed==length ? '\0' : (consumed++, *file++))
70 #define _UNGETC_(nch, file) do { file--; consumed--; } while(0)
73 #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)
75 #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)
77 #else /* WIDE_SCANF */
79 #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)
81 #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)
83 #endif /* WIDE_SCANF */
84 #else /* STRING_LEN */
85 #define _GETC_(file) (consumed++, *file++)
86 #define _UNGETC_(nch, file) do { file--; consumed--; } while(0)
89 #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)
91 #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)
93 #else /* WIDE_SCANF */
95 #define _FUNCTION_ static int MSVCRT_vsscanf_s_l(const char *file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
97 #define _FUNCTION_ static int MSVCRT_vsscanf_l(const char *file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
99 #endif /* WIDE_SCANF */
100 #endif /* STRING_LEN */
103 #define _GETC_(file) (consumed++, MSVCRT_fgetwc(file))
104 #define _UNGETC_(nch, file) do { MSVCRT_ungetwc(nch, file); consumed--; } while(0)
106 #define _FUNCTION_ static int MSVCRT_vfwscanf_s_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
108 #define _FUNCTION_ static int MSVCRT_vfwscanf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
110 #else /* WIDE_SCANF */
111 #define _GETC_(file) (consumed++, MSVCRT_fgetc(file))
112 #define _UNGETC_(nch, file) do { MSVCRT_ungetc(nch, file); consumed--; } while(0)
114 #define _FUNCTION_ static int MSVCRT_vfscanf_s_l(MSVCRT_FILE* file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
116 #define _FUNCTION_ static int MSVCRT_vfscanf_l(MSVCRT_FILE* file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
118 #endif /* WIDE_SCANF */
123 int rd = 0, consumed = 0;
125 if (!*format) return 0;
128 TRACE("(%s):\n", debugstr_a(format));
131 TRACE("%s (%s)\n", debugstr_a(file), debugstr_a(format));
133 TRACE("%p (%s)\n", file, debugstr_a(format));
136 #endif /* WIDE_SCANF */
138 if (nch == _EOF_) return _EOF_RET;
141 locale = get_locale();
144 /* a whitespace character in the format string causes scanf to read,
145 * but not store, all consecutive white-space characters in the input
146 * up to the next non-white-space character. One white space character
147 * in the input matches any number (including zero) and combination of
148 * white-space characters in the input. */
149 if (_ISSPACE_(*format)) {
150 /* skip whitespace */
151 while ((nch!=_EOF_) && _ISSPACE_(nch))
154 /* a format specification causes scanf to read and convert characters
155 * in the input into values of a specified type. The value is assigned
156 * to an argument in the argument list. Format specifications have
157 * the form %[*][width][{h | l | I64 | L}]type */
158 else if (*format == '%') {
159 int st = 0; int suppress = 0; int width = 0;
165 int prefix_finished = 0;
168 /* look for leading asterisk, which means 'suppress assignment of
174 /* look for width specification */
175 while (_ISDIGIT_(*format)) {
177 width+=*format++ - '0';
179 if (width==0) width=-1; /* no width spec seen */
180 /* read prefix (if any) */
181 while (!prefix_finished) {
183 case 'h': h_prefix = 1; break;
184 case 'l': l_prefix = 1; break;
185 case 'w': w_prefix = 1; break;
186 case 'L': L_prefix = 1; break;
188 if (*(format + 1) == '6' &&
189 *(format + 2) == '4') {
197 if (!prefix_finished) format++;
202 case 'P': /* pointer. */
203 if (sizeof(void *) == sizeof(LONGLONG)) I64_prefix = 1;
206 case 'X': /* hexadecimal integer. */
209 case 'o': /* octal integer */
212 case 'u': /* unsigned decimal integer */
215 case 'd': /* signed decimal integer */
218 case 'i': /* generic integer */
221 /* read an integer */
225 /* skip initial whitespace */
226 while ((nch!=_EOF_) && _ISSPACE_(nch))
229 if (nch == '-' || nch == '+') {
230 negative = (nch=='-');
232 if (width>0) width--;
234 /* look for leading indication of base */
235 if (width!=0 && nch == '0' && *format != 'p' && *format != 'P') {
237 if (width>0) width--;
239 if (width!=0 && (nch=='x' || nch=='X')) {
244 if (width>0) width--;
250 /* format %i without indication of base */
253 /* throw away leading zeros */
254 while (width!=0 && nch=='0') {
256 if (width>0) width--;
259 if (width!=0 && _CHAR2DIGIT_(nch, base)!=-1) {
260 cur = _CHAR2DIGIT_(nch, base);
262 if (width>0) width--;
265 /* read until no more digits */
266 while (width!=0 && (nch!=_EOF_) && _CHAR2DIGIT_(nch, base)!=-1) {
267 cur = cur*base + _CHAR2DIGIT_(nch, base);
269 if (width>0) width--;
273 if (!seendigit) break; /* not a valid number */
276 #define _SET_NUMBER_(type) *va_arg(ap, type*) = negative ? -cur : cur
277 if (I64_prefix) _SET_NUMBER_(LONGLONG);
278 else if (l_prefix) _SET_NUMBER_(LONG);
279 else if (h_prefix) _SET_NUMBER_(short int);
280 else _SET_NUMBER_(int);
288 case 'G': { /* read a float */
291 /* skip initial whitespace */
292 while ((nch!=_EOF_) && _ISSPACE_(nch))
295 if (nch == '-' || nch == '+') {
296 negative = (nch=='-');
297 if (width>0) width--;
301 /* get first digit. */
302 if (*locale->locinfo->lconv->decimal_point != nch) {
303 if (!_ISDIGIT_(nch)) break;
306 if (width>0) width--;
307 /* read until no more digits */
308 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
309 cur = cur*10 + (nch - '0');
311 if (width>0) width--;
314 cur = 0; /* Fix: .8 -> 0.8 */
316 /* handle decimals */
317 if (width!=0 && nch == *locale->locinfo->lconv->decimal_point) {
320 if (width>0) width--;
321 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
323 cur += dec * (nch - '0');
325 if (width>0) width--;
328 /* handle exponent */
329 if (width!=0 && (nch == 'e' || nch == 'E')) {
330 int exponent = 0, negexp = 0;
333 if (width>0) width--;
334 /* possible sign on the exponent */
335 if (width!=0 && (nch=='+' || nch=='-')) {
338 if (width>0) width--;
340 /* exponent digits */
341 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
343 exponent += (nch - '0');
345 if (width>0) width--;
347 /* update 'cur' with this exponent. */
348 expcnt = negexp ? .1 : 10;
349 while (exponent!=0) {
353 expcnt=expcnt*expcnt;
358 if (L_prefix) _SET_NUMBER_(long double);
359 else if (l_prefix) _SET_NUMBER_(double);
360 else _SET_NUMBER_(float);
364 /* According to msdn,
365 * 's' reads a character string in a call to fscanf
366 * and 'S' a wide character string and vice versa in a
367 * call to fwscanf. The 'h', 'w' and 'l' prefixes override
368 * this behaviour. 'h' forces reading char * but 'l' and 'w'
369 * force reading WCHAR. */
371 if (w_prefix || l_prefix) goto widecharstring;
372 else if (h_prefix) goto charstring;
374 else goto widecharstring;
375 #else /* WIDE_SCANF */
376 else goto charstring;
377 #endif /* WIDE_SCANF */
379 if (w_prefix || l_prefix) goto widecharstring;
380 else if (h_prefix) goto charstring;
382 else goto charstring;
383 #else /* WIDE_SCANF */
384 else goto widecharstring;
385 #endif /* WIDE_SCANF */
386 charstring: { /* read a word into a char */
387 char *sptr = suppress ? NULL : va_arg(ap, char*);
388 char *sptr_beg = sptr;
390 unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned);
392 unsigned size = UINT_MAX;
394 /* skip initial whitespace */
395 while ((nch!=_EOF_) && _ISSPACE_(nch))
397 /* read until whitespace */
398 while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
400 *sptr++ = _CHAR2SUPPORTED_(nch);
409 if (width>0) width--;
412 if (!suppress) *sptr = 0;
415 widecharstring: { /* read a word into a wchar_t* */
416 MSVCRT_wchar_t *sptr = suppress ? NULL : va_arg(ap, MSVCRT_wchar_t*);
417 MSVCRT_wchar_t *sptr_beg = sptr;
419 unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned);
421 unsigned size = UINT_MAX;
423 /* skip initial whitespace */
424 while ((nch!=_EOF_) && _ISSPACE_(nch))
426 /* read until whitespace */
427 while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
429 *sptr++ = _WIDE2SUPPORTED_(nch);
438 if (width>0) width--;
441 if (!suppress) *sptr = 0;
444 /* 'c' and 'C work analogously to 's' and 'S' as described
447 if (w_prefix || l_prefix) goto widecharacter;
448 else if (h_prefix) goto character;
450 else goto widecharacter;
451 #else /* WIDE_SCANF */
453 #endif /* WIDE_SCANF */
455 if (w_prefix || l_prefix) goto widecharacter;
456 else if (h_prefix) goto character;
459 #else /* WIDE_SCANF */
460 else goto widecharacter;
461 #endif /* WIDE_SCANF */
462 character: { /* read single character into char */
463 char *str = suppress ? NULL : va_arg(ap, char*);
466 unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned)/sizeof(char);
468 unsigned size = UINT_MAX;
470 if (width == -1) width = 1;
471 while (width && (nch != _EOF_))
474 *str++ = _CHAR2SUPPORTED_(nch);
487 widecharacter: { /* read single character into a wchar_t */
488 MSVCRT_wchar_t *str = suppress ? NULL : va_arg(ap, MSVCRT_wchar_t*);
489 MSVCRT_wchar_t *pstr = str;
491 unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned)/sizeof(MSVCRT_wchar_t);
493 unsigned size = UINT_MAX;
495 if (width == -1) width = 1;
496 while (width && (nch != _EOF_))
499 *str++ = _WIDE2SUPPORTED_(nch);
514 int*n = va_arg(ap, int*);
517 /* This is an odd one: according to the standard,
518 * "Execution of a %n directive does not increment the
519 * assignment count returned at the completion of
520 * execution" even if it wasn't suppressed with the
521 * '*' flag. The Corrigendum to the standard seems
522 * to contradict this (comment out the assignment to
523 * suppress below if you want to implement these
524 * alternate semantics) but the windows program I'm
525 * looking at expects the behavior I've coded here
526 * (which happens to be what glibc does as well).
533 _CHAR_ *str = suppress ? NULL : va_arg(ap, _CHAR_*);
537 int invert = 0; /* Set if we are NOT to find the chars */
539 unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned)/sizeof(_CHAR_);
541 unsigned size = UINT_MAX;
544 /* Init our bitmap */
545 Mask = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, _BITMAPSIZE_/8);
546 RtlInitializeBitMap(&bitMask, Mask, _BITMAPSIZE_);
548 /* Read the format */
555 RtlSetBits(&bitMask, ']', 1);
558 while(*format && (*format != ']')) {
559 /* According to msdn:
560 * "Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]." */
561 if((*format == '-') && (*(format + 1) != ']')) {
562 if ((*(format - 1)) < *(format + 1))
563 RtlSetBits(&bitMask, *(format - 1) +1 , *(format + 1) - *(format - 1));
565 RtlSetBits(&bitMask, *(format + 1) , *(format - 1) - *(format + 1));
568 RtlSetBits(&bitMask, *format, 1);
571 /* read until char is not suitable */
572 while ((width != 0) && (nch != _EOF_)) {
574 if(RtlAreBitsSet(&bitMask, nch, 1)) {
575 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
579 if(RtlAreBitsClear(&bitMask, nch, 1)) {
580 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
586 if (width>0) width--;
594 if (!suppress) *sptr = 0;
595 HeapFree(GetProcessHeap(), 0, Mask);
599 /* From spec: "if a percent sign is followed by a character
600 * that has no meaning as a format-control character, that
601 * character and the following characters are treated as
602 * an ordinary sequence of characters, that is, a sequence
603 * of characters that must match the input. For example,
604 * to specify that a percent-sign character is to be input,
606 while ((nch!=_EOF_) && _ISSPACE_(nch))
609 suppress = 1; /* whoops no field to be read */
610 st = 1; /* but we got what we expected */
615 if (st && !suppress) rd++;
618 /* a non-white-space character causes scanf to read, but not store,
619 * a matching non-white-space character. */
621 /* check for character match */
622 if (nch == *format) {
631 TRACE("returning %d\n", rd);
640 #undef _CHAR2SUPPORTED_
641 #undef _WIDE2SUPPORTED_