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