comctl32: toolbar: Don't execute TB_GETBUTTONINFO if cbSize is invalid.
[wine/multimedia.git] / dlls / msvcrt / scanf.h
blob7fe3cc4de4942a392a8e4181c0648dfdcc824d12
1 /*
2 * general implementation of scanf used by scanf, sscanf, fscanf,
3 * _cscanf, wscanf, swscanf and fwscanf
5 * Copyright 1996,1998 Marcus Meissner
6 * Copyright 1996 Jukka Iivonen
7 * Copyright 1997,2000, 2003 Uwe Bonnes
8 * Copyright 2000 Jon Griffiths
9 * Copyright 2002 Daniel Gudbjartsson
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #ifdef WIDE_SCANF
27 #define _CHAR_ MSVCRT_wchar_t
28 #define _EOF_ MSVCRT_WEOF
29 #define _EOF_RET MSVCRT_WEOF
30 #define _ISSPACE_(c) MSVCRT_iswspace(c)
31 #define _ISDIGIT_(c) MSVCRT_iswdigit(c)
32 #define _WIDE2SUPPORTED_(c) c /* No conversion needed (wide to wide) */
33 #define _CHAR2SUPPORTED_(c) c /* FIXME: convert char to wide char */
34 #define _CHAR2DIGIT_(c, base) wchar2digit((c), (base))
35 #define _BITMAPSIZE_ 256*256
36 #else /* WIDE_SCANF */
37 #define _CHAR_ char
38 #define _EOF_ MSVCRT_EOF
39 #define _EOF_RET MSVCRT_EOF
40 #define _ISSPACE_(c) isspace(c)
41 #define _ISDIGIT_(c) isdigit(c)
42 #define _WIDE2SUPPORTED_(c) c /* FIXME: convert wide char to char */
43 #define _CHAR2SUPPORTED_(c) c /* No conversion needed (char to char) */
44 #define _CHAR2DIGIT_(c, base) char2digit((c), (base))
45 #define _BITMAPSIZE_ 256
46 #endif /* WIDE_SCANF */
48 #ifdef CONSOLE
49 #define _GETC_(file) (consumed++, _getch())
50 #define _UNGETC_(nch, file) do { _ungetch(nch); consumed--; } while(0)
51 #define _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 */
76 /*********************************************************************
77 * Implemented based on
78 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt_format_specification_fields_.2d_.scanf_and_wscanf_functions.asp
79 * Extended by C. Scott Ananian <cananian@alumni.princeton.edu> to handle
80 * more types of format spec.
82 _FUNCTION_ {
83 int rd = 0, consumed = 0;
84 int nch;
85 if (!*format) return 0;
86 #ifndef WIDE_SCANF
87 #ifdef CONSOLE
88 TRACE("(%s): \n", debugstr_a(format));
89 #else /* CONSOLE */
90 #ifdef STRING
91 TRACE("%s (%s)\n", file, debugstr_a(format));
92 #else /* STRING */
93 TRACE("%p (%s)\n", file, debugstr_a(format));
94 #endif /* STRING */
95 #endif /* CONSOLE */
96 #endif /* WIDE_SCANF */
97 nch = _GETC_(file);
98 if (nch == _EOF_) return _EOF_RET;
100 while (*format) {
101 /* a whitespace character in the format string causes scanf to read,
102 * but not store, all consecutive white-space characters in the input
103 * up to the next non-white-space character. One white space character
104 * in the input matches any number (including zero) and combination of
105 * white-space characters in the input. */
106 if (_ISSPACE_(*format)) {
107 /* skip whitespace */
108 while ((nch!=_EOF_) && _ISSPACE_(nch))
109 nch = _GETC_(file);
111 /* a format specification causes scanf to read and convert characters
112 * in the input into values of a specified type. The value is assigned
113 * to an argument in the argument list. Format specifications have
114 * the form %[*][width][{h | l | I64 | L}]type */
115 else if (*format == '%') {
116 int st = 0; int suppress = 0; int width = 0;
117 int base;
118 int h_prefix = 0;
119 int l_prefix = 0;
120 int L_prefix = 0;
121 int w_prefix = 0;
122 int prefix_finished = 0;
123 int I64_prefix = 0;
124 format++;
125 /* look for leading asterisk, which means 'suppress assignment of
126 * this field'. */
127 if (*format=='*') {
128 format++;
129 suppress=1;
131 /* look for width specification */
132 while (_ISDIGIT_(*format)) {
133 width*=10;
134 width+=*format++ - '0';
136 if (width==0) width=-1; /* no width spec seen */
137 /* read prefix (if any) */
138 while (!prefix_finished) {
139 switch(*format) {
140 case 'h': h_prefix = 1; break;
141 case 'l': l_prefix = 1; break;
142 case 'w': w_prefix = 1; break;
143 case 'L': L_prefix = 1; break;
144 case 'I':
145 if (*(format + 1) == '6' &&
146 *(format + 2) == '4') {
147 I64_prefix = 1;
148 format += 2;
150 break;
151 default:
152 prefix_finished = 1;
154 if (!prefix_finished) format++;
156 /* read type */
157 switch(*format) {
158 case 'x':
159 case 'X': /* hexadecimal integer. */
160 base = 16;
161 goto number;
162 case 'o': /* octal integer */
163 base = 8;
164 goto number;
165 case 'u': /* unsigned decimal integer */
166 base = 10;
167 goto number;
168 case 'd': /* signed decimal integer */
169 base = 10;
170 goto number;
171 case 'i': /* generic integer */
172 base = 0;
173 number: {
174 /* read an integer */
175 ULONGLONG cur = 0;
176 int negative = 0;
177 int seendigit=0;
178 /* skip initial whitespace */
179 while ((nch!=_EOF_) && _ISSPACE_(nch))
180 nch = _GETC_(file);
181 /* get sign */
182 if (nch == '-' || nch == '+') {
183 negative = (nch=='-');
184 nch = _GETC_(file);
185 if (width>0) width--;
187 /* look for leading indication of base */
188 if (width!=0 && nch == '0') {
189 nch = _GETC_(file);
190 if (width>0) width--;
191 seendigit=1;
192 if (width!=0 && (nch=='x' || nch=='X')) {
193 if (base==0)
194 base=16;
195 if (base==16) {
196 nch = _GETC_(file);
197 if (width>0) width--;
198 seendigit=0;
200 } else if (base==0)
201 base = 8;
203 /* format %i without indication of base */
204 if (base==0)
205 base = 10;
206 /* throw away leading zeros */
207 while (width!=0 && nch=='0') {
208 nch = _GETC_(file);
209 if (width>0) width--;
210 seendigit=1;
212 if (width!=0 && _CHAR2DIGIT_(nch, base)!=-1) {
213 cur = _CHAR2DIGIT_(nch, base);
214 nch = _GETC_(file);
215 if (width>0) width--;
216 seendigit=1;
218 /* read until no more digits */
219 while (width!=0 && (nch!=_EOF_) && _CHAR2DIGIT_(nch, base)!=-1) {
220 cur = cur*base + _CHAR2DIGIT_(nch, base);
221 nch = _GETC_(file);
222 if (width>0) width--;
223 seendigit=1;
225 /* okay, done! */
226 if (!seendigit) break; /* not a valid number */
227 st = 1;
228 if (!suppress) {
229 #define _SET_NUMBER_(type) *va_arg(ap, type*) = negative ? -cur : cur
230 if (I64_prefix) _SET_NUMBER_(LONGLONG);
231 else if (l_prefix) _SET_NUMBER_(long int);
232 else if (h_prefix) _SET_NUMBER_(short int);
233 else _SET_NUMBER_(int);
236 break;
237 case 'e':
238 case 'E':
239 case 'f':
240 case 'g':
241 case 'G': { /* read a float */
242 long double cur = 0;
243 int negative = 0;
244 /* skip initial whitespace */
245 while ((nch!=_EOF_) && _ISSPACE_(nch))
246 nch = _GETC_(file);
247 /* get sign. */
248 if (nch == '-' || nch == '+') {
249 negative = (nch=='-');
250 if (width>0) width--;
251 if (width==0) break;
252 nch = _GETC_(file);
254 /* get first digit. */
255 if ('.' != nch) {
256 if (!_ISDIGIT_(nch)) break;
257 cur = (nch - '0');
258 nch = _GETC_(file);
259 if (width>0) width--;
260 /* read until no more digits */
261 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
262 cur = cur*10 + (nch - '0');
263 nch = _GETC_(file);
264 if (width>0) width--;
266 } else {
267 cur = 0; /* MaxPayneDemo Fix: .8 -> 0.8 */
269 /* handle decimals */
270 if (width!=0 && nch == '.') {
271 float dec = 1;
272 nch = _GETC_(file);
273 if (width>0) width--;
274 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
275 dec /= 10;
276 cur += dec * (nch - '0');
277 nch = _GETC_(file);
278 if (width>0) width--;
281 /* handle exponent */
282 if (width!=0 && (nch == 'e' || nch == 'E')) {
283 int exponent = 0, negexp = 0;
284 float expcnt;
285 nch = _GETC_(file);
286 if (width>0) width--;
287 /* possible sign on the exponent */
288 if (width!=0 && (nch=='+' || nch=='-')) {
289 negexp = (nch=='-');
290 nch = _GETC_(file);
291 if (width>0) width--;
293 /* exponent digits */
294 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
295 exponent *= 10;
296 exponent += (nch - '0');
297 nch = _GETC_(file);
298 if (width>0) width--;
300 /* update 'cur' with this exponent. */
301 expcnt = negexp ? .1 : 10;
302 while (exponent!=0) {
303 if (exponent&1)
304 cur*=expcnt;
305 exponent/=2;
306 expcnt=expcnt*expcnt;
309 st = 1;
310 if (!suppress) {
311 if (L_prefix) _SET_NUMBER_(long double);
312 else if (l_prefix) _SET_NUMBER_(double);
313 else _SET_NUMBER_(float);
316 break;
317 /* According to
318 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_scanf_type_field_characters.asp
319 * 's' reads a character string in a call to fscanf
320 * and 'S' a wide character string and vice versa in a
321 * call to fwscanf. The 'h', 'w' and 'l' prefixes override
322 * this behaviour. 'h' forces reading char * but 'l' and 'w'
323 * force reading WCHAR. */
324 case 's':
325 if (w_prefix || l_prefix) goto widecharstring;
326 else if (h_prefix) goto charstring;
327 #ifdef WIDE_SCANF
328 else goto widecharstring;
329 #else /* WIDE_SCANF */
330 else goto charstring;
331 #endif /* WIDE_SCANF */
332 case 'S':
333 if (w_prefix || l_prefix) goto widecharstring;
334 else if (h_prefix) goto charstring;
335 #ifdef WIDE_SCANF
336 else goto charstring;
337 #else /* WIDE_SCANF */
338 else goto widecharstring;
339 #endif /* WIDE_SCANF */
340 charstring: { /* read a word into a char */
341 char*str = suppress ? NULL : va_arg(ap, char*);
342 char*sptr = str;
343 /* skip initial whitespace */
344 while ((nch!=_EOF_) && _ISSPACE_(nch))
345 nch = _GETC_(file);
346 /* read until whitespace */
347 while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
348 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
349 st++;
350 nch = _GETC_(file);
351 if (width>0) width--;
353 /* terminate */
354 if (!suppress) *sptr = 0;
356 break;
357 widecharstring: { /* read a word into a wchar_t* */
358 MSVCRT_wchar_t*str =
359 suppress ? NULL : va_arg(ap, MSVCRT_wchar_t*);
360 MSVCRT_wchar_t*sptr = str;
361 /* skip initial whitespace */
362 while ((nch!=_EOF_) && _ISSPACE_(nch))
363 nch = _GETC_(file);
364 /* read until whitespace */
365 while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
366 if (!suppress) *sptr++ = _WIDE2SUPPORTED_(nch);
367 st++;
368 nch = _GETC_(file);
369 if (width>0) width--;
371 /* terminate */
372 if (!suppress) *sptr = 0;
374 break;
375 /* 'c' and 'C work analogously to 's' and 'S' as described
376 * above */
377 case 'c':
378 if (w_prefix || l_prefix) goto widecharacter;
379 else if (h_prefix) goto character;
380 #ifdef WIDE_SCANF
381 else goto widecharacter;
382 #else /* WIDE_SCANF */
383 else goto character;
384 #endif /* WIDE_SCANF */
385 case 'C':
386 if (w_prefix || l_prefix) goto widecharacter;
387 else if (h_prefix) goto character;
388 #ifdef WIDE_SCANF
389 else goto character;
390 #else /* WIDE_SCANF */
391 else goto widecharacter;
392 #endif /* WIDE_SCANF */
393 character: { /* read single character into char */
394 if (nch!=_EOF_) {
395 if (!suppress) {
396 char*c = va_arg(ap, char*);
397 *c = _CHAR2SUPPORTED_(nch);
399 st = 1;
400 nch = _GETC_(file);
403 break;
404 widecharacter: { /* read single character into a wchar_t */
405 if (nch!=_EOF_) {
406 if (!suppress) {
407 MSVCRT_wchar_t*c = va_arg(ap, MSVCRT_wchar_t*);
408 *c = _WIDE2SUPPORTED_(nch);
410 nch = _GETC_(file);
411 st = 1;
414 break;
415 case 'n': {
416 if (!suppress) {
417 int*n = va_arg(ap, int*);
418 *n = consumed - 1;
420 /* This is an odd one: according to the standard,
421 * "Execution of a %n directive does not increment the
422 * assignment count returned at the completion of
423 * execution" even if it wasn't suppressed with the
424 * '*' flag. The Corrigendum to the standard seems
425 * to contradict this (comment out the assignment to
426 * suppress below if you want to implement these
427 * alternate semantics) but the windows program I'm
428 * looking at expects the behavior I've coded here
429 * (which happens to be what glibc does as well).
431 suppress = 1;
432 st = 1;
434 break;
435 case '[': {
436 _CHAR_ *str = suppress ? NULL : va_arg(ap, _CHAR_*);
437 _CHAR_ *sptr = str;
438 RTL_BITMAP bitMask;
439 ULONG *Mask;
440 int invert = 0; /* Set if we are NOT to find the chars */
442 /* Init our bitmap */
443 Mask = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, _BITMAPSIZE_/8);
444 RtlInitializeBitMap(&bitMask, Mask, _BITMAPSIZE_);
446 /* Read the format */
447 format++;
448 if(*format == '^') {
449 invert = 1;
450 format++;
452 if(*format == ']') {
453 RtlSetBits(&bitMask, ']', 1);
454 format++;
456 while(*format && (*format != ']')) {
457 /* According to:
458 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt_scanf_width_specification.asp
459 * "Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]." */
460 if((*format == '-') && (*(format + 1) != ']')) {
461 if ((*(format - 1)) < *(format + 1))
462 RtlSetBits(&bitMask, *(format - 1) +1 , *(format + 1) - *(format - 1));
463 else
464 RtlSetBits(&bitMask, *(format + 1) , *(format - 1) - *(format + 1));
465 format++;
466 } else
467 RtlSetBits(&bitMask, *format, 1);
468 format++;
470 /* read until char is not suitable */
471 while ((width != 0) && (nch != _EOF_)) {
472 if(!invert) {
473 if(RtlAreBitsSet(&bitMask, nch, 1)) {
474 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
475 } else
476 break;
477 } else {
478 if(RtlAreBitsClear(&bitMask, nch, 1)) {
479 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
480 } else
481 break;
483 st++;
484 nch = _GETC_(file);
485 if (width>0) width--;
487 /* terminate */
488 if (!suppress) *sptr = 0;
489 HeapFree(GetProcessHeap(), 0, Mask);
491 break;
492 default:
493 /* From spec: "if a percent sign is followed by a character
494 * that has no meaning as a format-control character, that
495 * character and the following characters are treated as
496 * an ordinary sequence of characters, that is, a sequence
497 * of characters that must match the input. For example,
498 * to specify that a percent-sign character is to be input,
499 * use %%." */
500 while ((nch!=_EOF_) && _ISSPACE_(nch))
501 nch = _GETC_(file);
502 if (nch==*format) {
503 suppress = 1; /* whoops no field to be read */
504 st = 1; /* but we got what we expected */
505 nch = _GETC_(file);
507 break;
509 if (st && !suppress) rd++;
510 else if (!st) break;
512 /* a non-white-space character causes scanf to read, but not store,
513 * a matching non-white-space character. */
514 else {
515 /* check for character match */
516 if (nch == *format) {
517 nch = _GETC_(file);
518 } else break;
520 format++;
522 if (nch!=_EOF_) {
523 _UNGETC_(nch, file);
525 TRACE("returning %d\n", rd);
526 return rd;
529 #undef _CHAR_
530 #undef _EOF_
531 #undef _EOF_RET
532 #undef _ISSPACE_
533 #undef _ISDIGIT_
534 #undef _CHAR2SUPPORTED_
535 #undef _WIDE2SUPPORTED_
536 #undef _CHAR2DIGIT_
537 #undef _GETC_
538 #undef _UNGETC_
539 #undef _FUNCTION_
540 #undef _BITMAPSIZE_