Skip to content
Newer
Older
100644 104 lines (85 sloc) 2.94 KB
31159de @jdelStrother Initial checkin
authored Oct 15, 2006
1 //
2 // HumanDiskSizeTransformer.m
15a086d @jdelStrother Adding licensing crap
authored May 28, 2008
3 // NanoFibre
31159de @jdelStrother Initial checkin
authored Oct 15, 2006
4 //
5 // Created by Jonathan del Strother on 08/10/2006.
15a086d @jdelStrother Adding licensing crap
authored May 28, 2008
6 //
7 // Copyright (c) 2006 Jonathan del Strother
8 //
9 // Permission is hereby granted, free of charge, to any person
10 // obtaining a copy of this software and associated documentation
11 // files (the "Software"), to deal in the Software without
12 // restriction, including without limitation the rights to use,
13 // copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following
16 // conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 // OTHER DEALINGS IN THE SOFTWARE.
9535069 @jdelStrother A few improvements to make it fit for public consumption. Proper daem…
authored Oct 22, 2006
29 // Copyright 2006. All rights reserved.
31159de @jdelStrother Initial checkin
authored Oct 15, 2006
30 //
31
32 #import "HumanDiskSizeTransformer.h"
33
34
35 @implementation HumanDiskSizeTransformer
36
37 +(Class)transformedValueClass;
38 {
39 return [NSNumber class];
40 }
41
42 +(BOOL)allowsReverseTransformation;
43 {
44 return YES;
45 }
46
47
48 -(id)reverseTransformedValue:(id)value; // NSString -> NSNumber
49 {
50 float size;
51
52 if (value == nil) return nil;
53
54 // Attempt to get a reasonable value from the
55 // value object.
56 if ([value isKindOfClass:[NSString class]])
57 {
58 NSString* strSize = [[[value lowercaseString]
59 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
60 stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"s"]];
61
62 if ([strSize hasSuffix:@"megabyte"]||[strSize hasSuffix:@"mb"])
63 size = [value floatValue]/1024;
64 else //Assume we want gigabytes
65 size = [value floatValue];
66 }
67 else
68 {
69 [NSException raise: NSInternalInconsistencyException
70 format: @"Value (%@) needs to be a string", value];
71 }
72
73 id returnValue = [NSNumber numberWithFloat:size];
74
75 return returnValue;
76 }
77
78
79 -(id)transformedValue:(id)value // NSNumber -> NSString
80 {
81 float size;
82
83 if (value == nil) return nil;
84
85 // Attempt to get a reasonable value from the
86 // value object.
87 if ([value respondsToSelector: @selector(floatValue)]) {
88 size = [value floatValue];
89 } else {
90 [NSException raise: NSInternalInconsistencyException
91 format: @"Value (%@) does not respond to -floatValue.",
92 [value class]];
93 }
94
95 id returnValue;
96 if (size < 0.5)
97 returnValue = [NSString stringWithFormat:@"%.0fMB", size*1024];
98 else
99 returnValue = [NSString stringWithFormat:@"%0.3gGB", size];
9535069 @jdelStrother A few improvements to make it fit for public consumption. Proper daem…
authored Oct 22, 2006
100
31159de @jdelStrother Initial checkin
authored Oct 15, 2006
101 return returnValue;
102 }
103
104 @end
Something went wrong with that request. Please try again.