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

Go to the source code of this file.

Classes

category  OORoleSet(OOPrivate)
 

Functions

NSDictionary * OOParseRolesFromString (NSString *string)
 

Function Documentation

◆ OOParseRolesFromString()

NSDictionary * OOParseRolesFromString ( NSString *  string)

Definition at line 312 of file OORoleSet.m.

313{
314 NSMutableDictionary *result = nil;
315 NSArray *tokens = nil;
316 NSUInteger i, count;
317 NSString *role = nil;
318 float probability;
319 NSScanner *scanner = nil;
320
321 // Split string at spaces, sanity checks, set-up.
322 if (string == nil) return nil;
323
324 tokens = ScanTokensFromString(string);
325 count = [tokens count];
326 if (count == 0) return nil;
327
328 result = [NSMutableDictionary dictionaryWithCapacity:count];
329
330 // Scan tokens, looking for probabilities.
331 for (i = 0; i != count; ++i)
332 {
333 role = [tokens objectAtIndex:i];
334
335 probability = 1.0f;
336 if ([role rangeOfString:@"("].location != NSNotFound)
337 {
338 scanner = [[NSScanner alloc] initWithString:role];
339 [scanner scanUpToString:@"(" intoString:&role];
340 [scanner scanString:@"(" intoString:NULL];
341 if (![scanner scanFloat:&probability]) probability = 1.0f;
342 // Ignore rest of string
343
344 [scanner release];
345 }
346
347 // shipKey roles start with [ so other roles can't
348 if (0 <= probability && ![role hasPrefix:@"["])
349 {
350 [result setObject:[NSNumber numberWithFloat:probability] forKey:role];
351 }
352 }
353
354 if ([result count] == 0) result = nil;
355 return result;
356}
unsigned count
return nil
NSMutableArray * ScanTokensFromString(NSString *values)