scanf.h 17.5 KB
Newer Older
1 2 3 4 5 6
/*
 * general implementation of scanf used by scanf, sscanf, fscanf,
 * _cscanf, wscanf, swscanf and fwscanf
 *
 * Copyright 1996,1998 Marcus Meissner
 * Copyright 1996 Jukka Iivonen
7
 * Copyright 1997,2000, 2003 Uwe Bonnes
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * Copyright 2000 Jon Griffiths
 * Copyright 2002 Daniel Gudbjartsson
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifdef WIDE_SCANF
27
#define _CHAR_ MSVCRT_wchar_t
28
#define _EOF_ MSVCRT_WEOF
29
#define _EOF_RET MSVCRT_WEOF
30 31
#define _ISSPACE_(c) MSVCRT_iswspace(c)
#define _ISDIGIT_(c) MSVCRT_iswdigit(c)
32 33
#define _WIDE2SUPPORTED_(c) c /* No conversion needed (wide to wide) */
#define _CHAR2SUPPORTED_(c) c /* FIXME: convert char to wide char */
34
#define _CHAR2DIGIT_(c, base) wchar2digit((c), (base))
35
#define _BITMAPSIZE_ 256*256
36 37 38
#else /* WIDE_SCANF */
#define _CHAR_ char
#define _EOF_ MSVCRT_EOF
39
#define _EOF_RET MSVCRT_EOF
40 41
#define _ISSPACE_(c) isspace(c)
#define _ISDIGIT_(c) isdigit(c)
42 43
#define _WIDE2SUPPORTED_(c) c /* FIXME: convert wide char to char */
#define _CHAR2SUPPORTED_(c) c /* No conversion needed (char to char) */
44
#define _CHAR2DIGIT_(c, base) char2digit((c), (base))
45
#define _BITMAPSIZE_ 256
46 47 48
#endif /* WIDE_SCANF */

#ifdef CONSOLE
49 50
#define _GETC_(file) (consumed++, _getch())
#define _UNGETC_(nch, file) do { _ungetch(nch); consumed--; } while(0)
51
#define _FUNCTION_ static int MSVCRT_vcscanf(const char *format, va_list ap)
52 53
#else
#ifdef STRING
54 55
#undef _EOF_
#define _EOF_ 0
56 57
#define _GETC_(file) (consumed++, *file++)
#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 63 64
#endif /* WIDE_SCANF */
#else /* STRING */
#ifdef WIDE_SCANF
65 66
#define _GETC_(file) (consumed++, MSVCRT_fgetwc(file))
#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 70
#define _GETC_(file) (consumed++, MSVCRT_fgetc(file))
#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 73 74 75 76 77 78 79 80 81
#endif /* WIDE_SCANF */
#endif /* STRING */
#endif /* CONSOLE */

/*********************************************************************
 * Implemented based on
 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt_format_specification_fields_.2d_.scanf_and_wscanf_functions.asp
 * Extended by C. Scott Ananian <cananian@alumni.princeton.edu> to handle
 * more types of format spec.
 */
