msvcrt: Use WEOF from public header.
[wine.git] / dlls / msvcrt / scanf.h
blob06ad0000e5519eddf46788f388392d8a6a40b910
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_ wchar_t
28 #define _EOF_ WEOF
29 #define _EOF_RET (short)WEOF
30 #define _ISSPACE_(c) MSVCRT_iswspace(c)
31 #define _WIDE2SUPPORTED_(c) c /* No conversion needed (wide to wide) */
32 #define _CHAR2SUPPORTED_(c) c /* FIXME: convert char to wide char */
33 #define _CHAR2DIGIT_(c, base) wchar2digit((c), (base))
34 #define _BITMAPSIZE_ 256*256
35 #else /* WIDE_SCANF */
36 #define _CHAR_ char
37 #define _EOF_ MSVCRT_EOF
38 #define _EOF_RET MSVCRT_EOF
39 #define _ISSPACE_(c) MSVCRT_isspace(c)
40 #define _WIDE2SUPPORTED_(c) c /* FIXME: convert wide char to char */
41 #define _CHAR2SUPPORTED_(c) c /* No conversion needed (char to char) */
42 #define _CHAR2DIGIT_(c, base) char2digit((c), (base))
43 #define _BITMAPSIZE_ 256
44 #endif /* WIDE_SCANF */
46 #ifdef CONSOLE
47 #define _GETC_FUNC_(file) _getch()
48 #define _STRTOD_NAME_(func) console_ ## func
49 #define _GETC_(file) (consumed++, _getch())
50 #define _UNGETC_(nch, file) do { _ungetch(nch); consumed--; } while(0)
51 #define _LOCK_FILE_(file) MSVCRT__lock_file(MSVCRT_stdin)
52 #define _UNLOCK_FILE_(file) MSVCRT__unlock_file(MSVCRT_stdin)
53 #ifdef WIDE_SCANF
54 #ifdef SECURE
55 #define _FUNCTION_ static int MSVCRT_vcwscanf_s_l(const wchar_t *format, _locale_t locale, __ms_va_list ap)
56 #else /* SECURE */
57 #define _FUNCTION_ static int MSVCRT_vcwscanf_l(const wchar_t *format, _locale_t locale, __ms_va_list ap)
58 #endif /* SECURE */
59 #else /* WIDE_SCANF */
60 #ifdef SECURE
61 #define _FUNCTION_ static int MSVCRT_vcscanf_s_l(const char *format, _locale_t locale, __ms_va_list ap)
62 #else /* SECURE */
63 #define _FUNCTION_ static int MSVCRT_vcscanf_l(const char *format, _locale_t locale, __ms_va_list ap)
64 #endif /* SECURE */
65 #endif /* WIDE_SCANF */
66 #else
67 #ifdef STRING
68 #undef _EOF_
69 #define _EOF_ 0
70 #define _GETC_FUNC_(file) (*file++)
71 #ifdef WIDE_SCANF
72 #define _STRTOD_NAME_(func) wstr_ ## func
73 #else
74 #define _STRTOD_NAME_(func) str_ ## func
75 #endif
76 #ifdef STRING_LEN
77 #ifdef WIDE_SCANF
78 #define _GETC_(file) (consumed++, consumed>length ? '\0' : *file++)
79 #else /* WIDE_SCANF */
80 #define _GETC_(file) (consumed++, consumed>length ? '\0' : (unsigned char)*file++)
81 #endif /* WIDE_SCANF */
82 #define _UNGETC_(nch, file) do { file--; consumed--; } while(0)
83 #define _LOCK_FILE_(file) do {} while(0)
84 #define _UNLOCK_FILE_(file) do {} while(0)
85 #ifdef WIDE_SCANF
86 #ifdef SECURE
87 #define _FUNCTION_ static int MSVCRT_vsnwscanf_s_l(const wchar_t *file, size_t length, const wchar_t *format, _locale_t locale, __ms_va_list ap)
88 #else /* SECURE */
89 #define _FUNCTION_ static int MSVCRT_vsnwscanf_l(const wchar_t *file, size_t length, const wchar_t *format, _locale_t locale, __ms_va_list ap)
90 #endif /* SECURE */
91 #else /* WIDE_SCANF */
92 #ifdef SECURE
93 #define _FUNCTION_ static int MSVCRT_vsnscanf_s_l(const char *file, size_t length, const char *format, _locale_t locale, __ms_va_list ap)
94 #else /* SECURE */
95 #define _FUNCTION_ static int MSVCRT_vsnscanf_l(const char *file, size_t length, const char *format, _locale_t locale, __ms_va_list ap)
96 #endif /* SECURE */
97 #endif /* WIDE_SCANF */
98 #else /* STRING_LEN */
99 #ifdef WIDE_SCANF
100 #define _GETC_(file) (consumed++, *file++)
101 #else /* WIDE_SCANF */
102 #define _GETC_(file) (consumed++, (unsigned char)*file++)
103 #endif /* WIDE_SCANF */
104 #define _UNGETC_(nch, file) do { file--; consumed--; } while(0)
105 #define _LOCK_FILE_(file) do {} while(0)
106 #define _UNLOCK_FILE_(file) do {} while(0)
107 #ifdef WIDE_SCANF
108 #ifdef SECURE
109 #define _FUNCTION_ static int MSVCRT_vswscanf_s_l(const wchar_t *file, const wchar_t *format, _locale_t locale, __ms_va_list ap)
110 #else /* SECURE */
111 #define _FUNCTION_ static int MSVCRT_vswscanf_l(const wchar_t *file, const wchar_t *format, _locale_t locale, __ms_va_list ap)
112 #endif /* SECURE */
113 #else /* WIDE_SCANF */
114 #ifdef SECURE
115 #define _FUNCTION_ static int MSVCRT_vsscanf_s_l(const char *file, const char *format, _locale_t locale, __ms_va_list ap)
116 #else /* SECURE */
117 #define _FUNCTION_ static int MSVCRT_vsscanf_l(const char *file, const char *format, _locale_t locale, __ms_va_list ap)
118 #endif /* SECURE */
119 #endif /* WIDE_SCANF */
120 #endif /* STRING_LEN */
121 #else /* STRING */
122 #ifdef WIDE_SCANF
123 #define _GETC_FUNC_(file) MSVCRT_fgetwc(file)
124 #define _STRTOD_NAME_(func) filew_ ## func
125 #define _GETC_(file) (consumed++, MSVCRT_fgetwc(file))
126 #define _UNGETC_(nch, file) do { MSVCRT_ungetwc(nch, file); consumed--; } while(0)
127 #define _LOCK_FILE_(file) MSVCRT__lock_file(file)
128 #define _UNLOCK_FILE_(file) MSVCRT__unlock_file(file)
129 #ifdef SECURE
130 #define _FUNCTION_ static int MSVCRT_vfwscanf_s_l(MSVCRT_FILE* file, const wchar_t *format, _locale_t locale, __ms_va_list ap)
131 #else /* SECURE */
132 #define _FUNCTION_ static int MSVCRT_vfwscanf_l(MSVCRT_FILE* file, const wchar_t *format, _locale_t locale, __ms_va_list ap)
133 #endif /* SECURE */
134 #else /* WIDE_SCANF */
135 #define _GETC_FUNC_(file) MSVCRT_fgetc(file)
136 #define _STRTOD_NAME_(func) file_ ## func
137 #define _GETC_(file) (consumed++, MSVCRT_fgetc(file))
138 #define _UNGETC_(nch, file) do { MSVCRT_ungetc(nch, file); consumed--; } while(0)
139 #define _LOCK_FILE_(file) MSVCRT__lock_file(file)
140 #define _UNLOCK_FILE_(file) MSVCRT__unlock_file(file)
141 #ifdef SECURE
142 #define _FUNCTION_ static int MSVCRT_vfscanf_s_l(MSVCRT_FILE* file, const char *format, _locale_t locale, __ms_va_list ap)
143 #else /* SECURE */
144 #define _FUNCTION_ static int MSVCRT_vfscanf_l(MSVCRT_FILE* file, const char *format, _locale_t locale, __ms_va_list ap)
145 #endif /* SECURE */
146 #endif /* WIDE_SCANF */
147 #endif /* STRING */
148 #endif /* CONSOLE */
150 #if (!defined(SECURE) && !defined(STRING_LEN) && (!defined(CONSOLE) || !defined(WIDE_SCANF)))
151 struct _STRTOD_NAME_(strtod_scanf_ctx) {
152 pthreadlocinfo locinfo;
153 #ifdef STRING
154 const _CHAR_ *file;
155 #else
156 MSVCRT_FILE *file;
157 #endif
158 int length;
159 int read;
160 int cur;
161 int unget;
162 BOOL err;
165 static wchar_t _STRTOD_NAME_(strtod_scanf_get)(void *ctx)
167 struct _STRTOD_NAME_(strtod_scanf_ctx) *context = ctx;
169 context->cur = _EOF_;
170 if (!context->length) return WEOF;
171 if (context->unget != _EOF_) {
172 context->cur = context->unget;
173 context->unget = _EOF_;
174 } else {
175 context->cur = _GETC_FUNC_(context->file);
176 if (context->cur == _EOF_) return WEOF;
179 if (context->length > 0) context->length--;
180 context->read++;
181 return context->cur;
184 static void _STRTOD_NAME_(strtod_scanf_unget)(void *ctx)
186 struct _STRTOD_NAME_(strtod_scanf_ctx) *context = ctx;
188 if (context->length >= 0) context->length++;
189 context->read--;
190 if (context->unget != _EOF_ || context->cur == _EOF_) {
191 context->err = TRUE;
192 return;
194 context->unget = context->cur;
196 #endif
198 _FUNCTION_ {
199 pthreadlocinfo locinfo;
200 int rd = 0, consumed = 0;
201 int nch;
202 if (!*format) return 0;
203 #ifndef WIDE_SCANF
204 #ifdef CONSOLE
205 TRACE("(%s):\n", debugstr_a(format));
206 #else /* CONSOLE */
207 #ifdef STRING
208 TRACE("%s (%s)\n", debugstr_a(file), debugstr_a(format));
209 #else /* STRING */
210 TRACE("%p (%s)\n", file, debugstr_a(format));
211 #endif /* STRING */
212 #endif /* CONSOLE */
213 #endif /* WIDE_SCANF */
214 _LOCK_FILE_(file);
216 nch = _GETC_(file);
217 if (nch == _EOF_) {
218 _UNLOCK_FILE_(file);
219 return _EOF_RET;
222 if(!locale)
223 locinfo = get_locinfo();
224 else
225 locinfo = locale->locinfo;
227 while (*format) {
228 /* a whitespace character in the format string causes scanf to read,
229 * but not store, all consecutive white-space characters in the input
230 * up to the next non-white-space character. One white space character
231 * in the input matches any number (including zero) and combination of
232 * white-space characters in the input. */
233 if (_ISSPACE_(*format)) {
234 /* skip whitespace */
235 while ((nch!=_EOF_) && _ISSPACE_(nch))
236 nch = _GETC_(file);
238 /* a format specification causes scanf to read and convert characters
239 * in the input into values of a specified type. The value is assigned
240 * to an argument in the argument list. Format specifications have
241 * the form %[*][width][{h | l | I64 | L}]type */
242 else if (*format == '%') {
243 int st = 0; int suppress = 0; int width = 0;
244 int base;
245 int h_prefix = 0;
246 int l_prefix = 0;
247 int L_prefix = 0;
248 int w_prefix = 0;
249 int prefix_finished = 0;
250 int I64_prefix = 0;
251 format++;
252 /* look for leading asterisk, which means 'suppress assignment of
253 * this field'. */
254 if (*format=='*') {
255 format++;
256 suppress=1;
258 /* read prefix (if any) */
259 while (!prefix_finished) {
260 /* look for width specification */
261 while (*format >= '0' && *format <= '9') {
262 width *= 10;
263 width += *format++ - '0';
266 switch(*format) {
267 case 'h': h_prefix++; break;
268 case 'l':
269 if(*(format+1) == 'l') {
270 I64_prefix = 1;
271 format++;
273 l_prefix = 1;
274 break;
275 case 'w': w_prefix = 1; break;
276 case 'L': L_prefix = 1; break;
277 case 'I':
278 if (*(format + 1) == '6' &&
279 *(format + 2) == '4') {
280 I64_prefix = 1;
281 format += 2;
283 break;
284 default:
285 prefix_finished = 1;
287 if (!prefix_finished) format++;
289 if (width==0) width=-1; /* no width spec seen */
290 /* read type */
291 switch(*format) {
292 case 'p':
293 case 'P': /* pointer. */
294 if (sizeof(void *) == sizeof(LONGLONG)) I64_prefix = 1;
295 /* fall through */
296 case 'x':
297 case 'X': /* hexadecimal integer. */
298 base = 16;
299 goto number;
300 case 'o': /* octal integer */
301 base = 8;
302 goto number;
303 case 'u': /* unsigned decimal integer */
304 base = 10;
305 goto number;
306 case 'd': /* signed decimal integer */
307 base = 10;
308 goto number;
309 case 'i': /* generic integer */
310 base = 0;
311 number: {
312 /* read an integer */
313 ULONGLONG cur = 0;
314 int negative = 0;
315 int seendigit=0;
316 /* skip initial whitespace */
317 while ((nch!=_EOF_) && _ISSPACE_(nch))
318 nch = _GETC_(file);
319 /* get sign */
320 if (nch == '-' || nch == '+') {
321 negative = (nch=='-');
322 nch = _GETC_(file);
323 if (width>0) width--;
325 /* look for leading indication of base */
326 if (width!=0 && nch == '0' && *format != 'p' && *format != 'P') {
327 nch = _GETC_(file);
328 if (width>0) width--;
329 seendigit=1;
330 if (width!=0 && (nch=='x' || nch=='X')) {
331 if (base==0)
332 base=16;
333 if (base==16) {
334 nch = _GETC_(file);
335 if (width>0) width--;
336 seendigit=0;
338 } else if (base==0)
339 base = 8;
341 /* format %i without indication of base */
342 if (base==0)
343 base = 10;
344 /* throw away leading zeros */
345 while (width!=0 && nch=='0') {
346 nch = _GETC_(file);
347 if (width>0) width--;
348 seendigit=1;
350 if (width!=0 && _CHAR2DIGIT_(nch, base)!=-1) {
351 cur = _CHAR2DIGIT_(nch, base);
352 nch = _GETC_(file);
353 if (width>0) width--;
354 seendigit=1;
356 /* read until no more digits */
357 while (width!=0 && (nch!=_EOF_) && _CHAR2DIGIT_(nch, base)!=-1) {
358 cur = cur*base + _CHAR2DIGIT_(nch, base);
359 nch = _GETC_(file);
360 if (width>0) width--;
361 seendigit=1;
363 /* okay, done! */
364 if (!seendigit) break; /* not a valid number */
365 st = 1;
366 if (!suppress) {
367 #define _SET_NUMBER_(type) *va_arg(ap, type*) = negative ? -cur : cur
368 if (I64_prefix) _SET_NUMBER_(LONGLONG);
369 else if (l_prefix) _SET_NUMBER_(LONG);
370 else if (h_prefix == 1) _SET_NUMBER_(short int);
371 #if _MSVCR_VER >= 140
372 else if (h_prefix == 2) _SET_NUMBER_(char);
373 #endif
374 else _SET_NUMBER_(int);
377 break;
378 case 'e':
379 case 'E':
380 case 'f':
381 case 'g':
382 case 'G': { /* read a float */
383 #ifdef CONSOLE
384 struct _STRTOD_NAME_(strtod_scanf_ctx) ctx = {locinfo, 0, width};
385 #else
386 struct _STRTOD_NAME_(strtod_scanf_ctx) ctx = {locinfo, file, width};
387 #endif
388 int negative = 0;
389 struct fpnum fp;
390 double cur;
392 /* skip initial whitespace */
393 while ((nch!=_EOF_) && _ISSPACE_(nch))
394 nch = _GETC_(file);
395 if (nch == _EOF_)
396 break;
397 ctx.unget = nch;
398 #ifdef STRING
399 ctx.file = file;
400 #endif
401 #ifdef STRING_LEN
402 if(ctx.length > length-consumed+1) ctx.length = length-consumed+1;
403 #endif
405 fp = fpnum_parse(_STRTOD_NAME_(strtod_scanf_get),
406 _STRTOD_NAME_(strtod_scanf_unget), &ctx, locinfo, FALSE);
407 fpnum_double(&fp, &cur);
408 if(!rd && ctx.err) {
409 _UNLOCK_FILE_(file);
410 return _EOF_RET;
412 if(ctx.err || !ctx.read)
413 break;
414 consumed += ctx.read;
415 #ifdef STRING
416 file = ctx.file;
417 #endif
418 nch = ctx.cur;
420 st = 1;
421 if (!suppress) {
422 if (L_prefix || l_prefix) _SET_NUMBER_(double);
423 else _SET_NUMBER_(float);
426 break;
427 /* According to msdn,
428 * 's' reads a character string in a call to fscanf
429 * and 'S' a wide character string and vice versa in a
430 * call to fwscanf. The 'h', 'w' and 'l' prefixes override
431 * this behaviour. 'h' forces reading char * but 'l' and 'w'
432 * force reading WCHAR. */
433 case 's':
434 if (w_prefix || l_prefix) goto widecharstring;
435 else if (h_prefix) goto charstring;
436 #ifdef WIDE_SCANF
437 else goto widecharstring;
438 #else /* WIDE_SCANF */
439 else goto charstring;
440 #endif /* WIDE_SCANF */
441 case 'S':
442 if (w_prefix || l_prefix) goto widecharstring;
443 else if (h_prefix) goto charstring;
444 #ifdef WIDE_SCANF
445 else goto charstring;
446 #else /* WIDE_SCANF */
447 else goto widecharstring;
448 #endif /* WIDE_SCANF */
449 charstring: { /* read a word into a char */
450 char *sptr = suppress ? NULL : va_arg(ap, char*);
451 char *sptr_beg = sptr;
452 #ifdef SECURE
453 unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned);
454 #else
455 unsigned size = UINT_MAX;
456 #endif
457 /* skip initial whitespace */
458 while ((nch!=_EOF_) && _ISSPACE_(nch))
459 nch = _GETC_(file);
460 /* read until whitespace */
461 while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
462 if (!suppress) {
463 *sptr++ = _CHAR2SUPPORTED_(nch);
464 if(size>1) size--;
465 else {
466 _UNLOCK_FILE_(file);
467 *sptr_beg = 0;
468 return rd;
471 st++;
472 nch = _GETC_(file);
473 if (width>0) width--;
475 /* if we have reached the EOF and output nothing then report EOF */
476 if (nch==_EOF_ && rd==0 && st==0) {
477 _UNLOCK_FILE_(file);
478 return _EOF_RET;
480 /* terminate */
481 if (st && !suppress) *sptr = 0;
483 break;
484 widecharstring: { /* read a word into a wchar_t* */
485 wchar_t *sptr = suppress ? NULL : va_arg(ap, wchar_t*);
486 wchar_t *sptr_beg = sptr;
487 #ifdef SECURE
488 unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned);
489 #else
490 unsigned size = UINT_MAX;
491 #endif
492 /* skip initial whitespace */
493 while ((nch!=_EOF_) && _ISSPACE_(nch))
494 nch = _GETC_(file);
495 /* read until whitespace */
496 while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
497 if (!suppress) {
498 *sptr++ = _WIDE2SUPPORTED_(nch);
499 if(size>1) size--;
500 else {
501 _UNLOCK_FILE_(file);
502 *sptr_beg = 0;
503 return rd;
506 st++;
507 nch = _GETC_(file);
508 if (width>0) width--;
510 #if _MSVCR_VER >= 80
511 /* if we have reached the EOF and output nothing then report EOF */
512 if (nch==_EOF_ && rd==0 && st==0) {
513 _UNLOCK_FILE_(file);
514 return _EOF_RET;
516 #endif
517 /* terminate */
518 if (st && !suppress) *sptr = 0;
520 break;
521 /* 'c' and 'C work analogously to 's' and 'S' as described
522 * above */
523 case 'c':
524 if (w_prefix || l_prefix) goto widecharacter;
525 else if (h_prefix) goto character;
526 #ifdef WIDE_SCANF
527 else goto widecharacter;
528 #else /* WIDE_SCANF */
529 else goto character;
530 #endif /* WIDE_SCANF */
531 case 'C':
532 if (w_prefix || l_prefix) goto widecharacter;
533 else if (h_prefix) goto character;
534 #ifdef WIDE_SCANF
535 else goto character;
536 #else /* WIDE_SCANF */
537 else goto widecharacter;
538 #endif /* WIDE_SCANF */
539 character: { /* read single character into char */
540 char *str = suppress ? NULL : va_arg(ap, char*);
541 char *pstr = str;
542 #ifdef SECURE
543 unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned);
544 #else
545 unsigned size = UINT_MAX;
546 #endif
547 if (width == -1) width = 1;
548 while (width && (nch != _EOF_))
550 if (!suppress) {
551 if(size) size--;
552 else {
553 _UNLOCK_FILE_(file);
554 *pstr = 0;
555 return rd;
557 *str++ = _CHAR2SUPPORTED_(nch);
559 st++;
560 width--;
561 nch = _GETC_(file);
564 break;
565 widecharacter: { /* read single character into a wchar_t */
566 wchar_t *str = suppress ? NULL : va_arg(ap, wchar_t*);
567 wchar_t *pstr = str;
568 #ifdef SECURE
569 unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned);
570 #else
571 unsigned size = UINT_MAX;
572 #endif
573 if (width == -1) width = 1;
574 while (width && (nch != _EOF_))
576 if (!suppress) {
577 if(size) size--;
578 else {
579 _UNLOCK_FILE_(file);
580 *pstr = 0;
581 return rd;
583 *str++ = _WIDE2SUPPORTED_(nch);
585 st++;
586 width--;
587 nch = _GETC_(file);
590 break;
591 case 'n': {
592 if (!suppress) {
593 int*n = va_arg(ap, int*);
594 *n = consumed - 1;
596 /* This is an odd one: according to the standard,
597 * "Execution of a %n directive does not increment the
598 * assignment count returned at the completion of
599 * execution" even if it wasn't suppressed with the
600 * '*' flag. The Corrigendum to the standard seems
601 * to contradict this (comment out the assignment to
602 * suppress below if you want to implement these
603 * alternate semantics) but the windows program I'm
604 * looking at expects the behavior I've coded here
605 * (which happens to be what glibc does as well).
607 suppress = 1;
608 st = 1;
610 break;
611 case '[': {
612 _CHAR_ *str = suppress ? NULL : va_arg(ap, _CHAR_*);
613 _CHAR_ *sptr = str;
614 RTL_BITMAP bitMask;
615 ULONG *Mask;
616 int invert = 0; /* Set if we are NOT to find the chars */
617 #ifdef SECURE
618 unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned);
619 #else
620 unsigned size = UINT_MAX;
621 #endif
623 /* Init our bitmap */
624 Mask = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, _BITMAPSIZE_/8);
625 RtlInitializeBitMap(&bitMask, Mask, _BITMAPSIZE_);
627 /* Read the format */
628 format++;
629 if(*format == '^') {
630 invert = 1;
631 format++;
633 if(*format == ']') {
634 RtlSetBits(&bitMask, ']', 1);
635 format++;
637 while(*format && (*format != ']')) {
638 /* According to msdn:
639 * "Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]." */
640 if(format[1] == '-' && format[2] && format[2] != ']') {
641 if (format[0] < format[2])
642 RtlSetBits(&bitMask, format[0], format[2] - format[0] + 1);
643 else
644 RtlSetBits(&bitMask, format[2], format[0] - format[2] + 1);
645 format += 2;
646 } else
647 RtlSetBits(&bitMask, *format, 1);
648 format++;
650 /* read until char is not suitable */
651 while ((width != 0) && (nch != _EOF_)) {
652 if(!invert) {
653 if(RtlAreBitsSet(&bitMask, nch, 1)) {
654 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
655 } else
656 break;
657 } else {
658 if(RtlAreBitsClear(&bitMask, nch, 1)) {
659 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
660 } else
661 break;
663 st++;
664 nch = _GETC_(file);
665 if (width>0) width--;
666 if(size>1) size--;
667 else {
668 _UNLOCK_FILE_(file);
669 *str = 0;
670 HeapFree(GetProcessHeap(), 0, Mask);
671 return rd;
674 /* terminate */
675 if (!suppress) *sptr = 0;
676 HeapFree(GetProcessHeap(), 0, Mask);
678 break;
679 default:
680 /* From spec: "if a percent sign is followed by a character
681 * that has no meaning as a format-control character, that
682 * character and the following characters are treated as
683 * an ordinary sequence of characters, that is, a sequence
684 * of characters that must match the input. For example,
685 * to specify that a percent-sign character is to be input,
686 * use %%." */
687 while ((nch!=_EOF_) && _ISSPACE_(nch))
688 nch = _GETC_(file);
689 if ((_CHAR_)nch == *format) {
690 suppress = 1; /* whoops no field to be read */
691 st = 1; /* but we got what we expected */
692 nch = _GETC_(file);
694 break;
696 if (st && !suppress) rd++;
697 else if (!st) break;
699 /* a non-white-space character causes scanf to read, but not store,
700 * a matching non-white-space character. */
701 else {
702 /* check for character match */
703 if ((_CHAR_)nch == *format) {
704 nch = _GETC_(file);
705 } else break;
707 format++;
709 if (nch!=_EOF_) {
710 _UNGETC_(nch, file);
713 TRACE("returning %d\n", rd);
714 _UNLOCK_FILE_(file);
715 return rd;
718 #undef _CHAR_
719 #undef _EOF_
720 #undef _EOF_RET
721 #undef _ISSPACE_
722 #undef _CHAR2SUPPORTED_
723 #undef _WIDE2SUPPORTED_
724 #undef _CHAR2DIGIT_
725 #undef _GETC_FUNC_
726 #undef _STRTOD_NAME_
727 #undef _GETC_
728 #undef _UNGETC_
729 #undef _LOCK_FILE_
730 #undef _UNLOCK_FILE_
731 #undef _FUNCTION_
732 #undef _BITMAPSIZE_