Oolite
Loading...
Searching...
No Matches
OOConvertSystemDescriptions.m File Reference
Include dependency graph for OOConvertSystemDescriptions.m:

Go to the source code of this file.

Functions

static NSMutableDictionary * InitKeyToIndexDict (NSDictionary *dict, NSMutableSet **outUsedIndices)
static NSString * IndexToKey (NSUInteger index, NSDictionary *indicesToKeys, BOOL useFallback)
static NSArray * ConvertIndicesToKeys (NSArray *entry, NSDictionary *indicesToKeys)
static NSNumber * KeyToIndex (NSString *key, NSMutableDictionary *ioKeysToIndices, NSMutableSet *ioUsedIndicies, NSUInteger *ioSlotCache)
static NSArray * ConvertKeysToIndices (NSArray *entry, NSMutableDictionary *ioKeysToIndices, NSMutableSet *ioUsedIndicies, NSUInteger *ioSlotCache)
static NSUInteger HighestIndex (NSMutableDictionary *sparseArray)
void CompileSystemDescriptions (BOOL asXML)
void ExportSystemDescriptions (BOOL asXML)
NSArray * OOConvertSystemDescriptionsToArrayFormat (NSDictionary *descriptionsInDictionaryFormat, NSDictionary *indicesToKeys)
NSDictionary * OOConvertSystemDescriptionsToDictionaryFormat (NSArray *descriptionsInArrayFormat, NSDictionary *indicesToKeys)
NSString * OOStringifySystemDescriptionLine (NSString *line, NSDictionary *indicesToKeys, BOOL useFallback)

Function Documentation

◆ CompileSystemDescriptions()

void CompileSystemDescriptions ( BOOL asXML)

Definition at line 45 of file OOConvertSystemDescriptions.m.

46{
47 NSDictionary* sysDescDict = nil;
48 NSArray* sysDescArray = nil;
49 NSDictionary* keyMap = nil;
50 NSData* data = nil;
51 NSString* error = nil;
52
53 sysDescDict = [ResourceManager dictionaryFromFilesNamed:@"sysdesc.plist"
54 inFolder:@"Config"
55 andMerge:NO];
56 if (sysDescDict == nil) {
57 OOLog(@"sysdesc.compile.failed.fileNotFound", @"%@", @"Could not load a dictionary from sysdesc.plist, ignoring --compile-sysdesc option.");
58 return;
59 }
60
61 keyMap = [ResourceManager dictionaryFromFilesNamed:@"sysdesc_key_table.plist"
62 inFolder:@"Config"
63 andMerge:NO];
64 // keyMap is optional, so no nil check
65
66 sysDescArray = OOConvertSystemDescriptionsToArrayFormat(sysDescDict, keyMap);
67 if (sysDescArray == nil) {
68 OOLog(@"sysdesc.compile.failed.conversion", @"%@", @"Could not convert sysdesc.plist to descriptions.plist format for some reason.");
69 return;
70 }
71
72 sysDescDict = [NSDictionary dictionaryWithObject:sysDescArray forKey:@"system_description"];
73
74 if (asXML) {
75 data = [NSPropertyListSerialization dataFromPropertyList:sysDescDict
76 format:NSPropertyListXMLFormat_v1_0
77 errorDescription:&error];
78 } else {
79 data = [sysDescDict oldSchoolPListFormatWithErrorDescription:&error];
80 }
81
82 if (data == nil) {
83 OOLog(@"sysdesc.compile.failed.XML", @"Could not convert translated sysdesc.plist to property list: %@.", error);
84 return;
85 }
86
87 if ([ResourceManager writeDiagnosticData:data toFileNamed:@"sysdesc-compiled.plist"]) {
88 OOLog(@"sysdesc.compile.success", @"%@", @"Wrote translated sysdesc.plist to sysdesc-compiled.plist.");
89 } else {
90 OOLog(@"sysdesc.compile.failed.writeFailure", @"%@", @"Could not write translated sysdesc.plist to sysdesc-compiled.plist.");
91 }
92}
NSArray * OOConvertSystemDescriptionsToArrayFormat(NSDictionary *descriptionsInDictionaryFormat, NSDictionary *indicesToKeys)
#define OOLog(class, format,...)
Definition OOLogging.h:84
return nil
NSDictionary * dictionaryFromFilesNamed:inFolder:andMerge:(NSString *fileName,[inFolder] NSString *folderName,[andMerge] BOOL mergeFiles)

References ResourceManager::dictionaryFromFilesNamed:inFolder:andMerge:, nil, OOConvertSystemDescriptionsToArrayFormat(), and OOLog.

Referenced by Universe(OOPrivate)::runLocalizationTools.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ConvertIndicesToKeys()