82
_FUNCTION_ {
83
    int rd = 0, consumed = 0;
84 85 86 87
    int nch;
    if (!*format) return 0;
#ifndef WIDE_SCANF
#ifdef CONSOLE
88
    TRACE("(%s): \n", debugstr_a(format));
89 90
#else /* CONSOLE */
#ifdef STRING
91
    TRACE("%s (%s)\n", file, debugstr_a(format));
92
#else /* STRING */
93
    TRACE("%p (%s)\n", file, debugstr_a(format));
94 95 96 97
#endif /* STRING */
#endif /* CONSOLE */
#endif /* WIDE_SCANF */
    nch = _GETC_(file);
98 99
    if (nch == _EOF_) return _EOF_RET;

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    while (*format) {
	/* a whitespace character in the format string causes scanf to read,
	 * but not store, all consecutive white-space characters in the input
	 * up to the next non-white-space character.  One white space character
	 * in the input matches any number (including zero) and combination of
	 * white-space characters in the input. */
	if (_ISSPACE_(*format)) {
            /* skip whitespace */
            while ((nch!=_EOF_) && _ISSPACE_(nch))
                nch = _GETC_(file);
        }
	/* a format specification causes scanf to read and convert characters
	 * in the input into values of a specified type.  The value is assigned
	 * to an argument in the argument list.  Format specifications have
	 * the form %[*][width][{h | l | I64 | L}]type */
115
        else if (*format == '%') {
116 117 118 119 120 121 122
            int st = 0; int suppress = 0; int width = 0;
	    int base, number_signed;
	    int h_prefix = 0;
	    int l_prefix = 0;
	    int L_prefix = 0;
	    int w_prefix = 0;
	    int prefix_finished = 0;
123
	    int I64_prefix = 0;
124 125 126
            format++;
	    /* look for leading asterisk, which means 'suppress assignment of
	     * this field'. */
127
	    if (*format=='*') {
128 129 130 131 132 133
		format++;
		suppress=1;
	    }
	    /* look for width specification */
	    while (_ISDIGIT_(*format)) {
		width*=10;
134
		width+=*format++ - '0';
135 136 137 138 139
	    }
	    if (width==0) width=-1; /* no width spec seen */
	    /* read prefix (if any) */
	    while (!prefix_finished) {
		switch(*format) {
140 141 142 143 144 145 146
		case 'h': h_prefix = 1; break;
		case 'l': l_prefix = 1; break;
		case 'w': w_prefix = 1; break;
		case 'L': L_prefix = 1; break;
		case 'I':
		    if (*(format + 1) == '6' &&
			*(format + 2) == '4') {
147
			I64_prefix = 1;
148 149 150 151 152 153 154 155 156 157
			format += 2;
		    }
		    break;
		default:
		    prefix_finished = 1;
		}
		if (!prefix_finished) format++;
	    }
	    /* read type */
            switch(*format) {
158 159
	    case 'x':
	    case 'X': /* hexadecimal integer. */
160 161
		base = 16; number_signed = 0;
		goto number;
162
	    case 'o': /* octal integer */
163 164
		base = 8; number_signed = 0;
		goto number;
165
	    case 'u': /* unsigned decimal integer */
166 167
		base = 10; number_signed = 0;
		goto number;
168
	    case 'd': /* signed decimal integer */
169 170
		base = 10; number_signed = 1;
		goto number;
171
	    case 'i': /* generic integer */
172
		base = 10; number_signed = 1;
173 174
	    number: {
		    /* read an integer */
175
		    ULONGLONG cur = 0;
176 177 178 179 180 181
		    int negative = 0;
		    int seendigit=0;
                    /* skip initial whitespace */
                    while ((nch!=_EOF_) && _ISSPACE_(nch))
                        nch = _GETC_(file);
                    /* get sign */
182 183 184
                    if (number_signed && (nch == '-' ||
					  nch == '+')) {
			negative = (nch=='-');
185 186 187 188
                        nch = _GETC_(file);
			if (width>0) width--;
                    }
		    /* look for leading indication of base */
189
		    if (width!=0 && nch == '0') {
190 191 192
                        nch = _GETC_(file);
			if (width>0) width--;
			seendigit=1;
193
			if (width!=0 && (nch=='x' || nch=='X')) {
194 195 196 197 198 199 200 201 202 203 204
			    if (base==0)
				base=16;
			    if (base==16) {
				nch = _GETC_(file);
				if (width>0) width--;
				seendigit=0;
			    }
			} else if (base==0)
			    base = 8;
		    }
		    /* throw away leading zeros */
205
		    while (width!=0 && nch=='0') {
206 207 208 209 210 211 212 213 214 215 216
                        nch = _GETC_(file);
			if (width>0) width--;
			seendigit=1;
		    }
		    if (width!=0 && _CHAR2DIGIT_(nch, base)!=-1) {
			cur = _CHAR2DIGIT_(nch, base);
			nch = _GETC_(file);
			if (width>0) width--;
			seendigit=1;
		    }
                    /* read until no more digits */
217
                    while (width!=0 && (nch!=_EOF_) && _CHAR2DIGIT_(nch, base)!=-1) {
218 219 220 221 222 223 224 225 226 227 228
                        cur = cur*base + _CHAR2DIGIT_(nch, base);
                        nch = _GETC_(file);
			if (width>0) width--;
			seendigit=1;
                    }
		    /* okay, done! */
		    if (!seendigit) break; /* not a valid number */
                    st = 1;
                    if (!suppress) {
#define _SET_NUMBER_(type) *va_arg(ap, type*) = negative ? -cur : cur
			if (number_signed) {
229 230
			    if (I64_prefix) _SET_NUMBER_(LONGLONG);
			    else if (l_prefix) _SET_NUMBER_(long int);
231 232 233 234 235 236 237
			    else if (h_prefix) _SET_NUMBER_(short int);
			    else _SET_NUMBER_(int);
			} else {
			    if (negative) {
				WARN("Dropping sign in reading a negative number into an unsigned value");
				negative = 0;
			    }
238 239
			    if (I64_prefix) _SET_NUMBER_(ULONGLONG);
			    else if (l_prefix) _SET_NUMBER_(unsigned long int);
240 241 242 243 244 245 246
			    else if (h_prefix)
				_SET_NUMBER_(unsigned short int);
			    else _SET_NUMBER_(unsigned int);
			}
		    }
                }
                break;
247 248 249 250 251
	    case 'e':
	    case 'E':
	    case 'f':
	    case 'g':
            case 'G': { /* read a float */
252 253 254 255 256 257
                    long double cur = 0;
		    int negative = 0;
                    /* skip initial whitespace */
                    while ((nch!=_EOF_) && _ISSPACE_(nch))
                        nch = _GETC_(file);
		    /* get sign. */
258 259
                    if (nch == '-' || nch == '+') {
			negative = (nch=='-');
260 261 262 263 264
			if (width>0) width--;
			if (width==0) break;
                        nch = _GETC_(file);
                    }
		    /* get first digit. */
265 266 267 268 269 270 271
		    if ('.' != nch) {
		      if (!_ISDIGIT_(nch)) break;
		      cur = (nch - '0');
		      nch = _GETC_(file);
		      if (width>0) width--;
		      /* read until no more digits */
		      while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
272
                        cur = cur*10 + (nch - '0');
273 274
                        nch = _GETC_(file);
			if (width>0) width--;
275 276 277 278
		      }
		    } else {
		      cur = 0; /* MaxPayneDemo Fix: .8 -> 0.8 */
		    }
279
		    /* handle decimals */
280
                    if (width!=0 && nch == '.') {
281 282 283 284 285
                        float dec = 1;
                        nch = _GETC_(file);
			if (width>0) width--;
                        while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
                            dec /= 10;
286
                            cur += dec * (nch - '0');
287 288 289 290 291
                            nch = _GETC_(file);
			    if (width>0) width--;
                        }
                    }
		    /* handle exponent */
292
		    if (width!=0 && (nch == 'e' || nch == 'E')) {
293 294 295 296 297
			int exponent = 0, negexp = 0;
			float expcnt;
                        nch = _GETC_(file);
			if (width>0) width--;
			/* possible sign on the exponent */
298 299
			if (width!=0 && (nch=='+' || nch=='-')) {
			    negexp = (nch=='-');
300 301 302 303 304 305
                            nch = _GETC_(file);
			    if (width>0) width--;
			}
			/* exponent digits */
			while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
			    exponent *= 10;
306
			    exponent += (nch - '0');
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
                            nch = _GETC_(file);
			    if (width>0) width--;
                        }
			/* update 'cur' with this exponent. */
			expcnt =  negexp ? .1 : 10;
			while (exponent!=0) {
			    if (exponent&1)
				cur*=expcnt;
			    exponent/=2;
			    expcnt=expcnt*expcnt;
			}
		    }
                    st = 1;
                    if (!suppress) {
			if (L_prefix) _SET_NUMBER_(long double);
			else if (l_prefix) _SET_NUMBER_(double);
			else _SET_NUMBER_(float);
		    }
                }
                break;
		/* According to
		 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_scanf_type_field_characters.asp
		 * 's' reads a character string in a call to fscanf
		 * and 'S' a wide character string and vice versa in a
		 * call to fwscanf. The 'h', 'w' and 'l' prefixes override
		 * this behaviour. 'h' forces reading char * but 'l' and 'w'
		 * force reading WCHAR. */
334
	    case 's':
335 336 337 338 339 340 341
		    if (w_prefix || l_prefix) goto widecharstring;
		    else if (h_prefix) goto charstring;
#ifdef WIDE_SCANF
		    else goto widecharstring;
#else /* WIDE_SCANF */
		    else goto charstring;
#endif /* WIDE_SCANF */
342
	    case 'S':
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
		    if (w_prefix || l_prefix) goto widecharstring;
		    else if (h_prefix) goto charstring;
#ifdef WIDE_SCANF
		    else goto charstring;
#else /* WIDE_SCANF */
		    else goto widecharstring;
#endif /* WIDE_SCANF */
	    charstring: { /* read a word into a char */
		    char*str = suppress ? NULL : va_arg(ap, char*);
                    char*sptr = str;
                    /* skip initial whitespace */
                    while ((nch!=_EOF_) && _ISSPACE_(nch))
                        nch = _GETC_(file);
                    /* read until whitespace */
                    while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
358
                        if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
359 360 361 362 363 364 365 366
			st++;
                        nch = _GETC_(file);
			if (width>0) width--;
                    }
                    /* terminate */
                    if (!suppress) *sptr = 0;
                }
                break;
