Oolite
Loading...
Searching...
No Matches
OOPListParsing.m
Go to the documentation of this file.
1/*
2
3OOPListParsing.m
4
5Oolite
6Copyright (C) 2004-2013 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#import "OOPListParsing.h"
27#import "OOLogging.h"
28#import "OOStringParsing.h"
29#import "Universe.h"
30#include <ctype.h>
31#include <string.h>
32
33#if !OOLITE_GNUSTEP
34#define NO_DYNAMIC_PLIST_DTD_CHANGE
35#endif
36
37static NSString* const kOOLogPListFoundationParseError = @"plist.parse.failed";
38static NSString* const kOOLogPListWrongType = @"plist.wrongType";
39static NSString* const kOOLogPListInformation = @"plist.information";
40
41#ifndef NO_DYNAMIC_PLIST_DTD_CHANGE
42static NSData* ChangeDTDIfApplicable(NSData* data);
43#endif
44
45static NSData* CopyDataFromFile(NSString* path);
46static id ValueIfClass(id value, Class class);
47
48id OOPropertyListFromData(NSData* data, NSString* whereFrom)
49{
50 id result = nil;
51 NSString* error = nil;
52
53 if (whereFrom == nil)
54 whereFrom = @"<data in memory>";
55 if (UNIVERSE != nil) {
56 OOLog(kOOLogPListInformation, @"Parsing %@ as a property list.", whereFrom);
57 }
58
59 if (data != nil) {
60#ifndef NO_DYNAMIC_PLIST_DTD_CHANGE
61 data = ChangeDTDIfApplicable(data);
62#endif
63
64 result = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListImmutable format:NULL errorDescription:&error];
65 if (result == nil) // Foundation parser failed
66 {
67#if OOLITE_RELEASE_PLIST_ERROR_STRINGS
68 [error autorelease];
69#endif
70 // Ensure we can say something sensible...
71 if (error == nil)
72 error = @"<no error message>";
73 if (whereFrom == nil)
74 whereFrom = @"<data in memory>";
75
76 OOLog(kOOLogPListFoundationParseError, @"Failed to parse %@ as a property list.\n%@", whereFrom, error);
77 }
78 }
79
80 return result;
81}
82
83id OOPropertyListFromFile(NSString* path)
84{
85 id result = nil;
86 NSData* data = nil;
87
88 if (path != nil) {
89 // Load file, if it exists...
90 data = CopyDataFromFile(path);
91 if (data != nil) {
92 // ...and parse it
93 result = OOPropertyListFromData(data, path);
94 [data release];
95 }
96 // Non-existent file is not an error.
97 }
98
99 return result;
100}
101
102#ifndef NO_DYNAMIC_PLIST_DTD_CHANGE
103static NSData* ChangeDTDIfApplicable(NSData* data)
104{
105 const uint8_t* bytes = NULL;
106 uint8_t* newBytes = NULL;
107 size_t length,
108 newLength,
109 offset = 0,
110 newOffset = 0;
111 const char xmlDeclLine[] = "<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>";
112 const char* appleDTDLines[] = {
113 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">",
114 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">",
115 NULL
116 };
117 const char gstepDTDLine[] = "<!DOCTYPE plist PUBLIC \"-//GNUstep//DTD plist 0.9//EN\" \"http://www.gnustep.org/plist-0_9.xml\">";
118 const char* srcDTDLine = NULL;
119 size_t srcDTDLineSize = 0;
120 unsigned i;
121
122 length = [data length];
123 if (length < sizeof xmlDeclLine)
124 return data;
125
126 bytes = [data bytes];
127
128 // Check if it starts with an XML declaration. Bogus: there are valid XML declarations which don't match xmlDeclLine.
129 if (memcmp(bytes, xmlDeclLine, sizeof xmlDeclLine - 1) != 0)
130 return data;
131
132 offset += sizeof xmlDeclLine - 1;
133 while (offset < length && isspace(bytes[offset]))
134 ++offset;
135
136 // Check if first non-blank stuff after XML declaration is any known Apple plist DTD. Also somewhat bogus.
137 for (i = 0;; i++) {
138 srcDTDLine = appleDTDLines[i];
139 if (srcDTDLine == NULL)
140 return data; // No matches
141
142 srcDTDLineSize = strlen(appleDTDLines[i]);
143
144 if (srcDTDLineSize <= length - offset && memcmp(bytes + offset, srcDTDLine, srcDTDLineSize) == 0) {
145 // Match
146 break;
147 }
148 }
149
150 offset += srcDTDLineSize;
151
152 newLength = length - offset + sizeof xmlDeclLine + sizeof gstepDTDLine - 1;
153 newBytes = malloc(newLength);
154 if (newBytes == NULL)
155 return data;
156
157 // Construct modified version with altered DTD line
158 memcpy(newBytes, xmlDeclLine, sizeof xmlDeclLine - 1);
159 newOffset = sizeof xmlDeclLine - 1;
160 newBytes[newOffset++] = '\n';
161 memcpy(newBytes + newOffset, gstepDTDLine, sizeof gstepDTDLine - 1);
162 newOffset += sizeof gstepDTDLine - 1;
163 memcpy(newBytes + newOffset, bytes + offset, length - offset);
164
165 return [NSData dataWithBytes:newBytes length:newLength];
166}
167#endif
168
169/* Load data from file. Returns a retained pointer.
170 -initWithContentsOfMappedFile fails quietly under OS X if there's no file,
171 but GNUstep complains.
172*/
173static NSData* CopyDataFromFile(NSString* path)
174{
175 return [[NSData oo_dataWithOXZFile:path] retain];
176#if 0
177// without OXZ extension. Code to be deleted once everything is working
178#if OOLITE_MAC_OS_X
179 return [[NSData alloc] initWithContentsOfMappedFile:path];
180#else
181 NSFileManager *fmgr = [NSFileManager defaultManager];
182 BOOL dir;
183
184 if ([fmgr fileExistsAtPath:path isDirectory:&dir])
185 {
186 if (!dir)
187 {
188 return [[NSData alloc] initWithContentsOfMappedFile:path];
189 }
190 else
191 {
192 OOLog(kOOLogFileNotFound, @"Expected property list but found directory at %@", path);
193 }
194 }
195
196 return nil;
197#endif
198#endif
199}
200// Wrappers which ensure that the plist contains the right type of object.
201NSDictionary* OODictionaryFromData(NSData* data, NSString* whereFrom)
202{
203 id result = OOPropertyListFromData(data, whereFrom);
204 return ValueIfClass(result, [NSDictionary class]);
205}
206
207NSDictionary* OODictionaryFromFile(NSString* path)
208{
209 id result = OOPropertyListFromFile(path);
210 return ValueIfClass(result, [NSDictionary class]);
211}
212
213NSArray* OOArrayFromData(NSData* data, NSString* whereFrom)
214{
215 id result = OOPropertyListFromData(data, whereFrom);
216 return ValueIfClass(result, [NSArray class]);
217}
218
219NSArray* OOArrayFromFile(NSString* path)
220{
221 id result = OOPropertyListFromFile(path);
222 return ValueIfClass(result, [NSArray class]);
223}
224
225// Ensure that object is of desired class.
226static id ValueIfClass(id value, Class class)
227{
228 if (value != nil && ![value isKindOfClass:class]) {
229 OOLog(kOOLogPListWrongType, @"Property list is wrong type - expected %@, got %@.", class, [value class]);
230 value = nil;
231 }
232 return value;
233}
#define OOLog(class, format,...)
Definition OOLogging.h:84
NSString *const kOOLogFileNotFound
Definition OOLogging.m:601
NSArray * OOArrayFromData(NSData *data, NSString *whereFrom)
static NSString *const kOOLogPListFoundationParseError
static id ValueIfClass(id value, Class class)
id OOPropertyListFromData(NSData *data, NSString *whereFrom)
id OOPropertyListFromFile(NSString *path)
NSDictionary * OODictionaryFromFile(NSString *path)
static NSString *const kOOLogPListInformation
NSArray * OOArrayFromFile(NSString *path)
static NSString *const kOOLogPListWrongType
static NSData * CopyDataFromFile(NSString *path)
NSDictionary * OODictionaryFromData(NSData *data, NSString *whereFrom)
return nil
#define UNIVERSE
Definition Universe.h:827
voidpf uLong offset
Definition ioapi.h:132