msvcrt: Added _Getdays implementation.
[wine] / dlls / msvcrt / scanf.h
1 /*
2  * general implementation of scanf used by scanf, sscanf, fscanf,
3  * _cscanf, wscanf, swscanf and fwscanf
4  *
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
10  *
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.
15  *
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.
20  *
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
24  */
25
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 */
47
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 char *format, MSVCRT__locale_t locale, __ms_va_list ap)
56 #else  /* SECURE */
57 #define _FUNCTION_ static int MSVCRT_vcwscanf_l(const char *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 #define _GETC_(file) (consumed==length ? '\0' : (consumed++, *file++))
72 #define _UNGETC_(nch, file) do { file--; consumed--; } while(0)
73 #define _LOCK_FILE_(file) do {} while(0)
74 #define _UNLOCK_FILE_(file) do {} while(0)
75 #ifdef WIDE_SCANF
76 #ifdef SECURE
77 #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)
78 #else  /* SECURE */
79 #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)
80 #endif /* SECURE */
81 #else /* WIDE_SCANF */
82 #ifdef SECURE
83 #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)
84 #else  /* SECURE */
85 #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)
86 #endif /* SECURE */
87 #endif /* WIDE_SCANF */
88 #else /* STRING_LEN */
89 #define _GETC_(file) (consumed++, *file++)
90 #define _UNGETC_(nch, file) do { file--; consumed--; } while(0)
91 #define _LOCK_FILE_(file) do {} while(0)
92 #define _UNLOCK_FILE_(file) do {} while(0)
93 #ifdef WIDE_SCANF
94 #ifdef SECURE
95 #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)
96 #else  /* SECURE */
97 #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)
98 #endif /* SECURE */
99 #else /* WIDE_SCANF */
100 #ifdef SECURE
101 #define _FUNCTION_ static int MSVCRT_vsscanf_s_l(const char *file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
102 #else  /* SECURE */
103 #define _FUNCTION_ static int MSVCRT_vsscanf_l(const char *file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
104 #endif /* SECURE */
105 #endif /* WIDE_SCANF */
106 #endif /* STRING_LEN */
107 #else /* STRING */
108 #ifdef WIDE_SCANF
109 #define _GETC_(file) (consumed++, MSVCRT_fgetwc(file))
110 #define _UNGETC_(nch, file) do { MSVCRT_ungetwc(nch, file); consumed--; } while(0)
111 #define _LOCK_FILE_(file) MSVCRT__lock_file(file)
112 #define _UNLOCK_FILE_(file) MSVCRT__unlock_file(file)
113 #ifdef SECURE
114 #define _FUNCTION_ static int MSVCRT_vfwscanf_s_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
115 #else  /* SECURE */
116 #define _FUNCTION_ static int MSVCRT_vfwscanf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
117 #endif /* SECURE */
118 #else /* WIDE_SCANF */
119 #define _GETC_(file) (consumed++, MSVCRT_fgetc(file))
120 #define _UNGETC_(nch, file) do { MSVCRT_ungetc(nch, file); consumed--; } while(0)
121 #define _LOCK_FILE_(file) MSVCRT__lock_file(file)
122 #define _UNLOCK_FILE_(file) MSVCRT__unlock_file(file)
123 #ifdef SECURE
124 #define _FUNCTION_ static int MSVCRT_vfscanf_s_l(MSVCRT_FILE* file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
125 #else  /* SECURE */
126 #define _FUNCTION_ static int MSVCRT_vfscanf_l(MSVCRT_FILE* file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
127 #endif /* SECURE */
128 #endif /* WIDE_SCANF */
129 #endif /* STRING */
130 #endif /* CONSOLE */
131
132 _FUNCTION_ {
133     MSVCRT_pthreadlocinfo locinfo;
134     int rd = 0, consumed = 0;
135     int nch;
136     if (!*format) return 0;
137 #ifndef WIDE_SCANF
138 #ifdef CONSOLE
139     TRACE("(%s):\n", debugstr_a(format));
140 #else /* CONSOLE */
141 #ifdef STRING
142     TRACE("%s (%s)\n", debugstr_a(file), debugstr_a(format));
143 #else /* STRING */
144     TRACE("%p (%s)\n", file, debugstr_a(format));
145 #endif /* STRING */
146 #endif /* CONSOLE */
147 #endif /* WIDE_SCANF */
148     _LOCK_FILE_(file);
149
150     nch = _GETC_(file);
151     if (nch == _EOF_) {
152         _UNLOCK_FILE_(file);
153         return _EOF_RET;
154     }
155
156     if(!locale)
157         locinfo = get_locinfo();
158     else
159         locinfo = locale->locinfo;
160
161     while (*format) {
162         /* a whitespace character in the format string causes scanf to read,
163          * but not store, all consecutive white-space characters in the input
164          * up to the next non-white-space character.  One white space character
165          * in the input matches any number (including zero) and combination of
166          * white-space characters in the input. */
167         if (_ISSPACE_(*format)) {
168             /* skip whitespace */
169             while ((nch!=_EOF_) && _ISSPACE_(nch))
170                 nch = _GETC_(file);
171         }
172         /* a format specification causes scanf to read and convert characters
173          * in the input into values of a specified type.  The value is assigned
174          * to an argument in the argument list.  Format specifications have
175          * the form %[*][width][{h | l | I64 | L}]type */
176         else if (*format == '%') {
177             int st = 0; int suppress = 0; int width = 0;
178             int base;
179             int h_prefix = 0;
180             int l_prefix = 0;
181             int L_prefix = 0;
182             int w_prefix = 0;
183             int prefix_finished = 0;
184             int I64_prefix = 0;
185             format++;
186             /* look for leading asterisk, which means 'suppress assignment of
187              * this field'. */
188             if (*format=='*') {
189                 format++;
190                 suppress=1;
191             }
192             /* look for width specification */
193             while (_ISDIGIT_(*format)) {
194                 width*=10;
195                 width+=*format++ - '0';
196             }
197             if (width==0) width=-1; /* no width spec seen */
198             /* read prefix (if any) */
199             while (!prefix_finished) {
200                 switch(*format) {
201                 case 'h': h_prefix = 1; break;
202                 case 'l': l_prefix = 1; break;
203                 case 'w': w_prefix = 1; break;
204                 case 'L': L_prefix = 1; break;
205                 case 'I':
206                     if (*(format + 1) == '6' &&
207                         *(format + 2) == '4') {
208                         I64_prefix = 1;
209                         format += 2;
210                     }
211                     break;
212                 default:
213                     prefix_finished = 1;
214                 }
215                 if (!prefix_finished) format++;
216             }
217             /* read type */
218             switch(*format) {
219             case 'p':
220             case 'P': /* pointer. */
221                 if (sizeof(void *) == sizeof(LONGLONG)) I64_prefix = 1;
222                 /* fall through */
223             case 'x':
224             case 'X': /* hexadecimal integer. */
225                 base = 16;
226                 goto number;
227             case 'o': /* octal integer */
228                 base = 8;
229                 goto number;
230             case 'u': /* unsigned decimal integer */
231                 base = 10;
232                 goto number;
233             case 'd': /* signed decimal integer */
234                 base = 10;
235                 goto number;
236             case 'i': /* generic integer */
237                 base = 0;
238             number: {
239                     /* read an integer */
240                     ULONGLONG cur = 0;
241                     int negative = 0;
242                     int seendigit=0;
243                     /* skip initial whitespace */
244                     while ((nch!=_EOF_) && _ISSPACE_(nch))
245                         nch = _GETC_(file);
246                     /* get sign */
247                     if (nch == '-' || nch == '+') {
248                         negative = (nch=='-');
249                         nch = _GETC_(file);
250                         if (width>0) width--;
251                     }
252                     /* look for leading indication of base */
253                     if (width!=0 && nch == '0' && *format != 'p' && *format != 'P') {
254                         nch = _GETC_(file);
255                         if (width>0) width--;
256                         seendigit=1;
257                         if (width!=0 && (nch=='x' || nch=='X')) {
258                             if (base==0)
259                                 base=16;
260                             if (base==16) {
261                                 nch = _GETC_(file);
262                                 if (width>0) width--;
263                                 seendigit=0;
264                             }
265                         } else if (base==0)
266                             base = 8;
267                     }
268                     /* format %i without indication of base */
269                     if (base==0)
270                         base = 10;
271                     /* throw away leading zeros */
272                     while (width!=0 && nch=='0') {
273                         nch = _GETC_(file);
274                         if (width>0) width--;
275                         seendigit=1;
276                     }
277                     if (width!=0 && _CHAR2DIGIT_(nch, base)!=-1) {
278                         cur = _CHAR2DIGIT_(nch, base);
279                         nch = _GETC_(file);
280                         if (width>0) width--;
281                         seendigit=1;
282                     }
283                     /* read until no more digits */
284                     while (width!=0 && (nch!=_EOF_) && _CHAR2DIGIT_(nch, base)!=-1) {
285                         cur = cur*base + _CHAR2DIGIT_(nch, base);
286                         nch = _GETC_(file);
287                         if (width>0) width--;
288                         seendigit=1;
289                     }
290                     /* okay, done! */
291                     if (!seendigit) break; /* not a valid number */
292                     st = 1;
293                     if (!suppress) {
294 #define _SET_NUMBER_(type) *va_arg(ap, type*) = negative ? -cur : cur
295                         if (I64_prefix) _SET_NUMBER_(LONGLONG);
296                         else if (l_prefix) _SET_NUMBER_(LONG);
297                         else if (h_prefix) _SET_NUMBER_(short int);
298                         else _SET_NUMBER_(int);
299                     }
300                 }
301                 break;
302             case 'e':
303             case 'E':
304             case 'f':
305             case 'g':
306             case 'G': { /* read a float */
307                     long double cur = 0;
308                     int negative = 0;
309                     /* skip initial whitespace */
310                     while ((nch!=_EOF_) && _ISSPACE_(nch))
311                         nch = _GETC_(file);
312                     /* get sign. */
313                     if (nch == '-' || nch == '+') {
314                         negative = (nch=='-');
315                         if (width>0) width--;
316                         if (width==0) break;
317                         nch = _GETC_(file);
318                     }
319                     /* get first digit. */
320                     if (*locinfo->lconv->decimal_point != nch) {
321                       if (!_ISDIGIT_(nch)) break;
322                       cur = (nch - '0');
323                       nch = _GETC_(file);
324                       if (width>0) width--;
325                       /* read until no more digits */
326                       while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
327                         cur = cur*10 + (nch - '0');
328                         nch = _GETC_(file);
329                         if (width>0) width--;
330                       }
331                     } else {
332                       cur = 0; /* Fix: .8 -> 0.8 */
333                     }
334                     /* handle decimals */
335                     if (width!=0 && nch == *locinfo->lconv->decimal_point) {
336                         long double dec = 1;
337                         nch = _GETC_(file);
338                         if (width>0) width--;
339                         while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
340                             dec /= 10;
341                             cur += dec * (nch - '0');
342                             nch = _GETC_(file);
343                             if (width>0) width--;
344                         }
345                     }
346                     /* handle exponent */
347                     if (width!=0 && (nch == 'e' || nch == 'E')) {
348                         int exponent = 0, negexp = 0;
349                         float expcnt;
350                         nch = _GETC_(file);
351                         if (width>0) width--;
352                         /* possible sign on the exponent */
353                         if (width!=0 && (nch=='+' || nch=='-')) {
354                             negexp = (nch=='-');
355                             nch = _GETC_(file);
356                             if (width>0) width--;
357                         }
358                         /* exponent digits */
359                         while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
360                             exponent *= 10;
361                             exponent += (nch - '0');
362                             nch = _GETC_(file);
363                             if (width>0) width--;
364                         }
365                         /* update 'cur' with this exponent. */
366                         expcnt =  negexp ? .1 : 10;
367                         while (exponent!=0) {
368                             if (exponent&1)
369                                 cur*=expcnt;
370                             exponent/=2;
371                             expcnt=expcnt*expcnt;
372                         }
373                     }
374                     st = 1;
375                     if (!suppress) {
376                         if (L_prefix) _SET_NUMBER_(long double);
377                         else if (l_prefix) _SET_NUMBER_(double);
378                         else _SET_NUMBER_(float);
379                     }
380                 }
381                 break;
382                 /* According to msdn,
383                  * 's' reads a character string in a call to fscanf
384                  * and 'S' a wide character string and vice versa in a
385                  * call to fwscanf. The 'h', 'w' and 'l' prefixes override
386                  * this behaviour. 'h' forces reading char * but 'l' and 'w'
387                  * force reading WCHAR. */
388             case 's':
389                     if (w_prefix || l_prefix) goto widecharstring;
390                     else if (h_prefix) goto charstring;
391 #ifdef WIDE_SCANF
392                     else goto widecharstring;
393 #else /* WIDE_SCANF */
394                     else goto charstring;
395 #endif /* WIDE_SCANF */
396             case 'S':
397                     if (w_prefix || l_prefix) goto widecharstring;
398                     else if (h_prefix) goto charstring;
399 #ifdef WIDE_SCANF
400                     else goto charstring;
401 #else /* WIDE_SCANF */
402                     else goto widecharstring;
403 #endif /* WIDE_SCANF */
404             charstring: { /* read a word into a char */
405                     char *sptr = suppress ? NULL : va_arg(ap, char*);
406                     char *sptr_beg = sptr;
407 #ifdef SECURE
408                     unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned);
409 #else
410                     unsigned size = UINT_MAX;
411 #endif
412                     /* skip initial whitespace */
413                     while ((nch!=_EOF_) && _ISSPACE_(nch))
414                         nch = _GETC_(file);
415                     /* read until whitespace */
416                     while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
417                         if (!suppress) {
418                             *sptr++ = _CHAR2SUPPORTED_(nch);
419                             if(size>1) size--;
420                             else {
421                                 _UNLOCK_FILE_(file);
422                                 *sptr_beg = 0;
423                                 return rd;
424                             }
425                         }
426                         st++;
427                         nch = _GETC_(file);
428                         if (width>0) width--;
429                     }
430                     /* terminate */
431                     if (!suppress) *sptr = 0;
432                 }
433                 break;
434             widecharstring: { /* read a word into a wchar_t* */
435                     MSVCRT_wchar_t *sptr = suppress ? NULL : va_arg(ap, MSVCRT_wchar_t*);
436                     MSVCRT_wchar_t *sptr_beg = sptr;
437 #ifdef SECURE
438                     unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned);
439 #else
440                     unsigned size = UINT_MAX;
441 #endif
442                     /* skip initial whitespace */
443                     while ((nch!=_EOF_) && _ISSPACE_(nch))
444                         nch = _GETC_(file);
445                     /* read until whitespace */
446                     while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
447                         if (!suppress) {
448                             *sptr++ = _WIDE2SUPPORTED_(nch);
449                             if(size>1) size--;
450                             else {
451                                 _UNLOCK_FILE_(file);
452                                 *sptr_beg = 0;
453                                 return rd;
454                             }
455                         }
456                         st++;
457                         nch = _GETC_(file);
458                         if (width>0) width--;
459                     }
460                     /* terminate */
461                     if (!suppress) *sptr = 0;
462                 }
463                 break;
464             /* 'c' and 'C work analogously to 's' and 'S' as described
465              * above */
466             case 'c':
467                     if (w_prefix || l_prefix) goto widecharacter;
468                     else if (h_prefix) goto character;
469 #ifdef WIDE_SCANF
470                     else goto widecharacter;
471 #else /* WIDE_SCANF */
472                     else goto character;
473 #endif /* WIDE_SCANF */
474             case 'C':
475                     if (w_prefix || l_prefix) goto widecharacter;
476                     else if (h_prefix) goto character;
477 #ifdef WIDE_SCANF
478                     else goto character;
479 #else /* WIDE_SCANF */
480                     else goto widecharacter;
481 #endif /* WIDE_SCANF */
482           character: { /* read single character into char */
483                     char *str = suppress ? NULL : va_arg(ap, char*);
484                     char *pstr = str;
485 #ifdef SECURE
486                     unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned)/sizeof(char);
487 #else
488                     unsigned size = UINT_MAX;
489 #endif
490                     if (width == -1) width = 1;
491                     while (width && (nch != _EOF_))
492                     {
493                         if (!suppress) {
494                             *str++ = _CHAR2SUPPORTED_(nch);
495                             if(size) size--;
496                             else {
497                                 _UNLOCK_FILE_(file);
498                                 *pstr = 0;
499                                 return rd;
500                             }
501                         }
502                         st++;
503                         width--;
504                         nch = _GETC_(file);
505                     }
506                 }
507                 break;
508           widecharacter: { /* read single character into a wchar_t */
509                     MSVCRT_wchar_t *str = suppress ? NULL : va_arg(ap, MSVCRT_wchar_t*);
510                     MSVCRT_wchar_t *pstr = str;
511 #ifdef SECURE
512                     unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned)/sizeof(MSVCRT_wchar_t);
513 #else
514                     unsigned size = UINT_MAX;
515 #endif
516                     if (width == -1) width = 1;
517                     while (width && (nch != _EOF_))
518                     {
519                         if (!suppress) {
520                             *str++ = _WIDE2SUPPORTED_(nch);
521                             if(size) size--;
522                             else {
523                                 _UNLOCK_FILE_(file);
524                                 *pstr = 0;
525                                 return rd;
526                             }
527                         }
528                         st++;
529                         width--;
530                         nch = _GETC_(file);
531                     }
532                 }
533                 break;
534             case 'n': {
535                     if (!suppress) {
536                         int*n = va_arg(ap, int*);
537                         *n = consumed - 1;
538                     }
539                     /* This is an odd one: according to the standard,
540                      * "Execution of a %n directive does not increment the
541                      * assignment count returned at the completion of
542                      * execution" even if it wasn't suppressed with the
543                      * '*' flag.  The Corrigendum to the standard seems
544                      * to contradict this (comment out the assignment to
545                      * suppress below if you want to implement these
546                      * alternate semantics) but the windows program I'm
547                      * looking at expects the behavior I've coded here
548                      * (which happens to be what glibc does as well).
549                      */
550                     suppress = 1;
551                     st = 1;
552                 }
553                 break;
554             case '[': {
555                     _CHAR_ *str = suppress ? NULL : va_arg(ap, _CHAR_*);
556                     _CHAR_ *sptr = str;
557                     RTL_BITMAP bitMask;
558                     ULONG *Mask;
559                     int invert = 0; /* Set if we are NOT to find the chars */
560 #ifdef SECURE
561                     unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned)/sizeof(_CHAR_);
562 #else
563                     unsigned size = UINT_MAX;
564 #endif
565
566                     /* Init our bitmap */
567                     Mask = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, _BITMAPSIZE_/8);
568                     RtlInitializeBitMap(&bitMask, Mask, _BITMAPSIZE_);
569
570                     /* Read the format */
571                     format++;
572                     if(*format == '^') {
573                         invert = 1;
574                         format++;
575                     }
576                     if(*format == ']') {
577                         RtlSetBits(&bitMask, ']', 1);
578                         format++;
579                     }
580                     while(*format && (*format != ']')) {
581                         /* According to msdn:
582                          * "Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]." */
583                         if((*format == '-') && (*(format + 1) != ']')) {
584                             if ((*(format - 1)) < *(format + 1))
585                                 RtlSetBits(&bitMask, *(format - 1) +1 , *(format + 1) - *(format - 1));
586                             else
587                                 RtlSetBits(&bitMask, *(format + 1)    , *(format - 1) - *(format + 1));                       
588                             format++;
589                         } else
590                             RtlSetBits(&bitMask, *format, 1);
591                         format++;
592                     }
593                     /* read until char is not suitable */
594                     while ((width != 0) && (nch != _EOF_)) {
595                         if(!invert) {
596                             if(RtlAreBitsSet(&bitMask, nch, 1)) {
597                                 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
598                             } else
599                                 break;
600                         } else {
601                             if(RtlAreBitsClear(&bitMask, nch, 1)) {
602                                 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
603                             } else
604                                 break;
605                         }
606                         st++;
607                         nch = _GETC_(file);
608                         if (width>0) width--;
609                         if(size>1) size--;
610                         else {
611                             _UNLOCK_FILE_(file);
612                             *str = 0;
613                             return rd;
614                         }
615                     }
616                     /* terminate */
617                     if (!suppress) *sptr = 0;
618                     HeapFree(GetProcessHeap(), 0, Mask);
619                 }
620                 break;
621             default:
622                 /* From spec: "if a percent sign is followed by a character
623                  * that has no meaning as a format-control character, that
624                  * character and the following characters are treated as
625                  * an ordinary sequence of characters, that is, a sequence
626                  * of characters that must match the input.  For example,
627                  * to specify that a percent-sign character is to be input,
628                  * use %%." */
629                 while ((nch!=_EOF_) && _ISSPACE_(nch))
630                     nch = _GETC_(file);
631                 if (nch==*format) {
632                     suppress = 1; /* whoops no field to be read */
633                     st = 1; /* but we got what we expected */
634                     nch = _GETC_(file);
635                 }
636                 break;
637             }
638             if (st && !suppress) rd++;
639             else if (!st) break;
640         }
641         /* a non-white-space character causes scanf to read, but not store,
642          * a matching non-white-space character. */
643         else {
644             /* check for character match */
645             if (nch == *format) {
646                 nch = _GETC_(file);
647             } else break;
648         }
649         format++;
650     }
651     if (nch!=_EOF_) {
652         _UNGETC_(nch, file);
653     }
654
655     TRACE("returning %d\n", rd);
656     _UNLOCK_FILE_(file);
657     return rd;
658 }
659
660 #undef _CHAR_
661 #undef _EOF_
662 #undef _EOF_RET
663 #undef _ISSPACE_
664 #undef _ISDIGIT_
665 #undef _CHAR2SUPPORTED_
666 #undef _WIDE2SUPPORTED_
667 #undef _CHAR2DIGIT_
668 #undef _GETC_
669 #undef _UNGETC_
670 #undef _LOCK_FILE_
671 #undef _UNLOCK_FILE_
672 #undef _FUNCTION_
673 #undef _BITMAPSIZE_