367 368 369 370
	    widecharstring: { /* read a word into a wchar_t* */
		    MSVCRT_wchar_t*str =
			suppress ? NULL : va_arg(ap, MSVCRT_wchar_t*);
                    MSVCRT_wchar_t*sptr = str;
371 372 373 374 375
                    /* skip initial whitespace */
                    while ((nch!=_EOF_) && _ISSPACE_(nch))
                        nch = _GETC_(file);
                    /* read until whitespace */
                    while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
376
                        if (!suppress) *sptr++ = _WIDE2SUPPORTED_(nch);
377 378 379 380 381 382 383 384 385 386
			st++;
                        nch = _GETC_(file);
			if (width>0) width--;
                    }
                    /* terminate */
                    if (!suppress) *sptr = 0;
                }
                break;
            /* 'c' and 'C work analogously to 's' and 'S' as described
	     * above */
387
	    case 'c':
388 389 390 391 392 393 394
		    if (w_prefix || l_prefix) goto widecharacter;
		    else if (h_prefix) goto character;
#ifdef WIDE_SCANF
		    else goto widecharacter;
#else /* WIDE_SCANF */
		    else goto character;
#endif /* WIDE_SCANF */
395
	    case 'C':
396 397 398 399 400 401 402 403 404 405
		    if (w_prefix || l_prefix) goto widecharacter;
		    else if (h_prefix) goto character;
