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 _CONVERT_(c) c /*** FIXME ***/
33 #define _CHAR2DIGIT_(c, base) wchar2digit((c), (base))
34 #else /* WIDE_SCANF */
36 #define _EOF_ MSVCRT_EOF
37 #define _EOF_RET MSVCRT_EOF
38 #define _ISSPACE_(c) isspace(c)
39 #define _ISDIGIT_(c) isdigit(c)
40 #define _CONVERT_(c) c /*** FIXME ***/
41 #define _CHAR2DIGIT_(c, base) char2digit((c), (base))
42 #endif /* WIDE_SCANF */
45 #define _GETC_(file) _getch()
46 #define _UNGETC_(nch, file) _ungetch(nch)
47 #define _FUNCTION_ _cscanf(const _CHAR_ *format, ...)
52 #define _GETC_(file) *file++
53 #define _UNGETC_(nch, file) file--
55 #define _FUNCTION_ MSVCRT_swscanf(const MSVCRT_wchar_t *file, const MSVCRT_wchar_t *format, ...)
56 #else /* WIDE_SCANF */
57 #define _FUNCTION_ MSVCRT_sscanf(const char *file, const char *format, ...)
58 #endif /* WIDE_SCANF */
61 #define _GETC_(file) MSVCRT_fgetwc(file)
62 #define _UNGETC_(nch, file) MSVCRT_ungetwc(nch, file)
63 #define _FUNCTION_ MSVCRT_fwscanf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
64 #else /* WIDE_SCANF */
65 #define _GETC_(file) MSVCRT_fgetc(file)
66 #define _UNGETC_(nch, file) MSVCRT_ungetc(nch, file)
67 #define _FUNCTION_ MSVCRT_fscanf(MSVCRT_FILE* file, const char *format, ...)
68 #endif /* WIDE_SCANF */
72 /*********************************************************************
73 * Implemented based on
74 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt_format_specification_fields_.2d_.scanf_and_wscanf_functions.asp
75 * Extended by C. Scott Ananian <cananian@alumni.princeton.edu> to handle
76 * more types of format spec.
82 if (!*format
) return 0;
85 WARN("(%s): semi-stub\n", debugstr_a(format
));
88 WARN("%s (%s): semi-stub\n", file
, debugstr_a(format
));
90 WARN("%p (%s): semi-stub\n", file
, debugstr_a(format
));
93 #endif /* WIDE_SCANF */
95 if (nch
== _EOF_
) return _EOF_RET
;
99 /* a whitespace character in the format string causes scanf to read,
100 * but not store, all consecutive white-space characters in the input
101 * up to the next non-white-space character. One white space character
102 * in the input matches any number (including zero) and combination of
103 * white-space characters in the input. */
104 if (_ISSPACE_(*format
)) {
105 /* skip whitespace */
106 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
109 /* a format specification causes scanf to read and convert characters
110 * in the input into values of a specified type. The value is assigned
111 * to an argument in the argument list. Format specifications have
112 * the form %[*][width][{h | l | I64 | L}]type */
113 else if (*format
== '%') {
114 int st
= 0; int suppress
= 0; int width
= 0;
115 int base
, number_signed
;
120 int prefix_finished
= 0;
121 /* int I64_prefix = 0; */
123 /* look for leading asterisk, which means 'suppress assignment of
129 /* look for width specification */
130 while (_ISDIGIT_(*format
)) {
132 width
+=*format
++ - '0';
134 if (width
==0) width
=-1; /* no width spec seen */
135 /* read prefix (if any) */
136 while (!prefix_finished
) {
138 case 'h': h_prefix
= 1; break;
139 case 'l': l_prefix
= 1; break;
140 case 'w': w_prefix
= 1; break;
141 case 'L': L_prefix
= 1; break;
143 if (*(format
+ 1) == '6' &&
144 *(format
+ 2) == '4') {
145 /* I64_prefix = 1; */
147 FIXME("I64 prefix currently not implemented in fscanf/fwscanf");
153 if (!prefix_finished
) format
++;
158 case 'X': /* hexadecimal integer. */
159 base
= 16; number_signed
= 0;
161 case 'o': /* octal integer */
162 base
= 8; number_signed
= 0;
164 case 'u': /* unsigned decimal integer */
165 base
= 10; number_signed
= 0;
167 case 'd': /* signed decimal integer */
168 base
= 10; number_signed
= 1;
170 case 'i': /* generic integer */
171 base
= 0; number_signed
= 1;
173 /* read an integer */
174 long unsigned int cur
= 0;
177 /* skip initial whitespace */
178 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
181 if (number_signed
&& (nch
== '-' ||
183 negative
= (nch
=='-');
185 if (width
>0) width
--;
187 /* look for leading indication of base */
188 if (width
!=0 && nch
== '0') {
190 if (width
>0) width
--;
192 if (width
!=0 && (nch
=='x' || nch
=='X')) {
197 if (width
>0) width
--;
203 /* throw away leading zeros */
204 while (width
!=0 && nch
=='0') {
206 if (width
>0) width
--;
209 if (width
!=0 && _CHAR2DIGIT_(nch
, base
)!=-1) {
210 cur
= _CHAR2DIGIT_(nch
, base
);
212 if (width
>0) width
--;
215 /* read until no more digits */
216 while (width
!=0 && (nch
!=_EOF_
) && _CHAR2DIGIT_(nch
, base
)!=-1) {
217 cur
= cur
*base
+ _CHAR2DIGIT_(nch
, base
);
219 if (width
>0) width
--;
223 if (!seendigit
) break; /* not a valid number */
226 #define _SET_NUMBER_(type) *va_arg(ap, type*) = negative ? -cur : cur
228 if (l_prefix
) _SET_NUMBER_(long int);
229 else if (h_prefix
) _SET_NUMBER_(short int);
230 else _SET_NUMBER_(int);
233 WARN("Dropping sign in reading a negative number into an unsigned value");
236 if (l_prefix
) _SET_NUMBER_(unsigned long int);
238 _SET_NUMBER_(unsigned short int);
239 else _SET_NUMBER_(unsigned int);
248 case 'G': { /* read a float */
251 /* skip initial whitespace */
252 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
255 if (nch
== '-' || nch
== '+') {
256 negative
= (nch
=='-');
257 if (width
>0) width
--;
261 /* get first digit. */
262 if (!_ISDIGIT_(nch
)) break;
265 if (width
>0) width
--;
266 /* read until no more digits */
267 while (width
!=0 && (nch
!=_EOF_
) && _ISDIGIT_(nch
)) {
268 cur
= cur
*10 + (nch
- '0');
270 if (width
>0) width
--;
272 /* handle decimals */
273 if (width
!=0 && nch
== '.') {
276 if (width
>0) width
--;
277 while (width
!=0 && (nch
!=_EOF_
) && _ISDIGIT_(nch
)) {
279 cur
+= dec
* (nch
- '0');
281 if (width
>0) width
--;
284 /* handle exponent */
285 if (width
!=0 && (nch
== 'e' || nch
== 'E')) {
286 int exponent
= 0, negexp
= 0;
289 if (width
>0) width
--;
290 /* possible sign on the exponent */
291 if (width
!=0 && (nch
=='+' || nch
=='-')) {
294 if (width
>0) width
--;
296 /* exponent digits */
297 while (width
!=0 && (nch
!=_EOF_
) && _ISDIGIT_(nch
)) {
299 exponent
+= (nch
- '0');
301 if (width
>0) width
--;
303 /* update 'cur' with this exponent. */
304 expcnt
= negexp
? .1 : 10;
305 while (exponent
!=0) {
309 expcnt
=expcnt
*expcnt
;
314 if (L_prefix
) _SET_NUMBER_(long double);
315 else if (l_prefix
) _SET_NUMBER_(double);
316 else _SET_NUMBER_(float);
321 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_scanf_type_field_characters.asp
322 * 's' reads a character string in a call to fscanf
323 * and 'S' a wide character string and vice versa in a
324 * call to fwscanf. The 'h', 'w' and 'l' prefixes override
325 * this behaviour. 'h' forces reading char * but 'l' and 'w'
326 * force reading WCHAR. */
328 if (w_prefix
|| l_prefix
) goto widecharstring
;
329 else if (h_prefix
) goto charstring
;
331 else goto widecharstring
;
332 #else /* WIDE_SCANF */
333 else goto charstring
;
334 #endif /* WIDE_SCANF */
336 if (w_prefix
|| l_prefix
) goto widecharstring
;
337 else if (h_prefix
) goto charstring
;
339 else goto charstring
;
340 #else /* WIDE_SCANF */
341 else goto widecharstring
;
342 #endif /* WIDE_SCANF */
343 charstring
: { /* read a word into a char */
344 char*str
= suppress
? NULL
: va_arg(ap
, char*);
346 /* skip initial whitespace */
347 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
349 /* read until whitespace */
350 while (width
!=0 && (nch
!=_EOF_
) && !_ISSPACE_(nch
)) {
352 if (!suppress
) *sptr
++ = _CONVERT_(nch
);
353 #else /* WIDE_SCANF */
354 if (!suppress
) *sptr
++ = nch
;
355 #endif /* WIDE_SCANF */
358 if (width
>0) width
--;
361 if (!suppress
) *sptr
= 0;
364 widecharstring
: { /* read a word into a wchar_t* */
366 suppress
? NULL
: va_arg(ap
, MSVCRT_wchar_t
*);
367 MSVCRT_wchar_t
*sptr
= str
;
368 /* skip initial whitespace */
369 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
371 /* read until whitespace */
372 while (width
!=0 && (nch
!=_EOF_
) && !_ISSPACE_(nch
)) {
374 if (!suppress
) *sptr
++ = nch
;
375 #else /* WIDE_SCANF */
376 if (!suppress
) *sptr
++ = _CONVERT_(nch
);
377 #endif /* WIDE_SCANF */
380 if (width
>0) width
--;
383 if (!suppress
) *sptr
= 0;
386 /* 'c' and 'C work analogously to 's' and 'S' as described
389 if (w_prefix
|| l_prefix
) goto widecharacter
;
390 else if (h_prefix
) goto character
;
392 else goto widecharacter
;
393 #else /* WIDE_SCANF */
395 #endif /* WIDE_SCANF */
397 if (w_prefix
|| l_prefix
) goto widecharacter
;
398 else if (h_prefix
) goto character
;
401 #else /* WIDE_SCANF */
402 else goto widecharacter
;
403 #endif /* WIDE_SCANF */
404 character
: { /* read single character into char */
406 char*c
= va_arg(ap
, char*);
409 #else /* WIDE_SCANF */
411 #endif /* WIDE_SCANF */
418 if (!suppress
) { /* read single character into a wchar_t */
419 MSVCRT_wchar_t
*c
= va_arg(ap
, MSVCRT_wchar_t
*);
422 #else /* WIDE_SCANF */
424 #endif /* WIDE_SCANF */
432 int*n
= va_arg(ap
, int*);
438 _CHAR_
*str
= suppress
? NULL
: va_arg(ap
, _CHAR_
*);
442 int invert
= 0; /* Set if we are NOT to find the chars */
444 /* Init our bitmap */
446 Mask
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, 65536/8);
447 RtlInitializeBitMap(&bitMask
, Mask
, 65536);
448 #else /* WIDE_SCANF */
449 Mask
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, 256/8);
450 RtlInitializeBitMap(&bitMask
, Mask
, 256);
451 #endif /* WIDE_SCANF */
453 /* Read the format */
460 RtlSetBits(&bitMask
, ']', 1);
463 while(*format
&& (*format
!= ']')) {
464 if((*format
== '-') && (*(format
+ 1) != ']')) {
466 for(;(n
+ *(format
- 1)) < *(format
+ 1); n
++)
467 RtlSetBits(&bitMask
, n
+ *(format
- 1), 1);
470 RtlSetBits(&bitMask
, *format
, 1);
473 /* read until char is not suitable */
474 while ((width
!= 0) && (nch
!= _EOF_
)) {
476 if(RtlAreBitsSet(&bitMask
, nch
, 1)) {
478 if (!suppress
) *sptr
++ = _CONVERT_(nch
);
479 #else /* WIDE_SCANF */
480 if (!suppress
) *sptr
++ = nch
;
481 #endif /* WIDE_SCANF */
485 if(RtlAreBitsClear(&bitMask
, nch
, 1)) {
487 if (!suppress
) *sptr
++ = _CONVERT_(nch
);
488 #else /* WIDE_SCANF */
489 if (!suppress
) *sptr
++ = nch
;
490 #endif /* WIDE_SCANF */
496 if (width
>0) width
--;
499 if (!suppress
) *sptr
= 0;
500 HeapFree(GetProcessHeap(), 0, Mask
);
504 /* From spec: "if a percent sign is followed by a character
505 * that has no meaning as a format-control character, that
506 * character and the following characters are treated as
507 * an ordinary sequence of characters, that is, a sequence
508 * of characters that must match the input. For example,
509 * to specify that a percent-sign character is to be input,
511 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
514 suppress
= 1; /* whoops no field to be read */
515 st
= 1; /* but we got what we expected */
520 if (st
&& !suppress
) rd
++;
523 /* a non-white-space character causes scanf to read, but not store,
524 * a matching non-white-space character. */
526 /* check for character match */
527 if (nch
== *format
) {
537 TRACE("returning %d\n", rd
);