Oolite
Loading...
Searching...
No Matches
OOCollectionExtractors.m File Reference
import "OOFoundation.h"
import "OOStringParsing.h"
import "OOCollectionExtractors.h"
import "OOMaths.h"
#include <limits.h>
Include dependency graph for OOCollectionExtractors.m:

Go to the source code of this file.

Macros

#define PEEK()

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 ( )
Value:
((i >= count) ? -1 : [string characterAtIndex:i])
unsigned count

Referenced by IsZeroString().

Function Documentation

◆ BooleanFromString()

BOOL BooleanFromString ( NSString * string,
BOOL defaultValue )
static

Definition at line 1098 of file OOCollectionExtractors.m.

1099{
1100 if (NSOrderedSame == [string caseInsensitiveCompare:@"yes"] || NSOrderedSame == [string caseInsensitiveCompare:@"true"] || NSOrderedSame == [string caseInsensitiveCompare:@"on"] ||
1101 [string doubleValue] != 0.0) // Floating point is used so values like @"0.1" are treated as nonzero.
1102 {
1103 return YES;
1104 } else if (NSOrderedSame == [string caseInsensitiveCompare:@"no"] || NSOrderedSame == [string caseInsensitiveCompare:@"false"] || NSOrderedSame == [string caseInsensitiveCompare:@"off"] || IsZeroString(string)) {
1105 return NO;
1106 }
1107 return defaultValue;
1108}
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()

float FuzzyBooleanProbabilityFromString ( NSString * string,
float defaultValue )
static

Definition at line 1111 of file OOCollectionExtractors.m.

1112{
1113 if (NSOrderedSame == [string caseInsensitiveCompare:@"yes"] || NSOrderedSame == [string caseInsensitiveCompare:@"true"] || NSOrderedSame == [string caseInsensitiveCompare:@"on"] ||
1114 [string doubleValue] != 0.0) // Floating point is used so values like @"0.1" are treated as nonzero.
1115 {
1116 return 1.0f;
1117 } else if (NSOrderedSame == [string caseInsensitiveCompare:@"no"] || NSOrderedSame == [string caseInsensitiveCompare:@"false"] || NSOrderedSame == [string caseInsensitiveCompare:@"off"] || IsZeroString(string)) {
1118 return 0.0f;
1119 }
1120 return defaultValue;
1121}

References IsZeroString().

Referenced by OOFuzzyBooleanFromObject().

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

◆ IsSpaceOrTab()

BOOL IsSpaceOrTab ( int value)
inlinestatic

Definition at line 1071 of file OOCollectionExtractors.m.

1072{
1073 return value == ' ' || value == '\t';
1074}

Referenced by IsZeroString().

Here is the caller graph for this function:

◆ IsZeroString()

BOOL IsZeroString ( NSString * string)
static

Definition at line 1076 of file OOCollectionExtractors.m.

1077{
1078 /* I don't particularly like regexps, but there are occasions...
1079 To match NSString's behaviour for intValue etc. with non-zero numbers,
1080 we need to skip any leading spaces or tabs (but not line breaks), get
1081 an optional minus sign, then at least one 0. Any trailing junk is
1082 ignored. It is assumed that this function is called for strings whose
1083 numerical value has already been determined to be 0.
1084 */
1085
1086 unsigned long i = 0, count = [string length];
1087#define PEEK() ((i >= count) ? -1 : [string characterAtIndex:i])
1088
1089 while (IsSpaceOrTab(PEEK()))
1090 ++i; // Skip spaces and tabs
1091 if (PEEK() == ' ')
1092 ++i; // Skip optional hyphen-minus
1093 return PEEK() == '0'; // If this is a 0, it's a numerical string.
1094
1095#undef PEEK
1096}
static BOOL IsSpaceOrTab(int value)
#define PEEK()

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 1124 of file OOCollectionExtractors.m.

1125{
1126 BOOL result;
1127
1128 if ([object isKindOfClass:[NSString class]]) {
1129 result = BooleanFromString(object, defaultValue);
1130 } else {
1131 if ([object respondsToSelector:@selector(boolValue)])
1132 result = [object boolValue];
1133 else if ([object respondsToSelector:@selector(intValue)])
1134 result = [object intValue] != 0;
1135 else
1136 result = defaultValue;
1137 }
1138
1139 return result;
1140}
static BOOL BooleanFromString(NSString *string, BOOL defaultValue)

