|
Wired Foundation
2.0
A foundation framework for the Wired implementation on Mac OS X
|
00001 /* $Id$ */ 00002 00003 /* 00004 * Copyright (c) 2009 Axel Andersson 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 1. Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * 2. Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in the 00014 * documentation and/or other materials provided with the distribution. 00015 * 00016 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00017 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00018 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00019 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 00020 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00021 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00022 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00023 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 00024 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00025 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00026 * POSSIBILITY OF SUCH DAMAGE. 00027 */ 00028 00029 #import <WiredFoundation/WISizeFormatter.h> 00030 00031 @implementation WISizeFormatter 00032 00033 - (id)init { 00034 self = [super init]; 00035 00036 if(self) { 00037 _numberFormatter = [[NSNumberFormatter alloc] init]; 00038 [_numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4]; 00039 [_numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; 00040 [_numberFormatter setMaximumFractionDigits:1]; 00041 00042 _rawNumberFormatter = [[NSNumberFormatter alloc] init]; 00043 [_rawNumberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4]; 00044 [_rawNumberFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; 00045 } 00046 00047 return self; 00048 } 00049 00050 00051 00052 - (void)dealloc { 00053 [_numberFormatter release]; 00054 [_rawNumberFormatter release]; 00055 00056 [super dealloc]; 00057 } 00058 00059 00060 00061 #pragma mark - 00062 00063 - (void)setSizeStyle:(WISizeFormatterStyle)sizeStyle { 00064 _sizeStyle = sizeStyle; 00065 } 00066 00067 00068 00069 - (WISizeFormatterStyle)sizeStyle { 00070 return _sizeStyle; 00071 } 00072 00073 00074 00075 - (void)setAppendsRawNumber:(BOOL)appendsRawNumber { 00076 _appendsRawNumber = appendsRawNumber; 00077 } 00078 00079 00080 00081 - (BOOL)appendsRawNumber { 00082 return _appendsRawNumber; 00083 } 00084 00085 00086 00087 #pragma mark - 00088 00089 - (NSString *)stringFromSize:(unsigned long long)size { 00090 NSString *string, *unit; 00091 double kb, mb, gb, tb, pb; 00092 00093 kb = (double) size / 1024.0; 00094 mb = (double) kb / 1024.0; 00095 gb = (double) mb / 1024.0; 00096 tb = (double) gb / 1024.0; 00097 pb = (double) tb / 1024.0; 00098 00099 if(size < 1000) { 00100 if([self sizeStyle] == WISizeFormatterStyleBytes) { 00101 if(size == 1) 00102 unit = WILS(@"byte", @"WISizeFormatter: byte size strings"); 00103 else 00104 unit = WILS(@"bytes", @"WISizeFormatter: byte size strings"); 00105 } else { 00106 if(size == 1) 00107 unit = WILS(@"bit", @"WISizeFormatter: bit size strings"); 00108 else 00109 unit = WILS(@"bits", @"WISizeFormatter: bit size strings"); 00110 } 00111 00112 string = [NSSWF:@"%llu %@", size, unit]; 00113 } 00114 else if(kb < 1000.0) { 00115 if([self sizeStyle] == WISizeFormatterStyleBytes) 00116 unit = WILS(@"KB", @"WISizeFormatter: byte size strings"); 00117 else 00118 unit = WILS(@"Kbit", @"WISizeFormatter: byte size strings"); 00119 00120 string = [NSSWF:@"%@ %@", [_numberFormatter stringFromNumber:[NSNumber numberWithDouble:kb]], unit]; 00121 } 00122 else if(mb < 1000.0) { 00123 if([self sizeStyle] == WISizeFormatterStyleBytes) 00124 unit = WILS(@"MB", @"WISizeFormatter: byte size strings"); 00125 else 00126 unit = WILS(@"Mbit", @"WISizeFormatter: byte size strings"); 00127 00128 string = [NSSWF:@"%@ %@", [_numberFormatter stringFromNumber:[NSNumber numberWithDouble:mb]], unit]; 00129 } 00130 else if(gb < 1000.0) { 00131 if([self sizeStyle] == WISizeFormatterStyleBytes) 00132 unit = WILS(@"GB", @"WISizeFormatter: byte size strings"); 00133 else 00134 unit = WILS(@"Gbit", @"WISizeFormatter: byte size strings"); 00135 00136 string = [NSSWF:@"%@ %@", [_numberFormatter stringFromNumber:[NSNumber numberWithDouble:gb]], unit]; 00137 } 00138 else if(tb < 1000.0) { 00139 if([self sizeStyle] == WISizeFormatterStyleBytes) 00140 unit = WILS(@"TB", @"WISizeFormatter: byte size strings"); 00141 else 00142 unit = WILS(@"Tbit", @"WISizeFormatter: byte size strings"); 00143 00144 string = [NSSWF:@"%@ %@", [_numberFormatter stringFromNumber:[NSNumber numberWithDouble:tb]], unit]; 00145 } 00146 else { 00147 if([self sizeStyle] == WISizeFormatterStyleBytes) 00148 unit = WILS(@"PB", @"WISizeFormatter: byte size strings"); 00149 else 00150 unit = WILS(@"Pbit", @"WISizeFormatter: byte size strings"); 00151 00152 string = [NSSWF:@"%@ %@", [_numberFormatter stringFromNumber:[NSNumber numberWithDouble:pb]], unit]; 00153 } 00154 00155 if([self appendsRawNumber]) { 00156 if([self sizeStyle] == WISizeFormatterStyleBytes) { 00157 if(size == 1) 00158 unit = WILS(@"byte", @"WISizeFormatter: byte size strings"); 00159 else 00160 unit = WILS(@"bytes", @"WISizeFormatter: byte size strings"); 00161 } else { 00162 if(size == 1) 00163 unit = WILS(@"bit", @"WISizeFormatter: bit size strings"); 00164 else 00165 unit = WILS(@"bits", @"WISizeFormatter: bit size strings"); 00166 } 00167 00168 string = [string stringByAppendingFormat:@" (%@ %@)", 00169 [_rawNumberFormatter stringFromNumber:[NSNumber numberWithUnsignedLongLong:size]], 00170 unit]; 00171 } 00172 00173 return string; 00174 } 00175 00176 00177 00178 - (NSString *)stringFromNumber:(NSNumber *)number { 00179 return [self stringFromSize:[number unsignedLongLongValue]]; 00180 } 00181 00182 00183 00184 - (NSString *)stringForObjectValue:(id)object { 00185 return [self stringFromSize:[object unsignedLongLongValue]]; 00186 } 00187 00188 @end