Oolite 1.91.0.7604-240417-a536cbe
Loading...
Searching...
No Matches
Macros | Functions
OOCollectionExtractors.m File Reference
import "OOCocoa.h"
import "OOStringParsing.h"
import "OOCollectionExtractors.h"
#include <limits.h>
import "OOMaths.h"
+ Include dependency graph for OOCollectionExtractors.m:

Go to the source code of this file.

Macros

#define PEEK()   ((i >= count) ? -1 : [string characterAtIndex:i])
 

Functions

static NSSet * SetForObject (id object, NSSet *defaultValue)
 
static NSString * StringForObject (id object, NSString *defaultValue)
 
long long OOLongLongFromObject (id object, long long defaultValue)
 
unsigned long long OOUnsignedLongLongFromObject (id object, unsigned long long defaultValue)
 
static BOOL IsSpaceOrTab (int value)
 
static BOOL IsZeroString (NSString *string)
 
static BOOL BooleanFromString (NSString *string, BOOL defaultValue)
 
static float FuzzyBooleanProbabilityFromString (NSString *string, float defaultValue)
 
BOOL OOBooleanFromObject (id object, BOOL defaultValue)
 
BOOL OOFuzzyBooleanFromObject (id object, float defaultValue)
 
float OOFloatFromObject (id object, float defaultValue)
 
double OODoubleFromObject (id object, double defaultValue)
 
float OONonNegativeFloatFromObject (id object, float defaultValue)
 
double OONonNegativeDoubleFromObject (id object, double defaultValue)
 
Vector OOVectorFromObject (id object, Vector defaultValue)
 
HPVector OOHPVectorFromObject (id object, HPVector defaultValue)
 
Quaternion OOQuaternionFromObject (id object, Quaternion defaultValue)
 
NSDictionary * OOPropertyListFromVector (Vector value)
 
NSDictionary * OOPropertyListFromHPVector (HPVector value)
 
NSDictionary * OOPropertyListFromQuaternion (Quaternion value)
 

Macro Definition Documentation

◆ PEEK

#define PEEK ( )    ((i >= count) ? -1 : [string characterAtIndex:i])

Function Documentation

◆ BooleanFromString()

static BOOL BooleanFromString ( NSString * string,
BOOL defaultValue )
static

Definition at line 1255 of file OOCollectionExtractors.m.

1256{
1257 if (NSOrderedSame == [string caseInsensitiveCompare:@"yes"] ||
1258 NSOrderedSame == [string caseInsensitiveCompare:@"true"] ||
1259 NSOrderedSame == [string caseInsensitiveCompare:@"on"] ||
1260 [string doubleValue] != 0.0) // Floating point is used so values like @"0.1" are treated as nonzero.
1261 {
1262 return YES;
1263 }
1264 else if (NSOrderedSame == [string caseInsensitiveCompare:@"no"] ||
1265 NSOrderedSame == [string caseInsensitiveCompare:@"false"] ||
1266 NSOrderedSame == [string caseInsensitiveCompare:@"off"] ||
1267 IsZeroString(string))
1268 {
1269 return NO;
1270 }
1271 return defaultValue;
1272}
static BOOL IsZeroString(NSString *string)

References IsZeroString().

Referenced by OOBooleanFromObject().

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

◆ FuzzyBooleanProbabilityFromString()

static float FuzzyBooleanProbabilityFromString ( NSString * string,
float defaultValue )
static

Definition at line 1276 of file OOCollectionExtractors.m.

1277{
1278 if (NSOrderedSame == [string caseInsensitiveCompare:@"yes"] ||
1279 NSOrderedSame == [string caseInsensitiveCompare:@"true"] ||
1280 NSOrderedSame == [string caseInsensitiveCompare:@"on"] ||
1281 [string doubleValue] != 0.0) // Floating point is used so values like @"0.1" are treated as nonzero.
1282 {
1283 return 1.0f;
1284 }
1285 else if (NSOrderedSame == [string caseInsensitiveCompare:@"no"] ||
1286 NSOrderedSame == [string caseInsensitiveCompare:@"false"] ||
1287 NSOrderedSame == [string caseInsensitiveCompare:@"off"] ||
1288 IsZeroString(string))
1289 {
1290 return 0.0f;
1291 }
1292 return defaultValue;
1293}

