]> err.no Git - sope/blob - sope-core/NGExtensions/FdExt.subproj/NSObject+Values.m
added -isNotEmpty to NSSet
[sope] / sope-core / NGExtensions / FdExt.subproj / NSObject+Values.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include "NSObject+Values.h"
23 #include "common.h"
24
25 @implementation NSObject(NGValues)
26
27 - (BOOL)boolValue {
28   // this returns always YES (the id != nil)
29   return YES;
30 }
31
32 - (char)charValue {
33   return (char)[self intValue];
34 }
35 - (unsigned char)unsignedCharValue {
36   return (unsigned char)[self intValue];
37 }
38 - (short)shortValue {
39   return (short)[self intValue];
40 }
41 - (unsigned short)unsignedShortValue {
42   return (unsigned short)[self unsignedIntValue];
43 }
44
45 - (int)intValue {
46   return [[self stringValue] intValue];
47 }
48 - (unsigned int)unsignedIntValue {
49   return (unsigned int)[self intValue];
50 }
51
52 - (long)longValue {
53   return (long)[self intValue];
54 }
55 - (unsigned long)unsignedLongValue {
56   return (unsigned long)[self unsignedIntValue];
57 }
58
59 - (long long)longLongValue {
60   return [[self stringValue] longLongValue];
61 }
62 - (unsigned long long)unsignedLongLongValue {
63   return [[self stringValue] unsignedLongLongValue];
64 }
65
66 - (float)floatValue {
67   return [[self stringValue] floatValue];
68 }
69 - (double)doubleValue {
70   return [[self stringValue] doubleValue];
71 }
72
73 - (NSString *)stringValue {
74   return [self description];
75 }
76
77 @end /* NSObject(Values) */
78
79 @implementation NSString(NGValues)
80
81 - (BOOL)boolValue {
82   unsigned len;
83   unichar  c1;
84
85   if ((len = [self length]) == 0)
86     return NO;
87
88   switch (len) {
89   case 1:
90     c1 = [self characterAtIndex:0];
91     if (c1 == '1') return YES;
92     return NO;
93     
94   case 2:
95     // NO, no (this is false in any case ;-)
96     return NO;
97     
98   case 3:
99     c1 = [self characterAtIndex:0];
100     if (c1 != 'Y' && c1 != 'y')
101       return NO;
102     
103     if ([@"YES" isEqualToString:self]) return YES;
104     if ([@"yes" isEqualToString:self]) return YES;
105     break;
106     
107   case 4:
108     c1 = [self characterAtIndex:0];
109     if (c1 != 'T' && c1 != 't')
110       return NO;
111     
112     if ([@"TRUE" isEqualToString:self]) return YES;
113     if ([@"true" isEqualToString:self]) return YES;
114     break;
115     
116   case 5:
117     // FALSE, false (this is false in any case ;-)
118     return NO;
119   }
120   
121   return NO;
122 }
123
124 - (NSString *)stringValue {
125   return self;
126 }
127
128 - (unsigned char)unsignedCharValue {
129   /*
130     Note: this is a hack to support bool values with KVC operations. Problem 
131           is, that bools in Objective-C have no own type code and the runtime 
132           will use uchar to represent a bool.
133           
134     Note: there are platforms where int as used as the BOOL base type?
135   */
136   register unsigned len;
137   register unichar  c1;
138   
139   if ((len = [self length]) == 0)
140     return 0;
141   
142   c1 = [self characterAtIndex:0];
143   if (!isdigit(c1)) {
144     switch (len) {
145     case 2:
146       // NO, no (this is false in any case ;-)
147       break;
148     case 3:
149       c1 = [self characterAtIndex:0];
150       if (c1 != 'Y' && c1 != 'y')
151         return NO;
152     
153       if ([@"YES" isEqualToString:self]) return YES;
154       if ([@"yes" isEqualToString:self]) return YES;
155       break;
156     case 4:
157       c1 = [self characterAtIndex:0];
158       if (c1 != 'T' && c1 != 't')
159         return NO;
160     
161       if ([@"TRUE" isEqualToString:self]) return YES;
162       if ([@"true" isEqualToString:self]) return YES;
163       break;
164     }
165   }
166   
167   return [self intValue];
168 }
169
170 @end /* NSString(Values) */
171
172 @implementation NSMutableString(NGValues)
173
174 - (NSString *)stringValue {
175   return [[self copy] autorelease];
176 }
177
178 @end /* NSMutableString(Values) */
179
180 void __link_NGExtensions_NSObjectValues(void) {
181   /* required for static linking */
182   __link_NGExtensions_NSObjectValues();
183 }