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 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
29 #define _EOF_ 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 */
37 #define _EOF_ 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 WCHAR *file, const WCHAR *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 WCHAR *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", format);
88 WARN("%s (\"%s\"): semi-stub\n", file, format);
90 WARN("%p (\"%s\"): semi-stub\n", file, format);
93 #endif /* WIDE_SCANF */
97 /* a whitespace character in the format string causes scanf to read,
98 * but not store, all consecutive white-space characters in the input
99 * up to the next non-white-space character. One white space character
100 * in the input matches any number (including zero) and combination of
101 * white-space characters in the input. */
102 if (_ISSPACE_(*format)) {
103 /* skip whitespace */
104 while ((nch!=_EOF_) && _ISSPACE_(nch))
107 /* a format specification causes scanf to read and convert characters
108 * in the input into values of a specified type. The value is assigned
109 * to an argument in the argument list. Format specifications have
110 * the form %[*][width][{h | l | I64 | L}]type */
111 else if (*format == _L_('%')) {
112 int st = 0; int suppress = 0; int width = 0;
113 int base, number_signed;
118 int prefix_finished = 0;
119 /* int I64_prefix = 0; */
121 /* look for leading asterisk, which means 'suppress assignment of
123 if (*format==_L_('*')) {
127 /* look for width specification */
128 while (_ISDIGIT_(*format)) {
130 width+=*format++ - _L_('0');
132 if (width==0) width=-1; /* no width spec seen */
133 /* read prefix (if any) */
134 while (!prefix_finished) {
136 case _L_('h'): h_prefix = 1; break;
137 case _L_('l'): l_prefix = 1; break;
138 case _L_('w'): w_prefix = 1; break;
139 case _L_('L'): L_prefix = 1; break;
141 if (*(format + 1) == _L_('6') &&
142 *(format + 2) == _L_('4')) {
143 /* I64_prefix = 1; */
145 FIXME("I64 prefix currently not implemented in fscanf/fwscanf");
151 if (!prefix_finished) format++;
155 case _L_('%'): { /* read a percent symbol */
156 while ((nch!=_EOF_) && _ISSPACE_(nch))
159 suppress = 1; /* whoops no field to be read */
160 st = 1; /* but we got what we expected */
166 case _L_('X'): /* hexadecimal integer. */
167 base = 16; number_signed = 0;
169 case _L_('o'): /* octal integer */
170 base = 8; number_signed = 0;
172 case _L_('u'): /* unsigned decimal integer */
173 base = 10; number_signed = 0;
175 case _L_('d'): /* signed decimal integer */
176 base = 10; number_signed = 1;
178 case _L_('i'): /* generic integer */
179 base = 0; number_signed = 1;
181 /* read an integer */
182 long unsigned int cur = 0;
185 /* skip initial whitespace */
186 while ((nch!=_EOF_) && _ISSPACE_(nch))
189 if (number_signed && (nch == _L_('-') ||
191 negative = (nch==_L_('-'));
193 if (width>0) width--;
195 /* look for leading indication of base */
196 if (width!=0 && nch == _L_('0')) {
198 if (width>0) width--;
200 if (width!=0 && (nch==_L_('x') || nch==_L_('X'))) {
205 if (width>0) width--;
211 /* throw away leading zeros */
212 while (width!=0 && nch==_L_('0')) {
214 if (width>0) width--;
217 if (width!=0 && _CHAR2DIGIT_(nch, base)!=-1) {
218 cur = _CHAR2DIGIT_(nch, base);
220 if (width>0) width--;
223 /* read until no more digits */
224 while (width!=0 && (nch!=_EOF_) && _CHAR2DIGIT_(nch, base)!=-1) {
225 cur = cur*base + _CHAR2DIGIT_(nch, base);
227 if (width>0) width--;
231 if (!seendigit) break; /* not a valid number */
234 #define _SET_NUMBER_(type) *va_arg(ap, type*) = negative ? -cur : cur
236 if (l_prefix) _SET_NUMBER_(long int);
237 else if (h_prefix) _SET_NUMBER_(short int);
238 else _SET_NUMBER_(int);
241 WARN("Dropping sign in reading a negative number into an unsigned value");
244 if (l_prefix) _SET_NUMBER_(unsigned long int);
246 _SET_NUMBER_(unsigned short int);
247 else _SET_NUMBER_(unsigned int);
256 case _L_('G'): { /* read a float */
259 /* skip initial whitespace */
260 while ((nch!=_EOF_) && _ISSPACE_(nch))
263 if (nch == _L_('-') || nch == _L_('+')) {
264 negative = (nch==_L_('-'));
265 if (width>0) width--;
269 /* get first digit. */
270 if (!_ISDIGIT_(nch)) break;
271 cur = (nch - _L_('0')) * (negative ? -1 : 1);
273 if (width>0) width--;
274 /* read until no more digits */
275 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
276 cur = cur*10 + (nch - _L_('0'));
278 if (width>0) width--;
280 /* handle decimals */
281 if (width!=0 && nch == _L_('.')) {
284 if (width>0) width--;
285 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
287 cur += dec * (nch - _L_('0'));
289 if (width>0) width--;
292 /* handle exponent */
293 if (width!=0 && (nch == _L_('e') || nch == _L_('E'))) {
294 int exponent = 0, negexp = 0;
297 if (width>0) width--;
298 /* possible sign on the exponent */
299 if (width!=0 && (nch==_L_('+') || nch==_L_('-'))) {
300 negexp = (nch==_L_('-'));
302 if (width>0) width--;
304 /* exponent digits */
305 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
307 exponent += (nch - _L_('0'));
309 if (width>0) width--;
311 /* update 'cur' with this exponent. */
312 expcnt = negexp ? .1 : 10;
313 while (exponent!=0) {
317 expcnt=expcnt*expcnt;
322 if (L_prefix) _SET_NUMBER_(long double);
323 else if (l_prefix) _SET_NUMBER_(double);
324 else _SET_NUMBER_(float);
329 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_scanf_type_field_characters.asp
330 * 's' reads a character string in a call to fscanf
331 * and 'S' a wide character string and vice versa in a
332 * call to fwscanf. The 'h', 'w' and 'l' prefixes override
333 * this behaviour. 'h' forces reading char * but 'l' and 'w'
334 * force reading WCHAR. */
336 if (w_prefix || l_prefix) goto widecharstring;
337 else if (h_prefix) goto charstring;
339 else goto widecharstring;
340 #else /* WIDE_SCANF */
341 else goto charstring;
342 #endif /* WIDE_SCANF */
344 if (w_prefix || l_prefix) goto widecharstring;
345 else if (h_prefix) goto charstring;
347 else goto charstring;
348 #else /* WIDE_SCANF */
349 else goto widecharstring;
350 #endif /* WIDE_SCANF */
351 charstring: { /* read a word into a char */
352 char*str = suppress ? NULL : va_arg(ap, char*);
354 /* skip initial whitespace */
355 while ((nch!=_EOF_) && _ISSPACE_(nch))
357 /* read until whitespace */
358 while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
360 if (!suppress) *sptr++ = _CONVERT_(nch);
361 #else /* WIDE_SCANF */
362 if (!suppress) *sptr++ = nch;
363 #endif /* WIDE_SCANF */
366 if (width>0) width--;
369 if (!suppress) *sptr = 0;
372 widecharstring: { /* read a word into a WCHAR * */
374 suppress ? NULL : va_arg(ap, WCHAR*);
376 /* skip initial whitespace */
377 while ((nch!=_EOF_) && _ISSPACE_(nch))
379 /* read until whitespace */
380 while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
382 if (!suppress) *sptr++ = nch;
383 #else /* WIDE_SCANF */
384 if (!suppress) *sptr++ = _CONVERT_(nch);
385 #endif /* WIDE_SCANF */
388 if (width>0) width--;
391 if (!suppress) *sptr = 0;
394 /* 'c' and 'C work analogously to 's' and 'S' as described
397 if (w_prefix || l_prefix) goto widecharacter;
398 else if (h_prefix) goto character;
400 else goto widecharacter;
401 #else /* WIDE_SCANF */
403 #endif /* WIDE_SCANF */
405 if (w_prefix || l_prefix) goto widecharacter;
406 else if (h_prefix) goto character;
409 #else /* WIDE_SCANF */
410 else goto widecharacter;
411 #endif /* WIDE_SCANF */
412 character: { /* read single character into char */
414 char*c = va_arg(ap, char*);
417 #else /* WIDE_SCANF */
419 #endif /* WIDE_SCANF */
426 if (!suppress) { /* read single character into WCHAR */
427 WCHAR*c = va_arg(ap, WCHAR*);
430 #else /* WIDE_SCANF */
432 #endif /* WIDE_SCANF */
440 int*n = va_arg(ap, int*);
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 */
454 Mask = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 65536/8);
455 RtlInitializeBitMap(&bitMask, Mask, 65536);
456 #else /* WIDE_SCANF */
457 Mask = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 256/8);
458 RtlInitializeBitMap(&bitMask, Mask, 256);
459 #endif /* WIDE_SCANF */
461 /* Read the format */
468 RtlSetBits(&bitMask, ']', 1);
471 while(*format && (*format != ']')) {
472 if((*format == '-') && (*(format + 1) != ']')) {
474 for(;(n + *(format - 1)) < *(format + 1); n++)
475 RtlSetBits(&bitMask, n + *(format - 1), 1);
478 RtlSetBits(&bitMask, *format, 1);
481 /* read until char is not suitable */
482 while ((width != 0) && (nch != _EOF_)) {
484 if(RtlAreBitsSet(&bitMask, nch, 1)) {
486 if (!suppress) *sptr++ = _CONVERT_(nch);
487 #else /* WIDE_SCANF */
488 if (!suppress) *sptr++ = nch;
489 #endif /* WIDE_SCANF */
493 if(RtlAreBitsClear(&bitMask, nch, 1)) {
495 if (!suppress) *sptr++ = _CONVERT_(nch);
496 #else /* WIDE_SCANF */
497 if (!suppress) *sptr++ = nch;
498 #endif /* WIDE_SCANF */
504 if (width>0) width--;
507 if (!suppress) *sptr = 0;
508 HeapFree(GetProcessHeap(), 0, Mask);
511 default: FIXME("unhandled: %%%c\n", *format);
512 /* From spec: "if a percent sign is followed by a character
513 * that has no meaning as a format-control character, that
514 * character and the following characters are treated as
515 * an ordinary sequence of characters, that is, a sequence
516 * of characters that must match the input. For example,
517 * to specify that a percent-sign character is to be input,
519 * LEAVING AS-IS because we catch bugs better that way. */
521 if (st && !suppress) rd++;
524 /* a non-white-space character causes scanf to read, but not store,
525 * a matching non-white-space character. */
527 /* check for character match */
528 if (nch == *format) {
538 TRACE("returning %d\n", rd);