2019-10-09 11:32:30 +00:00
|
|
|
|
|
|
|
/* Implementation of class NSLengthFormatter
|
|
|
|
Copyright (C) 2019 Free Software Foundation, Inc.
|
|
|
|
|
2019-10-29 17:09:37 +00:00
|
|
|
By: Gregory John Casamento <greg.casamento@gmail.com>
|
2019-10-09 11:32:30 +00:00
|
|
|
Date: Tue Oct 8 13:30:33 EDT 2019
|
|
|
|
|
|
|
|
This file is part of the GNUstep Library.
|
|
|
|
|
|
|
|
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 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
|
2019-12-09 23:36:00 +00:00
|
|
|
Lesser General Public License for more details.
|
2019-10-09 11:32:30 +00:00
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free
|
2024-11-07 13:37:59 +00:00
|
|
|
Software Foundation, Inc., 31 Milk Street #960789 Boston, MA 02196 USA.
|
2019-10-09 11:32:30 +00:00
|
|
|
*/
|
|
|
|
|
2019-10-29 17:09:37 +00:00
|
|
|
#import "Foundation/NSLengthFormatter.h"
|
|
|
|
#import "Foundation/NSMeasurement.h"
|
|
|
|
#import "Foundation/NSMeasurementFormatter.h"
|
|
|
|
#import "Foundation/NSNumberFormatter.h"
|
|
|
|
#import "Foundation/NSUnit.h"
|
2019-10-09 11:32:30 +00:00
|
|
|
|
|
|
|
@implementation NSLengthFormatter
|
|
|
|
|
2019-10-24 04:03:40 +00:00
|
|
|
- (instancetype) init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if(self != nil)
|
|
|
|
{
|
|
|
|
_numberFormatter = nil;
|
|
|
|
_unitStyle = NSFormattingUnitStyleMedium;
|
|
|
|
_isForPersonHeightUse = NO;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2019-10-24 04:17:10 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
RELEASE(_numberFormatter);
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2019-10-23 20:03:52 +00:00
|
|
|
- (NSNumberFormatter *) numberFormatter
|
|
|
|
{
|
2019-10-23 20:49:50 +00:00
|
|
|
return _numberFormatter;
|
2019-10-23 20:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setNumberFormatter: (NSNumberFormatter *)formatter
|
|
|
|
{
|
2019-10-23 20:49:50 +00:00
|
|
|
ASSIGN(_numberFormatter, formatter);
|
2019-10-23 20:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSFormattingUnitStyle) unitStyle
|
|
|
|
{
|
2019-10-23 20:49:50 +00:00
|
|
|
return _unitStyle;
|
2019-10-23 20:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setUnitStyle: (NSFormattingUnitStyle)style
|
|
|
|
{
|
2019-10-23 20:49:50 +00:00
|
|
|
_unitStyle = style;
|
2019-10-23 20:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isForPersonHeightUse
|
|
|
|
{
|
2019-10-23 20:49:50 +00:00
|
|
|
return _isForPersonHeightUse;
|
2019-10-23 20:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setForPersonHeightUse: (BOOL)flag
|
|
|
|
{
|
2019-10-23 20:49:50 +00:00
|
|
|
_isForPersonHeightUse = flag;
|
2019-10-23 20:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) stringFromValue: (double)value unit: (NSLengthFormatterUnit)unit
|
|
|
|
{
|
2019-10-24 18:18:47 +00:00
|
|
|
NSUnit *u = nil;
|
|
|
|
NSMeasurement *m = nil;
|
|
|
|
NSMeasurementFormatter *mf = nil;
|
|
|
|
|
|
|
|
switch(unit)
|
|
|
|
{
|
|
|
|
case NSLengthFormatterUnitMillimeter:
|
|
|
|
u = [NSUnitLength millimeters];
|
|
|
|
break;
|
|
|
|
case NSLengthFormatterUnitCentimeter:
|
|
|
|
u = [NSUnitLength centimeters];
|
|
|
|
break;
|
|
|
|
case NSLengthFormatterUnitMeter:
|
|
|
|
u = [NSUnitLength meters];
|
|
|
|
break;
|
|
|
|
case NSLengthFormatterUnitKilometer:
|
|
|
|
u = [NSUnitLength kilometers];
|
|
|
|
break;
|
|
|
|
case NSLengthFormatterUnitInch:
|
|
|
|
u = [NSUnitLength inches];
|
|
|
|
break;
|
|
|
|
case NSLengthFormatterUnitFoot:
|
|
|
|
u = [NSUnitLength feet];
|
|
|
|
break;
|
|
|
|
case NSLengthFormatterUnitYard:
|
|
|
|
u = [NSUnitLength yards];
|
|
|
|
break;
|
|
|
|
case NSLengthFormatterUnitMile:
|
|
|
|
u = [NSUnitLength miles];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
m = [[NSMeasurement alloc] initWithDoubleValue: value
|
|
|
|
unit: u];
|
|
|
|
AUTORELEASE(m);
|
|
|
|
mf = [[NSMeasurementFormatter alloc] init];
|
|
|
|
AUTORELEASE(mf);
|
|
|
|
[mf setUnitStyle: _unitStyle];
|
|
|
|
[mf setNumberFormatter: _numberFormatter];
|
|
|
|
|
|
|
|
return [mf stringFromMeasurement: m];
|
2019-10-23 20:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) stringFromMeters: (double)numberInMeters
|
|
|
|
{
|
2019-10-24 21:06:06 +00:00
|
|
|
return [self stringFromValue: numberInMeters unit: NSLengthFormatterUnitMeter];
|
2019-10-23 20:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) unitStringFromValue: (double)value unit: (NSLengthFormatterUnit)unit
|
|
|
|
{
|
2019-10-24 18:18:47 +00:00
|
|
|
return [self stringFromValue: value unit: unit];
|
2019-10-23 20:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) unitStringFromMeters: (double)numberInMeters usedUnit: (NSLengthFormatterUnit *)unit
|
|
|
|
{
|
2019-10-24 21:06:06 +00:00
|
|
|
*unit = NSLengthFormatterUnitMeter;
|
|
|
|
return [self stringFromValue: numberInMeters unit: *unit];
|
2019-10-23 20:03:52 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 17:09:37 +00:00
|
|
|
- (BOOL) getObjectValue: (id *)obj forString: (NSString *)string errorDescription: (NSString **)error
|
2019-10-23 20:03:52 +00:00
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2019-10-09 11:32:30 +00:00
|
|
|
@end
|
|
|
|
|