NSArray * ConvertIndicesToKeys ( NSArray * entry,
NSDictionary * indicesToKeys )
static

Definition at line 265 of file OOConvertSystemDescriptions.m.

266{
267 NSString* line = nil;
268 NSMutableArray* result = nil;
269
270 result = [NSMutableArray arrayWithCapacity:[entry count]];
271
272 foreach (line, entry) {
273 [result addObject:OOStringifySystemDescriptionLine(line, indicesToKeys, YES)];
274 }
275
276 return result;
277}

References nil.

Referenced by OOConvertSystemDescriptionsToDictionaryFormat().

Here is the caller graph for this function:

◆ ConvertKeysToIndices()

NSArray * ConvertKeysToIndices ( NSArray * entry,
NSMutableDictionary * ioKeysToIndices,
NSMutableSet * ioUsedIndicies,
NSUInteger * ioSlotCache )
static

Definition at line 300 of file OOConvertSystemDescriptions.m.

301{
302 NSString* line = nil;
303 NSUInteger p1, p2;
304 NSRange searchRange;
305 NSMutableArray* result = nil;
306 NSString *before = nil, *after = nil, *middle = nil;
307
308 result = [NSMutableArray arrayWithCapacity:[entry count]];
309
310 foreach (line, entry) {
311 searchRange.location = 0;
312 searchRange.length = [line length];
313
314 while ([line rangeOfString:@"[" options:NSLiteralSearch range:searchRange].location != NSNotFound) {
315 p1 = [line rangeOfString:@"[" options:NSLiteralSearch range:searchRange].location;
316 p2 = [line rangeOfString:@"]" options:NSLiteralSearch range:searchRange].location + 1;
317
318 before = [line substringWithRange:NSMakeRange(0, p1)];
319 after = [line substringWithRange:NSMakeRange(p2, [line length] - p2)];
320 middle = [line substringWithRange:NSMakeRange(p1 + 1, p2 - p1 - 2)];
321
322 if ([middle length] > 1 && [middle hasPrefix:@"#"]) {
323 // Found [] around key
324 line = [NSString stringWithFormat:@"%@[%@]%@", before, KeyToIndex([middle substringFromIndex:1], ioKeysToIndices, ioUsedIndicies, ioSlotCache), after];
325 }
326
327 searchRange.length -= p2 - searchRange.location;
328 searchRange.location = [line length] - searchRange.length;
329 }
330
331 [result addObject:line];
332 }
333
334 return result;
335}

References nil.

Referenced by OOConvertSystemDescriptionsToArrayFormat().

Here is the caller graph for this function:

◆ ExportSystemDescriptions()

void ExportSystemDescriptions ( BOOL asXML)

Definition at line 94 of file OOConvertSystemDescriptions.m.

95{
96 NSArray* sysDescArray = nil;
97 NSDictionary* sysDescDict = nil;
98 NSDictionary* keyMap = nil;
99 NSData* data = nil;
100 NSString* error = nil;
101
102 sysDescArray = [[UNIVERSE descriptions] oo_arrayForKey:@"system_description"];
103
104 keyMap = [ResourceManager dictionaryFromFilesNamed:@"sysdesc_key_table.plist"
105 inFolder:@"Config"
106 andMerge:NO];
107 // keyMap is optional, so no nil check
108
109 sysDescDict = OOConvertSystemDescriptionsToDictionaryFormat(sysDescArray, keyMap);
110 if (sysDescArray == nil) {
111 OOLog(@"sysdesc.export.failed.conversion", @"%@", @"Could not convert system_description do sysdesc.plist format for some reason.");
112 return;
113 }
114
115 if (asXML) {
116 data = [NSPropertyListSerialization dataFromPropertyList:sysDescDict
117 format:NSPropertyListXMLFormat_v1_0
118 errorDescription:&error];
119 } else {
120 data = [sysDescDict oldSchoolPListFormatWithErrorDescription:&error];
121 }
122
123 if (data == nil) {
124 OOLog(@"sysdesc.export.failed.XML", @"Could not convert translated system_description to XML property list: %@.", error);
125 return;
126 }
127
128 if ([ResourceManager writeDiagnosticData:data toFileNamed:@"sysdesc.plist"]) {
129 OOLog(@"sysdesc.export.success", @"%@", @"Wrote translated system_description to sysdesc.plist.");
130 } else {
131 OOLog(@"sysdesc.export.failed.writeFailure", @"%@", @"Could not write translated system_description to sysdesc.plist.");
132 }
133}
NSDictionary * OOConvertSystemDescriptionsToDictionaryFormat(NSArray *descriptionsInArrayFormat, NSDictionary *indicesToKeys)

