]> err.no Git - sope/blob - sope-core/NGExtensions/FdExt.subproj/NSObject+Values.m
renamed packages as discussed in the developer list
[sope] / sope-core / NGExtensions / FdExt.subproj / NSObject+Values.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
6   OGo 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   OGo 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 OGo; 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 // $Id$
22
23 #include "NSObject+Values.h"
24 #include "common.h"
25
26 @implementation NSObject(NGValues)
27
28 - (BOOL)boolValue {
29   // this returns always YES (the id != nil)
30   return YES;
31 }
32
33 - (char)charValue {
34   return (char)[self intValue];
35 }
36 - (unsigned char)unsignedCharValue {
37   return (unsigned char)[self intValue];
38 }
39 - (short)shortValue {
40   return (short)[self intValue];
41 }
42 - (unsigned short)unsignedShortValue {
43   return (unsigned short)[self unsignedIntValue];
44 }
45
46 - (int)intValue {
47   return [[self stringValue] intValue];
48 }
49 - (unsigned int)unsignedIntValue {
50   return (unsigned int)[self intValue];
51 }
52
53 - (long)longValue {
54   return (long)[self intValue];
55 }
56 - (unsigned long)unsignedLongValue {
57   return (unsigned long)[self unsignedIntValue];
58 }
59
60 - (long long)longLongValue {
61   return [[self stringValue] longLongValue];
62 }
63 - (unsigned long long)unsignedLongLongValue {
64   return [[self stringValue] unsignedLongLongValue];
65 }
66
67 - (float)floatValue {
68   return [[self stringValue] floatValue];
69 }
70 - (double)doubleValue {
71   return [[self stringValue] doubleValue];
72 }
73
74 - (NSString *)stringValue {
75   return [self description];
76 }
77
78 @end /* NSObject(Values) */
79
80 @implementation NSString(NGValues)
81
82 - (BOOL)boolValue {
83   unsigned len;
84   unichar  c1;
85
86   if ((len = [self length]) == 0)
87     return NO;
88
89   switch (len) {
90   case 1:
91     c1 = [self characterAtIndex:0];
92     if (c1 == '1') return YES;
93     return NO;
94     
95   case 2:
96     // NO, no (this is false in any case ;-)
97     return NO;
98     
99   case 3:
100     c1 = [self characterAtIndex:0];
101     if (c1 != 'Y' && c1 != 'y')
102       return NO;
103     
104     if ([@"YES" isEqualToString:self]) return YES;
105     if ([@"yes" isEqualToString:self]) return YES;
106     break;
107     
108   case 4:
109     c1 = [self characterAtIndex:0];
110     if (c1 != 'T' && c1 != 't')
111       return NO;
112     
113     if ([@"TRUE" isEqualToString:self]) return YES;
114     if ([@"true" isEqualToString:self]) return YES;
115     break;
116     
117   case 5:
118     // FALSE, false (this is false in any case ;-)
119     return NO;
120   }
121   
122   return NO;
123 }
124
125 - (NSString *)stringValue {
126   return self;
127 }
128
129 - (unsigned char)unsignedCharValue {
130   /*
131     Note: this is a hack to support bool values with KVC operations. Problem 
132           is, that bools in Objective-C have no own type code and the runtime 
133           will use uchar to represent a bool.
134           
135     Note: there are platforms where int as used as the BOOL base type?
136   */
137   register unsigned len;
138   register unichar  c1;
139   
140   if ((len = [self length]) == 0)
141     return 0;
142   
143   c1 = [self characterAtIndex:0];
144   if (!isdigit(c1)) {
145     switch (len) {
146     case 2:
147       // NO, no (this is false in any case ;-)
148       break;
149     case 3:
150       c1 = [self characterAtIndex:0];
151       if (c1 != 'Y' && c1 != 'y')
152         return NO;
153     
154       if ([@"YES" isEqualToString:self]) return YES;
155       if ([@"yes" isEqualToString:self]) return YES;
156       break;
157     case 4:
158       c1 = [self characterAtIndex:0];
159       if (c1 != 'T' && c1 != 't')
160         return NO;
161     
162       if ([@"TRUE" isEqualToString:self]) return YES;
163       if ([@"true" isEqualToString:self]) return YES;
164       break;
165     }
166   }
167   
168   return [self intValue];
169 }
170
171 @end /* NSString(Values) */
172
173 @implementation NSMutableString(NGValues)
174
175 - (NSString *)stringValue {
176   return [[self copy] autorelease];
177 }
178
179 @end /* NSMutableString(Values) */
180
181 void __link_NGExtensions_NSObjectValues(void) {
182   /* required for static linking */
183   __link_NGExtensions_NSObjectValues();
184 }