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