References BooleanFromString().

Referenced by NSArray(OOExtractor)::oo_boolAtIndex:defaultValue:, NSDictionary(OOExtractor)::oo_boolForKey:defaultValue:, NSUserDefaults(OOExtractor)::oo_boolForKey:defaultValue:, OOOXPVerifier(OOPrivate)::setUpLogOverrides, 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 1185 of file OOCollectionExtractors.m.

1186{
1187 double result;
1188
1189 if ([object respondsToSelector:@selector(doubleValue)]) {
1190 result = [object doubleValue];
1191 if (result == 0.0 && [object isKindOfClass:[NSString class]] && !IsZeroString(object))
1192 result = defaultValue;
1193 } else if ([object respondsToSelector:@selector(floatValue)])
1194 result = [object floatValue];
1195 else if ([object respondsToSelector:@selector(intValue)])
1196 result = [object intValue];
1197 else
1198 result = defaultValue;
1199
1200 return result;
1201}

References IsZeroString().

Referenced by Universe::addEntity:, NSArray(OOExtractor)::oo_doubleAtIndex:, NSArray(OOExtractor)::oo_doubleAtIndex:defaultValue:, NSDictionary(OOExtractor)::oo_doubleForKey:, NSUserDefaults(OOExtractor)::oo_doubleForKey:, NSDictionary(OOExtractor)::oo_doubleForKey:defaultValue:, NSUserDefaults(OOExtractor)::oo_doubleForKey:defaultValue:, 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 1167 of file OOCollectionExtractors.m.

1168{
1169 float result;
1170
1171 if ([object respondsToSelector:@selector(floatValue)]) {
1172 result = [object floatValue];
1173 if (result == 0.0f && [object isKindOfClass:[NSString class]] && !IsZeroString(object))
1174 result = defaultValue;
1175 } else if ([object respondsToSelector:@selector(doubleValue)])
1176 result = [object doubleValue];
1177 else if ([object respondsToSelector:@selector(intValue)])
1178 result = [object intValue];
1179 else
1180 result = defaultValue;
1181
1182 return result;
1183}

References IsZeroString().

Referenced by NSArray(OOExtractor)::oo_floatAtIndex:, NSArray(OOExtractor)::oo_floatAtIndex:defaultValue:, NSDictionary(OOExtractor)::oo_floatForKey:, NSDictionary(OOExtractor)::oo_floatForKey:defaultValue:, NSUserDefaults(OOExtractor)::oo_floatForKey:defaultValue:, and 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 1143 of file OOCollectionExtractors.m.

1144{
1145 float probability;
1146
1147 if ([object isKindOfClass:[NSString class]]) {
1148 probability = [object floatValue];
1149
1150 // If our string represents zero, it might be erroneous input or simply yes/no,
1151 // true/false or on/off valid boolean strings. Act on it.
1152 if (probability == 0.0f && !IsZeroString(object)) {
1153 probability = FuzzyBooleanProbabilityFromString(object, defaultValue);
1154 }
1155 } else {
1156 probability = OOFloatFromObject(object, defaultValue);
1157 }
1158
1159 /* This will always be NO for negative values and YES for values
1160 greater than 1, as expected. randf() is always less than 1, so
1161 < is the correct operator here.
1162 */
1163 return randf() < probability;
1164}
static float FuzzyBooleanProbabilityFromString(NSString *string, float defaultValue)
float OOFloatFromObject(id object, float defaultValue)
float randf(void)

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

Referenced by StationEntity::hasShipyard, NSArray(OOExtractor)::oo_fuzzyBooleanAtIndex:defaultValue:, NSDictionary(OOExtractor)::oo_fuzzyBooleanForKey:defaultValue:, and NSUserDefaults(OOExtractor)::oo_fuzzyBooleanForKey:defaultValue:.

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

◆ OOHPVectorFromObject()

HPVector OOHPVectorFromObject ( id object,
HPVector defaultValue )

Definition at line 1266 of file OOCollectionExtractors.m.

