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