]> err.no Git - sope/blob - sope-core/NGExtensions/FdExt.subproj/NSNull+misc.m
improved -isNotEmpty
[sope] / sope-core / NGExtensions / FdExt.subproj / NSNull+misc.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 "NSNull+misc.h"
23 #include "common.h"
24
25 #if LIB_FOUNDATION_LIBRARY || GNUSTEP_BASE_LIBRARY
26 #  include <objc/objc-api.h>
27 #  include <objc/objc.h>
28 #  include <objc/encoding.h>
29 #  ifndef GNUSTEP
30 #    import <extensions/objc-runtime.h>
31 #  endif
32 #else
33 #  import <objc/objc-class.h>
34 #endif
35
36 @implementation NSNull(misc)
37
38 static int _doAbort = -1;
39 static inline BOOL doAbort(void) {
40   if (_doAbort == -1) {
41     _doAbort = [[NSUserDefaults standardUserDefaults]
42                                 boolForKey:@"NSNullAbortOnMessage"] ? 1 : 0;
43   }
44   return _doAbort ? YES : NO;
45 }
46
47 - (BOOL)isNotNull {
48   return NO;
49 }
50 - (BOOL)isNotEmpty {
51   return NO;
52 }
53 - (BOOL)isNull {
54 #if DEBUG
55   NSLog(@"WARNING(%s): called deprecated -isNull on NSNull (use -isNotNull) !",
56         __PRETTY_FUNCTION__);
57   if (doAbort()) abort();
58 #endif
59   return YES;
60 }
61
62 - (NSString *)stringValue {
63 #if DEBUG && 0
64   NSLog(@"WARNING(%s): "
65         @"NSNull -stringValue returns empty string.",
66         __PRETTY_FUNCTION__);
67   if (doAbort()) abort();
68 #endif
69   return @"";
70 }
71 - (double)doubleValue {
72 #if DEBUG && 0
73   NSLog(@"WARNING(%s): "
74         @"NSNull -doubleValue returns 0.0.",
75         __PRETTY_FUNCTION__);
76   if (doAbort()) abort();
77 #endif
78   return 0.0;
79 }
80
81 - (unsigned int)length {
82 #if DEBUG
83   NSLog(@"WARNING(%s): "
84         @"called NSNull -length (returns 0) !!!",
85         __PRETTY_FUNCTION__);
86   if (doAbort()) abort();
87 #endif
88   return 0;
89 }
90 - (unsigned int)count {
91 #if DEBUG
92   NSLog(@"WARNING(%s): "
93         @"called NSNull -count (returns 0) !!!",
94         __PRETTY_FUNCTION__);
95   if (doAbort()) abort();
96 #endif
97   return 0;
98 }
99
100 - (BOOL)isEqualToString:(NSString *)_s {
101   /* Note: I think we can keep this as a regular method */
102   return _s == (id)self || _s == nil ? YES : NO;
103 }
104 - (BOOL)hasPrefix:(NSString *)_s {
105   /* Note: I think we can keep this as a regular method */
106   return _s == (id)self || _s == nil ? YES : NO;
107 }
108 - (BOOL)hasSuffix:(NSString *)_s {
109   /* Note: I think we can keep this as a regular method */
110   return _s == (id)self || _s == nil ? YES : NO;
111 }
112
113 - (unichar)characterAtIndex:(unsigned int)_idx {
114 #if DEBUG
115   NSLog(@"WARNING(%s): "
116         @"called NSNull -characterAtIndex:%d - returning 0!",
117         __PRETTY_FUNCTION__, _idx);
118   if (doAbort()) abort();
119 #endif
120   return 0;
121 }
122
123 /* key-value coding */
124
125 - (void)takeValue:(id)_value forKey:(NSString *)_key {
126   /* do nothing */
127 }
128 - (id)valueForKey:(NSString *)_key {
129   if ([_key isEqualToString:@"isNotNull"]) {
130     static NSNumber *noNum  = nil;
131     
132     if (noNum == nil)
133       noNum = [NSNumber numberWithBool:NO];
134     
135     return noNum;
136   }
137   if ([_key isEqualToString:@"isNull"]) {
138     static NSNumber *yesNum = nil;
139     
140     if (yesNum == nil)
141       yesNum = [NSNumber numberWithBool:YES];
142     return yesNum;
143   }
144   
145   /* do nothing */
146   return nil;
147 }
148
149 /* forwarding ... */
150
151 #if !GNU_RUNTIME
152 - (BOOL)respondsToSelector:(SEL)_sel {
153   /* fake that we have a selector */
154   return YES;
155 }
156
157 - (NSString *)descriptionWithLocale:(id)_locale indent:(int)_indent {
158   return @"<null>";
159 }
160 - (NSString *)descriptionWithLocale:(id)_locale {
161   return @"<null>";
162 }
163
164 #endif
165
166 - (void)forwardInvocation:(NSInvocation *)_invocation {
167   NSMethodSignature *sig;
168   
169   NSLog(@"ERROR(%s): called selector %@ on NSNull !",
170         __PRETTY_FUNCTION__,
171         NSStringFromSelector([_invocation selector]));
172   if (doAbort()) abort();
173   
174   if ((sig = [_invocation methodSignature])) {
175     const char *ret;
176     
177     if ((ret = [sig methodReturnType])) {
178       switch (*ret) {
179         case _C_INT: {
180           int v = 0;
181           [_invocation setReturnValue:&v];
182           break;
183         }
184         case _C_UINT: {
185           unsigned int v = 0;
186           [_invocation setReturnValue:&v];
187           break;
188         }
189           
190         case _C_ID:
191         case _C_CLASS: {
192           id v = nil;
193           [_invocation setReturnValue:&v];
194           break;
195         }
196         
197         default:
198           NSLog(@"  didn't set return value for type '%s'", ret);
199           break;
200       }
201     }
202     else
203       NSLog(@"  no method return type in signature %@", sig);
204   }
205   else
206     NSLog(@"  no method signature in invocation %@", _invocation);
207 }
208
209 @end /* NSNull(misc) */
210
211
212 @implementation NSObject(NSNullMisc)
213
214 - (BOOL)isNotNull {
215   return YES;
216 }
217 - (BOOL)isNotEmpty {
218   return [self isNotNull];
219 }
220
221 - (BOOL)isNull {
222 #if DEBUG
223   NSLog(@"%s: WARNING, called -isNull on NSObject (use -isNotNull) !",
224         __PRETTY_FUNCTION__);
225 #endif
226   return NO;
227 }
228
229 @end /* NSObject(NSNullMisc) */
230
231
232 @implementation NSString(NSNullMisc)
233
234 - (BOOL)isNotEmpty {
235   unsigned i, len;
236   
237   if ((len = [self length]) == 0)
238     return NO;
239   
240   for (i = 0; i < len; i++) {
241     if (!isspace([self characterAtIndex:i]))
242       return YES;
243   }
244   
245   return NO;
246 }
247
248 @end /* NSString(NSNullMisc) */
249
250 @implementation NSArray(NSNullMisc)
251
252 - (BOOL)isNotEmpty {
253   return [self count] == 0 ? YES : NO;
254 }
255
256 @end /* NSArray(NSNullMisc) */
257
258 @implementation NSDictionary(NSNullMisc)
259
260 - (BOOL)isNotEmpty {
261   return [self count] == 0 ? YES : NO;
262 }
263
264 @end /* NSDictionary(NSNullMisc) */