References ResourceManager::dictionaryFromFilesNamed:inFolder:andMerge:, nil, OOConvertSystemDescriptionsToDictionaryFormat(), and OOLog.

Referenced by Universe(OOPrivate)::runLocalizationTools.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ HighestIndex()

NSUInteger HighestIndex ( NSMutableDictionary * sparseArray)
static

Definition at line 337 of file OOConvertSystemDescriptions.m.

338{
339 NSNumber* key = nil;
340 NSUInteger curr, highest = 0;
341
342 foreachkey(key, sparseArray)
343 {
344 curr = [key intValue];
345 if (highest < curr)
346 highest = curr;
347 }
348
349 return highest;
350}
#define foreachkey(VAR, DICT)

References foreachkey, and nil.

Referenced by OOConvertSystemDescriptionsToArrayFormat().

Here is the caller graph for this function:

◆ IndexToKey()

NSString * IndexToKey ( NSUInteger index,
NSDictionary * indicesToKeys,
BOOL useFallback )
static

Definition at line 256 of file OOConvertSystemDescriptions.m.

257{
258 NSString* result = [indicesToKeys objectForKey:[NSString stringWithFormat:@"%zu", index]];
259 if (result == nil && useFallback)
260 result = [NSString stringWithFormat:@"block_%zu", index];
261
262 return result;
263}

References nil.

Referenced by OOConvertSystemDescriptionsToDictionaryFormat(), and OOStringifySystemDescriptionLine().

Here is the caller graph for this function:

◆ InitKeyToIndexDict()

NSMutableDictionary * InitKeyToIndexDict ( NSDictionary * dict,
NSMutableSet ** outUsedIndices )
static

Definition at line 232 of file OOConvertSystemDescriptions.m.

233{
234 NSString* key = nil;
235 NSNumber* number = nil;
236 NSMutableDictionary* result = nil;
237 NSMutableSet* used = nil;
238
239 assert(outUsedIndices != NULL);
240
241 result = [NSMutableDictionary dictionaryWithCapacity:[dict count]];
242 used = [NSMutableSet setWithCapacity:[dict count]];
243
244 foreachkey(key, dict)
245 {
246 // Convert keys of dict to array indices
247 number = [NSNumber numberWithInt:[key intValue]];
248 [result setObject:number forKey:[dict objectForKey:key]];
249 [used addObject:number];
250 }
251
252 *outUsedIndices = used;
253 return result;
254}

References foreachkey, and nil.

Referenced by OOConvertSystemDescriptionsToArrayFormat().

Here is the caller graph for this function:

◆ KeyToIndex()

NSNumber * KeyToIndex ( NSString * key,
NSMutableDictionary * ioKeysToIndices,
NSMutableSet * ioUsedIndicies,
NSUInteger * ioSlotCache )
static

Definition at line 279 of file OOConvertSystemDescriptions.m.

280{
281 NSNumber* result = nil;
282
283 assert(ioSlotCache != NULL);
284
285 result = [ioKeysToIndices objectForKey:key];
286 if (result == nil) {
287 // Search for free index
288 do {
289 result = [NSNumber numberWithUnsignedInteger:(*ioSlotCache)++];
290 } while ([ioUsedIndicies containsObject:result]);
291
292 [ioKeysToIndices setObject:result forKey:key];
293 [ioUsedIndicies addObject:result];
294 OOLog(@"sysdesc.compile.unknownKey", @"Assigning key \"%@\" to index %@.", key, result);
295 }
296
297 return result;
298}

References nil, and OOLog.

Referenced by OOConvertSystemDescriptionsToArrayFormat().

Here is the caller graph for this function:

◆ OOConvertSystemDescriptionsToArrayFormat()

NSArray * OOConvertSystemDescriptionsToArrayFormat ( NSDictionary * descriptionsInDictionaryFormat,
NSDictionary * indicesToKeys )

Definition at line 135 of file OOConvertSystemDescriptions.m.

