quartz/tests: Skip some tests on NT4.
[wine.git] / dlls / msvcrt / scanf.h
blobc236f48c4fdf93a2aee2d6ff37a77bdf24b5fd29
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, 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, 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 */
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, 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 */
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 case 'x':
155 case 'X': /* hexadecimal integer. */
156 base = 16;
157 goto number;
158 case 'o': /* octal integer */
159 base = 8;
160 goto number;
161 case 'u': /* unsigned decimal integer */
162 base = 10;
163 goto number;
164 case 'd': /* signed decimal integer */
165 base = 10;
166 goto number;
167 case 'i': /* generic integer */
168 base = 0;
169 number: {
170 /* read an integer */
171 ULONGLONG cur = 0;
172 int negative = 0;
173 int seendigit=0;
174 /* skip initial whitespace */
175 while ((nch!=_EOF_) && _ISSPACE_(nch))
176 nch = _GETC_(file);
177 /* get sign */
178 if (nch == '-' || nch == '+') {
179 negative = (nch=='-');
180 nch = _GETC_(file);
181 if (width>0) width--;
183 /* look for leading indication of base */
184 if (width!=0 && nch == '0' && *format != 'p' && *format != 'P') {
185 nch = _GETC_(file);
186 if (width>0) width--;
187 seendigit=1;
188 if (width!=0 && (nch=='x' || nch=='X')) {
189 if (base==0)
190 base=16;
191 if (base==16) {
192 nch = _GETC_(file);
193 if (width>0) width--;
194 seendigit=0;
196 } else if (base==0)
197 base = 8;
199 /* format %i without indication of base */
200 if (base==0)
201 base = 10;
202 /* throw away leading zeros */
203 while (width!=0 && nch=='0') {
204 nch = _GETC_(file);
205 if (width>0) width--;
206 seendigit=1;
208 if (width!=0 && _CHAR2DIGIT_(nch, base)!=-1) {
209 cur = _CHAR2DIGIT_(nch, base);
210 nch = _GETC_(file);
211 if (width>0) width--;
212 seendigit=1;
214 /* read until no more digits */
215 while (width!=0 && (nch!=_EOF_) && _CHAR2DIGIT_(nch, base)!=-1) {
216 cur = cur*base + _CHAR2DIGIT_(nch, base);
217 nch = _GETC_(file);
218 if (width>0) width--;
219 seendigit=1;
221 /* okay, done! */
222 if (!seendigit) break; /* not a valid number */
223 st = 1;
224 if (!suppress) {
225 #define _SET_NUMBER_(type) *va_arg(ap, type*) = negative ? -cur : cur
226 if (I64_prefix) _SET_NUMBER_(LONGLONG);
227 else if (l_prefix) _SET_NUMBER_(long int);
228 else if (h_prefix) _SET_NUMBER_(short int);
229 else _SET_NUMBER_(int);
232 break;
233 case 'e':
234 case 'E':
235 case 'f':
236 case 'g':
237 case 'G': { /* read a float */
238 long double cur = 0;
239 int negative = 0;
240 /* skip initial whitespace */
241 while ((nch!=_EOF_) && _ISSPACE_(nch))
242 nch = _GETC_(file);
243 /* get sign. */
244 if (nch == '-' || nch == '+') {
245 negative = (nch=='-');
246 if (width>0) width--;
247 if (width==0) break;
248 nch = _GETC_(file);
250 /* get first digit. */
251 if ('.' != nch) {
252 if (!_ISDIGIT_(nch)) break;
253 cur = (nch - '0');
254 nch = _GETC_(file);
255 if (width>0) width--;
256 /* read until no more digits */
257 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
258 cur = cur*10 + (nch - '0');
259 nch = _GETC_(file);
260 if (width>0) width--;
262 } else {
263 cur = 0; /* MaxPayneDemo Fix: .8 -> 0.8 */
265 /* handle decimals */
266 if (width!=0 && nch == '.') {
267 float dec = 1;
268 nch = _GETC_(file);
269 if (width>0) width--;
270 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
271 dec /= 10;
272 cur += dec * (nch - '0');
273 nch = _GETC_(file);
274 if (width>0) width--;
277 /* handle exponent */
278 if (width!=0 && (nch == 'e' || nch == 'E')) {
279 int exponent = 0, negexp = 0;
280 float expcnt;
281 nch = _GETC_(file);
282 if (width>0) width--;
283 /* possible sign on the exponent */
284 if (width!=0 && (nch=='+' || nch=='-')) {
285 negexp = (nch=='-');
286 nch = _GETC_(file);
287 if (width>0) width--;
289 /* exponent digits */
290 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
291 exponent *= 10;
292 exponent += (nch - '0');
293 nch = _GETC_(file);
294 if (width>0) width--;
296 /* update 'cur' with this exponent. */
297 expcnt = negexp ? .1 : 10;
298 while (exponent!=0) {
299 if (exponent&1)
300 cur*=expcnt;
301 exponent/=2;
302 expcnt=expcnt*expcnt;
305 st = 1;
306 if (!suppress) {
307 if (L_prefix) _SET_NUMBER_(long double);
308 else if (l_prefix) _SET_NUMBER_(double);
309 else _SET_NUMBER_(float);
312 break;
313 /* According to msdn,
314 * 's' reads a character string in a call to fscanf
315 * and 'S' a wide character string and vice versa in a
316 * call to fwscanf. The 'h', 'w' and 'l' prefixes override
317 * this behaviour. 'h' forces reading char * but 'l' and 'w'
318 * force reading WCHAR. */
319 case 's':
320 if (w_prefix || l_prefix) goto widecharstring;
321 else if (h_prefix) goto charstring;
322 #ifdef WIDE_SCANF
323 else goto widecharstring;
324 #else /* WIDE_SCANF */
325 else goto charstring;
326 #endif /* WIDE_SCANF */
327 case 'S':
328 if (w_prefix || l_prefix) goto widecharstring;
329 else if (h_prefix) goto charstring;
330 #ifdef WIDE_SCANF
331 else goto charstring;
332 #else /* WIDE_SCANF */
333 else goto widecharstring;
334 #endif /* WIDE_SCANF */
335 charstring: { /* read a word into a char */
336 char *sptr = suppress ? NULL : va_arg(ap, char*);
337 /* skip initial whitespace */
338 while ((nch!=_EOF_) && _ISSPACE_(nch))
339 nch = _GETC_(file);
340 /* read until whitespace */
341 while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
342 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
343 st++;
344 nch = _GETC_(file);
345 if (width>0) width--;
347 /* terminate */
348 if (!suppress) *sptr = 0;
350 break;
351 widecharstring: { /* read a word into a wchar_t* */
352 MSVCRT_wchar_t *sptr = suppress ? NULL : va_arg(ap, MSVCRT_wchar_t*);
353 /* skip initial whitespace */
354 while ((nch!=_EOF_) && _ISSPACE_(nch))
355 nch = _GETC_(file);
356 /* read until whitespace */
357 while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
358 if (!suppress) *sptr++ = _WIDE2SUPPORTED_(nch);
359 st++;
360 nch = _GETC_(file);
361 if (width>0) width--;
363 /* terminate */
364 if (!suppress) *sptr = 0;
366 break;
367 /* 'c' and 'C work analogously to 's' and 'S' as described
368 * above */
369 case 'c':
370 if (w_prefix || l_prefix) goto widecharacter;
371 else if (h_prefix) goto character;
372 #ifdef WIDE_SCANF
373 else goto widecharacter;
374 #else /* WIDE_SCANF */
375 else goto character;
376 #endif /* WIDE_SCANF */
377 case 'C':
378 if (w_prefix || l_prefix) goto widecharacter;
379 else if (h_prefix) goto character;
380 #ifdef WIDE_SCANF
381 else goto character;
382 #else /* WIDE_SCANF */
383 else goto widecharacter;
384 #endif /* WIDE_SCANF */
385 character: { /* read single character into char */
386 char *str = suppress ? NULL : va_arg(ap, char*);
387 if (width == -1) width = 1;
388 while ((width != 0) && (nch != _EOF_))
390 if (!suppress) *str++ = _CHAR2SUPPORTED_(nch);
391 st++;
392 width--;
393 nch = _GETC_(file);
396 break;
397 widecharacter: { /* read single character into a wchar_t */
398 MSVCRT_wchar_t *str = suppress ? NULL : va_arg(ap, MSVCRT_wchar_t*);
399 if (width == -1) width = 1;
400 while ((width != 0) && (nch != _EOF_))
402 if (!suppress) *str++ = _WIDE2SUPPORTED_(nch);
403 st++;
404 width--;
405 nch = _GETC_(file);
408 break;
409 case 'n': {
410 if (!suppress) {
411 int*n = va_arg(ap, int*);
412 *n = consumed - 1;
414 /* This is an odd one: according to the standard,
415 * "Execution of a %n directive does not increment the
416 * assignment count returned at the completion of
417 * execution" even if it wasn't suppressed with the
418 * '*' flag. The Corrigendum to the standard seems
419 * to contradict this (comment out the assignment to
420 * suppress below if you want to implement these
421 * alternate semantics) but the windows program I'm
422 * looking at expects the behavior I've coded here
423 * (which happens to be what glibc does as well).
425 suppress = 1;
426 st = 1;
428 break;
429 case '[': {
430 _CHAR_ *str = suppress ? NULL : va_arg(ap, _CHAR_*);
431 _CHAR_ *sptr = str;
432 RTL_BITMAP bitMask;
433 ULONG *Mask;
434 int invert = 0; /* Set if we are NOT to find the chars */
436 /* Init our bitmap */
437 Mask = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, _BITMAPSIZE_/8);
438 RtlInitializeBitMap(&bitMask, Mask, _BITMAPSIZE_);
440 /* Read the format */
441 format++;
442 if(*format == '^') {
443 invert = 1;
444 format++;
446 if(*format == ']') {
447 RtlSetBits(&bitMask, ']', 1);
448 format++;
450 while(*format && (*format != ']')) {
451 /* According to msdn:
452 * "Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]." */
453 if((*format == '-') && (*(format + 1) != ']')) {
454 if ((*(format - 1)) < *(format + 1))
455 RtlSetBits(&bitMask, *(format - 1) +1 , *(format + 1) - *(format - 1));
456 else
457 RtlSetBits(&bitMask, *(format + 1) , *(format - 1) - *(format + 1));
458 format++;
459 } else
460 RtlSetBits(&bitMask, *format, 1);
461 format++;
463 /* read until char is not suitable */
464 while ((width != 0) && (nch != _EOF_)) {
465 if(!invert) {
466 if(RtlAreBitsSet(&bitMask, nch, 1)) {
467 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
468 } else
469 break;
470 } else {
471 if(RtlAreBitsClear(&bitMask, nch, 1)) {
472 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
473 } else
474 break;
476 st++;
477 nch = _GETC_(file);
478 if (width>0) width--;
480 /* terminate */
481 if (!suppress) *sptr = 0;
482 HeapFree(GetProcessHeap(), 0, Mask);
484 break;
485 default:
486 /* From spec: "if a percent sign is followed by a character
487 * that has no meaning as a format-control character, that
488 * character and the following characters are treated as
489 * an ordinary sequence of characters, that is, a sequence
490 * of characters that must match the input. For example,
491 * to specify that a percent-sign character is to be input,
492 * use %%." */
493 while ((nch!=_EOF_) && _ISSPACE_(nch))
494 nch = _GETC_(file);
495 if (nch==*format) {
496 suppress = 1; /* whoops no field to be read */
497 st = 1; /* but we got what we expected */
498 nch = _GETC_(file);
500 break;
502 if (st && !suppress) rd++;
503 else if (!st) break;
505 /* a non-white-space character causes scanf to read, but not store,
506 * a matching non-white-space character. */
507 else {
508 /* check for character match */
509 if (nch == *format) {
510 nch = _GETC_(file);
511 } else break;
513 format++;
515 if (nch!=_EOF_) {
516 _UNGETC_(nch, file);
518 TRACE("returning %d\n", rd);
519 return rd;
522 #undef _CHAR_
523 #undef _EOF_
524 #undef _EOF_RET
525 #undef _ISSPACE_
526 #undef _ISDIGIT_
527 #undef _CHAR2SUPPORTED_
528 #undef _WIDE2SUPPORTED_
529 #undef _CHAR2DIGIT_
530 #undef _GETC_
531 #undef _UNGETC_
532 #undef _FUNCTION_
533 #undef _BITMAPSIZE_