root/trunk/cocoa/mogenerator/FoundationAdditions.m

Revision 146, 8.4 kB (checked in by rentzsch, 2 years ago)

[NEW] mogenerator. TODO: momc invocation.

Line 
1 /* FoundationAdditions.m created by lindberg on Mon 20-Dec-1999 */
2 /*-
3 * Copyright (c) 2002 Carl Lindberg and Mike Gentry
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION, HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 /* NOTE: Do not put any code dealing with anything other than Foundation in here.
29  * This class is used by TGen also. So has to be pure Foundation.
30  */
31
32 #import "FoundationAdditions.h"
33 #import <Foundation/Foundation.h>
34 #import "NSString+MiscAdditions.h"
35
36
37 /* Writes to stderr */
38 void ErrVPrintf(NSString *format, va_list arguments)
39 {
40     NSString *logString = [[NSString alloc] initWithFormat:format arguments:arguments];
41     fwrite([logString cString], 1, [logString cStringLength], stderr);
42     if (![logString hasSuffix:@"\n"]) fputc('\n', stdout);
43     [logString release];
44 }
45
46 void ErrPrintf(NSString *format, ...)
47 {
48     va_list arguments;
49
50     va_start(arguments, format);
51     ErrVPrintf(format, arguments);
52     va_end(arguments);
53 }
54
55 void VPrintf(NSString *format, va_list arguments)
56 {
57     NSString *logString = [[NSString alloc] initWithFormat:format arguments:arguments];
58     fwrite([logString cString], 1, [logString cStringLength], stdout);
59     if (![logString hasSuffix:@"\n"]) fputc('\n', stdout);
60     [logString release];
61 }
62
63 void Printf(NSString *format, ...)
64 {
65     va_list arguments;
66
67     va_start(arguments, format);
68     VPrintf(format, arguments);
69     va_end(arguments);
70 }
71
72
73 @interface NSObject (WarningAvoidance)
74 - (NSString *)name;  // method on EOAttribute/EOEntity etc.
75 - (id)valueForKey:(NSString *)key;
76 - (id)valueForKeyPath:(NSString *)keyPath;
77 @end
78
79 @implementation NSArray (FoundationAdditions)
80
81 - (NSArray *)reversedArray
82 {
83     return [[self reverseObjectEnumerator] allObjects];
84 }
85
86 // does an @reversedArray KeyValue key
87 - (id)computeReversedArrayForKey:(NSString *) key
88 {
89     if ([key length] == 0)
90         return [self reversedArray];
91     else
92         return [[self reversedArray] valueForKeyPath:key];
93 }
94
95 static int sortByName(id obj1, id obj2, void *context)
96 {
97     return [(NSString *)[obj1 valueForKey:@"name"] compare:[obj2 valueForKey:@"name"]];
98 }
99
100 // does an @sortedNameArray KeyValue key
101 - (id)computeSortedNameArrayForKey:(NSString *)key
102 {
103     if ([key length] == 0)
104         return [self sortedArrayUsingFunction:sortByName context:NULL];
105     else
106         return [[self sortedArrayUsingFunction:sortByName context:NULL] valueForKeyPath:key];
107 }
108
109 // does an @sortedStringArray KeyValue key
110 - (id)computeSortedStringArrayForKey:(NSString *)key
111 {
112     if ([key length] == 0)
113         return [self sortedArrayUsingSelector:@selector(compare:)];
114     else
115         return [[self sortedArrayUsingSelector:@selector(compare:)] valueForKeyPath:key];
116 }
117
118 /*"
119  * Calls -#performSelector: on each object in the receiver, and returns an
120  * array of the return values from the method calls.  If a result is nil,
121  * it is not added to the array.
122 "*/
123 - (NSArray *)arrayByMakingObjectsPerformSelector:(SEL)aSelector
124 {
125     return [self arrayByMakingObjectsPerformSelector:aSelector withObject:nil withObject:nil];
126 }
127
128 /*"
129  * Calls -#performSelector:withObject: on each object in the receiver, and
130  * returns an array of the return values from the method calls.  If a
131  * result is nil, it is not added to the array.
132 "*/
133 - (NSArray *)arrayByMakingObjectsPerformSelector:(SEL)aSelector withObject:anObject
134 {
135     return [self arrayByMakingObjectsPerformSelector:aSelector withObject:anObject withObject:nil];
136 }
137
138 /*"
139  * Calls -#performSelector:withObject:withObject: on each object in the
140  * receiver, and returns an array of the return values from the method
141  * calls.  If a result is nil, it is not added to the array.
142 "*/
143 - (NSArray *)arrayByMakingObjectsPerformSelector:(SEL)aSelector withObject:anObject
144                                       withObject:anObject2
145 {
146     unsigned            i, count = [self count];
147     NSMutableArray      *array = [NSMutableArray arrayWithCapacity:count];
148
149     for(i=0; i<count; i++)
150     {
151         id object = [self objectAtIndex:i];
152         id value = [object performSelector:aSelector withObject:anObject withObject:anObject2];
153
154         if (value != nil) {
155             [array addObject:value];
156         }
157     }
158
159     return array;
160 }
161
162 @end
163
164 @implementation NSString (FoundationAdditions)
165
166 - (NSString *)initialCapitalString
167 {
168     NSRange  firstLetterRange;
169     NSString *firstLetterString;
170     NSString *restOfString;
171
172     if ([self length] == 0) return self;
173
174     firstLetterRange  = [self rangeOfComposedCharacterSequenceAtIndex:0];
175     firstLetterString = [[self substringWithRange:firstLetterRange] uppercaseString];
176     restOfString      = [self substringFromIndex:NSMaxRange(firstLetterRange)];
177
178     return [firstLetterString stringByAppendingString:restOfString];
179 }
180
181 - (NSString *)beautifyString
182 {
183     NSString *newString;
184     NSCharacterSet *invalidSet;
185     NSMutableCharacterSet *validSet = [[[NSCharacterSet letterCharacterSet] mutableCopy] autorelease];
186     [validSet formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];
187     [validSet formUnionWithCharacterSet:[NSCharacterSet decimalDigitCharacterSet]];
188     invalidSet = [[[validSet copy] autorelease] invertedSet];
189
190     newString = [self stringByReplacingEveryOccurrenceOfCharactersFromSet:invalidSet withString:@" "];
191
192     if ( [newString rangeOfString:@" "].location != NSNotFound ) {
193         newString = [newString stringByTrimmingWhitespace];
194         newString = [newString stringBySquashingWhitespace];
195
196         if ( [newString rangeOfString:@" "].location != NSNotFound ) {
197             NSArray *components = [newString componentsSeparatedByString:@" "];
198             components = [components arrayByMakingObjectsPerformSelector:@selector(initialCapitalString)];
199             newString = [components componentsJoinedByString:@""];
200         }
201     }
202
203     return [newString initialCapitalString];
204 }
205
206 @end
207
208 @implementation NSFileManager (FoundationAdditions)
209
210 - (BOOL)directoryExistsAtPath:(NSString *)path
211 {
212     BOOL isDir = NO;
213     BOOL isFile = [self fileExistsAtPath:path isDirectory:&isDir];
214     return (isFile && isDir);
215 }
216
217 - (BOOL)regularFileExistsAtPath:(NSString *)path
218 {
219     BOOL isDir = NO;
220     BOOL isFile = [self fileExistsAtPath:path isDirectory:&isDir];
221     return (isFile && !isDir);
222 }
223
224 - (NSString *)findFile:(NSString *)filename inSearchPath:(NSArray *)paths
225 {
226     int i, count = [paths count];
227
228     for (i=0; i<count; i++)
229     {
230         NSString *currPath = [paths objectAtIndex:i];
231         NSString *fullPath = [currPath stringByAppendingPathComponent:filename];
232
233         if ([self regularFileExistsAtPath:fullPath])
234             return fullPath;
235     }
236
237     return nil;
238 }
239
240 - (void)touchPath:(NSString *)filePath
241 {
242     NSMutableDictionary *attributes;
243
244     attributes = [[NSMutableDictionary alloc] initWithCapacity:1];
245     [attributes setObject:[NSDate date] forKey:NSFileModificationDate];
246     [self changeFileAttributes:attributes atPath:filePath];
247     [attributes release];
248 }
249
250 - (BOOL)deepCreateDirectoryAtPath:(NSString *)path attributes:(NSDictionary *)attributes
251 {
252     NSString *parent = [path stringByDeletingLastPathComponent];
253
254     if ([parent length] > 0 && ![self directoryExistsAtPath:parent])
255     {
256         // May want to ensure owner write permissions here...
257         if (![self deepCreateDirectoryAtPath:parent attributes:attributes])
258             return NO;
259     }
260
261     return [self createDirectoryAtPath:path attributes:attributes];
262 }
263
264 @end
Note: See TracBrowser for help on using the browser.