Oolite 1.91.0.7745-260117-205bce7
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#if OOLITE_MODERN_BUILD
32
33@implementation NSUserDefaults (Override)
34
35- (BOOL) writeDictionary: (NSDictionary*)dict
36 toFile: (NSString*)file
37{
38 if ([file length] == 0)
39 {
40 OOLog(@"NSUserDefaultsOverride", @"%@", @"Defaults database filename is empty when writing");
41 }
42 else if (nil == dict)
43 {
44 NSFileManager *mgr = [NSFileManager defaultManager];
45
46 return [mgr removeFileAtPath: file handler: nil];
47 }
48 else
49 {
50 NSData *data;
51 NSString *err;
52
53 err = nil;
54 data = [NSPropertyListSerialization dataFromPropertyList: dict
55 format: NSPropertyListOpenStepFormat
56 errorDescription: &err];
57 if (data == nil)
58 {
59 OOLog(@"NSUserDefaultsOverride", @"Failed to serialize defaults database for writing: %@", err);
60 }
61 else if ([data writeToFile: file atomically: YES] == NO)
62 {
63 OOLog(@"NSUserDefaultsOverride", @"Failed to write defaults database to file: %@", file);
64 }
65 else
66 {
67 return YES;
68 }
69 }
70
71 return NO;
72}
73
74@end
75
76#endif // OOLITE_MODERN_BUILD
#define OOLog(class, format,...)
Definition OOLogging.h:88
return nil