1267{
1268 HPVector result = defaultValue;
1269 NSDictionary* dict = nil;
1270
1271 if ([object isKindOfClass:[NSString class]]) {
1272 // This will only write result if a valid vector is found, and will write an error message otherwise.
1273 ScanHPVectorFromString(object, &result);
1274 } else if ([object isKindOfClass:[NSArray class]] && [object count] == 3) {
1275 result.x = [object oo_doubleAtIndex:0];
1276 result.y = [object oo_doubleAtIndex:1];
1277 result.z = [object oo_doubleAtIndex:2];
1278 } else if ([object isKindOfClass:[NSDictionary class]]) {
1279 dict = object;
1280 // Require at least one of the keys x, y, or z
1281 if ([dict objectForKey:@"x"] != nil ||
1282 [dict objectForKey:@"y"] != nil ||
1283 [dict objectForKey:@"z"] != nil) {
1284 // Note: uses 0 for unknown components rather than components of defaultValue.
1285 result.x = [dict oo_doubleForKey:@"x" defaultValue:0.0];
1286 result.y = [dict oo_doubleForKey:@"y" defaultValue:0.0];
1287 result.z = [dict oo_doubleForKey:@"z" defaultValue:0.0];
1288 }
1289 }
1290
1291 return result;
1292}
return nil
BOOL ScanHPVectorFromString(NSString *xyzString, HPVector *outVector)
OOHPScalar x
Definition OOHPVector.h:36
OOHPScalar y
Definition OOHPVector.h:37
OOHPScalar z
Definition OOHPVector.h:38

References count, nil, ScanHPVectorFromString(), HPVector::x, HPVector::y, and HPVector::z.

Referenced by NSDictionary(OOExtractor)::oo_hpvectorForKey:defaultValue:.

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

◆ OOLongLongFromObject()

long long OOLongLongFromObject ( id object,
long long defaultValue )

Definition at line 1037 of file OOCollectionExtractors.m.

1038{
1039 long long llValue;
1040
1041 if ([object respondsToSelector:@selector(longLongValue)])
1042 llValue = [object longLongValue];
1043 else if ([object respondsToSelector:@selector(longValue)])
1044 llValue = [object longValue];
1045 else if ([object respondsToSelector:@selector(intValue)])
1046 llValue = [object intValue];
1047 else
1048 llValue = defaultValue;
1049
1050 return llValue;
1051}

Referenced by NSArray(OOExtractor)::oo_longLongAtIndex:defaultValue:, NSDictionary(OOExtractor)::oo_longLongForKey:defaultValue:, NSUserDefaults(OOExtractor)::oo_longLongForKey:defaultValue:, and Verify_Integer().

Here is the caller graph for this function:

◆ OONonNegativeDoubleFromObject()

double OONonNegativeDoubleFromObject ( id object,
double defaultValue )

Definition at line 1219 of file OOCollectionExtractors.m.

1220{
1221 double result;
1222
1223 if ([object respondsToSelector:@selector(doubleValue)])
1224 result = [object doubleValue];
1225 else if ([object respondsToSelector:@selector(floatValue)])
1226 result = [object floatValue];
1227 else if ([object respondsToSelector:@selector(intValue)])
1228 result = [object intValue];
1229 else
1230 return defaultValue; // Don't clamp default
1231
1232 return fmax(result, 0.0);
1233}

Referenced by NSArray(OOExtractor)::oo_nonNegativeDoubleAtIndex:, NSArray(OOExtractor)::oo_nonNegativeDoubleAtIndex:defaultValue:, NSDictionary(OOExtractor)::oo_nonNegativeDoubleForKey:, NSUserDefaults(OOExtractor)::oo_nonNegativeDoubleForKey:, NSDictionary(OOExtractor)::oo_nonNegativeDoubleForKey:defaultValue:, and NSUserDefaults(OOExtractor)::oo_nonNegativeDoubleForKey:defaultValue:.

Here is the caller graph for this function:

◆ OONonNegativeFloatFromObject()

float OONonNegativeFloatFromObject ( id object,
float defaultValue )

Definition at line 1203 of file OOCollectionExtractors.m.

1204{
1205 float result;
1206
1207 if ([object respondsToSelector:@selector(floatValue)])
1208 result = [object floatValue];
1209 else if ([object respondsToSelector:@selector(doubleValue)])
1210 result = [object doubleValue];
1211 else if ([object respondsToSelector:@selector(intValue)])
1212 result = [object intValue];
1213 else
1214 return defaultValue; // Don't clamp default
1215
1216 return fmax(result, 0.0f);
1217}

