Oolite
Loading...
Searching...
No Matches
NSUserDefaults+Override.m
Go to the documentation of this file.
1/*
2
3NSUserDefaults+Override.m
4
5Oolite
6Copyright (C) 2004-2025 Giles C Williams and contributors
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21MA 02110-1301, USA.
22
23*/
24
25
27#import <Foundation/NSData.h>
28#import <Foundation/NSFileManager.h>
29#import <Foundation/NSPropertyList.h>
30
31@implementation NSUserDefaults (Override)
32
33- (BOOL) writeDictionary: (NSDictionary*)dict
34 toFile: (NSString*)file
35{
36 if ([file length] == 0)
37 {
38 OOLog(@"NSUserDefaultsOverride", @"%@", @"Defaults database filename is empty when writing");
39 }
40 else if (nil == dict)
41 {
42 NSFileManager *mgr = [NSFileManager defaultManager];
43
44 return [mgr removeFileAtPath: file handler: nil];
45 }
46 else
47 {
48 NSData *data;
49 NSString *err;
50
51 err = nil;
52 data = [NSPropertyListSerialization dataFromPropertyList: dict
53 format: NSPropertyListOpenStepFormat
54 errorDescription: &err];
55 if (data == nil)
56 {
57 OOLog(@"NSUserDefaultsOverride", @"Failed to serialize defaults database for writing: %@", err);
58 }
59 else if ([data writeToFile: file atomically: YES] == NO)
60 {
61 OOLog(@"NSUserDefaultsOverride", @"Failed to write defaults database to file: %@", file);
62 }
63 else
64 {
65 return YES;
66 }
67 }
68
69 return NO;
70}
71
72@end
#define OOLog(class, format,...)
Definition OOLogging.h:88
return nil