
#import "MinMaxFormat.h"

@implementation MinMaxFormat

- initWithMin:(int)min andMax:(int)max
{
    [super init];

    minval = min;
    maxval = max;

    return self;
}


- (BOOL)getObjectValue:(id *)obj forString:(NSString *)string errorDescription:(NSString  **)error
{
    int intres;
    NSScanner *scanner;
    NSString *errstr;
    BOOL retval = NO;

    scanner = [NSScanner scannerWithString:string];
    if([scanner scanInt:&intres] && [scanner isAtEnd]){
	if(minval <= intres && intres <= maxval){
	    retval = YES;

	    if(obj!=NULL){
		*obj = [NSNumber numberWithInt:intres];
	    }
	} 
	else{
	    errstr =
		[NSString stringWithFormat:_(@"Value %d out of range (%d-%d)"),
			  intres, minval, maxval];
	}
    }
    else{
	errstr = [NSString stringWithFormat:_(@"Couldn't convert %@ to a number."),
			   string];
    }

    if(error!=NULL){
	*error = errstr;
    }
    return retval;
}


- (NSString *)stringForObjectValue:(id)anObject
{
    if(![anObject isKindOfClass:[NSNumber class]]){
        return nil;
    }

    return [NSString stringWithFormat:@"%d", [anObject intValue]];
}
@end