Referenced by NSArray(OOExtractor)::oo_nonNegativeFloatAtIndex:, NSArray(OOExtractor)::oo_nonNegativeFloatAtIndex:defaultValue:, NSDictionary(OOExtractor)::oo_nonNegativeFloatForKey:, NSUserDefaults(OOExtractor)::oo_nonNegativeFloatForKey:, NSDictionary(OOExtractor)::oo_nonNegativeFloatForKey:defaultValue:, and NSUserDefaults(OOExtractor)::oo_nonNegativeFloatForKey:defaultValue:.

Here is the caller graph for this function:

◆ OOPropertyListFromHPVector()

NSDictionary * OOPropertyListFromHPVector ( HPVector value)

Definition at line 1334 of file OOCollectionExtractors.m.

1335{
1336 return [NSDictionary dictionaryWithObjectsAndKeys:
1337 [NSNumber numberWithDouble:value.x], @"x",
1338 [NSNumber numberWithDouble:value.y], @"y",
1339 [NSNumber numberWithDouble:value.z], @"z",
1340 nil];
1341}

◆ OOPropertyListFromQuaternion()

NSDictionary * OOPropertyListFromQuaternion ( Quaternion value)

Definition at line 1343 of file OOCollectionExtractors.m.

1344{
1345 return [NSDictionary dictionaryWithObjectsAndKeys:
1346 [NSNumber numberWithFloat:value.w], @"w",
1347 [NSNumber numberWithFloat:value.x], @"x",
1348 [NSNumber numberWithFloat:value.y], @"y",
1349 [NSNumber numberWithFloat:value.z], @"z",
1350 nil];
1351}
OOScalar w
OOScalar x
OOScalar y
OOScalar z

◆ OOPropertyListFromVector()

NSDictionary * OOPropertyListFromVector ( Vector value)

Definition at line 1325 of file OOCollectionExtractors.m.

1326{
1327 return [NSDictionary dictionaryWithObjectsAndKeys:
1328 [NSNumber numberWithFloat:value.x], @"x",
1329 [NSNumber numberWithFloat:value.y], @"y",
1330 [NSNumber numberWithFloat:value.z], @"z",
1331 nil];
1332}
OOScalar x
Definition OOVector.h:32
OOScalar y
Definition OOVector.h:33
OOScalar z
Definition OOVector.h:34

◆ OOQuaternionFromObject()

Quaternion OOQuaternionFromObject ( id object,
Quaternion defaultValue )

Definition at line 1294 of file OOCollectionExtractors.m.

1295{
1296 Quaternion result = defaultValue;
1297 NSDictionary* dict = nil;
1298
1299 if ([object isKindOfClass:[NSString class]]) {
1300 // This will only write result if a valid quaternion is found, and will write an error message otherwise.
1301 ScanQuaternionFromString(object, &result);
1302 } else if ([object isKindOfClass:[NSArray class]] && [object count] == 4) {
1303 result.w = [object oo_floatAtIndex:0];
1304 result.x = [object oo_floatAtIndex:1];
1305 result.y = [object oo_floatAtIndex:2];
1306 result.z = [object oo_floatAtIndex:3];
1307 } else if ([object isKindOfClass:[NSDictionary class]]) {
1308 dict = object;
1309 // Require at least one of the keys w, x, y, or z
1310 if ([dict objectForKey:@"w"] != nil ||
1311 [dict objectForKey:@"x"] != nil ||
1312 [dict objectForKey:@"y"] != nil ||
1313 [dict objectForKey:@"z"] != nil) {
1314 // Note: uses 0 for unknown components rather than components of defaultValue.
1315 result.w = [dict oo_floatForKey:@"w" defaultValue:0.0f];
1316 result.x = [dict oo_floatForKey:@"x" defaultValue:0.0f];
1317 result.y = [dict oo_floatForKey:@"y" defaultValue:0.0f];
1318 result.z = [dict oo_floatForKey:@"z" defaultValue:0.0f];
1319 }
1320 }
1321
1322 return result;
1323}
BOOL ScanQuaternionFromString(NSString *wxyzString, Quaternion *outQuaternion)

References count, nil, ScanQuaternionFromString(), Quaternion::w, Quaternion::x, Quaternion::y, and Quaternion::z.

Referenced by NSArray(OOExtractor)::oo_quaternionAtIndex:defaultValue:, NSDictionary(OOExtractor)::oo_quaternionForKey:defaultValue:, and 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 1053 of file OOCollectionExtractors.m.