References IsZeroString().

Referenced by OOFuzzyBooleanFromObject().

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

◆ IsSpaceOrTab()

static BOOL IsSpaceOrTab ( int value)
inlinestatic

Definition at line 1228 of file OOCollectionExtractors.m.

1229{
1230 return value == ' ' || value == '\t';
1231}

Referenced by IsZeroString().

+ Here is the caller graph for this function:

◆ IsZeroString()

static BOOL IsZeroString ( NSString * string)
static

Definition at line 1234 of file OOCollectionExtractors.m.

1235{
1236 /* I don't particularly like regexps, but there are occasions...
1237 To match NSString's behaviour for intValue etc. with non-zero numbers,
1238 we need to skip any leading spaces or tabs (but not line breaks), get
1239 an optional minus sign, then at least one 0. Any trailing junk is
1240 ignored. It is assumed that this function is called for strings whose
1241 numerical value has already been determined to be 0.
1242 */
1243
1244 unsigned long i = 0, count = [string length];
1245#define PEEK() ((i >= count) ? -1 : [string characterAtIndex:i])
1246
1247 while (IsSpaceOrTab(PEEK())) ++i; // Skip spaces and tabs
1248 if (PEEK() == ' ') ++i; // Skip optional hyphen-minus
1249 return PEEK() == '0'; // If this is a 0, it's a numerical string.
1250
1251#undef PEEK
1252}
static BOOL IsSpaceOrTab(int value)
#define PEEK()
unsigned count

References count, IsSpaceOrTab(), and PEEK.

Referenced by BooleanFromString(), FuzzyBooleanProbabilityFromString(), OODoubleFromObject(), OOFloatFromObject(), and OOFuzzyBooleanFromObject().

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

◆ OOBooleanFromObject()

BOOL OOBooleanFromObject ( id object,
BOOL defaultValue )

Definition at line 1297 of file OOCollectionExtractors.m.

1298{
1299 BOOL result;
1300
1301 if ([object isKindOfClass:[NSString class]])
1302 {
1303 result = BooleanFromString(object, defaultValue);
1304 }
1305 else
1306 {
1307 if ([object respondsToSelector:@selector(boolValue)]) result = [object boolValue];
1308 else if ([object respondsToSelector:@selector(intValue)]) result = [object intValue] != 0;
1309 else result = defaultValue;
1310 }
1311
1312 return result;
1313}
static BOOL BooleanFromString(NSString *string, BOOL defaultValue)

References BooleanFromString().

Referenced by OOJSCallObjCObjectMethod(), Verify_Boolean(), and Verify_FuzzyBoolean().

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

◆ OODoubleFromObject()

double OODoubleFromObject ( id object,
double defaultValue )

Definition at line 1363 of file OOCollectionExtractors.m.

1364{
1365 double result;
1366
1367 if ([object respondsToSelector:@selector(doubleValue)])
1368 {
1369 result = [object doubleValue];
1370 if (result == 0.0 && [object isKindOfClass:[NSString class]] && !IsZeroString(object)) result = defaultValue;
1371 }
1372 else if ([object respondsToSelector:@selector(floatValue)]) result = [object floatValue];
1373 else if ([object respondsToSelector:@selector(intValue)]) result = [object intValue];
1374 else result = defaultValue;
1375
1376 return result;
1377}

References IsZeroString().

Referenced by Verify_Float(), Verify_FuzzyBoolean(), and Verify_PositiveFloat().

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

◆ OOFloatFromObject()

float OOFloatFromObject ( id object,
float defaultValue )

Definition at line 1346 of file OOCollectionExtractors.m.