#ifdef WIDE_SCANF
		    else goto character;
#else /* WIDE_SCANF */
		    else goto widecharacter;
#endif /* WIDE_SCANF */
	  character: { /* read single character into char */
		    if (!suppress) {
			char*c = va_arg(ap, char*);
406
			*c = _CHAR2SUPPORTED_(nch);
407
		    }
408
		    st = 1;
409 410 411 412
 		    nch = _GETC_(file);
	        }
		break;
	  widecharacter: {
413 414
		    if (!suppress) { /* read single character into a wchar_t */
			MSVCRT_wchar_t*c = va_arg(ap, MSVCRT_wchar_t*);
415
			*c = _WIDE2SUPPORTED_(nch);
416 417
		    }
		    nch = _GETC_(file);
418
		    st = 1;
419 420
	        }
		break;
421
	    case 'n': {
422 423
 		    if (!suppress) {
			int*n = va_arg(ap, int*);
424
			*n = consumed - (nch!=_EOF_);
425 426 427
		    }
	        }
		break;
428
	    case '[': {
429 430 431 432 433 434 435
                    _CHAR_ *str = suppress ? NULL : va_arg(ap, _CHAR_*);
                    _CHAR_ *sptr = str;
		    RTL_BITMAP bitMask;
                    LPBYTE Mask;
		    int invert = 0; /* Set if we are NOT to find the chars */

		    /* Init our bitmap */
436 437
		    Mask = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, _BITMAPSIZE_/8);
		    RtlInitializeBitMap(&bitMask, Mask, _BITMAPSIZE_);
