/* 
   scanInt.def

   Copyright (C) 1995, 1996 Ovidiu Predescu and Mircea Oancea.
   All rights reserved.

   Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>

   This file is part of libFoundation.

   Permission to use, copy, modify, and distribute this software and its
   documentation for any purpose and without fee is hereby granted, provided
   that the above copyright notice appear in all copies and that both that
   copyright notice and this permission notice appear in supporting
   documentation.

   We disclaim all warranties with regard to this software, including all
   implied warranties of merchantability and fitness, in no event shall
   we be liable for any special, indirect or consequential damages or any
   damages whatsoever resulting from loss of use, data or profits, whether in
   an action of contract, negligence or other tortious action, arising out of
   or in connection with the use or performance of this software.
*/

/* This file is included by NSScanner.m to produce the definitions of
   -scanInt: and -scanLongLong: methods. In order to work, either INT_TYPE or
   LONG_LONG_TYPE should be defined. */

#if defined(INT_TYPE)
# define TYPE int
# define MIN_VALUE INT_MIN
# define MAX_VALUE INT_MAX
#elif defined(LONG_LONG_TYPE)
# define TYPE long long
# define MIN_VALUE LONG_LONG_MIN
# define MAX_VALUE LONG_LONG_MAX
#else
# error You should define INT_TYPE or LONG_LONG_TYPE
#endif

    NSCharacterSet* decimals = nil;
    id string = [self string];
    unsigned int orig;
    unsigned int location;
    unsigned int length = [string length];
    unsigned TYPE maxTruncValue = MAX_VALUE / 10;
    unsigned TYPE maxvalue = MAX_VALUE;
    unsigned TYPE result = 0;
    BOOL isNegative = NO;
    BOOL exceedsMaximumValue = NO;
    NSDictionary* locale = [self locale];
    unichar zeroChar = '0';
    unichar thousandSep = ',';
    unichar c;

    if ([self isAtEnd])
	return NO;

    /* First skip the blank characters */
    [self scanCharactersFromSet:[self charactersToBeSkipped] intoString:NULL];

    /* Create the decimals set */
    if (locale) {
	NSString* thousandString = [locale objectForKey:NSThousandsSeparator];
	NSString* decimalsString = [locale objectForKey:NSDecimalDigits];

	thousandString = [locale objectForKey:NSThousandsSeparator];

	if (decimalsString) {
	    zeroChar = [decimalsString characterAtIndex:0];
	    decimals = [NSCharacterSet characterSetWithCharactersInString:
				    decimalsString];
	}

	if (thousandString)
	    thousandSep = [thousandString characterAtIndex:0];
    }

    if (!decimals)
	decimals = [NSCharacterSet decimalDigitCharacterSet];

    orig = [self scanLocation];
    c = [string characterAtIndex:orig];
    if (c == '-' || c == '+') {
	isNegative = (c == '-');
	orig++;
    }

    for (location = orig; location < length; location++) {
	c = [string characterAtIndex:location];
	if ([decimals characterIsMember:c]) {
	    /* Even if result is greater than the maximum value that can be 
	       represented, continue the scanning to skip the hole number. */
	    if (result <= maxTruncValue && !exceedsMaximumValue) {
		result = result * 10 + (c - zeroChar);
		if (result > maxvalue)
		    exceedsMaximumValue = YES;
	    }
	    else
		exceedsMaximumValue = YES;
	    continue;
	}
	if (c == thousandSep)
	    continue;

	/* If `c' is neither a decimal nor a thousand separator, break. */
	break;
    }

    if (location != orig) {
	[self setScanLocation:location];

	if (exceedsMaximumValue) {
	    if (value)
		*value = (isNegative ? MIN_VALUE : MAX_VALUE);
	    return NO;
	}
	else {
	    if (value)
		*value = (isNegative ? -result : result);
	    return YES;
	}
    }

    return NO;

#undef TYPE
#undef MIN_VALUE
#undef MAX_VALUE
