Minor defaults/property list improvements.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@28345 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2009-06-12 09:14:28 +00:00
parent 6addb6b049
commit 9d2d7c5bbf
3 changed files with 54 additions and 8 deletions

View file

@ -2,6 +2,10 @@
* Source/NSDistributedNotificationCenter.m: add explicit size casts
to avoid compiler warnings.
* Source/NSUserDefaults.m: Save defaults in xml format and add some
error checking.
* Source/NSPropertyList.m: tolerate NSNumber values used as dictionary
keys, by using their string representation.
2009-06-09 Richard Frith-Macdonald <rfm@gnu.org>

View file

@ -1996,8 +1996,15 @@ OAppend(id obj, NSDictionary *loc, unsigned lev, unsigned step,
{
if ([keys[i] isKindOfClass: NSStringClass] == NO)
{
[NSException raise: NSInvalidArgumentException
format: @"Bad key in property list: '%@'", keys[i]];
if ([keys[i] isKindOfClass: NSNumberClass] == YES)
{
keys[i] = [keys[i] description];
}
else
{
[NSException raise: NSInvalidArgumentException
format: @"Bad key in property list: '%@'", keys[i]];
}
}
}
}

View file

@ -46,6 +46,7 @@
#include "Foundation/NSNotification.h"
#include "Foundation/NSPathUtilities.h"
#include "Foundation/NSProcessInfo.h"
#include "Foundation/NSPropertyList.h"
#include "Foundation/NSRunLoop.h"
#include "Foundation/NSSet.h"
#include "Foundation/NSThread.h"
@ -134,6 +135,42 @@ static void updateCache(NSUserDefaults *self)
}
}
static BOOL
writeDictionary(NSDictionary *dict, NSString *file)
{
if (dict == nil)
{
NSLog(@"Defaults database is nil when writing");
}
else if ([file length] == 0)
{
NSLog(@"Defaults database filename is empty when writing");
}
else
{
NSData *data;
NSString *err;
err = nil;
data = [NSPropertyListSerialization dataFromPropertyList: dict
format: NSPropertyListXMLFormat_v1_0
errorDescription: &err];
if (data == nil)
{
NSLog(@"Failed to serialize defaults database for writing: %@", err);
}
else if ([data writeToFile: file atomically: YES] == NO)
{
NSLog(@"Failed to write defaults database to file: %@", file);
}
else
{
return YES;
}
}
return NO;
}
/*************************************************************************
*** Local method definitions
*************************************************************************/
@ -1431,16 +1468,17 @@ static BOOL isLocked = NO;
uint32_t attributes;
/*
* If the lock did not exist ... make sure the databsase exists.
* If the lock did not exist ... make sure the database exists.
*/
if ([mgr isReadableFileAtPath: _defaultsDatabase] == NO)
{
NSDictionary *empty = [NSDictionary new];
NSLog(@"Creating empty user defaults database");
/*
* Create empty database.
*/
if ([empty writeToFile: _defaultsDatabase atomically: NO] == NO)
if (writeDictionary(empty, _defaultsDatabase) == NO)
{
NSLog(@"Failed to create defaults database file %@",
_defaultsDatabase);
@ -1522,10 +1560,7 @@ static BOOL isLocked = NO;
// Save the changes if we have an external database file
if (_fileLock != nil)
{
if ([defaults writeToFile: _defaultsDatabase atomically: YES] == NO)
{
return NO;
}
return writeDictionary(defaults, _defaultsDatabase);
}
return YES;
}