| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
#import "FoundationAdditions.h" |
|---|
| 33 |
#import <Foundation/Foundation.h> |
|---|
| 34 |
#import "NSString+MiscAdditions.h" |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
- (NSArray *)arrayByMakingObjectsPerformSelector:(SEL)aSelector |
|---|
| 124 |
{ |
|---|
| 125 |
return [self arrayByMakingObjectsPerformSelector:aSelector withObject:nil withObject:nil]; |
|---|
| 126 |
} |
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
- (NSArray *)arrayByMakingObjectsPerformSelector:(SEL)aSelector withObject:anObject |
|---|
| 134 |
{ |
|---|
| 135 |
return [self arrayByMakingObjectsPerformSelector:aSelector withObject:anObject withObject:nil]; |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 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 |
|
|---|
| 257 |
if (![self deepCreateDirectoryAtPath:parent attributes:attributes]) |
|---|
| 258 |
return NO; |
|---|
| 259 |
} |
|---|
| 260 |
|
|---|
| 261 |
return [self createDirectoryAtPath:path attributes:attributes]; |
|---|
| 262 |
} |
|---|
| 263 |
|
|---|
| 264 |
@end |
|---|