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, ...)
50 #define _GETC_(file) *file++
51 #define _UNGETC_(nch, file) file--
53 #define _FUNCTION_ MSVCRT_swscanf(const WCHAR *file, const WCHAR *format, ...)
54 #else /* WIDE_SCANF */
55 #define _FUNCTION_ MSVCRT_sscanf(const char *file, const char *format, ...)
56 #endif /* WIDE_SCANF */
59 #define _GETC_(file) MSVCRT_fgetwc(file)
60 #define _UNGETC_(nch, file) MSVCRT_ungetwc(nch, file)
61 #define _FUNCTION_ MSVCRT_fwscanf(MSVCRT_FILE* file, const WCHAR *format, ...)
62 #else /* WIDE_SCANF */
63 #define _GETC_(file) MSVCRT_fgetc(file)
64 #define _UNGETC_(nch, file) MSVCRT_ungetc(nch, file)
65 #define _FUNCTION_ MSVCRT_fscanf(MSVCRT_FILE* file, const char *format, ...)
66 #endif /* WIDE_SCANF */
70 /*********************************************************************
71 * Implemented based on
72 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt_format_specification_fields_.2d_.scanf_and_wscanf_functions.asp
73 * Extended by C. Scott Ananian <cananian@alumni.princeton.edu> to handle
74 * more types of format spec.
80 if (!*format) return 0;
83 WARN("(\"%s\"): semi-stub\n", format);
86 WARN("%s (\"%s\"): semi-stub\n", file, format);
88 WARN("%p (\"%s\"): semi-stub\n", file, format);
91 #endif /* WIDE_SCANF */
95 /* a whitespace character in the format string causes scanf to read,
96 * but not store, all consecutive white-space characters in the input
97 * up to the next non-white-space character. One white space character
98 * in the input matches any number (including zero) and combination of
99 * white-space characters in the input. */
100 if (_ISSPACE_(*format)) {
101 /* skip whitespace */
102 while ((nch!=_EOF_) && _ISSPACE_(nch))
105 /* a format specification causes scanf to read and convert characters
106 * in the input into values of a specified type. The value is assigned
107 * to an argument in the argument list. Format specifications have
108 * the form %[*][width][{h | l | I64 | L}]type */
109 else if (*format == _L_('%')) {
110 int st = 0; int suppress = 0; int width = 0;
111 int base, number_signed;
116 int prefix_finished = 0;
117 /* int I64_prefix = 0; */
119 /* look for leading asterisk, which means 'suppress assignment of
121 if (*format==_L_('*')) {
125 /* look for width specification */
126 while (_ISDIGIT_(*format)) {
128 width+=*format++ - _L_('0');
130 if (width==0) width=-1; /* no width spec seen */
131 /* read prefix (if any) */
132 while (!prefix_finished) {
134 case _L_('h'): h_prefix = 1; break;
135 case _L_('l'): l_prefix = 1; break;
136 case _L_('w'): w_prefix = 1; break;
137 case _L_('L'): L_prefix = 1; break;
139 if (*(format + 1) == _L_('6') &&
140 *(format + 2) == _L_('4')) {
141 /* I64_prefix = 1; */
143 FIXME("I64 prefix currently not implemented in fscanf/fwscanf");
149 if (!prefix_finished) format++;
153 case _L_('%'): { /* read a percent symbol */
154 while ((nch!=_EOF_) && _ISSPACE_(nch))
157 suppress = 1; /* whoops no field to be read */
158 st = 1; /* but we got what we expected */
164 case _L_('X'): /* hexadecimal integer. */
165 base = 16; number_signed = 0;
167 case _L_('o'): /* octal integer */
168 base = 8; number_signed = 0;
170 case _L_('u'): /* unsigned decimal integer */
171 base = 10; number_signed = 0;
173 case _L_('d'): /* signed decimal integer */
174 base = 10; number_signed = 1;
176 case _L_('i'): /* generic integer */
177 base = 0; number_signed = 1;
179 /* read an integer */
180 long unsigned int cur = 0;
183 /* skip initial whitespace */
184 while ((nch!=_EOF_) && _ISSPACE_(nch))
187 if (number_signed && (nch == _L_('-') ||
189 negative = (nch==_L_('-'));
191 if (width>0) width--;
193 /* look for leading indication of base */
194 if (width!=0 && nch == _L_('0')) {
196 if (width>0) width--;
198 if (width!=0 && (nch==_L_('x') || nch==_L_('X'))) {
203 if (width>0) width--;
209 /* throw away leading zeros */
210 while (width!=0 && nch==_L_('0')) {
212 if (width>0) width--;
215 if (width!=0 && _CHAR2DIGIT_(nch, base)!=-1) {
216 cur = _CHAR2DIGIT_(nch, base);
218 if (width>0) width--;
221 /* read until no more digits */
222 while (width!=0 && (nch!=_EOF_) && _CHAR2DIGIT_(nch, base)!=-1) {
223 cur = cur*base + _CHAR2DIGIT_(nch, base);
225 if (width>0) width--;
229 if (!seendigit) break; /* not a valid number */
232 #define _SET_NUMBER_(type) *va_arg(ap, type*) = negative ? -cur : cur
234 if (l_prefix) _SET_NUMBER_(long int);
235 else if (h_prefix) _SET_NUMBER_(short int);
236 else _SET_NUMBER_(int);
239 WARN("Dropping sign in reading a negative number into an unsigned value");
242 if (l_prefix) _SET_NUMBER_(unsigned long int);
244 _SET_NUMBER_(unsigned short int);
245 else _SET_NUMBER_(unsigned int);
254 case _L_('G'): { /* read a float */
257 /* skip initial whitespace */
258 while ((nch!=_EOF_) && _ISSPACE_(nch))
261 if (nch == _L_('-') || nch == _L_('+')) {
262 negative = (nch==_L_('-'));
263 if (width>0) width--;
267 /* get first digit. */
268 if (!_ISDIGIT_(nch)) break;
269 cur = (nch - _L_('0')) * (negative ? -1 : 1);
271 if (width>0) width--;
272 /* read until no more digits */
273 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
274 cur = cur*10 + (nch - _L_('0'));
276 if (width>0) width--;
278 /* handle decimals */
279 if (width!=0 && nch == _L_('.')) {
282 if (width>0) width--;
283 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
285 cur += dec * (nch - _L_('0'));
287 if (width>0) width--;
290 /* handle exponent */
291 if (width!=0 && (nch == _L_('e') || nch == _L_('E'))) {
292 int exponent = 0, negexp = 0;
295 if (width>0) width--;
296 /* possible sign on the exponent */
297 if (width!=0 && (nch==_L_('+') || nch==_L_('-'))) {
298 negexp = (nch==_L_('-'));
300 if (width>0) width--;
302 /* exponent digits */
303 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
305 exponent += (nch - _L_('0'));
307 if (width>0) width--;
309 /* update 'cur' with this exponent. */
310 expcnt = negexp ? .1 : 10;
311 while (exponent!=0) {
315 expcnt=expcnt*expcnt;
320 if (L_prefix) _SET_NUMBER_(long double);
321 else if (l_prefix) _SET_NUMBER_(double);
322 else _SET_NUMBER_(float);
327 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_scanf_type_field_characters.asp
328 * 's' reads a character string in a call to fscanf
329 * and 'S' a wide character string and vice versa in a
330 * call to fwscanf. The 'h', 'w' and 'l' prefixes override
331 * this behaviour. 'h' forces reading char * but 'l' and 'w'
332 * force reading WCHAR. */
334 if (w_prefix || l_prefix) goto widecharstring;
335 else if (h_prefix) goto charstring;
337 else goto widecharstring;
338 #else /* WIDE_SCANF */
339 else goto charstring;
340 #endif /* WIDE_SCANF */
342 if (w_prefix || l_prefix) goto widecharstring;
343 else if (h_prefix) goto charstring;
345 else goto charstring;
346 #else /* WIDE_SCANF */
347 else goto widecharstring;
348 #endif /* WIDE_SCANF */
349 charstring: { /* read a word into a char */
350 char*str = suppress ? NULL : va_arg(ap, char*);
352 /* skip initial whitespace */
353 while ((nch!=_EOF_) && _ISSPACE_(nch))
355 /* read until whitespace */
356 while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
358 if (!suppress) *sptr++ = _CONVERT_(nch);
359 #else /* WIDE_SCANF */
360 if (!suppress) *sptr++ = nch;
361 #endif /* WIDE_SCANF */
364 if (width>0) width--;
367 if (!suppress) *sptr = 0;
370 widecharstring: { /* read a word into a WCHAR * */
372 suppress ? NULL : va_arg(ap, WCHAR*);
374 /* skip initial whitespace */
375 while ((nch!=_EOF_) && _ISSPACE_(nch))
377 /* read until whitespace */
378 while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
380 if (!suppress) *sptr++ = nch;
381 #else /* WIDE_SCANF */
382 if (!suppress) *sptr++ = _CONVERT_(nch);
383 #endif /* WIDE_SCANF */
386 if (width>0) width--;
389 if (!suppress) *sptr = 0;
392 /* 'c' and 'C work analogously to 's' and 'S' as described
395 if (w_prefix || l_prefix) goto widecharacter;
396 else if (h_prefix) goto character;
398 else goto widecharacter;
399 #else /* WIDE_SCANF */
401 #endif /* WIDE_SCANF */
403 if (w_prefix || l_prefix) goto widecharacter;
404 else if (h_prefix) goto character;
407 #else /* WIDE_SCANF */
408 else goto widecharacter;
409 #endif /* WIDE_SCANF */
410 character: { /* read single character into char */
412 char*c = va_arg(ap, char*);
415 #else /* WIDE_SCANF */
417 #endif /* WIDE_SCANF */
424 if (!suppress) { /* read single character into WCHAR */
425 WCHAR*c = va_arg(ap, WCHAR*);
428 #else /* WIDE_SCANF */
430 #endif /* WIDE_SCANF */
438 int*n = va_arg(ap, int*);
443 default: FIXME("unhandled: %%%c\n", *format);
444 /* From spec: "if a percent sign is followed by a character
445 * that has no meaning as a format-control character, that
446 * character and the following characters are treated as
447 * an ordinary sequence of characters, that is, a sequence
448 * of characters that must match the input. For example,
449 * to specify that a percent-sign character is to be input,
451 * LEAVING AS-IS because we catch bugs better that way. */
453 if (st && !suppress) rd++;
456 /* a non-white-space character causes scanf to read, but not store,
457 * a matching non-white-space character. */
459 /* check for character match */
460 if (nch == *format) {
470 TRACE("returning %d\n", rd);