1347{
1348 float result;
1349
1350 if ([object respondsToSelector:@selector(floatValue)])
1351 {
1352 result = [object floatValue];
1353 if (result == 0.0f && [object isKindOfClass:[NSString class]] && !IsZeroString(object)) result = defaultValue;
1354 }
1355 else if ([object respondsToSelector:@selector(doubleValue)]) result = [object doubleValue];
1356 else if ([object respondsToSelector:@selector(intValue)]) result = [object intValue];
1357 else result = defaultValue;
1358
1359 return result;
1360}

References IsZeroString().

Referenced by OOFuzzyBooleanFromObject().

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

◆ OOFuzzyBooleanFromObject()

BOOL OOFuzzyBooleanFromObject ( id object,
float defaultValue )

Definition at line 1317 of file OOCollectionExtractors.m.

1318{
1319 float probability;
1320
1321 if ([object isKindOfClass:[NSString class]])
1322 {
1323 probability = [object floatValue];
1324
1325 // If our string represents zero, it might be erroneous input or simply yes/no,
1326 // true/false or on/off valid boolean strings. Act on it.
1327 if (probability == 0.0f && !IsZeroString(object))
1328 {
1329 probability = FuzzyBooleanProbabilityFromString(object, defaultValue);
1330 }
1331 }
1332 else
1333 {
1334 probability = OOFloatFromObject(object, defaultValue);
1335 }
1336
1337 /* This will always be NO for negative values and YES for values
1338 greater than 1, as expected. randf() is always less than 1, so
1339 < is the correct operator here.
1340 */
1341 return randf() < probability;
1342}
static float FuzzyBooleanProbabilityFromString(NSString *string, float defaultValue)
float OOFloatFromObject(id object, float defaultValue)
float randf(void)

References FuzzyBooleanProbabilityFromString(), IsZeroString(), OOFloatFromObject(), and randf().

+ Here is the call graph for this function:

◆ OOHPVectorFromObject()

HPVector OOHPVectorFromObject ( id object,
HPVector defaultValue )

Definition at line 1445 of file OOCollectionExtractors.m.

1446{
1447 HPVector result = defaultValue;
1448 NSDictionary *dict = nil;
1449
1450 if ([object isKindOfClass:[NSString class]])
1451 {
1452 // This will only write result if a valid vector is found, and will write an error message otherwise.
1453 ScanHPVectorFromString(object, &result);
1454 }
1455 else if ([object isKindOfClass:[NSArray class]] && [object count] == 3)
1456 {
1457 result.x = [object oo_doubleAtIndex:0];
1458 result.y = [object oo_doubleAtIndex:1];
1459 result.z = [object oo_doubleAtIndex:2];
1460 }
1461 else if ([object isKindOfClass:[NSDictionary class]])
1462 {
1463 dict = object;
1464 // Require at least one of the keys x, y, or z
1465 if ([dict objectForKey:@"x"] != nil ||
1466 [dict objectForKey:@"y"] != nil ||
1467 [dict objectForKey:@"z"] != nil)
1468 {
1469 // Note: uses 0 for unknown components rather than components of defaultValue.
1470 result.x = [dict oo_doubleForKey:@"x" defaultValue:0.0];
1471 result.y = [dict oo_doubleForKey:@"y" defaultValue:0.0];
1472 result.z = [dict oo_doubleForKey:@"z" defaultValue:0.0];
1473 }
1474 }
1475
1476 return result;
1477}
return nil
BOOL ScanHPVectorFromString(NSString *xyzString, HPVector *outVector)

References nil, and ScanHPVectorFromString().

+ Here is the call graph for this function:

◆ OOLongLongFromObject()

long long OOLongLongFromObject ( id object,
long long defaultValue )

Definition at line 1201 of file OOCollectionExtractors.m.

1202{
1203 long long llValue;
1204
1205 if ([object respondsToSelector:@selector(longLongValue)]) llValue = [object longLongValue];
1206 else if ([object respondsToSelector:@selector(longValue)]) llValue = [object longValue];
1207 else if ([object respondsToSelector:@selector(intValue)]) llValue = [object intValue];
1208 else llValue = defaultValue;
1209
1210 return llValue;
1211}

Referenced by Verify_Integer().

+ Here is the caller graph for this function:

◆ OONonNegativeDoubleFromObject()