136{
137 NSMutableDictionary* result = nil;
138 NSAutoreleasePool* pool = nil;
139 NSString* key = nil;
140 NSArray* entry = nil;
141 NSMutableDictionary* keysToIndices = nil;
142 NSMutableSet* usedIndices = nil;
143 NSUInteger slotCache = 0;
144 NSNumber* index = nil;
145 NSUInteger i, count;
146 NSMutableArray* realResult = nil;
147
148 pool = [[NSAutoreleasePool alloc] init];
149
150 // Use a dictionary as a sparse array.
151 result = [NSMutableDictionary dictionaryWithCapacity:[descriptionsInDictionaryFormat count]];
152
153 keysToIndices = InitKeyToIndexDict(indicesToKeys, &usedIndices);
154
155 foreachkey(key, descriptionsInDictionaryFormat)
156 {
157 entry = ConvertKeysToIndices([descriptionsInDictionaryFormat objectForKey:key], keysToIndices, usedIndices, &slotCache);
158 index = KeyToIndex(key, keysToIndices, usedIndices, &slotCache);
159
160 [result setObject:entry forKey:index];
161 }
162
163 count = HighestIndex(result);
164 realResult = [NSMutableArray arrayWithCapacity:count];
165 for (i = 0; i < count; i++) {
166 entry = [result objectForKey:[NSNumber numberWithUnsignedInteger:i]];
167 if (entry == nil)
168 entry = [NSArray array];
169 [realResult addObject:entry];
170 }
171
172 [realResult retain];
173 [pool release];
174 return [realResult autorelease];
175}
static NSMutableDictionary * InitKeyToIndexDict(NSDictionary *dict, NSMutableSet **outUsedIndices)
static NSArray * ConvertKeysToIndices(NSArray *entry, NSMutableDictionary *ioKeysToIndices, NSMutableSet *ioUsedIndicies, NSUInteger *ioSlotCache)
static NSUInteger HighestIndex(NSMutableDictionary *sparseArray)
static NSNumber * KeyToIndex(NSString *key, NSMutableDictionary *ioKeysToIndices, NSMutableSet *ioUsedIndicies, NSUInteger *ioSlotCache)
unsigned count

References ConvertKeysToIndices(), count, foreachkey, HighestIndex(), InitKeyToIndexDict(), KeyToIndex(), and nil.

Referenced by CompileSystemDescriptions().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OOConvertSystemDescriptionsToDictionaryFormat()

NSDictionary * OOConvertSystemDescriptionsToDictionaryFormat ( NSArray * descriptionsInArrayFormat,
NSDictionary * indicesToKeys )

Definition at line 177 of file OOConvertSystemDescriptions.m.

178{
179 NSMutableDictionary* result = nil;
180 NSAutoreleasePool* pool = nil;
181 NSArray* entry = nil;
182 NSString* key = nil;
183 NSUInteger i = 0;
184
185 result = [NSMutableDictionary dictionaryWithCapacity:[descriptionsInArrayFormat count]];
186 pool = [[NSAutoreleasePool alloc] init];
187
188 foreach (entry, descriptionsInArrayFormat) {
189 entry = ConvertIndicesToKeys(entry, indicesToKeys);
190 key = IndexToKey(i, indicesToKeys, YES);
191 ++i;
192
193 [result setObject:entry forKey:key];
194 }
195
196 [pool release];
197 return result;
198}
static NSArray * ConvertIndicesToKeys(NSArray *entry, NSDictionary *indicesToKeys)
static NSString * IndexToKey(NSUInteger index, NSDictionary *indicesToKeys, BOOL useFallback)

References ConvertIndicesToKeys(), IndexToKey(), and nil.

Referenced by ExportSystemDescriptions().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OOStringifySystemDescriptionLine()

NSString * OOStringifySystemDescriptionLine ( NSString * line,
NSDictionary * indicesToKeys,
BOOL useFallback )

Definition at line 200 of file OOConvertSystemDescriptions.m.

201{
202 NSUInteger p1, p2;
203 NSRange searchRange;
204 NSString *before = nil, *after = nil, *middle = nil;
205 NSString* key = nil;
206
207 searchRange.location = 0;
208 searchRange.length = [line length];
209
210 while ([line rangeOfString:@"[" options:NSLiteralSearch range:searchRange].location != NSNotFound) {
211 p1 = [line rangeOfString:@"[" options:NSLiteralSearch range:searchRange].location;
212 p2 = [line rangeOfString:@"]" options:NSLiteralSearch range:searchRange].location + 1;
213
214 before = [line substringWithRange:NSMakeRange(0, p1)];
215 after = [line substringWithRange:NSMakeRange(p2, [line length] - p2)];
216 middle = [line substringWithRange:NSMakeRange(p1 + 1, p2 - p1 - 2)];
217
218 if ([[middle stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"0123456789"]] isEqual:@""] && ![middle isEqual:@""]) {
219 // Found [] around integers only
220 key = IndexToKey([middle intValue], indicesToKeys, useFallback);
221 if (key != nil) {
222 line = [NSString stringWithFormat:@"%@[#%@]%@", before, key, after];
223 }
224 }
225
226 searchRange.length -= p2 - searchRange.location;
227 searchRange.location = [line length] - searchRange.length;
228 }
229 return line;
230}

References IndexToKey(), and nil.

Here is the call graph for this function: