Line data Source code
1 0 : /* 2 : 3 : NSUserDefaults+Override.m 4 : 5 : Oolite 6 : Copyright (C) 2004-2025 Giles C Williams and contributors 7 : 8 : This program is free software; you can redistribute it and/or 9 : modify it under the terms of the GNU General Public License 10 : as published by the Free Software Foundation; either version 2 11 : of the License, or (at your option) any later version. 12 : 13 : This program is distributed in the hope that it will be useful, 14 : but WITHOUT ANY WARRANTY; without even the implied warranty of 15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 : GNU General Public License for more details. 17 : 18 : You should have received a copy of the GNU General Public License 19 : along with this program; if not, write to the Free Software 20 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 21 : MA 02110-1301, USA. 22 : 23 : */ 24 : 25 : 26 : #import "NSUserDefaults+Override.h" 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