1054{
1055 unsigned long long ullValue;
1056
1057 if ([object respondsToSelector:@selector(unsignedLongLongValue)])
1058 ullValue = [object unsignedLongLongValue];
1059 else if ([object respondsToSelector:@selector(unsignedLongValue)])
1060 ullValue = [object unsignedLongValue];
1061 else if ([object respondsToSelector:@selector(unsignedIntValue)])
1062 ullValue = [object unsignedIntValue];
1063 else if ([object respondsToSelector:@selector(intValue)])
1064 ullValue = [object intValue];
1065 else
1066 ullValue = defaultValue;
1067
1068 return ullValue;
1069}

Referenced by NSArray(OOExtractor)::oo_unsignedLongLongAtIndex:defaultValue:, NSDictionary(OOExtractor)::oo_unsignedLongLongForKey:defaultValue:, NSUserDefaults(OOExtractor)::oo_unsignedLongLongForKey:defaultValue:, OODeciCreditsFromObject(), and Verify_PositiveInteger().

Here is the caller graph for this function:

◆ OOVectorFromObject()

Vector OOVectorFromObject ( id object,
Vector defaultValue )

Definition at line 1236 of file OOCollectionExtractors.m.

1237{
1238 Vector result = defaultValue;
1239 NSDictionary* dict = nil;
1240
1241 if ([object isKindOfClass:[OONativeVector class]]) {
1242 result = [object getVector];
1243 } else if ([object isKindOfClass:[NSString class]]) {
1244 // This will only write result if a valid vector is found, and will write an error message otherwise.
1245 ScanVectorFromString(object, &result);
1246 } else if ([object isKindOfClass:[NSArray class]] && [object count] == 3) {
1247 result.x = [object oo_floatAtIndex:0];
1248 result.y = [object oo_floatAtIndex:1];
1249 result.z = [object oo_floatAtIndex:2];
1250 } else if ([object isKindOfClass:[NSDictionary class]]) {
1251 dict = object;
1252 // Require at least one of the keys x, y, or z
1253 if ([dict objectForKey:@"x"] != nil ||
1254 [dict objectForKey:@"y"] != nil ||
1255 [dict objectForKey:@"z"] != nil) {
1256 // Note: uses 0 for unknown components rather than components of defaultValue.
1257 result.x = [dict oo_floatForKey:@"x" defaultValue:0.0f];
1258 result.y = [dict oo_floatForKey:@"y" defaultValue:0.0f];
1259 result.z = [dict oo_floatForKey:@"z" defaultValue:0.0f];
1260 }
1261 }
1262
1263 return result;
1264}
BOOL ScanVectorFromString(NSString *xyzString, Vector *outVector)

References count, nil, ScanVectorFromString(), Vector::x, Vector::y, and Vector::z.

Referenced by NSArray(OOExtractor)::oo_vectorAtIndex:defaultValue:, NSDictionary(OOExtractor)::oo_vectorForKey:defaultValue:, and Verify_Vector().

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

◆ SetForObject()

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

Definition at line 1354 of file OOCollectionExtractors.m.

1355{
1356 if ([object isKindOfClass:[NSArray class]])
1357 return [NSSet setWithArray:object];
1358 else if ([object isKindOfClass:[NSSet class]])
1359 return [[object copy] autorelease];
1360
1361 return defaultValue;
1362}

Referenced by NSArray(OOExtractor)::oo_setAtIndex:defaultValue:, NSDictionary(OOExtractor)::oo_setForKey:defaultValue:, and NSUserDefaults(OOExtractor)::oo_setForKey:defaultValue:.

Here is the caller graph for this function:

◆ StringForObject()

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

Definition at line 1364 of file OOCollectionExtractors.m.

1365{
1366 if ([object isKindOfClass:[NSString class]])
1367 return object;
1368 else if ([object respondsToSelector:@selector(stringValue)])
1369 return [object stringValue];
1370
1371 return defaultValue;
1372}

Referenced by NSArray(OOExtractor)::oo_stringAtIndex:defaultValue:, NSDictionary(OOExtractor)::oo_stringForKey:defaultValue:, and NSUserDefaults(OOExtractor)::oo_stringForKey:defaultValue:.

Here is the caller graph for this function: