msvcrt: Fix fscanf return when EOF is immediately after an end of line.
[wine.git] / dlls / msvcrt / scanf.h
blobe2f216110d3c412883e13c775875aee9ff913696
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 #if _MSVCR_VER >= 140
313 else if (h_prefix == 2) _SET_NUMBER_(char);
314 #endif
315 else _SET_NUMBER_(int);
318 break;
319 case 'e':
320 case 'E':
321 case 'f':
322 case 'g':
323 case 'G': { /* read a float */
324 long double cur = 1, expcnt = 10;
325 ULONGLONG d, hlp;
326 int exp = 0, negative = 0;
327 unsigned fpcontrol;
328 BOOL negexp;
330 /* skip initial whitespace */
331 while ((nch!=_EOF_) && _ISSPACE_(nch))
332 nch = _GETC_(file);
334 /* get sign. */
335 if (nch == '-' || nch == '+') {
336 negative = (nch=='-');
337 if (width>0) width--;
338 if (width==0) break;
339 nch = _GETC_(file);
342 /* get first digit. */
343 if (*locinfo->lconv->decimal_point != nch) {
344 if (!_ISDIGIT_(nch)) break;
345 d = nch - '0';
346 nch = _GETC_(file);
347 if (width>0) width--;
348 /* read until no more digits */
349 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
350 hlp = d*10 + nch - '0';
351 nch = _GETC_(file);
352 if (width>0) width--;
353 if(d > (ULONGLONG)-1/10 || hlp<d) {
354 exp++;
355 break;
357 else
358 d = hlp;
360 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
361 exp++;
362 nch = _GETC_(file);
363 if (width>0) width--;
365 } else {
366 d = 0; /* Fix: .8 -> 0.8 */
369 /* handle decimals */
370 if (width!=0 && nch == *locinfo->lconv->decimal_point) {
371 nch = _GETC_(file);
372 if (width>0) width--;
374 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
375 hlp = d*10 + nch - '0';
376 nch = _GETC_(file);
377 if (width>0) width--;
378 if(d > (ULONGLONG)-1/10 || hlp<d)
379 break;
381 d = hlp;
382 exp--;
384 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
385 nch = _GETC_(file);
386 if (width>0) width--;
390 /* handle exponent */
391 if (width!=0 && (nch == 'e' || nch == 'E')) {
392 int sign = 1, e = 0;
394 nch = _GETC_(file);
395 if (width>0) width--;
396 if (width!=0 && (nch=='+' || nch=='-')) {
397 if(nch == '-')
398 sign = -1;
399 nch = _GETC_(file);
400 if (width>0) width--;
403 /* exponent digits */
404 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
405 if(e > INT_MAX/10 || (e = e*10 + nch - '0')<0)
406 e = INT_MAX;
407 nch = _GETC_(file);
408 if (width>0) width--;
410 e *= sign;
412 if(exp<0 && e<0 && e+exp>0) exp = INT_MIN;
413 else if(exp>0 && e>0 && e+exp<0) exp = INT_MAX;
414 else exp += e;
417 fpcontrol = _control87(0, 0);
418 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
419 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
421 negexp = (exp < 0);
422 if(negexp)
423 exp = -exp;
424 /* update 'cur' with this exponent. */
425 while(exp) {
426 if(exp & 1)
427 cur *= expcnt;
428 exp /= 2;
429 expcnt = expcnt*expcnt;
431 cur = (negexp ? d/cur : d*cur);
433 _control87(fpcontrol, 0xffffffff);
435 st = 1;
436 if (!suppress) {
437 if (L_prefix || l_prefix) _SET_NUMBER_(double);
438 else _SET_NUMBER_(float);
441 break;
442 /* According to msdn,
443 * 's' reads a character string in a call to fscanf
444 * and 'S' a wide character string and vice versa in a
445 * call to fwscanf. The 'h', 'w' and 'l' prefixes override
446 * this behaviour. 'h' forces reading char * but 'l' and 'w'
447 * force reading WCHAR. */
448 case 's':
449 if (w_prefix || l_prefix) goto widecharstring;
450 else if (h_prefix) goto charstring;
451 #ifdef WIDE_SCANF
452 else goto widecharstring;
453 #else /* WIDE_SCANF */
454 else goto charstring;
455 #endif /* WIDE_SCANF */
456 case 'S':
457 if (w_prefix || l_prefix) goto widecharstring;
458 else if (h_prefix) goto charstring;
459 #ifdef WIDE_SCANF
460 else goto charstring;
461 #else /* WIDE_SCANF */
462 else goto widecharstring;
463 #endif /* WIDE_SCANF */
464 charstring: { /* read a word into a char */
465 char *sptr = suppress ? NULL : va_arg(ap, char*);
466 char *sptr_beg = sptr;
467 #ifdef SECURE
468 unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned);
469 #else
470 unsigned size = UINT_MAX;
471 #endif
472 /* skip initial whitespace */
473 while ((nch!=_EOF_) && _ISSPACE_(nch))
474 nch = _GETC_(file);
475 /* read until whitespace */
476 while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
477 if (!suppress) {
478 *sptr++ = _CHAR2SUPPORTED_(nch);
479 if(size>1) size--;
480 else {
481 _UNLOCK_FILE_(file);
482 *sptr_beg = 0;
483 return rd;
486 st++;
487 nch = _GETC_(file);
488 if (width>0) width--;
490 /* if we have reached the EOF and output nothing then report EOF */
491 if (nch==_EOF_ && rd==0 && st==0) {
492 return _EOF_RET;
494 /* terminate */
495 if (st && !suppress) *sptr = 0;
497 break;
498 widecharstring: { /* read a word into a wchar_t* */
499 MSVCRT_wchar_t *sptr = suppress ? NULL : va_arg(ap, MSVCRT_wchar_t*);
500 MSVCRT_wchar_t *sptr_beg = sptr;
501 #ifdef SECURE
502 unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned);
503 #else
504 unsigned size = UINT_MAX;
505 #endif
506 /* skip initial whitespace */
507 while ((nch!=_EOF_) && _ISSPACE_(nch))
508 nch = _GETC_(file);
509 /* read until whitespace */
510 while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
511 if (!suppress) {
512 *sptr++ = _WIDE2SUPPORTED_(nch);
513 if(size>1) size--;
514 else {
515 _UNLOCK_FILE_(file);
516 *sptr_beg = 0;
517 return rd;
520 st++;
521 nch = _GETC_(file);
522 if (width>0) width--;
524 #if _MSVCR_VER >= 80
525 /* if we have reached the EOF and output nothing then report EOF */
526 if (nch==_EOF_ && rd==0 && st==0) {
527 return _EOF_RET;
529 #endif
530 /* terminate */
531 if (st && !suppress) *sptr = 0;
533 break;
534 /* 'c' and 'C work analogously to 's' and 'S' as described
535 * above */
536 case 'c':
537 if (w_prefix || l_prefix) goto widecharacter;
538 else if (h_prefix) goto character;
539 #ifdef WIDE_SCANF
540 else goto widecharacter;
541 #else /* WIDE_SCANF */
542 else goto character;
543 #endif /* WIDE_SCANF */
544 case 'C':
545 if (w_prefix || l_prefix) goto widecharacter;
546 else if (h_prefix) goto character;
547 #ifdef WIDE_SCANF
548 else goto character;
549 #else /* WIDE_SCANF */
550 else goto widecharacter;
551 #endif /* WIDE_SCANF */
552 character: { /* read single character into char */
553 char *str = suppress ? NULL : va_arg(ap, char*);
554 char *pstr = str;
555 #ifdef SECURE
556 unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned);
557 #else
558 unsigned size = UINT_MAX;
559 #endif
560 if (width == -1) width = 1;
561 while (width && (nch != _EOF_))
563 if (!suppress) {
564 if(size) size--;
565 else {
566 _UNLOCK_FILE_(file);
567 *pstr = 0;
568 return rd;
570 *str++ = _CHAR2SUPPORTED_(nch);
572 st++;
573 width--;
574 nch = _GETC_(file);
577 break;
578 widecharacter: { /* read single character into a wchar_t */
579 MSVCRT_wchar_t *str = suppress ? NULL : va_arg(ap, MSVCRT_wchar_t*);
580 MSVCRT_wchar_t *pstr = str;
581 #ifdef SECURE
582 unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned);
583 #else
584 unsigned size = UINT_MAX;
585 #endif
586 if (width == -1) width = 1;
587 while (width && (nch != _EOF_))
589 if (!suppress) {
590 if(size) size--;
591 else {
592 _UNLOCK_FILE_(file);
593 *pstr = 0;
594 return rd;
596 *str++ = _WIDE2SUPPORTED_(nch);
598 st++;
599 width--;
600 nch = _GETC_(file);
603 break;
604 case 'n': {
605 if (!suppress) {
606 int*n = va_arg(ap, int*);
607 *n = consumed - 1;
609 /* This is an odd one: according to the standard,
610 * "Execution of a %n directive does not increment the
611 * assignment count returned at the completion of
612 * execution" even if it wasn't suppressed with the
613 * '*' flag. The Corrigendum to the standard seems
614 * to contradict this (comment out the assignment to
615 * suppress below if you want to implement these
616 * alternate semantics) but the windows program I'm
617 * looking at expects the behavior I've coded here
618 * (which happens to be what glibc does as well).
620 suppress = 1;
621 st = 1;
623 break;
624 case '[': {
625 _CHAR_ *str = suppress ? NULL : va_arg(ap, _CHAR_*);
626 _CHAR_ *sptr = str;
627 RTL_BITMAP bitMask;
628 ULONG *Mask;
629 int invert = 0; /* Set if we are NOT to find the chars */
630 #ifdef SECURE
631 unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned);
632 #else
633 unsigned size = UINT_MAX;
634 #endif
636 /* Init our bitmap */
637 Mask = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, _BITMAPSIZE_/8);
638 RtlInitializeBitMap(&bitMask, Mask, _BITMAPSIZE_);
640 /* Read the format */
641 format++;
642 if(*format == '^') {
643 invert = 1;
644 format++;
646 if(*format == ']') {
647 RtlSetBits(&bitMask, ']', 1);
648 format++;
650 while(*format && (*format != ']')) {
651 /* According to msdn:
652 * "Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]." */
653 if((*format == '-') && (*(format + 1) != ']')) {
654 if ((*(format - 1)) < *(format + 1))
655 RtlSetBits(&bitMask, *(format - 1) +1 , *(format + 1) - *(format - 1));
656 else
657 RtlSetBits(&bitMask, *(format + 1) , *(format - 1) - *(format + 1));
658 format++;
659 } else
660 RtlSetBits(&bitMask, *format, 1);
661 format++;
663 /* read until char is not suitable */
664 while ((width != 0) && (nch != _EOF_)) {
665 if(!invert) {
666 if(RtlAreBitsSet(&bitMask, nch, 1)) {
667 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
668 } else
669 break;
670 } else {
671 if(RtlAreBitsClear(&bitMask, nch, 1)) {
672 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
673 } else
674 break;
676 st++;
677 nch = _GETC_(file);
678 if (width>0) width--;
679 if(size>1) size--;
680 else {
681 _UNLOCK_FILE_(file);
682 *str = 0;
683 HeapFree(GetProcessHeap(), 0, Mask);
684 return rd;
687 /* terminate */
688 if (!suppress) *sptr = 0;
689 HeapFree(GetProcessHeap(), 0, Mask);
691 break;
692 default:
693 /* From spec: "if a percent sign is followed by a character
694 * that has no meaning as a format-control character, that
695 * character and the following characters are treated as
696 * an ordinary sequence of characters, that is, a sequence
697 * of characters that must match the input. For example,
698 * to specify that a percent-sign character is to be input,
699 * use %%." */
700 while ((nch!=_EOF_) && _ISSPACE_(nch))
701 nch = _GETC_(file);
702 if ((_CHAR_)nch == *format) {
703 suppress = 1; /* whoops no field to be read */
704 st = 1; /* but we got what we expected */
705 nch = _GETC_(file);
707 break;
709 if (st && !suppress) rd++;
710 else if (!st) break;
712 /* a non-white-space character causes scanf to read, but not store,
713 * a matching non-white-space character. */
714 else {
715 /* check for character match */
716 if ((_CHAR_)nch == *format) {
717 nch = _GETC_(file);
718 } else break;
720 format++;
722 if (nch!=_EOF_) {
723 _UNGETC_(nch, file);
726 TRACE("returning %d\n", rd);
727 _UNLOCK_FILE_(file);
728 return rd;
731 #undef _CHAR_
732 #undef _EOF_
733 #undef _EOF_RET
734 #undef _ISSPACE_
735 #undef _ISDIGIT_
736 #undef _CHAR2SUPPORTED_
737 #undef _WIDE2SUPPORTED_
738 #undef _CHAR2DIGIT_
739 #undef _GETC_
740 #undef _UNGETC_
741 #undef _LOCK_FILE_
742 #undef _UNLOCK_FILE_
743 #undef _FUNCTION_
744 #undef _BITMAPSIZE_