double OONonNegativeDoubleFromObject ( id object,
double defaultValue )

Definition at line 1393 of file OOCollectionExtractors.m.

1394{
1395 double result;
1396
1397 if ([object respondsToSelector:@selector(doubleValue)]) result = [object doubleValue];
1398 else if ([object respondsToSelector:@selector(floatValue)]) result = [object floatValue];
1399 else if ([object respondsToSelector:@selector(intValue)]) result = [object intValue];
1400 else return defaultValue; // Don't clamp default
1401
1402 return fmax(result, 0.0);
1403}

◆ OONonNegativeFloatFromObject()

float OONonNegativeFloatFromObject ( id object,
float defaultValue )

Definition at line 1380 of file OOCollectionExtractors.m.

1381{
1382 float result;
1383
1384 if ([object respondsToSelector:@selector(floatValue)]) result = [object floatValue];
1385 else if ([object respondsToSelector:@selector(doubleValue)]) result = [object doubleValue];
1386 else if ([object respondsToSelector:@selector(intValue)]) result = [object intValue];
1387 else return defaultValue; // Don't clamp default
1388
1389 return fmax(result, 0.0f);
1390}

◆ OOPropertyListFromHPVector()

NSDictionary * OOPropertyListFromHPVector ( HPVector value)

Definition at line 1527 of file OOCollectionExtractors.m.

1528{
1529 return [NSDictionary dictionaryWithObjectsAndKeys:
1530 [NSNumber numberWithDouble:value.x], @"x",
1531 [NSNumber numberWithDouble:value.y], @"y",
1532 [NSNumber numberWithDouble:value.z], @"z",
1533 nil];
1534}

◆ OOPropertyListFromQuaternion()

NSDictionary * OOPropertyListFromQuaternion ( Quaternion value)

Definition at line 1537 of file OOCollectionExtractors.m.

1538{
1539 return [NSDictionary dictionaryWithObjectsAndKeys:
1540 [NSNumber numberWithFloat:value.w], @"w",
1541 [NSNumber numberWithFloat:value.x], @"x",
1542 [NSNumber numberWithFloat:value.y], @"y",
1543 [NSNumber numberWithFloat:value.z], @"z",
1544 nil];
1545}

◆ OOPropertyListFromVector()

NSDictionary * OOPropertyListFromVector ( Vector value)

Definition at line 1518 of file OOCollectionExtractors.m.

1519{
1520 return [NSDictionary dictionaryWithObjectsAndKeys:
1521 [NSNumber numberWithFloat:value.x], @"x",
1522 [NSNumber numberWithFloat:value.y], @"y",
1523 [NSNumber numberWithFloat:value.z], @"z",
1524 nil];
1525}

◆ OOQuaternionFromObject()

Quaternion OOQuaternionFromObject ( id object,
Quaternion defaultValue )

Definition at line 1480 of file OOCollectionExtractors.m.

1481{
1482 Quaternion result = defaultValue;
1483 NSDictionary *dict = nil;
1484
1485 if ([object isKindOfClass:[NSString class]])
1486 {
1487 // This will only write result if a valid quaternion is found, and will write an error message otherwise.
1488 ScanQuaternionFromString(object, &result);
1489 }
1490 else if ([object isKindOfClass:[NSArray class]] && [object count] == 4)
1491 {
1492 result.w = [object oo_floatAtIndex:0];
1493 result.x = [object oo_floatAtIndex:1];
1494 result.y = [object oo_floatAtIndex:2];
1495 result.z = [object oo_floatAtIndex:3];
1496 }
1497 else if ([object isKindOfClass:[NSDictionary class]])
1498 {
1499 dict = object;
1500 // Require at least one of the keys w, x, y, or z
1501 if ([dict objectForKey:@"w"] != nil ||
1502 [dict objectForKey:@"x"] != nil ||
1503 [dict objectForKey:@"y"] != nil ||
1504 [dict objectForKey:@"z"] != nil)
1505 {
1506 // Note: uses 0 for unknown components rather than components of defaultValue.
1507 result.w = [dict oo_floatForKey:@"w" defaultValue:0.0f];
1508 result.x = [dict oo_floatForKey:@"x" defaultValue:0.0f];
1509 result.y = [dict oo_floatForKey:@"y" defaultValue:0.0f];
1510 result.z = [dict oo_floatForKey:@"z" defaultValue:0.0f];
1511 }
1512 }
1513
1514 return result;
1515}
BOOL ScanQuaternionFromString(NSString *wxyzString, Quaternion *outQuaternion)

