diff --git a/ChangeLog b/ChangeLog index 9534f2b20..81eeb115c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2011-10-02 Richard Frith-Macdonald + + * Headers/Foundation/NSFileManager.h + * Source/NSFileManager.m + * Tests/base/NSFileManager/general.m + * Headers/Foundation/NSUserDefaults.h + * Source/NSUserDefaults.m + * Tests/base/NSUserDefaults/basic.m + Add a few newer OSX methods for compatibility. + 2011-10-01 Niels Grewe * Source/NSXMLNode.m: Properly implement -initWithKind: initializer. diff --git a/Headers/Foundation/NSFileManager.h b/Headers/Foundation/NSFileManager.h index 733e29279..10915f342 100644 --- a/Headers/Foundation/NSFileManager.h +++ b/Headers/Foundation/NSFileManager.h @@ -234,6 +234,18 @@ typedef uint32_t OSType; - (NSData*) contentsAtPath: (NSString*)path; - (BOOL) contentsEqualAtPath: (NSString*)path1 andPath: (NSString*)path2; + +#if OS_API_VERSION(MAC_OS_X_VERSION_10_5, GS_API_LATEST) +/** + * Returns an array of the contents of the specified directory.
+ * The listing does not recursively list subdirectories.
+ * The special files '.' and '..' are not listed.
+ * Indicates an error by returning nil (eg. if path is not a directory or + * it can't be read for some reason). + */ +- (NSArray*) contentsOfDirectoryAtPath: (NSString*)path error: (NSError**)error; +#endif + - (BOOL) copyPath: (NSString*)source toPath: (NSString*)destination handler: (id)handler; diff --git a/Headers/Foundation/NSUserDefaults.h b/Headers/Foundation/NSUserDefaults.h index 8768ee3d7..6cd770177 100644 --- a/Headers/Foundation/NSUserDefaults.h +++ b/Headers/Foundation/NSUserDefaults.h @@ -314,6 +314,14 @@ GS_EXPORT NSString* const GSLocale; */ - (NSDictionary*) dictionaryForKey: (NSString*)defaultName; +#if OS_API_VERSION(MAC_OS_X_VERSION_10_5, GS_API_LATEST) +/** + * Looks up a value for a specified default using -objectForKey: + * and checks that it is a double. Returns 0.0 if it is not. + */ +- (double) doubleForKey: (NSString*)defaultName; +#endif + /** * Looks up a value for a specified default using -objectForKey: * and checks that it is a float. Returns 0.0 if it is not. @@ -380,6 +388,15 @@ GS_EXPORT NSString* const GSLocale; */ - (void) setBool: (BOOL)value forKey: (NSString*)defaultName; +#if OS_API_VERSION(MAC_OS_X_VERSION_10_5, GS_API_LATEST) +/** + * Sets a double value for defaultName in the application domain.
+ * Calls -setObject:forKey: to make the change by storing a double + * [NSNumber] instance. + */ +- (void) setDouble: (double)value forKey: (NSString*)defaultName; +#endif + /** * Sets a float value for defaultName in the application domain.
* Calls -setObject:forKey: to make the change by storing a float diff --git a/Source/NSFileManager.m b/Source/NSFileManager.m index a64690d5a..3f2d1a885 100644 --- a/Source/NSFileManager.m +++ b/Source/NSFileManager.m @@ -691,6 +691,11 @@ static NSStringEncoding defaultEncoding; } } +- (NSArray*) contentsOfDirectoryAtPath: (NSString*)path error: (NSError**)error +{ + return [self directoryContentsAtPath: path]; +} + /** * Creates a new directory and all intermediate directories * if flag is YES, creates only the last directory in the path diff --git a/Source/NSUserDefaults.m b/Source/NSUserDefaults.m index d8de51db3..891068c61 100644 --- a/Source/NSUserDefaults.m +++ b/Source/NSUserDefaults.m @@ -1063,6 +1063,18 @@ newLanguages(NSArray *oldNames) return nil; } +- (double) doubleForKey: (NSString*)defaultName +{ + id obj = [self objectForKey: defaultName]; + + if (obj != nil && ([obj isKindOfClass: NSStringClass] + || [obj isKindOfClass: NSNumberClass])) + { + return [obj doubleValue]; + } + return 0.0; +} + - (float) floatForKey: (NSString*)defaultName { id obj = [self objectForKey: defaultName]; @@ -1177,6 +1189,13 @@ newLanguages(NSArray *oldNames) } } +- (void) setDouble: (double)value forKey: (NSString*)defaultName +{ + NSNumber *n = [NSNumberClass numberWithDouble: value]; + + [self setObject: n forKey: defaultName]; +} + - (void) setFloat: (float)value forKey: (NSString*)defaultName { NSNumber *n = [NSNumberClass numberWithFloat: value]; @@ -1272,7 +1291,11 @@ static BOOL isPlistObject(id o) NS_DURING { obj = [_persDomains objectForKey: processName]; - if ([obj isKindOfClass: NSMutableDictionaryClass] == YES) + if (nil == obj) + { + dict = [NSMutableDictionary new]; + } + else if ([obj isKindOfClass: NSMutableDictionaryClass] == YES) { dict = obj; } diff --git a/Tests/base/NSFileManager/general.m b/Tests/base/NSFileManager/general.m index 22507cc85..457e3d931 100644 --- a/Tests/base/NSFileManager/general.m +++ b/Tests/base/NSFileManager/general.m @@ -10,8 +10,12 @@ int main() NSAutoreleasePool *arp = [NSAutoreleasePool new]; NSFileManager *mgr = [NSFileManager defaultManager]; NSString *dir = @"NSFileManagerTestDir"; - NSString *dirInDir = [@"TestDirectory" stringByAppendingPathComponent: @"WithinDirectory"]; + NSString *dirInDir; NSString *str1,*str2; + + dirInDir + = [@"TestDirectory" stringByAppendingPathComponent: @"WithinDirectory"]; + PASS(mgr != nil && [mgr isKindOfClass: [NSFileManager class]], "NSFileManager understands +defaultManager"); @@ -63,6 +67,14 @@ NSLog(@"'%@', '%@'", NSUserName(), [attr fileOwnerAccountName]); "NSFileManager creates a file"); PASS([mgr fileExistsAtPath: @"NSFMFile"],"-fileExistsAtPath: agrees"); + { + NSArray *a; + + a = [mgr contentsOfDirectoryAtPath: @"." error: 0]; + PASS(1 == [a count] && [[a lastObject] isEqual: @"NSFMFile"], + "-contentsOfDirectoryAtPath: agrees"); + } + { NSData *dat1 = [mgr contentsAtPath: @"NSFMFile"]; str2 = [[NSString alloc] initWithData: dat1 encoding: 1]; diff --git a/Tests/base/NSFileManager/unrepresentable_filenames.m b/Tests/base/NSFileManager/unrepresentable_filenames.m index 5599c9fe0..d4efba457 100644 --- a/Tests/base/NSFileManager/unrepresentable_filenames.m +++ b/Tests/base/NSFileManager/unrepresentable_filenames.m @@ -10,31 +10,32 @@ copyright 2004 Alexander Malmberg int main(int argc, char **argv) { - NSAutoreleasePool *arp = [NSAutoreleasePool new]; - NSFileManager *fm=[NSFileManager defaultManager]; - NSArray *files; - int i; - BOOL e,d; + NSAutoreleasePool *arp = [NSAutoreleasePool new]; + NSFileManager *fm = [NSFileManager defaultManager]; + NSArray *files; + int i; + BOOL e, d; - files=[fm directoryContentsAtPath: @"."]; - printf("%lu files\n", (unsigned long)[files count]); - for (i=0;i<[files count];i++) - { - int j; - NSString *f=[files objectAtIndex: i]; - e=[fm fileExistsAtPath: f - isDirectory: &d]; - printf("%5i: %i %i %s\n",i,e,d, [f lossyCString]); - for (j=0;j<[f length];j++) - printf(" %3i: %04x\n",j,[f characterAtIndex: j]); - } + files = [fm directoryContentsAtPath: @"."]; + printf("%lu files\n", (unsigned long)[files count]); + for (i = 0; i < [files count]; i++) + { + int j; + NSString *f = [files objectAtIndex: i]; -/* const char *test="hallå.txt"; - NSString *s=[NSString stringWithCString: test]; - printf("s=%s\n", [s lossyCString]);*/ + e = [fm fileExistsAtPath: f + isDirectory: &d]; + printf("%5i: %i %i %s\n",i,e,d, [f lossyCString]); + for (j = 0; j < [f length]; j++) + printf(" %3i: %04x\n",j,[f characterAtIndex: j]); + } - [arp release]; arp = nil; + /* const char *test="hallå.txt"; + NSString *s=[NSString stringWithCString: test]; + printf("s=%s\n", [s lossyCString]);*/ - return 0; + [arp release]; arp = nil; + + return 0; } diff --git a/Tests/base/NSUserDefaults/basic.m b/Tests/base/NSUserDefaults/basic.m index 7ebfae67a..5a46806a4 100644 --- a/Tests/base/NSUserDefaults/basic.m +++ b/Tests/base/NSUserDefaults/basic.m @@ -4,11 +4,15 @@ int main() { - NSAutoreleasePool *arp = [NSAutoreleasePool new]; - NSArray *testObj = [NSUserDefaults new]; + NSAutoreleasePool *arp = [NSAutoreleasePool new]; + NSUserDefaults *defs = [NSUserDefaults new]; - test_NSObject(@"NSUserDefaults", [NSArray arrayWithObject:testObj]); + test_NSObject(@"NSUserDefaults", [NSArray arrayWithObject: defs]); + defs = [NSUserDefaults standardUserDefaults]; + [defs setDouble: (double)42.42 forKey: @"aDouble"]; + PASS(EQ((double)42.42, [defs doubleForKey: @"aDouble"]), + "can store double"); [arp release]; arp = nil; return 0; }