Moved ts_xlib.c into x11drv and removed libwine_tsx11.
[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 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25
26 #ifdef WIDE_SCANF
27 #define _L_(x) L##x
28 #define _CHAR_ WCHAR
29 #define _EOF_ MSVCRT_WEOF
30 #define _ISSPACE_(c) MSVCRT_iswspace(c)
31 #define _ISDIGIT_(c) MSVCRT_iswdigit(c)
32 #define _CONVERT_(c) c /*** FIXME ***/
33 #define _CHAR2DIGIT_(c, base) wchar2digit((c), (base))
34 #else /* WIDE_SCANF */
35 #define _L_(x) x
36 #define _CHAR_ char
37 #define _EOF_ MSVCRT_EOF
38 #define _ISSPACE_(c) isspace(c)
39 #define _ISDIGIT_(c) isdigit(c)
40 #define _CONVERT_(c) c /*** FIXME ***/
41 #define _CHAR2DIGIT_(c, base) char2digit((c), (base))
42 #endif /* WIDE_SCANF */
43
44 #ifdef CONSOLE
45 #define _GETC_(file) _getch()
46 #define _UNGETC_(nch, file) _ungetch(nch)
47 #define _FUNCTION_ _cscanf(const _CHAR_ *format, ...)
48 #else
49 #ifdef STRING
50 #define _GETC_(file) *file++
51 #define _UNGETC_(nch, file) file--
52 #ifdef WIDE_SCANF
53 #define _FUNCTION_ MSVCRT_swscanf(const WCHAR *file, const WCHAR *format, ...)
54 #else /* WIDE_SCANF */
55 #define _FUNCTION_ MSVCRT_sscanf(const char *file, const char *format, ...)
56 #endif /* WIDE_SCANF */
57 #else /* STRING */
58 #ifdef WIDE_SCANF
59 #define _GETC_(file) MSVCRT_fgetwc(file)
60 #define _UNGETC_(nch, file) MSVCRT_ungetwc(nch, file)
61 #define _FUNCTION_ MSVCRT_fwscanf(MSVCRT_FILE* file, const WCHAR *format, ...)
62 #else /* WIDE_SCANF */
63 #define _GETC_(file) MSVCRT_fgetc(file)
64 #define _UNGETC_(nch, file) MSVCRT_ungetc(nch, file)
65 #define _FUNCTION_ MSVCRT_fscanf(MSVCRT_FILE* file, const char *format, ...)
66 #endif /* WIDE_SCANF */
67 #endif /* STRING */
68 #endif /* CONSOLE */
69
70 /*********************************************************************
71  * Implemented based on
72  * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt_format_specification_fields_.2d_.scanf_and_wscanf_functions.asp
73  * Extended by C. Scott Ananian <cananian@alumni.princeton.edu> to handle
74  * more types of format spec.
75  */
76 int _FUNCTION_ {
77     int rd = 0;
78     int nch;
79     va_list ap;
80     if (!*format) return 0;
81 #ifndef WIDE_SCANF
82 #ifdef CONSOLE
83     WARN("(\"%s\"): semi-stub\n", format);
84 #else /* CONSOLE */
85 #ifdef STRING
86     WARN("%s (\"%s\"): semi-stub\n", file, format);
87 #else /* STRING */
88     WARN("%p (\"%s\"): semi-stub\n", file, format);
89 #endif /* STRING */
90 #endif /* CONSOLE */
91 #endif /* WIDE_SCANF */
92     nch = _GETC_(file);
93     va_start(ap, format);
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 == _L_('%')) {
110             int st = 0; int suppress = 0; int width = 0;
111             int base, number_signed;
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==_L_('*')) {
122                 format++;
123                 suppress=1;
124             }
125             /* look for width specification */
126             while (_ISDIGIT_(*format)) {
127                 width*=10;
128                 width+=*format++ - _L_('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 _L_('h'): h_prefix = 1; break;
135                 case _L_('l'): l_prefix = 1; break;
136                 case _L_('w'): w_prefix = 1; break;
137                 case _L_('L'): L_prefix = 1; break;
138                 case _L_('I'):
139                     if (*(format + 1) == _L_('6') &&
140                         *(format + 2) == _L_('4')) {
141                         /* I64_prefix = 1; */
142                         format += 2;
143                         FIXME("I64 prefix currently not implemented in fscanf/fwscanf");
144                     }
145                     break;
146                 default:
147                     prefix_finished = 1;
148                 }
149                 if (!prefix_finished) format++;
150             }
151             /* read type */
152             switch(*format) {
153             case _L_('%'): { /* read a percent symbol */
154                     while ((nch!=_EOF_) && _ISSPACE_(nch))
155                         nch = _GETC_(file);
156                     if (nch==_L_('%')) {
157                         suppress = 1; /* whoops no field to be read */
158                         st = 1; /* but we got what we expected */
159                         nch = _GETC_(file);
160                     }
161                 }
162                 break;
163             case _L_('x'):
164             case _L_('X'): /* hexadecimal integer. */
165                 base = 16; number_signed = 0;
166                 goto number;
167             case _L_('o'): /* octal integer */
168                 base = 8; number_signed = 0;
169                 goto number;
170             case _L_('u'): /* unsigned decimal integer */
171                 base = 10; number_signed = 0;
172                 goto number;
173             case _L_('d'): /* signed decimal integer */
174                 base = 10; number_signed = 1;
175                 goto number;
176             case _L_('i'): /* generic integer */
177                 base = 0; number_signed = 1;
178             number: {
179                     /* read an integer */
180                     long unsigned int cur = 0;
181                     int negative = 0;
182                     int seendigit=0;
183                     /* skip initial whitespace */
184                     while ((nch!=_EOF_) && _ISSPACE_(nch))
185                         nch = _GETC_(file);
186                     /* get sign */
187                     if (number_signed && (nch == _L_('-') ||
188                                           nch == _L_('+'))) {
189                         negative = (nch==_L_('-'));
190                         nch = _GETC_(file);
191                         if (width>0) width--;
192                     }
193                     /* look for leading indication of base */
194                     if (width!=0 && nch == _L_('0')) {
195                         nch = _GETC_(file);
196                         if (width>0) width--;
197                         seendigit=1;
198                         if (width!=0 && (nch==_L_('x') || nch==_L_('X'))) {
199                             if (base==0)
200                                 base=16;
201                             if (base==16) {
202                                 nch = _GETC_(file);
203                                 if (width>0) width--;
204                                 seendigit=0;
205                             }
206                         } else if (base==0)
207                             base = 8;
208                     }
209                     /* throw away leading zeros */
210                     while (width!=0 && nch==_L_('0')) {
211                         nch = _GETC_(file);
212                         if (width>0) width--;
213                         seendigit=1;
214                     }
215                     if (width!=0 && _CHAR2DIGIT_(nch, base)!=-1) {
216                         cur = _CHAR2DIGIT_(nch, base);
217                         nch = _GETC_(file);
218                         if (width>0) width--;
219                         seendigit=1;
220                     }
221                     /* read until no more digits */
222                     while (width!=0 && (nch!=_EOF_) && _CHAR2DIGIT_(nch, base)!=-1) {
223                         cur = cur*base + _CHAR2DIGIT_(nch, base);
224                         nch = _GETC_(file);
225                         if (width>0) width--;
226                         seendigit=1;
227                     }
228                     /* okay, done! */
229                     if (!seendigit) break; /* not a valid number */
230                     st = 1;
231                     if (!suppress) {
232 #define _SET_NUMBER_(type) *va_arg(ap, type*) = negative ? -cur : cur
233                         if (number_signed) {
234                             if (l_prefix) _SET_NUMBER_(long int);
235                             else if (h_prefix) _SET_NUMBER_(short int);
236                             else _SET_NUMBER_(int);
237                         } else {
238                             if (negative) {
239                                 WARN("Dropping sign in reading a negative number into an unsigned value");
240                                 negative = 0;
241                             }
242                             if (l_prefix) _SET_NUMBER_(unsigned long int);
243                             else if (h_prefix)
244                                 _SET_NUMBER_(unsigned short int);
245                             else _SET_NUMBER_(unsigned int);
246                         }
247                     }
248                 }
249                 break;
250             case _L_('e'):
251             case _L_('E'):
252             case _L_('f'):
253             case _L_('g'):
254             case _L_('G'): { /* read a float */
255                     long double cur = 0;
256                     int negative = 0;
257                     /* skip initial whitespace */
258                     while ((nch!=_EOF_) && _ISSPACE_(nch))
259                         nch = _GETC_(file);
260                     /* get sign. */
261                     if (nch == _L_('-') || nch == _L_('+')) {
262                         negative = (nch==_L_('-'));
263                         if (width>0) width--;
264                         if (width==0) break;
265                         nch = _GETC_(file);
266                     }
267                     /* get first digit. */
268                     if (!_ISDIGIT_(nch)) break;
269                     cur = (nch - _L_('0')) * (negative ? -1 : 1);
270                     nch = _GETC_(file);
271                     if (width>0) width--;
272                     /* read until no more digits */
273                     while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
274                         cur = cur*10 + (nch - _L_('0'));
275                         nch = _GETC_(file);
276                         if (width>0) width--;
277                     }
278                     /* handle decimals */
279                     if (width!=0 && nch == _L_('.')) {
280                         float dec = 1;
281                         nch = _GETC_(file);
282                         if (width>0) width--;
283                         while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
284                             dec /= 10;
285                             cur += dec * (nch - _L_('0'));
286                             nch = _GETC_(file);
287                             if (width>0) width--;
288                         }
289                     }
290                     /* handle exponent */
291                     if (width!=0 && (nch == _L_('e') || nch == _L_('E'))) {
292                         int exponent = 0, negexp = 0;
293                         float expcnt;
294                         nch = _GETC_(file);
295                         if (width>0) width--;
296                         /* possible sign on the exponent */
297                         if (width!=0 && (nch==_L_('+') || nch==_L_('-'))) {
298                             negexp = (nch==_L_('-'));
299                             nch = _GETC_(file);
300                             if (width>0) width--;
301                         }
302                         /* exponent digits */
303                         while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
304                             exponent *= 10;
305                             exponent += (nch - _L_('0'));
306                             nch = _GETC_(file);
307                             if (width>0) width--;
308                         }
309                         /* update 'cur' with this exponent. */
310                         expcnt =  negexp ? .1 : 10;
311                         while (exponent!=0) {
312                             if (exponent&1)
313                                 cur*=expcnt;
314                             exponent/=2;
315                             expcnt=expcnt*expcnt;
316                         }
317                     }
318                     st = 1;
319                     if (!suppress) {
320                         if (L_prefix) _SET_NUMBER_(long double);
321                         else if (l_prefix) _SET_NUMBER_(double);
322                         else _SET_NUMBER_(float);
323                     }
324                 }
325                 break;
326                 /* According to
327                  * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_scanf_type_field_characters.asp
328                  * 's' reads a character string in a call to fscanf
329                  * and 'S' a wide character string and vice versa in a
330                  * call to fwscanf. The 'h', 'w' and 'l' prefixes override
331                  * this behaviour. 'h' forces reading char * but 'l' and 'w'
332                  * force reading WCHAR. */
333             case _L_('s'):
334                     if (w_prefix || l_prefix) goto widecharstring;
335                     else if (h_prefix) goto charstring;
336 #ifdef WIDE_SCANF
337                     else goto widecharstring;
338 #else /* WIDE_SCANF */
339                     else goto charstring;
340 #endif /* WIDE_SCANF */
341             case _L_('S'):
342                     if (w_prefix || l_prefix) goto widecharstring;
343                     else if (h_prefix) goto charstring;
344 #ifdef WIDE_SCANF
345                     else goto charstring;
346 #else /* WIDE_SCANF */
347                     else goto widecharstring;
348 #endif /* WIDE_SCANF */
349             charstring: { /* read a word into a char */
350                     char*str = suppress ? NULL : va_arg(ap, char*);
351                     char*sptr = str;
352                     /* skip initial whitespace */
353                     while ((nch!=_EOF_) && _ISSPACE_(nch))
354                         nch = _GETC_(file);
355                     /* read until whitespace */
356                     while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
357 #ifdef WIDE_SCANF
358                         if (!suppress) *sptr++ = _CONVERT_(nch);
359 #else /* WIDE_SCANF */
360                         if (!suppress) *sptr++ = nch;
361 #endif /* WIDE_SCANF */
362                         st++;
363                         nch = _GETC_(file);
364                         if (width>0) width--;
365                     }
366                     /* terminate */
367                     if (!suppress) *sptr = 0;
368                 }
369                 break;
370             widecharstring: { /* read a word into a WCHAR * */
371                     WCHAR*str =
372                         suppress ? NULL : va_arg(ap, WCHAR*);
373                     WCHAR*sptr = str;
374                     /* skip initial whitespace */
375                     while ((nch!=_EOF_) && _ISSPACE_(nch))
376                         nch = _GETC_(file);
377                     /* read until whitespace */
378                     while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
379 #ifdef WIDE_SCANF
380                         if (!suppress) *sptr++ = nch;
381 #else /* WIDE_SCANF */
382                         if (!suppress) *sptr++ = _CONVERT_(nch);
383 #endif /* WIDE_SCANF */
384                         st++;
385                         nch = _GETC_(file);
386                         if (width>0) width--;
387                     }
388                     /* terminate */
389                     if (!suppress) *sptr = 0;
390                 }
391                 break;
392             /* 'c' and 'C work analogously to 's' and 'S' as described
393              * above */
394             case _L_('c'):
395                     if (w_prefix || l_prefix) goto widecharacter;
396                     else if (h_prefix) goto character;
397 #ifdef WIDE_SCANF
398                     else goto widecharacter;
399 #else /* WIDE_SCANF */
400                     else goto character;
401 #endif /* WIDE_SCANF */
402             case _L_('C'):
403                     if (w_prefix || l_prefix) goto widecharacter;
404                     else if (h_prefix) goto character;
405 #ifdef WIDE_SCANF
406                     else goto character;
407 #else /* WIDE_SCANF */
408                     else goto widecharacter;
409 #endif /* WIDE_SCANF */
410           character: { /* read single character into char */
411                     if (!suppress) {
412                         char*c = va_arg(ap, char*);
413 #ifdef WIDE_SCANF
414                         *c = _CONVERT_(nch);
415 #else /* WIDE_SCANF */
416                         *c = nch;
417 #endif /* WIDE_SCANF */
418                         st = 1;
419                     }
420                     nch = _GETC_(file);
421                 }
422                 break;
423           widecharacter: {
424                     if (!suppress) { /* read single character into WCHAR */
425                         WCHAR*c = va_arg(ap, WCHAR*);
426 #ifdef WIDE_SCANF
427                         *c = nch;
428 #else /* WIDE_SCANF */
429                         *c = _CONVERT_(nch);
430 #endif /* WIDE_SCANF */
431                         st = 1;
432                     }
433                     nch = _GETC_(file);
434                 }
435                 break;
436             case _L_('n'): {
437                     if (!suppress) {
438                         int*n = va_arg(ap, int*);
439                         *n = rd;
440                     }
441                 }
442                 break;
443             default: FIXME("unhandled: %%%c\n", *format);
444                 /* From spec: "if a percent sign is followed by a character
445                  * that has no meaning as a format-control character, that
446                  * character and the following characters are treated as
447                  * an ordinary sequence of characters, that is, a sequence
448                  * of characters that must match the input.  For example,
449                  * to specify that a percent-sign character is to be input,
450                  * use %%."
451                  * LEAVING AS-IS because we catch bugs better that way. */
452             }
453             if (st && !suppress) rd++;
454             else if (!st) break;
455         }
456         /* a non-white-space character causes scanf to read, but not store,
457          * a matching non-white-space character. */
458         else {
459             /* check for character match */
460             if (nch == *format) {
461                 nch = _GETC_(file);
462             } else break;
463         }
464         format++;
465     }
466     if (nch!=_EOF_) {
467         _UNGETC_(nch, file);
468     }
469     va_end(ap);
470     TRACE("returning %d\n", rd);
471     return rd;
472 }
473
474 #undef _L_
475 #undef _CHAR_
476 #undef _EOF_
477 #undef _ISSPACE_
478 #undef _ISDIGIT_
479 #undef _CONVERT_
480 #undef _CHAR2DIGIT_
481 #undef _GETC_
482 #undef _UNGETC_
483 #undef _FUNCTION_