References nil, and ScanQuaternionFromString().

Referenced by Verify_Quaternion().

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

◆ OOUnsignedLongLongFromObject()

unsigned long long OOUnsignedLongLongFromObject ( id object,
unsigned long long defaultValue )

Definition at line 1214 of file OOCollectionExtractors.m.

1215{
1216 unsigned long long ullValue;
1217
1218 if ([object respondsToSelector:@selector(unsignedLongLongValue)]) ullValue = [object unsignedLongLongValue];
1219 else if ([object respondsToSelector:@selector(unsignedLongValue)]) ullValue = [object unsignedLongValue];
1220 else if ([object respondsToSelector:@selector(unsignedIntValue)]) ullValue = [object unsignedIntValue];
1221 else if ([object respondsToSelector:@selector(intValue)]) ullValue = [object intValue];
1222 else ullValue = defaultValue;
1223
1224 return ullValue;
1225}

Referenced by PlayerEntity(LoadSave)::addScenarioModel:, and Verify_PositiveInteger().

+ Here is the caller graph for this function:

◆ OOVectorFromObject()

Vector OOVectorFromObject ( id object,
Vector defaultValue )

Definition at line 1407 of file OOCollectionExtractors.m.

1408{
1409 Vector result = defaultValue;
1410 NSDictionary *dict = nil;
1411
1412 if ([object isKindOfClass:[OONativeVector class]])
1413 {
1414 result = [object getVector];
1415 }
1416 else if ([object isKindOfClass:[NSString class]])
1417 {
1418 // This will only write result if a valid vector is found, and will write an error message otherwise.
1419 ScanVectorFromString(object, &result);
1420 }
1421 else if ([object isKindOfClass:[NSArray class]] && [object count] == 3)
1422 {
1423 result.x = [object oo_floatAtIndex:0];
1424 result.y = [object oo_floatAtIndex:1];
1425 result.z = [object oo_floatAtIndex:2];
1426 }
1427 else if ([object isKindOfClass:[NSDictionary class]])
1428 {
1429 dict = object;
1430 // Require at least one of the keys x, y, or z
1431 if ([dict objectForKey:@"x"] != nil ||
1432 [dict objectForKey:@"y"] != nil ||
1433 [dict objectForKey:@"z"] != nil)
1434 {
1435 // Note: uses 0 for unknown components rather than components of defaultValue.
1436 result.x = [dict oo_floatForKey:@"x" defaultValue:0.0f];
1437 result.y = [dict oo_floatForKey:@"y" defaultValue:0.0f];
1438 result.z = [dict oo_floatForKey:@"z" defaultValue:0.0f];
1439 }
1440 }
1441
1442 return result;
1443}
BOOL ScanVectorFromString(NSString *xyzString, Vector *outVector)

References nil, and ScanVectorFromString().

Referenced by Verify_Vector().

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

◆ SetForObject()

static NSSet * SetForObject ( id object,
NSSet * defaultValue )
static

Definition at line 1549 of file OOCollectionExtractors.m.

1550{
1551 if ([object isKindOfClass:[NSArray class]]) return [NSSet setWithArray:object];
1552 else if ([object isKindOfClass:[NSSet class]]) return [[object copy] autorelease];
1553
1554 return defaultValue;
1555}

◆ StringForObject()

static NSString * StringForObject ( id object,
NSString * defaultValue )
static

Definition at line 1558 of file OOCollectionExtractors.m.

1559{
1560 if ([object isKindOfClass:[NSString class]]) return object;
1561 else if ([object respondsToSelector:@selector(stringValue)]) return [object stringValue];
1562
1563 return defaultValue;
1564}