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