Oolite
Loading...
Searching...
No Matches
OldSchoolPropertyListWriting.m
Go to the documentation of this file.
1/*
2 OldSchoolPropertyListWriting.m
3 Copyright 2006-2013 Jens Ayton
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
6 and associated documentation files (the "Software"), to deal in the Software without
7 restriction, including without limitation the rights to use, copy, modify, merge, publish,
8 distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
9 Software is furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all copies or
12 substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
15 BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19*/
20
21#include <assert.h>
22
25
26static void AppendNewLineAndIndent(NSMutableString* ioString, unsigned indentDepth);
27
28@implementation NSString (OldSchoolPropertyListWriting)
29
30- (NSString*)oldSchoolPListFormatWithIndentation:(unsigned)inIndentation errorDescription:(NSString**)outErrorDescription
31{
32 NSCharacterSet* charSet;
33 NSRange foundRange, searchRange;
34 NSString* foundString;
35 NSMutableString* newString;
36 NSUInteger length;
37
38 length = [self length];
39 if (0 != length
40 && [self rangeOfCharacterFromSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]].location == NSNotFound
41 && ![[NSCharacterSet decimalDigitCharacterSet] longCharacterIsMember:[self characterAtIndex:0]]) {
42 // This is an alphanumeric string whose first character is not a digit
43 return [[self copy] autorelease];
44 } else {
45 charSet = [NSCharacterSet characterSetWithCharactersInString:@"\"\r\n\\"];
46 foundRange = [self rangeOfCharacterFromSet:charSet options:NSLiteralSearch];
47 if (NSNotFound == foundRange.location) {
48 newString = (NSMutableString*)self;
49 } else {
50 // Escape quotes, backslashes and newlines
51 newString = [[[self substringToIndex:foundRange.location] mutableCopy] autorelease];
52
53 for (;;) {
54 // Append escaped character
55 foundString = [self substringWithRange:foundRange];
56 if ([foundString isEqual:@"\""])
57 [newString appendString:@"\\\""];
58 else if ([foundString isEqual:@"\n"])
59 [newString appendString:@"\\\n"];
60 else if ([foundString isEqual:@"\r"])
61 [newString appendString:@"\\\r"];
62 else if ([foundString isEqual:@"\\"])
63 [newString appendString:@"\\\\"];
64 else {
65 [NSException raise:NSInternalInconsistencyException format:@"%s: expected \" or newline, found %@", __PRETTY_FUNCTION__, foundString];
66 }
67
68 // Use rest of string…
69 searchRange.location = foundRange.location + foundRange.length;
70 searchRange.length = length - searchRange.location;
71
72 // …to search for next char needing escaping
73 foundRange = [self rangeOfCharacterFromSet:charSet options:NSLiteralSearch range:searchRange];
74 if (NSNotFound == foundRange.location) {
75 [newString appendString:[self substringWithRange:searchRange]];
76 break;
77 }
78 }
79 }
80
81 return [NSString stringWithFormat:@"\"%@\"", newString];
82 }
83}
84
85@end
86
87@implementation NSNumber (OldSchoolPropertyListWriting)
88
89- (NSString*)oldSchoolPListFormatWithIndentation:(unsigned)inIndentation errorDescription:(NSString**)outErrorDescription
90{
91 NSString* result;
92 double dVal;
93
94 if ([self oo_isBoolean]) {
95 if ([self boolValue])
96 result = @"true";
97 else
98 result = @"false";
99 } else if ([self oo_isFloatingPointNumber]) {
100 dVal = [self doubleValue];
101 result = [NSString stringWithFormat:@"%.8g", dVal];
102 } else
103 result = [NSString stringWithFormat:@"%@", self];
104
105 // Allow infinities, but remember that they’ll be read in as strings
106#if 0
107 if ([result isEqual:@"inf"] || [result isEqual:@"-inf"])
108 {
109 *outErrorDescription = @"infinities cannot be represented in old-school property lists";
110 return nil;
111 }
112#endif
113
114 return result;
115}
116
117@end
118
119@implementation NSData (OldSchoolPropertyListWriting)
120
121- (NSString*)oldSchoolPListFormatWithIndentation:(unsigned)inIndentation errorDescription:(NSString**)outErrorDescription
122{
123 const uint8_t* srcBytes;
124 uint8_t *dstBytes, *curr;
125 NSUInteger i, j, srcLength, dstLength;
126 const char hexTable[] = "0123456789ABCDEF";
127 NSString* result;
128
129 srcBytes = [self bytes];
130 srcLength = [self length];
131
132 dstLength = 2 * srcLength + srcLength / 8 + 2 + (srcLength / 64 * (1 + inIndentation));
133
134 dstBytes = malloc(dstLength);
135 if (dstBytes == NULL) {
136 if (NULL != outErrorDescription) {
137 *outErrorDescription = [NSString stringWithFormat:@"failed to allocate space (%zu bytes) for conversion of NSData to old-school property list representation", dstLength];
138 }
139 return nil;
140 }
141
142 curr = dstBytes;
143 *curr++ = '<';
144 for (i = 0; i != srcLength; ++i) {
145 if (0 != i && 0 == (i & 3)) {
146 if (0 == (i & 31)) {
147 *curr++ = '\n';
148 j = inIndentation;
149 while (--j)
150 *curr++ = '\t';
151 }
152 *curr++ = ' ';
153 }
154 *curr++ = hexTable[srcBytes[i] >> 4];
155 *curr++ = hexTable[srcBytes[i] & 0xF];
156 }
157 *curr = '>';
158
159 assert((size_t)(curr - dstBytes) <= dstLength);
160
161 result = [[NSString alloc] initWithBytesNoCopy:dstBytes length:dstLength encoding:NSASCIIStringEncoding freeWhenDone:YES];
162 return [result autorelease];
163}
164
165@end
166
167@implementation NSArray (OldSchoolPropertyListWriting)
168
169- (NSString*)oldSchoolPListFormatWithIndentation:(unsigned)inIndentation errorDescription:(NSString**)outErrorDescription
170{
171 NSMutableString* result;
172 NSUInteger i, count;
173 id object;
174
175 result = [NSMutableString string];
176
177 [result appendString:@"("];
178
179 count = [self count];
180 AppendNewLineAndIndent(result, inIndentation + 1);
181
182 for (i = 0; i != count; ++i) {
183 if (0 != i) {
184 [result appendString:@","];
185 AppendNewLineAndIndent(result, inIndentation + 1);
186 }
187
188 object = [self objectAtIndex:i];
189 if (![object conformsToProtocol:@protocol(OldSchoolPropertyListWriting)]) {
190 if (nil != object && NULL != outErrorDescription) {
191 *outErrorDescription = [NSString stringWithFormat:@"non-plist object in dictionary"];
192 }
193 return nil;
194 }
195
196 object = [object oldSchoolPListFormatWithIndentation:inIndentation + 1 errorDescription:outErrorDescription];
197 if (nil == object)
198 return nil;
199 [result appendString:object];
200 }
201
202 AppendNewLineAndIndent(result, inIndentation);
203 [result appendString:@")"];
204 return result;
205}
206
207@end
208
209@implementation NSDictionary (OldSchoolPropertyListWriting)
210
211- (NSString*)oldSchoolPListFormatWithIndentation:(unsigned)inIndentation errorDescription:(NSString**)outErrorDescription
212{
213 NSMutableString* result;
214 NSUInteger i, count;
215 NSArray* allKeys;
216 id key, value;
217 NSString* valueDesc;
218
219 result = [NSMutableString string];
220 allKeys = [[self allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
221 count = [allKeys count];
222
223 [result appendString:@"{"];
224
225 AppendNewLineAndIndent(result, inIndentation + 1);
226
227 for (i = 0; i != count; ++i) {
228 if (0 != i) {
229 AppendNewLineAndIndent(result, inIndentation + 1);
230 }
231
232 key = [allKeys objectAtIndex:i];
233 if (![key isKindOfClass:[NSString class]]) {
234 if (NULL != outErrorDescription)
235 *outErrorDescription = [NSString stringWithFormat:@"non-string key in dictionary"];
236 return nil;
237 }
238 value = [self objectForKey:key];
239 if (![value conformsToProtocol:@protocol(OldSchoolPropertyListWriting)]) {
240 if (nil != value && NULL != outErrorDescription) {
241 *outErrorDescription = [NSString stringWithFormat:@"non-plist object in dictionary"];
242 }
243 return nil;
244 }
245
246 key = [key oldSchoolPListFormatWithIndentation:inIndentation + 1 errorDescription:outErrorDescription];
247 if (nil == key)
248 return nil;
249 valueDesc = [value oldSchoolPListFormatWithIndentation:inIndentation + 1 errorDescription:outErrorDescription];
250 if (nil == valueDesc)
251 return nil;
252
253 [result appendFormat:@"%@ =", key];
254 if ([value isKindOfClass:[NSArray class]] || [value isKindOfClass:[NSDictionary class]]) {
255 AppendNewLineAndIndent(result, inIndentation + 1);
256 } else {
257 [result appendString:@" "];
258 }
259 [result appendFormat:@"%@;", valueDesc];
260 }
261
262 AppendNewLineAndIndent(result, inIndentation);
263 [result appendString:@"}"];
264
265 return result;
266}
267
268@end
269
270@interface NSObject (OldSchoolPropertyListWriting_Private)
271
272- (NSString*)oldSchoolPListFormatWithIndentation:(unsigned)inIndentation errorDescription:(NSString**)outErrorDescription;
273
274@end
275
276@implementation NSObject (OldSchoolPropertyListWriting)
277
278- (NSData*)oldSchoolPListFormatWithErrorDescription:(NSString**)outErrorDescription
279{
280 NSString* string;
281
282 string = [self oldSchoolPListFormatWithIndentation:0 errorDescription:outErrorDescription];
283 return [[string stringByAppendingString:@"\n"] dataUsingEncoding:NSUTF8StringEncoding];
284}
285
286- (NSString*)oldSchoolPListFormatWithIndentation:(unsigned)inIndentation errorDescription:(NSString**)outErrorDescription
287{
288 if (NULL != outErrorDescription) {
289 *outErrorDescription = [NSString stringWithFormat:@"Class %@ does not support OldSchoolPropertyListWriting", [self className]];
290 }
291 return nil;
292}
293
294@end
295
296static void AppendNewLineAndIndent(NSMutableString* ioString, unsigned indentDepth)
297{
298 [ioString appendString:@"\n"];
299 while (indentDepth--)
300 [ioString appendString:@"\t"];
301}
unsigned count
return nil
static void AppendNewLineAndIndent(NSMutableString *ioString, unsigned indentDepth)