%n returns the number of consumed characters.
[wine/multimedia.git] / dlls / msvcrt / scanf.h
blob0a4502ab2b21a30110ebb17bd5451f872cdee658
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 _CONVERT_(c) c /*** FIXME ***/
33 #define _CHAR2DIGIT_(c, base) wchar2digit((c), (base))
34 #else /* WIDE_SCANF */
35 #define _CHAR_ char
36 #define _EOF_ MSVCRT_EOF
37 #define _EOF_RET 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 */
44 #ifdef CONSOLE
45 #define _GETC_(file) (consumed++, _getch())
46 #define _UNGETC_(nch, file) do { _ungetch(nch); consumed--; } while(0)
47 #define _FUNCTION_ _cscanf(const _CHAR_ *format, ...)
48 #else
49 #ifdef STRING
50 #undef _EOF_
51 #define _EOF_ 0
52 #define _GETC_(file) (consumed++, *file++)
53 #define _UNGETC_(nch, file) do { file--; consumed--; } while(0)
54 #ifdef WIDE_SCANF
55 #define _FUNCTION_ MSVCRT_swscanf(const MSVCRT_wchar_t *file, const MSVCRT_wchar_t *format, ...)
56 #else /* WIDE_SCANF */
57 #define _FUNCTION_ MSVCRT_sscanf(const char *file, const char *format, ...)
58 #endif /* WIDE_SCANF */
59 #else /* STRING */
60 #ifdef WIDE_SCANF
61 #define _GETC_(file) (consumed++, MSVCRT_fgetwc(file))
62 #define _UNGETC_(nch, file) do { MSVCRT_ungetwc(nch, file); consumed--; } while(0)
63 #define _FUNCTION_ MSVCRT_fwscanf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
64 #else /* WIDE_SCANF */
65 #define _GETC_(file) (consumed++, MSVCRT_fgetc(file))
66 #define _UNGETC_(nch, file) do { MSVCRT_ungetc(nch, file); consumed--; } while(0)
67 #define _FUNCTION_ MSVCRT_fscanf(MSVCRT_FILE* file, const char *format, ...)
68 #endif /* WIDE_SCANF */
69 #endif /* STRING */
70 #endif /* CONSOLE */
72 /*********************************************************************
73 * Implemented based on
74 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt_format_specification_fields_.2d_.scanf_and_wscanf_functions.asp
75 * Extended by C. Scott Ananian <cananian@alumni.princeton.edu> to handle
76 * more types of format spec.
78 int _FUNCTION_ {
79 int rd = 0, consumed = 0;
80 int nch;
81 va_list ap;
82 if (!*format) return 0;
83 #ifndef WIDE_SCANF
84 #ifdef CONSOLE
85 WARN("(%s): semi-stub\n", debugstr_a(format));
86 #else /* CONSOLE */
87 #ifdef STRING
88 WARN("%s (%s): semi-stub\n", file, debugstr_a(format));
89 #else /* STRING */
90 WARN("%p (%s): semi-stub\n", file, debugstr_a(format));
91 #endif /* STRING */
92 #endif /* CONSOLE */
93 #endif /* WIDE_SCANF */
94 nch = _GETC_(file);
95 if (nch == _EOF_) return _EOF_RET;
97 va_start(ap, format);
98 while (*format) {
99 /* a whitespace character in the format string causes scanf to read,
100 * but not store, all consecutive white-space characters in the input
101 * up to the next non-white-space character. One white space character
102 * in the input matches any number (including zero) and combination of
103 * white-space characters in the input. */
104 if (_ISSPACE_(*format)) {
105 /* skip whitespace */
106 while ((nch!=_EOF_) && _ISSPACE_(nch))
107 nch = _GETC_(file);
109 /* a format specification causes scanf to read and convert characters
110 * in the input into values of a specified type. The value is assigned
111 * to an argument in the argument list. Format specifications have
112 * the form %[*][width][{h | l | I64 | L}]type */
113 else if (*format == '%') {
114 int st = 0; int suppress = 0; int width = 0;
115 int base, number_signed;
116 int h_prefix = 0;
117 int l_prefix = 0;
118 int L_prefix = 0;
119 int w_prefix = 0;
120 int prefix_finished = 0;
121 /* int I64_prefix = 0; */
122 format++;
123 /* look for leading asterisk, which means 'suppress assignment of
124 * this field'. */
125 if (*format=='*') {
126 format++;
127 suppress=1;
129 /* look for width specification */
130 while (_ISDIGIT_(*format)) {
131 width*=10;
132 width+=*format++ - '0';
134 if (width==0) width=-1; /* no width spec seen */
135 /* read prefix (if any) */
136 while (!prefix_finished) {
137 switch(*format) {
138 case 'h': h_prefix = 1; break;
139 case 'l': l_prefix = 1; break;
140 case 'w': w_prefix = 1; break;
141 case 'L': L_prefix = 1; break;
142 case 'I':
143 if (*(format + 1) == '6' &&
144 *(format + 2) == '4') {
145 /* I64_prefix = 1; */
146 format += 2;
147 FIXME("I64 prefix currently not implemented in fscanf/fwscanf");
149 break;
150 default:
151 prefix_finished = 1;
153 if (!prefix_finished) format++;
155 /* read type */
156 switch(*format) {
157 case 'x':
158 case 'X': /* hexadecimal integer. */
159 base = 16; number_signed = 0;
160 goto number;
161 case 'o': /* octal integer */
162 base = 8; number_signed = 0;
163 goto number;
164 case 'u': /* unsigned decimal integer */
165 base = 10; number_signed = 0;
166 goto number;
167 case 'd': /* signed decimal integer */
168 base = 10; number_signed = 1;
169 goto number;
170 case 'i': /* generic integer */
171 base = 0; number_signed = 1;
172 number: {
173 /* read an integer */
174 long unsigned int cur = 0;
175 int negative = 0;
176 int seendigit=0;
177 /* skip initial whitespace */
178 while ((nch!=_EOF_) && _ISSPACE_(nch))
179 nch = _GETC_(file);
180 /* get sign */
181 if (number_signed && (nch == '-' ||
182 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 /* throw away leading zeros */
204 while (width!=0 && nch=='0') {
205 nch = _GETC_(file);
206 if (width>0) width--;
207 seendigit=1;
209 if (width!=0 && _CHAR2DIGIT_(nch, base)!=-1) {
210 cur = _CHAR2DIGIT_(nch, base);
211 nch = _GETC_(file);
212 if (width>0) width--;
213 seendigit=1;
215 /* read until no more digits */
216 while (width!=0 && (nch!=_EOF_) && _CHAR2DIGIT_(nch, base)!=-1) {
217 cur = cur*base + _CHAR2DIGIT_(nch, base);
218 nch = _GETC_(file);
219 if (width>0) width--;
220 seendigit=1;
222 /* okay, done! */
223 if (!seendigit) break; /* not a valid number */
224 st = 1;
225 if (!suppress) {
226 #define _SET_NUMBER_(type) *va_arg(ap, type*) = negative ? -cur : cur
227 if (number_signed) {
228 if (l_prefix) _SET_NUMBER_(long int);
229 else if (h_prefix) _SET_NUMBER_(short int);
230 else _SET_NUMBER_(int);
231 } else {
232 if (negative) {
233 WARN("Dropping sign in reading a negative number into an unsigned value");
234 negative = 0;
236 if (l_prefix) _SET_NUMBER_(unsigned long int);
237 else if (h_prefix)
238 _SET_NUMBER_(unsigned short int);
239 else _SET_NUMBER_(unsigned int);
243 break;
244 case 'e':
245 case 'E':
246 case 'f':
247 case 'g':
248 case 'G': { /* read a float */
249 long double cur = 0;
250 int negative = 0;
251 /* skip initial whitespace */
252 while ((nch!=_EOF_) && _ISSPACE_(nch))
253 nch = _GETC_(file);
254 /* get sign. */
255 if (nch == '-' || nch == '+') {
256 negative = (nch=='-');
257 if (width>0) width--;
258 if (width==0) break;
259 nch = _GETC_(file);
261 /* get first digit. */
262 if (!_ISDIGIT_(nch)) break;
263 cur = (nch - '0');
264 nch = _GETC_(file);
265 if (width>0) width--;
266 /* read until no more digits */
267 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
268 cur = cur*10 + (nch - '0');
269 nch = _GETC_(file);
270 if (width>0) width--;
272 /* handle decimals */
273 if (width!=0 && nch == '.') {
274 float dec = 1;
275 nch = _GETC_(file);
276 if (width>0) width--;
277 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
278 dec /= 10;
279 cur += dec * (nch - '0');
280 nch = _GETC_(file);
281 if (width>0) width--;
284 /* handle exponent */
285 if (width!=0 && (nch == 'e' || nch == 'E')) {
286 int exponent = 0, negexp = 0;
287 float expcnt;
288 nch = _GETC_(file);
289 if (width>0) width--;
290 /* possible sign on the exponent */
291 if (width!=0 && (nch=='+' || nch=='-')) {
292 negexp = (nch=='-');
293 nch = _GETC_(file);
294 if (width>0) width--;
296 /* exponent digits */
297 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
298 exponent *= 10;
299 exponent += (nch - '0');
300 nch = _GETC_(file);
301 if (width>0) width--;
303 /* update 'cur' with this exponent. */
304 expcnt = negexp ? .1 : 10;
305 while (exponent!=0) {
306 if (exponent&1)
307 cur*=expcnt;
308 exponent/=2;
309 expcnt=expcnt*expcnt;
312 st = 1;
313 if (!suppress) {
314 if (L_prefix) _SET_NUMBER_(long double);
315 else if (l_prefix) _SET_NUMBER_(double);
316 else _SET_NUMBER_(float);
319 break;
320 /* According to
321 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_scanf_type_field_characters.asp
322 * 's' reads a character string in a call to fscanf
323 * and 'S' a wide character string and vice versa in a
324 * call to fwscanf. The 'h', 'w' and 'l' prefixes override
325 * this behaviour. 'h' forces reading char * but 'l' and 'w'
326 * force reading WCHAR. */
327 case 's':
328 if (w_prefix || l_prefix) goto widecharstring;
329 else if (h_prefix) goto charstring;
330 #ifdef WIDE_SCANF
331 else goto widecharstring;
332 #else /* WIDE_SCANF */
333 else goto charstring;
334 #endif /* WIDE_SCANF */
335 case 'S':
336 if (w_prefix || l_prefix) goto widecharstring;
337 else if (h_prefix) goto charstring;
338 #ifdef WIDE_SCANF
339 else goto charstring;
340 #else /* WIDE_SCANF */
341 else goto widecharstring;
342 #endif /* WIDE_SCANF */
343 charstring: { /* read a word into a char */
344 char*str = suppress ? NULL : va_arg(ap, char*);
345 char*sptr = str;
346 /* skip initial whitespace */
347 while ((nch!=_EOF_) && _ISSPACE_(nch))
348 nch = _GETC_(file);
349 /* read until whitespace */
350 while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
351 #ifdef WIDE_SCANF
352 if (!suppress) *sptr++ = _CONVERT_(nch);
353 #else /* WIDE_SCANF */
354 if (!suppress) *sptr++ = nch;
355 #endif /* WIDE_SCANF */
356 st++;
357 nch = _GETC_(file);
358 if (width>0) width--;
360 /* terminate */
361 if (!suppress) *sptr = 0;
363 break;
364 widecharstring: { /* read a word into a wchar_t* */
365 MSVCRT_wchar_t*str =
366 suppress ? NULL : va_arg(ap, MSVCRT_wchar_t*);
367 MSVCRT_wchar_t*sptr = str;
368 /* skip initial whitespace */
369 while ((nch!=_EOF_) && _ISSPACE_(nch))
370 nch = _GETC_(file);
371 /* read until whitespace */
372 while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
373 #ifdef WIDE_SCANF
374 if (!suppress) *sptr++ = nch;
375 #else /* WIDE_SCANF */
376 if (!suppress) *sptr++ = _CONVERT_(nch);
377 #endif /* WIDE_SCANF */
378 st++;
379 nch = _GETC_(file);
380 if (width>0) width--;
382 /* terminate */
383 if (!suppress) *sptr = 0;
385 break;
386 /* 'c' and 'C work analogously to 's' and 'S' as described
387 * above */
388 case 'c':
389 if (w_prefix || l_prefix) goto widecharacter;
390 else if (h_prefix) goto character;
391 #ifdef WIDE_SCANF
392 else goto widecharacter;
393 #else /* WIDE_SCANF */
394 else goto character;
395 #endif /* WIDE_SCANF */
396 case 'C':
397 if (w_prefix || l_prefix) goto widecharacter;
398 else if (h_prefix) goto character;
399 #ifdef WIDE_SCANF
400 else goto character;
401 #else /* WIDE_SCANF */
402 else goto widecharacter;
403 #endif /* WIDE_SCANF */
404 character: { /* read single character into char */
405 if (!suppress) {
406 char*c = va_arg(ap, char*);
407 #ifdef WIDE_SCANF
408 *c = _CONVERT_(nch);
409 #else /* WIDE_SCANF */
410 *c = nch;
411 #endif /* WIDE_SCANF */
413 st = 1;
414 nch = _GETC_(file);
416 break;
417 widecharacter: {
418 if (!suppress) { /* read single character into a wchar_t */
419 MSVCRT_wchar_t*c = va_arg(ap, MSVCRT_wchar_t*);
420 #ifdef WIDE_SCANF
421 *c = nch;
422 #else /* WIDE_SCANF */
423 *c = _CONVERT_(nch);
424 #endif /* WIDE_SCANF */
426 nch = _GETC_(file);
427 st = 1;
429 break;
430 case 'n': {
431 if (!suppress) {
432 int*n = va_arg(ap, int*);
433 *n = consumed - (nch!=_EOF_);
436 break;
437 case '[': {
438 _CHAR_ *str = suppress ? NULL : va_arg(ap, _CHAR_*);
439 _CHAR_ *sptr = str;
440 RTL_BITMAP bitMask;
441 LPBYTE Mask;
442 int invert = 0; /* Set if we are NOT to find the chars */
444 /* Init our bitmap */
445 #ifdef WIDE_SCANF
446 Mask = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 65536/8);
447 RtlInitializeBitMap(&bitMask, Mask, 65536);
448 #else /* WIDE_SCANF */
449 Mask = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 256/8);
450 RtlInitializeBitMap(&bitMask, Mask, 256);
451 #endif /* WIDE_SCANF */
453 /* Read the format */
454 format++;
455 if(*format == '^') {
456 invert = 1;
457 format++;
459 if(*format == ']') {
460 RtlSetBits(&bitMask, ']', 1);
461 format++;
463 while(*format && (*format != ']')) {
464 if((*format == '-') && (*(format + 1) != ']')) {
465 int n = 0;
466 for(;(n + *(format - 1)) < *(format + 1); n++)
467 RtlSetBits(&bitMask, n + *(format - 1), 1);
468 format++;
470 RtlSetBits(&bitMask, *format, 1);
471 format++;
473 /* read until char is not suitable */
474 while ((width != 0) && (nch != _EOF_)) {
475 if(!invert) {
476 if(RtlAreBitsSet(&bitMask, nch, 1)) {
477 #ifdef WIDE_SCANF
478 if (!suppress) *sptr++ = _CONVERT_(nch);
479 #else /* WIDE_SCANF */
480 if (!suppress) *sptr++ = nch;
481 #endif /* WIDE_SCANF */
482 } else
483 break;
484 } else {
485 if(RtlAreBitsClear(&bitMask, nch, 1)) {
486 #ifdef WIDE_SCANF
487 if (!suppress) *sptr++ = _CONVERT_(nch);
488 #else /* WIDE_SCANF */
489 if (!suppress) *sptr++ = nch;
490 #endif /* WIDE_SCANF */
491 } else
492 break;
494 st++;
495 nch = _GETC_(file);
496 if (width>0) width--;
498 /* terminate */
499 if (!suppress) *sptr = 0;
500 HeapFree(GetProcessHeap(), 0, Mask);
502 break;
503 default:
504 /* From spec: "if a percent sign is followed by a character
505 * that has no meaning as a format-control character, that
506 * character and the following characters are treated as
507 * an ordinary sequence of characters, that is, a sequence
508 * of characters that must match the input. For example,
509 * to specify that a percent-sign character is to be input,
510 * use %%." */
511 while ((nch!=_EOF_) && _ISSPACE_(nch))
512 nch = _GETC_(file);
513 if (nch==*format) {
514 suppress = 1; /* whoops no field to be read */
515 st = 1; /* but we got what we expected */
516 nch = _GETC_(file);
518 break;
520 if (st && !suppress) rd++;
521 else if (!st) break;
523 /* a non-white-space character causes scanf to read, but not store,
524 * a matching non-white-space character. */
525 else {
526 /* check for character match */
527 if (nch == *format) {
528 nch = _GETC_(file);
529 } else break;
531 format++;
533 if (nch!=_EOF_) {
534 _UNGETC_(nch, file);
536 va_end(ap);
537 TRACE("returning %d\n", rd);
538 return rd;
541 #undef _CHAR_
542 #undef _EOF_
543 #undef _EOF_RET
544 #undef _ISSPACE_
545 #undef _ISDIGIT_
546 #undef _CONVERT_
547 #undef _CHAR2DIGIT_
548 #undef _GETC_
549 #undef _UNGETC_
550 #undef _FUNCTION_