ntdll: Move the plaform-independent thread data to the GdiTebBatch TEB field.
[wine.git] / dlls / msvcrt / scanf.h
blob2920b9b84562118d753ed61988ad3ec0ecfc9a4b
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 (short)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 _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 MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
56 #else /* SECURE */
57 #define _FUNCTION_ static int MSVCRT_vcwscanf_l(const MSVCRT_wchar_t *format, MSVCRT__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, MSVCRT__locale_t locale, __ms_va_list ap)
62 #else /* SECURE */
63 #define _FUNCTION_ static int MSVCRT_vcscanf_l(const char *format, MSVCRT__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 #ifdef STRING_LEN
71 #ifdef WIDE_SCANF
72 #define _GETC_(file) (consumed==length ? '\0' : (consumed++, *file++))
73 #else /* WIDE_SCANF */
74 #define _GETC_(file) (consumed==length ? '\0' : (consumed++, (unsigned char)*file++))
75 #endif /* WIDE_SCANF */
76 #define _UNGETC_(nch, file) do { file--; consumed--; } while(0)
77 #define _LOCK_FILE_(file) do {} while(0)
78 #define _UNLOCK_FILE_(file) do {} while(0)
79 #ifdef WIDE_SCANF
80 #ifdef SECURE
81 #define _FUNCTION_ static int MSVCRT_vsnwscanf_s_l(const MSVCRT_wchar_t *file, MSVCRT_size_t length, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
82 #else /* SECURE */
83 #define _FUNCTION_ static int MSVCRT_vsnwscanf_l(const MSVCRT_wchar_t *file, MSVCRT_size_t length, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
84 #endif /* SECURE */
85 #else /* WIDE_SCANF */
86 #ifdef SECURE
87 #define _FUNCTION_ static int MSVCRT_vsnscanf_s_l(const char *file, MSVCRT_size_t length, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
88 #else /* SECURE */
89 #define _FUNCTION_ static int MSVCRT_vsnscanf_l(const char *file, MSVCRT_size_t length, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
90 #endif /* SECURE */
91 #endif /* WIDE_SCANF */
92 #else /* STRING_LEN */
93 #ifdef WIDE_SCANF
94 #define _GETC_(file) (consumed++, *file++)
95 #else /* WIDE_SCANF */
96 #define _GETC_(file) (consumed++, (unsigned char)*file++)
97 #endif /* WIDE_SCANF */
98 #define _UNGETC_(nch, file) do { file--; consumed--; } while(0)
99 #define _LOCK_FILE_(file) do {} while(0)
100 #define _UNLOCK_FILE_(file) do {} while(0)
101 #ifdef WIDE_SCANF
102 #ifdef SECURE
103 #define _FUNCTION_ static int MSVCRT_vswscanf_s_l(const MSVCRT_wchar_t *file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
104 #else /* SECURE */
105 #define _FUNCTION_ static int MSVCRT_vswscanf_l(const MSVCRT_wchar_t *file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
106 #endif /* SECURE */
107 #else /* WIDE_SCANF */
108 #ifdef SECURE
109 #define _FUNCTION_ static int MSVCRT_vsscanf_s_l(const char *file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
110 #else /* SECURE */
111 #define _FUNCTION_ static int MSVCRT_vsscanf_l(const char *file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
112 #endif /* SECURE */
113 #endif /* WIDE_SCANF */
114 #endif /* STRING_LEN */
115 #else /* STRING */
116 #ifdef WIDE_SCANF
117 #define _GETC_(file) (consumed++, MSVCRT_fgetwc(file))
118 #define _UNGETC_(nch, file) do { MSVCRT_ungetwc(nch, file); consumed--; } while(0)
119 #define _LOCK_FILE_(file) MSVCRT__lock_file(file)
120 #define _UNLOCK_FILE_(file) MSVCRT__unlock_file(file)
121 #ifdef SECURE
122 #define _FUNCTION_ static int MSVCRT_vfwscanf_s_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
123 #else /* SECURE */
124 #define _FUNCTION_ static int MSVCRT_vfwscanf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
125 #endif /* SECURE */
126 #else /* WIDE_SCANF */
127 #define _GETC_(file) (consumed++, MSVCRT_fgetc(file))
128 #define _UNGETC_(nch, file) do { MSVCRT_ungetc(nch, file); consumed--; } while(0)
129 #define _LOCK_FILE_(file) MSVCRT__lock_file(file)
130 #define _UNLOCK_FILE_(file) MSVCRT__unlock_file(file)
131 #ifdef SECURE
132 #define _FUNCTION_ static int MSVCRT_vfscanf_s_l(MSVCRT_FILE* file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
133 #else /* SECURE */
134 #define _FUNCTION_ static int MSVCRT_vfscanf_l(MSVCRT_FILE* file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
135 #endif /* SECURE */
136 #endif /* WIDE_SCANF */
137 #endif /* STRING */
138 #endif /* CONSOLE */
140 _FUNCTION_ {
141 MSVCRT_pthreadlocinfo locinfo;
142 int rd = 0, consumed = 0;
143 int nch;
144 if (!*format) return 0;
145 #ifndef WIDE_SCANF
146 #ifdef CONSOLE
147 TRACE("(%s):\n", debugstr_a(format));
148 #else /* CONSOLE */
149 #ifdef STRING
150 TRACE("%s (%s)\n", debugstr_a(file), debugstr_a(format));
151 #else /* STRING */
152 TRACE("%p (%s)\n", file, debugstr_a(format));
153 #endif /* STRING */
154 #endif /* CONSOLE */
155 #endif /* WIDE_SCANF */
156 _LOCK_FILE_(file);
158 nch = _GETC_(file);
159 if (nch == _EOF_) {
160 _UNLOCK_FILE_(file);
161 return _EOF_RET;
164 if(!locale)
165 locinfo = get_locinfo();
166 else
167 locinfo = locale->locinfo;
169 while (*format) {
170 /* a whitespace character in the format string causes scanf to read,
171 * but not store, all consecutive white-space characters in the input
172 * up to the next non-white-space character. One white space character
173 * in the input matches any number (including zero) and combination of
174 * white-space characters in the input. */
175 if (_ISSPACE_(*format)) {
176 /* skip whitespace */
177 while ((nch!=_EOF_) && _ISSPACE_(nch))
178 nch = _GETC_(file);
180 /* a format specification causes scanf to read and convert characters
181 * in the input into values of a specified type. The value is assigned
182 * to an argument in the argument list. Format specifications have
183 * the form %[*][width][{h | l | I64 | L}]type */
184 else if (*format == '%') {
185 int st = 0; int suppress = 0; int width = 0;
186 int base;
187 int h_prefix = 0;
188 int l_prefix = 0;
189 int L_prefix = 0;
190 int w_prefix = 0;
191 int prefix_finished = 0;
192 int I64_prefix = 0;
193 format++;
194 /* look for leading asterisk, which means 'suppress assignment of
195 * this field'. */
196 if (*format=='*') {
197 format++;
198 suppress=1;
200 /* look for width specification */
201 while (_ISDIGIT_(*format)) {
202 width*=10;
203 width+=*format++ - '0';
205 if (width==0) width=-1; /* no width spec seen */
206 /* read prefix (if any) */
207 while (!prefix_finished) {
208 switch(*format) {
209 case 'h': h_prefix++; break;
210 case 'l':
211 if(*(format+1) == 'l') {
212 I64_prefix = 1;
213 format++;
215 l_prefix = 1;
216 break;
217 case 'w': w_prefix = 1; break;
218 case 'L': L_prefix = 1; break;
219 case 'I':
220 if (*(format + 1) == '6' &&
221 *(format + 2) == '4') {
222 I64_prefix = 1;
223 format += 2;
225 break;
226 default:
227 prefix_finished = 1;
229 if (!prefix_finished) format++;
231 /* read type */
232 switch(*format) {
233 case 'p':
234 case 'P': /* pointer. */
235 if (sizeof(void *) == sizeof(LONGLONG)) I64_prefix = 1;
236 /* fall through */
237 case 'x':
238 case 'X': /* hexadecimal integer. */
239 base = 16;
240 goto number;
241 case 'o': /* octal integer */
242 base = 8;
243 goto number;
244 case 'u': /* unsigned decimal integer */
245 base = 10;
246 goto number;
247 case 'd': /* signed decimal integer */
248 base = 10;
249 goto number;
250 case 'i': /* generic integer */
251 base = 0;
252 number: {
253 /* read an integer */
254 ULONGLONG cur = 0;
255 int negative = 0;
256 int seendigit=0;
257 /* skip initial whitespace */
258 while ((nch!=_EOF_) && _ISSPACE_(nch))
259 nch = _GETC_(file);
260 /* get sign */
261 if (nch == '-' || nch == '+') {
262 negative = (nch=='-');
263 nch = _GETC_(file);
264 if (width>0) width--;
266 /* look for leading indication of base */
267 if (width!=0 && nch == '0' && *format != 'p' && *format != 'P') {
268 nch = _GETC_(file);
269 if (width>0) width--;
270 seendigit=1;
271 if (width!=0 && (nch=='x' || nch=='X')) {
272 if (base==0)
273 base=16;
274 if (base==16) {
275 nch = _GETC_(file);
276 if (width>0) width--;
277 seendigit=0;
279 } else if (base==0)
280 base = 8;
282 /* format %i without indication of base */
283 if (base==0)
284 base = 10;
285 /* throw away leading zeros */
286 while (width!=0 && nch=='0') {
287 nch = _GETC_(file);
288 if (width>0) width--;
289 seendigit=1;
291 if (width!=0 && _CHAR2DIGIT_(nch, base)!=-1) {
292 cur = _CHAR2DIGIT_(nch, base);
293 nch = _GETC_(file);
294 if (width>0) width--;
295 seendigit=1;
297 /* read until no more digits */
298 while (width!=0 && (nch!=_EOF_) && _CHAR2DIGIT_(nch, base)!=-1) {
299 cur = cur*base + _CHAR2DIGIT_(nch, base);
300 nch = _GETC_(file);
301 if (width>0) width--;
302 seendigit=1;
304 /* okay, done! */
305 if (!seendigit) break; /* not a valid number */
306 st = 1;
307 if (!suppress) {
308 #define _SET_NUMBER_(type) *va_arg(ap, type*) = negative ? -cur : cur
309 if (I64_prefix) _SET_NUMBER_(LONGLONG);
310 else if (l_prefix) _SET_NUMBER_(LONG);
311 else if (h_prefix == 1) _SET_NUMBER_(short int);
312 else _SET_NUMBER_(int);
315 break;
316 case 'e':
317 case 'E':
318 case 'f':
319 case 'g':
320 case 'G': { /* read a float */
321 long double cur = 1, expcnt = 10;
322 ULONGLONG d, hlp;
323 int exp = 0, negative = 0;
324 unsigned fpcontrol;
325 BOOL negexp;
327 /* skip initial whitespace */
328 while ((nch!=_EOF_) && _ISSPACE_(nch))
329 nch = _GETC_(file);
331 /* get sign. */
332 if (nch == '-' || nch == '+') {
333 negative = (nch=='-');
334 if (width>0) width--;
335 if (width==0) break;
336 nch = _GETC_(file);
339 /* get first digit. */
340 if (*locinfo->lconv->decimal_point != nch) {
341 if (!_ISDIGIT_(nch)) break;
342 d = nch - '0';
343 nch = _GETC_(file);
344 if (width>0) width--;
345 /* read until no more digits */
346 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
347 hlp = d*10 + nch - '0';
348 nch = _GETC_(file);
349 if (width>0) width--;
350 if(d > (ULONGLONG)-1/10 || hlp<d) {
351 exp++;
352 break;
354 else
355 d = hlp;
357 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
358 exp++;
359 nch = _GETC_(file);
360 if (width>0) width--;
362 } else {
363 d = 0; /* Fix: .8 -> 0.8 */
366 /* handle decimals */
367 if (width!=0 && nch == *locinfo->lconv->decimal_point) {
368 nch = _GETC_(file);
369 if (width>0) width--;
371 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
372 hlp = d*10 + nch - '0';
373 nch = _GETC_(file);
374 if (width>0) width--;
375 if(d > (ULONGLONG)-1/10 || hlp<d)
376 break;
378 d = hlp;
379 exp--;
381 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
382 nch = _GETC_(file);
383 if (width>0) width--;
387 /* handle exponent */
388 if (width!=0 && (nch == 'e' || nch == 'E')) {
389 int sign = 1, e = 0;
391 nch = _GETC_(file);
392 if (width>0) width--;
393 if (width!=0 && (nch=='+' || nch=='-')) {
394 if(nch == '-')
395 sign = -1;
396 nch = _GETC_(file);
397 if (width>0) width--;
400 /* exponent digits */
401 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
402 if(e > INT_MAX/10 || (e = e*10 + nch - '0')<0)
403 e = INT_MAX;
404 nch = _GETC_(file);
405 if (width>0) width--;
407 e *= sign;
409 if(exp<0 && e<0 && e+exp>0) exp = INT_MIN;
410 else if(exp>0 && e>0 && e+exp<0) exp = INT_MAX;
411 else exp += e;
414 fpcontrol = _control87(0, 0);
415 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
416 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
418 negexp = (exp < 0);
419 if(negexp)
420 exp = -exp;
421 /* update 'cur' with this exponent. */
422 while(exp) {
423 if(exp & 1)
424 cur *= expcnt;
425 exp /= 2;
426 expcnt = expcnt*expcnt;
428 cur = (negexp ? d/cur : d*cur);
430 _control87(fpcontrol, 0xffffffff);
432 st = 1;
433 if (!suppress) {
434 if (L_prefix || l_prefix) _SET_NUMBER_(double);
435 else _SET_NUMBER_(float);
438 break;
439 /* According to msdn,
440 * 's' reads a character string in a call to fscanf
441 * and 'S' a wide character string and vice versa in a
442 * call to fwscanf. The 'h', 'w' and 'l' prefixes override
443 * this behaviour. 'h' forces reading char * but 'l' and 'w'
444 * force reading WCHAR. */
445 case 's':
446 if (w_prefix || l_prefix) goto widecharstring;
447 else if (h_prefix) goto charstring;
448 #ifdef WIDE_SCANF
449 else goto widecharstring;
450 #else /* WIDE_SCANF */
451 else goto charstring;
452 #endif /* WIDE_SCANF */
453 case 'S':
454 if (w_prefix || l_prefix) goto widecharstring;
455 else if (h_prefix) goto charstring;
456 #ifdef WIDE_SCANF
457 else goto charstring;
458 #else /* WIDE_SCANF */
459 else goto widecharstring;
460 #endif /* WIDE_SCANF */
461 charstring: { /* read a word into a char */
462 char *sptr = suppress ? NULL : va_arg(ap, char*);
463 char *sptr_beg = sptr;
464 #ifdef SECURE
465 unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned);
466 #else
467 unsigned size = UINT_MAX;
468 #endif
469 /* skip initial whitespace */
470 while ((nch!=_EOF_) && _ISSPACE_(nch))
471 nch = _GETC_(file);
472 /* read until whitespace */
473 while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
474 if (!suppress) {
475 *sptr++ = _CHAR2SUPPORTED_(nch);
476 if(size>1) size--;
477 else {
478 _UNLOCK_FILE_(file);
479 *sptr_beg = 0;
480 return rd;
483 st++;
484 nch = _GETC_(file);
485 if (width>0) width--;
487 /* terminate */
488 if (st && !suppress) *sptr = 0;
490 break;
491 widecharstring: { /* read a word into a wchar_t* */
492 MSVCRT_wchar_t *sptr = suppress ? NULL : va_arg(ap, MSVCRT_wchar_t*);
493 MSVCRT_wchar_t *sptr_beg = sptr;
494 #ifdef SECURE
495 unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned);
496 #else
497 unsigned size = UINT_MAX;
498 #endif
499 /* skip initial whitespace */
500 while ((nch!=_EOF_) && _ISSPACE_(nch))
501 nch = _GETC_(file);
502 /* read until whitespace */
503 while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
504 if (!suppress) {
505 *sptr++ = _WIDE2SUPPORTED_(nch);
506 if(size>1) size--;
507 else {
508 _UNLOCK_FILE_(file);
509 *sptr_beg = 0;
510 return rd;
513 st++;
514 nch = _GETC_(file);
515 if (width>0) width--;
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)/sizeof(char);
544 #else
545 unsigned size = UINT_MAX;
546 #endif
547 if (width == -1) width = 1;
548 while (width && (nch != _EOF_))
550 if (!suppress) {
551 *str++ = _CHAR2SUPPORTED_(nch);
552 if(size) size--;
553 else {
554 _UNLOCK_FILE_(file);
555 *pstr = 0;
556 return rd;
559 st++;
560 width--;
561 nch = _GETC_(file);
564 break;
565 widecharacter: { /* read single character into a wchar_t */
566 MSVCRT_wchar_t *str = suppress ? NULL : va_arg(ap, MSVCRT_wchar_t*);
567 MSVCRT_wchar_t *pstr = str;
568 #ifdef SECURE
569 unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned)/sizeof(MSVCRT_wchar_t);
570 #else
571 unsigned size = UINT_MAX;
572 #endif
573 if (width == -1) width = 1;
574 while (width && (nch != _EOF_))
576 if (!suppress) {
577 *str++ = _WIDE2SUPPORTED_(nch);
578 if(size) size--;
579 else {
580 _UNLOCK_FILE_(file);
581 *pstr = 0;
582 return rd;
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)/sizeof(_CHAR_);
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 == '-') && (*(format + 1) != ']')) {
641 if ((*(format - 1)) < *(format + 1))
642 RtlSetBits(&bitMask, *(format - 1) +1 , *(format + 1) - *(format - 1));
643 else
644 RtlSetBits(&bitMask, *(format + 1) , *(format - 1) - *(format + 1));
645 format++;
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 return rd;
673 /* terminate */
674 if (!suppress) *sptr = 0;
675 HeapFree(GetProcessHeap(), 0, Mask);
677 break;
678 default:
679 /* From spec: "if a percent sign is followed by a character
680 * that has no meaning as a format-control character, that
681 * character and the following characters are treated as
682 * an ordinary sequence of characters, that is, a sequence
683 * of characters that must match the input. For example,
684 * to specify that a percent-sign character is to be input,
685 * use %%." */
686 while ((nch!=_EOF_) && _ISSPACE_(nch))
687 nch = _GETC_(file);
688 if ((_CHAR_)nch == *format) {
689 suppress = 1; /* whoops no field to be read */
690 st = 1; /* but we got what we expected */
691 nch = _GETC_(file);
693 break;
695 if (st && !suppress) rd++;
696 else if (!st) break;
698 /* a non-white-space character causes scanf to read, but not store,
699 * a matching non-white-space character. */
700 else {
701 /* check for character match */
702 if ((_CHAR_)nch == *format) {
703 nch = _GETC_(file);
704 } else break;
706 format++;
708 if (nch!=_EOF_) {
709 _UNGETC_(nch, file);
712 TRACE("returning %d\n", rd);
713 _UNLOCK_FILE_(file);
714 return rd;
717 #undef _CHAR_
718 #undef _EOF_
719 #undef _EOF_RET
720 #undef _ISSPACE_
721 #undef _ISDIGIT_
722 #undef _CHAR2SUPPORTED_
723 #undef _WIDE2SUPPORTED_
724 #undef _CHAR2DIGIT_
725 #undef _GETC_
726 #undef _UNGETC_
727 #undef _LOCK_FILE_
728 #undef _UNLOCK_FILE_
729 #undef _FUNCTION_
730 #undef _BITMAPSIZE_