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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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)
51 #define _FUNCTION_ static int MSVCRT_vcscanf(const char *format, va_list ap)
56 #define _GETC_(file) (consumed++, *file++)
57 #define _UNGETC_(nch, file) do { file--; consumed--; } while(0)
59 #define _FUNCTION_ static int MSVCRT_vswscanf(const MSVCRT_wchar_t *file, const MSVCRT_wchar_t *format, va_list ap)
60 #else /* WIDE_SCANF */
61 #define _FUNCTION_ static int MSVCRT_vsscanf(const char *file, const char *format, va_list ap)
62 #endif /* WIDE_SCANF */
65 #define _GETC_(file) (consumed++, MSVCRT_fgetwc(file))
66 #define _UNGETC_(nch, file) do { MSVCRT_ungetwc(nch, file); consumed--; } while(0)
67 #define _FUNCTION_ static int MSVCRT_vfwscanf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, va_list ap)
68 #else /* WIDE_SCANF */
69 #define _GETC_(file) (consumed++, MSVCRT_fgetc(file))
70 #define _UNGETC_(nch, file) do { MSVCRT_ungetc(nch, file); consumed--; } while(0)
71 #define _FUNCTION_ static int MSVCRT_vfscanf(MSVCRT_FILE* file, const char *format, va_list ap)
72 #endif /* WIDE_SCANF */
76 /*********************************************************************
77 * Implemented based on
78 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt_format_specification_fields_.2d_.scanf_and_wscanf_functions.asp
79 * Extended by C. Scott Ananian <cananian@alumni.princeton.edu> to handle
80 * more types of format spec.
83 int rd = 0, consumed = 0;
85 if (!*format) return 0;
88 TRACE("(%s): \n", debugstr_a(format));
91 TRACE("%s (%s)\n", file, debugstr_a(format));
93 TRACE("%p (%s)\n", file, debugstr_a(format));
96 #endif /* WIDE_SCANF */
98 if (nch == _EOF_) return _EOF_RET;
101 /* a whitespace character in the format string causes scanf to read,
102 * but not store, all consecutive white-space characters in the input
103 * up to the next non-white-space character. One white space character
104 * in the input matches any number (including zero) and combination of
105 * white-space characters in the input. */
106 if (_ISSPACE_(*format)) {
107 /* skip whitespace */
108 while ((nch!=_EOF_) && _ISSPACE_(nch))
111 /* a format specification causes scanf to read and convert characters
112 * in the input into values of a specified type. The value is assigned
113 * to an argument in the argument list. Format specifications have
114 * the form %[*][width][{h | l | I64 | L}]type */
115 else if (*format == '%') {
116 int st = 0; int suppress = 0; int width = 0;
117 int base, number_signed;
122 int prefix_finished = 0;
125 /* look for leading asterisk, which means 'suppress assignment of
131 /* look for width specification */
132 while (_ISDIGIT_(*format)) {
134 width+=*format++ - '0';
136 if (width==0) width=-1; /* no width spec seen */
137 /* read prefix (if any) */
138 while (!prefix_finished) {
140 case 'h': h_prefix = 1; break;
141 case 'l': l_prefix = 1; break;
142 case 'w': w_prefix = 1; break;
143 case 'L': L_prefix = 1; break;
145 if (*(format + 1) == '6' &&
146 *(format + 2) == '4') {
154 if (!prefix_finished) format++;
159 case 'X': /* hexadecimal integer. */
160 base = 16; number_signed = 0;
162 case 'o': /* octal integer */
163 base = 8; number_signed = 0;
165 case 'u': /* unsigned decimal integer */
166 base = 10; number_signed = 0;
168 case 'd': /* signed decimal integer */
169 base = 10; number_signed = 1;
171 case 'i': /* generic integer */
172 base = 10; number_signed = 1;
174 /* read an integer */
178 /* skip initial whitespace */
179 while ((nch!=_EOF_) && _ISSPACE_(nch))
182 if (number_signed && (nch == '-' ||
184 negative = (nch=='-');
186 if (width>0) width--;
188 /* look for leading indication of base */
189 if (width!=0 && nch == '0') {
191 if (width>0) width--;
193 if (width!=0 && (nch=='x' || nch=='X')) {
198 if (width>0) width--;
204 /* throw away leading zeros */
205 while (width!=0 && nch=='0') {
207 if (width>0) width--;
210 if (width!=0 && _CHAR2DIGIT_(nch, base)!=-1) {
211 cur = _CHAR2DIGIT_(nch, base);
213 if (width>0) width--;
216 /* read until no more digits */
217 while (width!=0 && (nch!=_EOF_) && _CHAR2DIGIT_(nch, base)!=-1) {
218 cur = cur*base + _CHAR2DIGIT_(nch, base);
220 if (width>0) width--;
224 if (!seendigit) break; /* not a valid number */
227 #define _SET_NUMBER_(type) *va_arg(ap, type*) = negative ? -cur : cur
229 if (I64_prefix) _SET_NUMBER_(LONGLONG);
230 else if (l_prefix) _SET_NUMBER_(long int);
231 else if (h_prefix) _SET_NUMBER_(short int);
232 else _SET_NUMBER_(int);
235 WARN("Dropping sign in reading a negative number into an unsigned value");
238 if (I64_prefix) _SET_NUMBER_(ULONGLONG);
239 else if (l_prefix) _SET_NUMBER_(unsigned long int);
241 _SET_NUMBER_(unsigned short int);
242 else _SET_NUMBER_(unsigned int);
251 case 'G': { /* read a float */
254 /* skip initial whitespace */
255 while ((nch!=_EOF_) && _ISSPACE_(nch))
258 if (nch == '-' || nch == '+') {
259 negative = (nch=='-');
260 if (width>0) width--;
264 /* get first digit. */
266 if (!_ISDIGIT_(nch)) break;
269 if (width>0) width--;
270 /* read until no more digits */
271 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
272 cur = cur*10 + (nch - '0');
274 if (width>0) width--;
277 cur = 0; /* MaxPayneDemo Fix: .8 -> 0.8 */
279 /* handle decimals */
280 if (width!=0 && nch == '.') {
283 if (width>0) width--;
284 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
286 cur += dec * (nch - '0');
288 if (width>0) width--;
291 /* handle exponent */
292 if (width!=0 && (nch == 'e' || nch == 'E')) {
293 int exponent = 0, negexp = 0;
296 if (width>0) width--;
297 /* possible sign on the exponent */
298 if (width!=0 && (nch=='+' || nch=='-')) {
301 if (width>0) width--;
303 /* exponent digits */
304 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
306 exponent += (nch - '0');
308 if (width>0) width--;
310 /* update 'cur' with this exponent. */
311 expcnt = negexp ? .1 : 10;
312 while (exponent!=0) {
316 expcnt=expcnt*expcnt;
321 if (L_prefix) _SET_NUMBER_(long double);
322 else if (l_prefix) _SET_NUMBER_(double);
323 else _SET_NUMBER_(float);
328 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_scanf_type_field_characters.asp
329 * 's' reads a character string in a call to fscanf
330 * and 'S' a wide character string and vice versa in a
331 * call to fwscanf. The 'h', 'w' and 'l' prefixes override
332 * this behaviour. 'h' forces reading char * but 'l' and 'w'
333 * force reading WCHAR. */
335 if (w_prefix || l_prefix) goto widecharstring;
336 else if (h_prefix) goto charstring;
338 else goto widecharstring;
339 #else /* WIDE_SCANF */
340 else goto charstring;
341 #endif /* WIDE_SCANF */
343 if (w_prefix || l_prefix) goto widecharstring;
344 else if (h_prefix) goto charstring;
346 else goto charstring;
347 #else /* WIDE_SCANF */
348 else goto widecharstring;
349 #endif /* WIDE_SCANF */
350 charstring: { /* read a word into a char */
351 char*str = suppress ? NULL : va_arg(ap, char*);
353 /* skip initial whitespace */
354 while ((nch!=_EOF_) && _ISSPACE_(nch))
356 /* read until whitespace */
357 while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
358 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
361 if (width>0) width--;
364 if (!suppress) *sptr = 0;
367 widecharstring: { /* read a word into a wchar_t* */
369 suppress ? NULL : va_arg(ap, MSVCRT_wchar_t*);
370 MSVCRT_wchar_t*sptr = str;
371 /* skip initial whitespace */
372 while ((nch!=_EOF_) && _ISSPACE_(nch))
374 /* read until whitespace */
375 while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
376 if (!suppress) *sptr++ = _WIDE2SUPPORTED_(nch);
379 if (width>0) width--;
382 if (!suppress) *sptr = 0;
385 /* 'c' and 'C work analogously to 's' and 'S' as described
388 if (w_prefix || l_prefix) goto widecharacter;
389 else if (h_prefix) goto character;
391 else goto widecharacter;
392 #else /* WIDE_SCANF */
394 #endif /* WIDE_SCANF */
396 if (w_prefix || l_prefix) goto widecharacter;
397 else if (h_prefix) goto character;
400 #else /* WIDE_SCANF */
401 else goto widecharacter;
402 #endif /* WIDE_SCANF */
403 character: { /* read single character into char */
406 char*c = va_arg(ap, char*);
407 *c = _CHAR2SUPPORTED_(nch);
414 widecharacter: { /* read single character into a wchar_t */
417 MSVCRT_wchar_t*c = va_arg(ap, MSVCRT_wchar_t*);
418 *c = _WIDE2SUPPORTED_(nch);
427 int*n = va_arg(ap, int*);
428 *n = consumed - (nch!=_EOF_);
430 /* This is an odd one: according to the standard,
431 * "Execution of a %n directive does not increment the
432 * assignment count returned at the completion of
433 * execution" even if it wasn't suppressed with the
434 * '*' flag. The Corrigendum to the standard seems
435 * to contradict this (comment out the assignment to
436 * suppress below if you want to implement these
437 * alternate semantics) but the windows program I'm
438 * looking at expects the behavior I've coded here
439 * (which happens to be what glibc does as well).
446 _CHAR_ *str = suppress ? NULL : va_arg(ap, _CHAR_*);
450 int invert = 0; /* Set if we are NOT to find the chars */
452 /* Init our bitmap */
453 Mask = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, _BITMAPSIZE_/8);
454 RtlInitializeBitMap(&bitMask, Mask, _BITMAPSIZE_);
456 /* Read the format */
463 RtlSetBits(&bitMask, ']', 1);
466 while(*format && (*format != ']')) {
468 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt_scanf_width_specification.asp
469 * "Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]." */
470 if((*format == '-') && (*(format + 1) != ']')) {
471 if ((*(format - 1)) < *(format + 1))
472 RtlSetBits(&bitMask, *(format - 1) +1 , *(format + 1) - *(format - 1));
474 RtlSetBits(&bitMask, *(format + 1) , *(format - 1) - *(format + 1));
477 RtlSetBits(&bitMask, *format, 1);
480 /* read until char is not suitable */
481 while ((width != 0) && (nch != _EOF_)) {
483 if(RtlAreBitsSet(&bitMask, nch, 1)) {
484 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
488 if(RtlAreBitsClear(&bitMask, nch, 1)) {
489 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
495 if (width>0) width--;
498 if (!suppress) *sptr = 0;
499 HeapFree(GetProcessHeap(), 0, Mask);
503 /* From spec: "if a percent sign is followed by a character
504 * that has no meaning as a format-control character, that
505 * character and the following characters are treated as
506 * an ordinary sequence of characters, that is, a sequence
507 * of characters that must match the input. For example,
508 * to specify that a percent-sign character is to be input,
510 while ((nch!=_EOF_) && _ISSPACE_(nch))
513 suppress = 1; /* whoops no field to be read */
514 st = 1; /* but we got what we expected */
519 if (st && !suppress) rd++;
522 /* a non-white-space character causes scanf to read, but not store,
523 * a matching non-white-space character. */
525 /* check for character match */
526 if (nch == *format) {
535 TRACE("returning %d\n", rd);
544 #undef _CHAR2SUPPORTED_
545 #undef _WIDE2SUPPORTED_