438 439 440 441 442 443 444 445 446 447 448 449

		    /* Read the format */
		    format++;
		    if(*format == '^') {
			invert = 1;
			format++;
		    }
		    if(*format == ']') {
			RtlSetBits(&bitMask, ']', 1);
			format++;
		    }
                    while(*format && (*format != ']')) {
450 451 452
			/* According to:
			 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt_scanf_width_specification.asp
			 * "Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]." */
453
			if((*format == '-') && (*(format + 1) != ']')) {
454 455 456 457
			    if ((*(format - 1)) < *(format + 1))
				RtlSetBits(&bitMask, *(format - 1) +1 , *(format + 1) - *(format - 1));
			    else
				RtlSetBits(&bitMask, *(format + 1)    , *(format - 1) - *(format + 1));			      
458
			    format++;
459 460
			} else
			    RtlSetBits(&bitMask, *format, 1);
461 462 463 464 465 466
			format++;
		    }
                    /* read until char is not suitable */
                    while ((width != 0) && (nch != _EOF_)) {
			if(!invert) {
			    if(RtlAreBitsSet(&bitMask, nch, 1)) {
467
				if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
468 469 470 471
			    } else
				break;
			} else {
			    if(RtlAreBitsClear(&bitMask, nch, 1)) {
472
				if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
473 474 475 476 477 478 479 480 481 482 483 484
			    } else
				break;
			}
                        st++;
                        nch = _GETC_(file);
                        if (width>0) width--;
                    }
                    /* terminate */
                    if (!suppress) *sptr = 0;
		    HeapFree(GetProcessHeap(), 0, Mask);
                }
                break;
485
            default:
486 487 488 489 490 491
		/* From spec: "if a percent sign is followed by a character
		 * that has no meaning as a format-control character, that
		 * character and the following characters are treated as
		 * an ordinary sequence of characters, that is, a sequence
		 * of characters that must match the input.  For example,
		 * to specify that a percent-sign character is to be input,
492 493 494 495 496 497 498 499 500
		 * use %%." */
                while ((nch!=_EOF_) && _ISSPACE_(nch))
                    nch = _GETC_(file);
                if (nch==*format) {
                    suppress = 1; /* whoops no field to be read */
                    st = 1; /* but we got what we expected */
                    nch = _GETC_(file);
                }
                break;
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
            }
            if (st && !suppress) rd++;
            else if (!st) break;
        }
	/* a non-white-space character causes scanf to read, but not store,
	 * a matching non-white-space character. */
        else {
            /* check for character match */
            if (nch == *format) {
		nch = _GETC_(file);
            } else break;
        }
        format++;
    }
    if (nch!=_EOF_) {
	_UNGETC_(nch, file);
    }
    TRACE("returning %d\n", rd);
    return rd;
}

#undef _CHAR_
#undef _EOF_
Alexandre Julliard's avatar
Alexandre Julliard committed
524
#undef _EOF_RET
525 526
#undef _ISSPACE_
#undef _ISDIGIT_
527 528
#undef _CHAR2SUPPORTED_
#undef _WIDE2SUPPORTED_
529 530 531 532
#undef _CHAR2DIGIT_
#undef _GETC_
#undef _UNGETC_
#undef _FUNCTION_
533
#undef _BITMAPSIZE_