dplayx: Adjust GetCaps behaviour to documentation
[wine/gsoc_dplay.git] / dlls / msvcrt / scanf.h
blob3eb705b23766a3fae451d832d1e1abad3b321b19
1 /*
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
26 #ifdef WIDE_SCANF
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 */
37 #define _CHAR_ char
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 */
48 #ifdef CONSOLE
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, __ms_va_list ap)
52 #else
53 #ifdef STRING
54 #undef _EOF_
55 #define _EOF_ 0
56 #define _GETC_(file) (consumed++, *file++)
57 #define _UNGETC_(nch, file) do { file--; consumed--; } while(0)
58 #ifdef WIDE_SCANF
59 #define _FUNCTION_ static int MSVCRT_vswscanf(const MSVCRT_wchar_t *file, const MSVCRT_wchar_t *format, __ms_va_list ap)
60 #else /* WIDE_SCANF */
61 #define _FUNCTION_ static int MSVCRT_vsscanf(const char *file, const char *format, __ms_va_list ap)
62 #endif /* WIDE_SCANF */
63 #else /* STRING */
64 #ifdef 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, __ms_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, __ms_va_list ap)
72 #endif /* WIDE_SCANF */
73 #endif /* STRING */
74 #endif /* CONSOLE */
76 _FUNCTION_ {
77 int rd = 0, consumed = 0;
78 int nch;
79 if (!*format) return 0;
80 #ifndef WIDE_SCANF
81 #ifdef CONSOLE
82 TRACE("(%s):\n", debugstr_a(format));
83 #else /* CONSOLE */
84 #ifdef STRING
85 TRACE("%s (%s)\n", file, debugstr_a(format));
86 #else /* STRING */
87 TRACE("%p (%s)\n", file, debugstr_a(format));
88 #endif /* STRING */
89 #endif /* CONSOLE */
90 #endif /* WIDE_SCANF */
91 nch = _GETC_(file);
92 if (nch == _EOF_) return _EOF_RET;
94 while (*format) {
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))
103 nch = _GETC_(file);
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 == '%') {
110 int st = 0; int suppress = 0; int width = 0;
111 int base;
112 int h_prefix = 0;
113 int l_prefix = 0;
114 int L_prefix = 0;
115 int w_prefix = 0;
116 int prefix_finished = 0;
117 int I64_prefix = 0;
118 format++;
119 /* look for leading asterisk, which means 'suppress assignment of
120 * this field'. */
121 if (*format=='*') {
122 format++;
123 suppress=1;
125 /* look for width specification */
126 while (_ISDIGIT_(*format)) {
127 width*=10;
128 width+=*format++ - '0';
130 if (width==0) width=-1; /* no width spec seen */
131 /* read prefix (if any) */
132 while (!prefix_finished) {
133 switch(*format) {
134 case 'h': h_prefix = 1; break;
135 case 'l': l_prefix = 1; break;
136 case 'w': w_prefix = 1; break;
137 case 'L': L_prefix = 1; break;
138 case 'I':
139 if (*(format + 1) == '6' &&
140 *(format + 2) == '4') {
141 I64_prefix = 1;
142 format += 2;
144 break;
145 default:
146 prefix_finished = 1;
148 if (!prefix_finished) format++;
150 /* read type */
151 switch(*format) {
152 case 'p':
153 case 'P': /* pointer. */
154 if (sizeof(void *) == sizeof(LONGLONG)) I64_prefix = 1;
155 /* fall through */
156 case 'x':
157 case 'X': /* hexadecimal integer. */
158 base = 16;
159 goto number;
160 case 'o': /* octal integer */
161 base = 8;
162 goto number;
163 case 'u': /* unsigned decimal integer */
164 base = 10;
165 goto number;
166 case 'd': /* signed decimal integer */
167 base = 10;
168 goto number;
169 case 'i': /* generic integer */
170 base = 0;
171 number: {
172 /* read an integer */
173 ULONGLONG cur = 0;
174 int negative = 0;
175 int seendigit=0;
176 /* skip initial whitespace */
177 while ((nch!=_EOF_) && _ISSPACE_(nch))
178 nch = _GETC_(file);
179 /* get sign */
180 if (nch == '-' || nch == '+') {
181 negative = (nch=='-');
182 nch = _GETC_(file);
183 if (width>0) width--;
185 /* look for leading indication of base */
186 if (width!=0 && nch == '0' && *format != 'p' && *format != 'P') {
187 nch = _GETC_(file);
188 if (width>0) width--;
189 seendigit=1;
190 if (width!=0 && (nch=='x' || nch=='X')) {
191 if (base==0)
192 base=16;
193 if (base==16) {
194 nch = _GETC_(file);
195 if (width>0) width--;
196 seendigit=0;
198 } else if (base==0)
199 base = 8;
201 /* format %i without indication of base */
202 if (base==0)
203 base = 10;
204 /* throw away leading zeros */
205 while (width!=0 && nch=='0') {
206 nch = _GETC_(file);
207 if (width>0) width--;
208 seendigit=1;
210 if (width!=0 && _CHAR2DIGIT_(nch, base)!=-1) {
211 cur = _CHAR2DIGIT_(nch, base);
212 nch = _GETC_(file);
213 if (width>0) width--;
214 seendigit=1;
216 /* read until no more digits */
217 while (width!=0 && (nch!=_EOF_) && _CHAR2DIGIT_(nch, base)!=-1) {
218 cur = cur*base + _CHAR2DIGIT_(nch, base);
219 nch = _GETC_(file);
220 if (width>0) width--;
221 seendigit=1;
223 /* okay, done! */
224 if (!seendigit) break; /* not a valid number */
225 st = 1;
226 if (!suppress) {
227 #define _SET_NUMBER_(type) *va_arg(ap, type*) = negative ? -cur : cur
228 if (I64_prefix) _SET_NUMBER_(LONGLONG);
229 else if (l_prefix) _SET_NUMBER_(LONG);
230 else if (h_prefix) _SET_NUMBER_(short int);
231 else _SET_NUMBER_(int);
234 break;
235 case 'e':
236 case 'E':
237 case 'f':
238 case 'g':
239 case 'G': { /* read a float */
240 long double cur = 0;
241 int negative = 0;
242 /* skip initial whitespace */
243 while ((nch!=_EOF_) && _ISSPACE_(nch))
244 nch = _GETC_(file);
245 /* get sign. */
246 if (nch == '-' || nch == '+') {
247 negative = (nch=='-');
248 if (width>0) width--;
249 if (width==0) break;
250 nch = _GETC_(file);
252 /* get first digit. */
253 if ('.' != nch) {
254 if (!_ISDIGIT_(nch)) break;
255 cur = (nch - '0');
256 nch = _GETC_(file);
257 if (width>0) width--;
258 /* read until no more digits */
259 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
260 cur = cur*10 + (nch - '0');
261 nch = _GETC_(file);
262 if (width>0) width--;
264 } else {
265 cur = 0; /* MaxPayneDemo Fix: .8 -> 0.8 */
267 /* handle decimals */
268 if (width!=0 && nch == '.') {
269 float dec = 1;
270 nch = _GETC_(file);
271 if (width>0) width--;
272 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
273 dec /= 10;
274 cur += dec * (nch - '0');
275 nch = _GETC_(file);
276 if (width>0) width--;
279 /* handle exponent */
280 if (width!=0 && (nch == 'e' || nch == 'E')) {
281 int exponent = 0, negexp = 0;
282 float expcnt;
283 nch = _GETC_(file);
284 if (width>0) width--;
285 /* possible sign on the exponent */
286 if (width!=0 && (nch=='+' || nch=='-')) {
287 negexp = (nch=='-');
288 nch = _GETC_(file);
289 if (width>0) width--;
291 /* exponent digits */
292 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
293 exponent *= 10;
294 exponent += (nch - '0');
295 nch = _GETC_(file);
296 if (width>0) width--;
298 /* update 'cur' with this exponent. */
299 expcnt = negexp ? .1 : 10;
300 while (exponent!=0) {
301 if (exponent&1)
302 cur*=expcnt;
303 exponent/=2;
304 expcnt=expcnt*expcnt;
307 st = 1;
308 if (!suppress) {
309 if (L_prefix) _SET_NUMBER_(long double);
310 else if (l_prefix) _SET_NUMBER_(double);
311 else _SET_NUMBER_(float);
314 break;
315 /* According to msdn,
316 * 's' reads a character string in a call to fscanf
317 * and 'S' a wide character string and vice versa in a
318 * call to fwscanf. The 'h', 'w' and 'l' prefixes override
319 * this behaviour. 'h' forces reading char * but 'l' and 'w'
320 * force reading WCHAR. */
321 case 's':
322 if (w_prefix || l_prefix) goto widecharstring;
323 else if (h_prefix) goto charstring;
324 #ifdef WIDE_SCANF
325 else goto widecharstring;
326 #else /* WIDE_SCANF */
327 else goto charstring;
328 #endif /* WIDE_SCANF */
329 case 'S':
330 if (w_prefix || l_prefix) goto widecharstring;
331 else if (h_prefix) goto charstring;
332 #ifdef WIDE_SCANF
333 else goto charstring;
334 #else /* WIDE_SCANF */
335 else goto widecharstring;
336 #endif /* WIDE_SCANF */
337 charstring: { /* read a word into a char */
338 char *sptr = suppress ? NULL : va_arg(ap, char*);
339 /* skip initial whitespace */
340 while ((nch!=_EOF_) && _ISSPACE_(nch))
341 nch = _GETC_(file);
342 /* read until whitespace */
343 while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
344 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
345 st++;
346 nch = _GETC_(file);
347 if (width>0) width--;
349 /* terminate */
350 if (!suppress) *sptr = 0;
352 break;
353 widecharstring: { /* read a word into a wchar_t* */
354 MSVCRT_wchar_t *sptr = suppress ? NULL : va_arg(ap, MSVCRT_wchar_t*);
355 /* skip initial whitespace */
356 while ((nch!=_EOF_) && _ISSPACE_(nch))
357 nch = _GETC_(file);
358 /* read until whitespace */
359 while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
360 if (!suppress) *sptr++ = _WIDE2SUPPORTED_(nch);
361 st++;
362 nch = _GETC_(file);
363 if (width>0) width--;
365 /* terminate */
366 if (!suppress) *sptr = 0;
368 break;
369 /* 'c' and 'C work analogously to 's' and 'S' as described
370 * above */
371 case 'c':
372 if (w_prefix || l_prefix) goto widecharacter;
373 else if (h_prefix) goto character;
374 #ifdef WIDE_SCANF
375 else goto widecharacter;
376 #else /* WIDE_SCANF */
377 else goto character;
378 #endif /* WIDE_SCANF */
379 case 'C':
380 if (w_prefix || l_prefix) goto widecharacter;
381 else if (h_prefix) goto character;
382 #ifdef WIDE_SCANF
383 else goto character;
384 #else /* WIDE_SCANF */
385 else goto widecharacter;
386 #endif /* WIDE_SCANF */
387 character: { /* read single character into char */
388 char *str = suppress ? NULL : va_arg(ap, char*);
389 if (width == -1) width = 1;
390 while ((width != 0) && (nch != _EOF_))
392 if (!suppress) *str++ = _CHAR2SUPPORTED_(nch);
393 st++;
394 width--;
395 nch = _GETC_(file);
398 break;
399 widecharacter: { /* read single character into a wchar_t */
400 MSVCRT_wchar_t *str = suppress ? NULL : va_arg(ap, MSVCRT_wchar_t*);
401 if (width == -1) width = 1;
402 while ((width != 0) && (nch != _EOF_))
404 if (!suppress) *str++ = _WIDE2SUPPORTED_(nch);
405 st++;
406 width--;
407 nch = _GETC_(file);
410 break;
411 case 'n': {
412 if (!suppress) {
413 int*n = va_arg(ap, int*);
414 *n = consumed - 1;
416 /* This is an odd one: according to the standard,
417 * "Execution of a %n directive does not increment the
418 * assignment count returned at the completion of
419 * execution" even if it wasn't suppressed with the
420 * '*' flag. The Corrigendum to the standard seems
421 * to contradict this (comment out the assignment to
422 * suppress below if you want to implement these
423 * alternate semantics) but the windows program I'm
424 * looking at expects the behavior I've coded here
425 * (which happens to be what glibc does as well).
427 suppress = 1;
428 st = 1;
430 break;
431 case '[': {
432 _CHAR_ *str = suppress ? NULL : va_arg(ap, _CHAR_*);
433 _CHAR_ *sptr = str;
434 RTL_BITMAP bitMask;
435 ULONG *Mask;
436 int invert = 0; /* Set if we are NOT to find the chars */
438 /* Init our bitmap */
439 Mask = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, _BITMAPSIZE_/8);
440 RtlInitializeBitMap(&bitMask, Mask, _BITMAPSIZE_);
442 /* Read the format */
443 format++;
444 if(*format == '^') {
445 invert = 1;
446 format++;
448 if(*format == ']') {
449 RtlSetBits(&bitMask, ']', 1);
450 format++;
452 while(*format && (*format != ']')) {
453 /* According to msdn:
454 * "Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]." */
455 if((*format == '-') && (*(format + 1) != ']')) {
456 if ((*(format - 1)) < *(format + 1))
457 RtlSetBits(&bitMask, *(format - 1) +1 , *(format + 1) - *(format - 1));
458 else
459 RtlSetBits(&bitMask, *(format + 1) , *(format - 1) - *(format + 1));
460 format++;
461 } else
462 RtlSetBits(&bitMask, *format, 1);
463 format++;
465 /* read until char is not suitable */
466 while ((width != 0) && (nch != _EOF_)) {
467 if(!invert) {
468 if(RtlAreBitsSet(&bitMask, nch, 1)) {
469 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
470 } else
471 break;
472 } else {
473 if(RtlAreBitsClear(&bitMask, nch, 1)) {
474 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
475 } else
476 break;
478 st++;
479 nch = _GETC_(file);
480 if (width>0) width--;
482 /* terminate */
483 if (!suppress) *sptr = 0;
484 HeapFree(GetProcessHeap(), 0, Mask);
486 break;
487 default:
488 /* From spec: "if a percent sign is followed by a character
489 * that has no meaning as a format-control character, that
490 * character and the following characters are treated as
491 * an ordinary sequence of characters, that is, a sequence
492 * of characters that must match the input. For example,
493 * to specify that a percent-sign character is to be input,
494 * use %%." */
495 while ((nch!=_EOF_) && _ISSPACE_(nch))
496 nch = _GETC_(file);
497 if (nch==*format) {
498 suppress = 1; /* whoops no field to be read */
499 st = 1; /* but we got what we expected */
500 nch = _GETC_(file);
502 break;
504 if (st && !suppress) rd++;
505 else if (!st) break;
507 /* a non-white-space character causes scanf to read, but not store,
508 * a matching non-white-space character. */
509 else {
510 /* check for character match */
511 if (nch == *format) {
512 nch = _GETC_(file);
513 } else break;
515 format++;
517 if (nch!=_EOF_) {
518 _UNGETC_(nch, file);
520 TRACE("returning %d\n", rd);
521 return rd;
524 #undef _CHAR_
525 #undef _EOF_
526 #undef _EOF_RET
527 #undef _ISSPACE_
528 #undef _ISDIGIT_
529 #undef _CHAR2SUPPORTED_
530 #undef _WIDE2SUPPORTED_
531 #undef _CHAR2DIGIT_
532 #undef _GETC_
533 #undef _UNGETC_
534 #undef _FUNCTION_
535 #undef _BITMAPSIZE_