b27eb5626aa9df61575698bfdfb1158e32e5119d
[reactos.git] / reactos / 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 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 nch = _GETC_(file);
92 if (nch == _EOF_) return _EOF_RET;
93
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 == '%') {
110 int st = 0; int suppress = 0; int width = 0;
111 int base;
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=='*') {
122 format++;
123 suppress=1;
124 }
125 /* look for width specification */
126 while (_ISDIGIT_(*format)) {
127 width*=10;
128 width+=*format++ - '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 'h': h_prefix++; break;
135 case 'l':
136 if(*(format+1) == 'l') {
137 I64_prefix = 1;
138 format++;
139 }
140 l_prefix = 1;
141 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;
149 }
150 break;
151 default:
152 prefix_finished = 1;
153 }
154 if (!prefix_finished) format++;
155 }
156 /* read type */
157 switch(*format) {
158 case 'p':
159 case 'P': /* pointer. */
160 if (sizeof(void *) == sizeof(LONGLONG)) I64_prefix = 1;
161 /* fall through */
162 case 'x':
163 case 'X': /* hexadecimal integer. */
164 base = 16;
165 goto number;
166 case 'o': /* octal integer */
167 base = 8;
168 goto number;
169 case 'u': /* unsigned decimal integer */
170 base = 10;
171 goto number;
172 case 'd': /* signed decimal integer */
173 base = 10;
174 goto number;
175 case 'i': /* generic integer */
176 base = 0;
177 number: {
178 /* read an integer */
179 __int64 cur = 0;
180 int negative = 0;
181 int seendigit=0;
182 /* skip initial whitespace */
183 while ((nch!=_EOF_) && _ISSPACE_(nch))
184 nch = _GETC_(file);
185 /* get sign */
186 if (nch == '-' || nch == '+') {
187 negative = (nch=='-');
188 nch = _GETC_(file);
189 if (width>0) width--;
190 }
191 /* look for leading indication of base */
192 if (width!=0 && nch == '0' && *format != 'p' && *format != 'P') {
193 nch = _GETC_(file);
194 if (width>0) width--;
195 seendigit=1;
196 if (width!=0 && (nch=='x' || nch=='X')) {
197 if (base==0)
198 base=16;
199 if (base==16) {
200 nch = _GETC_(file);
201 if (width>0) width--;
202 seendigit=0;
203 }
204 } else if (base==0)
205 base = 8;
206 }
207 /* format %i without indication of base */
208 if (base==0)
209 base = 10;
210 /* throw away leading zeros */
211 while (width!=0 && nch=='0') {
212 nch = _GETC_(file);
213 if (width>0) width--;
214 seendigit=1;
215 }
216 if (width!=0 && _CHAR2DIGIT_(nch, base)!=-1) {
217 cur = _CHAR2DIGIT_(nch, base);
218 nch = _GETC_(file);
219 if (width>0) width--;
220 seendigit=1;
221 }
222 /* read until no more digits */
223 while (width!=0 && (nch!=_EOF_) && _CHAR2DIGIT_(nch, base)!=-1) {
224 cur = cur*base + _CHAR2DIGIT_(nch, base);
225 nch = _GETC_(file);
226 if (width>0) width--;
227 seendigit=1;
228 }
229 /* okay, done! */
230 if (!seendigit) break; /* not a valid number */
231 st = 1;
232 if (!suppress) {
233 #define _SET_NUMBER_(type) *va_arg(ap, type*) = (type)(negative ? -cur : cur)
234 if (I64_prefix) _SET_NUMBER_(LONGLONG);
235 else if (l_prefix) _SET_NUMBER_(LONG);
236 else if (h_prefix == 1) _SET_NUMBER_(short int);
237 else _SET_NUMBER_(int);
238 }
239 }
240 break;
241 case 'e':
242 case 'E':
243 case 'f':
244 case 'g':
245 case 'G': { /* read a float */
246 long double cur = 0;
247 int negative = 0;
248 /* skip initial whitespace */
249 while ((nch!=_EOF_) && _ISSPACE_(nch))
250 nch = _GETC_(file);
251 /* get sign. */
252 if (nch == '-' || nch == '+') {
253 negative = (nch=='-');
254 if (width>0) width--;
255 if (width==0) break;
256 nch = _GETC_(file);
257 }
258 /* get first digit. */
259 if ('.' != nch) {
260 if (!_ISDIGIT_(nch)) break;
261 cur = (nch - '0');
262 nch = _GETC_(file);
263 if (width>0) width--;
264 /* read until no more digits */
265 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
266 cur = cur*10 + (nch - '0');
267 nch = _GETC_(file);
268 if (width>0) width--;
269 }
270 } else {
271 cur = 0; /* MaxPayneDemo Fix: .8 -> 0.8 */
272 }
273 /* handle decimals */
274 if (width!=0 && nch == '.') {
275 long double dec = 1;
276 nch = _GETC_(file);
277 if (width>0) width--;
278 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
279 dec /= 10;
280 cur += dec * (nch - '0');
281 nch = _GETC_(file);
282 if (width>0) width--;
283 }
284 }
285 /* handle exponent */
286 if (width!=0 && (nch == 'e' || nch == 'E')) {
287 int exponent = 0, negexp = 0;
288 double expcnt, shift;
289 nch = _GETC_(file);
290 if (width>0) width--;
291 /* possible sign on the exponent */
292 if (width!=0 && (nch=='+' || nch=='-')) {
293 negexp = (nch=='-');
294 nch = _GETC_(file);
295 if (width>0) width--;
296 }
297 /* exponent digits */
298 while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
299 exponent *= 10;
300 exponent += (nch - '0');
301 nch = _GETC_(file);
302 if (width>0) width--;
303 }
304 /* update 'cur' with this exponent. */
305 expcnt = 10;
306 shift = 1.0;
307 while (exponent!=0) {
308 if (exponent&1)
309 shift *= expcnt;
310 exponent/=2;
311 expcnt=expcnt*expcnt;
312 }
313 cur = (negexp ? cur / shift : cur * shift);
314 }
315 st = 1;
316 if (!suppress) {
317 if (L_prefix) _SET_NUMBER_(long double);
318 else if (l_prefix) _SET_NUMBER_(double);
319 else _SET_NUMBER_(float);
320 }
321 }
322 break;
323 /* According to msdn,
324 * 's' reads a character string in a call to fscanf
325 * and 'S' a wide character string and vice versa in a
326 * call to fwscanf. The 'h', 'w' and 'l' prefixes override
327 * this behaviour. 'h' forces reading char * but 'l' and 'w'
328 * force reading WCHAR. */
329 case 's':
330 if (w_prefix || l_prefix) goto widecharstring;
331 else if (h_prefix) goto charstring;
332 #ifdef WIDE_SCANF
333 else goto widecharstring;
334 #else /* WIDE_SCANF */
335 else goto charstring;
336 #endif /* WIDE_SCANF */
337 case 'S':
338 if (w_prefix || l_prefix) goto widecharstring;
339 else if (h_prefix) goto charstring;
340 #ifdef WIDE_SCANF
341 else goto charstring;
342 #else /* WIDE_SCANF */
343 else goto widecharstring;
344 #endif /* WIDE_SCANF */
345 charstring: { /* read a word into a char */
346 char *sptr = suppress ? NULL : va_arg(ap, char*);
347 /* skip initial whitespace */
348 while ((nch!=_EOF_) && _ISSPACE_(nch))
349 nch = _GETC_(file);
350 /* read until whitespace */
351 while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
352 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
353 st++;
354 nch = _GETC_(file);
355 if (width>0) width--;
356 }
357 /* terminate */
358 if (!suppress) *sptr = 0;
359 }
360 break;
361 widecharstring: { /* read a word into a wchar_t* */
362 wchar_t *sptr = suppress ? NULL : va_arg(ap, wchar_t*);
363 /* skip initial whitespace */
364 while ((nch!=_EOF_) && _ISSPACE_(nch))
365 nch = _GETC_(file);
366 /* read until whitespace */
367 while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
368 if (!suppress) *sptr++ = _WIDE2SUPPORTED_(nch);
369 st++;
370 nch = _GETC_(file);
371 if (width>0) width--;
372 }
373 /* terminate */
374 if (!suppress) *sptr = 0;
375 }
376 break;
377 /* 'c' and 'C work analogously to 's' and 'S' as described
378 * above */
379 case 'c':
380 if (w_prefix || l_prefix) goto widecharacter;
381 else if (h_prefix) goto character;
382 #ifdef WIDE_SCANF
383 else goto widecharacter;
384 #else /* WIDE_SCANF */
385 else goto character;
386 #endif /* WIDE_SCANF */
387 case 'C':
388 if (w_prefix || l_prefix) goto widecharacter;
389 else if (h_prefix) goto character;
390 #ifdef WIDE_SCANF
391 else goto character;
392 #else /* WIDE_SCANF */
393 else goto widecharacter;
394 #endif /* WIDE_SCANF */
395 character: { /* read single character into char */
396 char *str = suppress ? NULL : va_arg(ap, char*);
397 if (width == -1) width = 1;
398 while ((width != 0) && (nch != _EOF_))
399 {
400 if (!suppress) *str++ = _CHAR2SUPPORTED_(nch);
401 st++;
402 width--;
403 nch = _GETC_(file);
404 }
405 }
406 break;
407 widecharacter: { /* read single character into a wchar_t */
408 wchar_t *str = suppress ? NULL : va_arg(ap, wchar_t*);
409 if (width == -1) width = 1;
410 while ((width != 0) && (nch != _EOF_))
411 {
412 if (!suppress) *str++ = _WIDE2SUPPORTED_(nch);
413 st++;
414 width--;
415 nch = _GETC_(file);
416 }
417 }
418 break;
419 case 'n': {
420 if (!suppress) {
421 int*n = va_arg(ap, int*);
422 *n = consumed - 1;
423 }
424 /* This is an odd one: according to the standard,
425 * "Execution of a %n directive does not increment the
426 * assignment count returned at the completion of
427 * execution" even if it wasn't suppressed with the
428 * '*' flag. The Corrigendum to the standard seems
429 * to contradict this (comment out the assignment to
430 * suppress below if you want to implement these
431 * alternate semantics) but the windows program I'm
432 * looking at expects the behavior I've coded here
433 * (which happens to be what glibc does as well).
434 */
435 suppress = 1;
436 st = 1;
437 }
438 break;
439 case '[': {
440 _CHAR_ *str = suppress ? NULL : va_arg(ap, _CHAR_*);
441 _CHAR_ *sptr = str;
442 RTL_BITMAP bitMask;
443 ULONG *Mask;
444 int invert = 0; /* Set if we are NOT to find the chars */
445
446 /* Init our bitmap */
447 #ifdef _LIBCNT_
448 Mask = RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, _BITMAPSIZE_/8);
449 #else
450 Mask = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, _BITMAPSIZE_/8);
451 #endif
452 RtlInitializeBitMap(&bitMask, Mask, _BITMAPSIZE_);
453
454 /* Read the format */
455 format++;
456 if(*format == '^') {
457 invert = 1;
458 format++;
459 }
460 if(*format == ']') {
461 RtlSetBits(&bitMask, ']', 1);
462 format++;
463 }
464 while(*format && (*format != ']')) {
465 /* According to msdn:
466 * "Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]." */
467 if((*format == '-') && (*(format + 1) != ']')) {
468 if ((*(format - 1)) < *(format + 1))
469 RtlSetBits(&bitMask, *(format - 1) +1 , *(format + 1) - *(format - 1));
470 else
471 RtlSetBits(&bitMask, *(format + 1) , *(format - 1) - *(format + 1));
472 format++;
473 } else
474 RtlSetBits(&bitMask, *format, 1);
475 format++;
476 }
477 /* read until char is not suitable */
478 while ((width != 0) && (nch != _EOF_)) {
479 if(!invert) {
480 if(RtlAreBitsSet(&bitMask, nch, 1)) {
481 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
482 } else
483 break;
484 } else {
485 if(RtlAreBitsClear(&bitMask, nch, 1)) {
486 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
487 } else
488 break;
489 }
490 st++;
491 nch = _GETC_(file);
492 if (width>0) width--;
493 }
494 /* terminate */
495 if (!suppress) *sptr = 0;
496 #ifdef _LIBCNT_
497 RtlFreeHeap(RtlGetProcessHeap(), 0, Mask);
498 #else
499 HeapFree(GetProcessHeap(), 0, Mask);
500 #endif
501 }
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);
517 }
518 break;
519 }
520 if (st && !suppress) rd++;
521 else if (!st) break;
522 }
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;
530 }
531 format++;
532 }
533 if (nch!=_EOF_) {
534 _UNGETC_(nch, file);
535 }
536
537 TRACE("returning %d\n", rd);
538 return rd;
539 }
540
541 #undef _CHAR_
542 #undef _EOF_
543 #undef _EOF_RET
544 #undef _ISSPACE_
545 #undef _ISDIGIT_
546 #undef _CHAR2SUPPORTED_
547 #undef _WIDE2SUPPORTED_
548 #undef _CHAR2DIGIT_
549 #undef _GETC_
550 #undef _UNGETC_
551 #undef _FUNCTION_
552 #undef _BITMAPSIZE_