Oolite 1.91.0.7645-241119-222d325
Loading...
Searching...
No Matches
OOStringParsing.h File Reference
import <Foundation/Foundation.h>
import "OOMaths.h"
import "OOTypes.h"
import "legacy_random.h"
+ Include dependency graph for OOStringParsing.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

category  NSString(OOUtilities)
 

Functions

NSMutableArray * ScanTokensFromString (NSString *values)
 
BOOL ScanVectorFromString (NSString *xyzString, Vector *outVector)
 
BOOL ScanHPVectorFromString (NSString *xyzString, HPVector *outVector)
 
BOOL ScanQuaternionFromString (NSString *wxyzString, Quaternion *outQuaternion)
 
BOOL ScanVectorAndQuaternionFromString (NSString *xyzwxyzString, Vector *outVector, Quaternion *outQuaternion)
 
Vector VectorFromString (NSString *xyzString, Vector defaultValue)
 
Quaternion QuaternionFromString (NSString *wxyzString, Quaternion defaultValue)
 
NSString * StringFromPoint (NSPoint point)
 
NSPoint PointFromString (NSString *xyString)
 
Random_Seed RandomSeedFromString (NSString *abcdefString)
 
NSString * StringFromRandomSeed (Random_Seed seed)
 
NSString * OOStringFromDeciCredits (OOCreditsQuantity tenthsOfCredits, BOOL includeDecimal, BOOL includeSymbol)
 
OOINLINE NSString * OOStringFromIntCredits (OOCreditsQuantity integerCredits, BOOL includeSymbol)
 
OOINLINE NSString * OOCredits (OOCreditsQuantity tenthsOfCredits)
 
OOINLINE NSString * OOIntCredits (OOCreditsQuantity integerCredits)
 
NSString * OOPadStringToEms (NSString *string, float numEms)
 
NSArray * ComponentsFromVersionString (NSString *string)
 
NSComparisonResult CompareVersions (NSArray *version1, NSArray *version2)
 
NSString * ClockToString (double clock, BOOL adjusting)
 

Function Documentation

◆ ClockToString()

NSString * ClockToString ( double clock,
BOOL adjusting )

Definition at line 418 of file OOStringParsing.m.

419{
420 int days, hrs, mins, secs;
421 NSString *format = nil;
422
423 days = floor(clock / 86400.0);
424 secs = floor(clock - days * 86400.0);
425 hrs = floor(secs / 3600.0);
426 secs %= 3600;
427 mins = floor(secs / 60.0);
428 secs %= 60;
429
430 if (adjusting) format = DESC(@"clock-format-adjusting");
431 else format = DESC(@"clock-format");
432
433 return [NSString stringWithFormat:format, days, hrs, mins, secs];
434}
return nil
#define DESC(key)
Definition Universe.h:839

Referenced by ClockClockStringForTime().

+ Here is the caller graph for this function:

◆ CompareVersions()

NSComparisonResult CompareVersions ( NSArray * version1,
NSArray * version2 )

Definition at line 386 of file OOStringParsing.m.

387{
388 NSEnumerator *leftEnum = nil,
389 *rightEnum = nil;
390 NSNumber *leftComponent = nil,
391 *rightComponent = nil;
392 unsigned leftValue,
393 rightValue;
394
395 leftEnum = [version1 objectEnumerator];
396 rightEnum = [version2 objectEnumerator];
397
398 for (;;)
399 {
400 leftComponent = [leftEnum nextObject];
401 rightComponent = [rightEnum nextObject];
402
403 if (leftComponent == nil && rightComponent == nil) break; // End of both versions
404
405 // We'll get 0 if the component is nil, which is what we want.
406 leftValue = [leftComponent unsignedIntValue];
407 rightValue = [rightComponent unsignedIntValue];
408
409 if (leftValue < rightValue) return NSOrderedAscending;
410 if (leftValue > rightValue) return NSOrderedDescending;
411 }
412
413 // If there was a difference, we'd have returned already.
414 return NSOrderedSame;
415}

References nil.

Referenced by OoliteCompareVersion().

+ Here is the caller graph for this function:

◆ ComponentsFromVersionString()

NSArray * ComponentsFromVersionString ( NSString * string)

Definition at line 359 of file OOStringParsing.m.

360{
361 NSArray *stringComponents = nil;
362 NSMutableArray *result = nil;
363 NSUInteger i, count;
364 int value;
365 id component;
366
367 stringComponents = [string componentsSeparatedByString:@" "];
368 stringComponents = [[stringComponents objectAtIndex:0] componentsSeparatedByString:@"-"];
369 stringComponents = [[stringComponents objectAtIndex:0] componentsSeparatedByString:@"."];
370 count = [stringComponents count];
371 result = [NSMutableArray arrayWithCapacity:count];
372
373 for (i = 0; i != count; ++i)
374 {
375 component = [stringComponents objectAtIndex:i];
376 if ([component respondsToSelector:@selector(intValue)]) value = MAX([component intValue], 0);
377 else value = 0;
378
379 [result addObject:[NSNumber numberWithUnsignedInt:value]];
380 }
381
382 return result;
383}
#define MAX(A, B)
Definition OOMaths.h:114
unsigned count

References count, MAX, and nil.

Referenced by OoliteCompareVersion(), and VersionComponents().

+ Here is the caller graph for this function:

◆ OOCredits()

OOINLINE NSString * OOCredits ( OOCreditsQuantity tenthsOfCredits)

Definition at line 59 of file OOStringParsing.h.

60{
61 return OOStringFromDeciCredits(tenthsOfCredits, YES, YES);
62}
NSString * OOStringFromDeciCredits(OOCreditsQuantity tenthsOfCredits, BOOL includeDecimal, BOOL includeSymbol)

References OOStringFromDeciCredits().

Referenced by Operator_cr(), and Operator_dcr().

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

◆ OOIntCredits()

OOINLINE NSString * OOIntCredits ( OOCreditsQuantity integerCredits)

Definition at line 63 of file OOStringParsing.h.

64{
65 return OOStringFromIntCredits(integerCredits, YES);
66}
OOINLINE NSString * OOStringFromIntCredits(OOCreditsQuantity integerCredits, BOOL includeSymbol)

References OOStringFromIntCredits().

Referenced by Operator_icr(), and Operator_idcr().

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

◆ OOPadStringToEms()

NSString * OOPadStringToEms ( NSString * string,
float numEms )

Definition at line 264 of file OOStringParsing.m.

265{
266 NSString *result = string;
267 float numEms = padEms - OOStringWidthInEm(result);
268 if (numEms>0)
269 {
270 numEms /= OOStringWidthInEm(@" "); // start with wide space
271 result=[[@"" stringByPaddingToLength:(NSUInteger)numEms withString: @" " startingAtIndex:0] stringByAppendingString: result];
272 }
273 // most of the way there, so switch to narrow space
274 numEms = padEms - OOStringWidthInEm(result);
275 if (numEms>0)
276 {
277 numEms /= OOStringWidthInEm(@"\037"); // 037 is narrow space
278 result=[[@"" stringByPaddingToLength:(NSUInteger)numEms withString: @"\037" startingAtIndex:0] stringByAppendingString: result];
279 }
280 return result;
281}
CGFloat OOStringWidthInEm(NSString *text)

References OOStringWidthInEm().

+ Here is the call graph for this function:

◆ OOStringFromDeciCredits()

NSString * OOStringFromDeciCredits ( OOCreditsQuantity tenthsOfCredits,
BOOL includeDecimal,
BOOL includeSymbol )

Definition at line 284 of file OOStringParsing.m.

285{
286 JSContext *context = OOJSAcquireContext();
287 JSObject *global = [[OOJavaScriptEngine sharedEngine] globalObject];
288 JSObject *fakeRoot;
289 jsval method;
290 jsval rval;
291 NSString *result = nil;
292 jsval exception;
293 BOOL hadException;
294
295 /* Because the |cr etc. formatting operators call this, and the
296 implementation may use string expansion, we need to ensure recursion
297 can't happen.
298 */
299 static BOOL reentrancyLock;
300 if (reentrancyLock) return [NSString stringWithFormat:@"%0.1f", tenthsOfCredits * 0.1];
301
302 reentrancyLock = YES;
303
304 hadException = JS_GetPendingException(context, &exception);
305 JS_ClearPendingException(context);
306
307 if (JS_GetMethodById(context, global, OOJSID("formatCredits"), &fakeRoot, &method))
308 {
309 jsval args[3];
310 if (JS_NewNumberValue(context, tenthsOfCredits * 0.1, &args[0]))
311 {
312 args[1] = OOJSValueFromBOOL(includeDecimal);
313 args[2] = OOJSValueFromBOOL(includeSymbol);
314
316 JS_CallFunctionValue(context, global, method, 3, args, &rval);
318
319 result = OOStringFromJSValue(context, rval);
320 }
321 }
322
323 if (hadException) JS_SetPendingException(context, exception);
324
325 OOJSRelinquishContext(context);
326
327 if (EXPECT_NOT(result == nil)) result = [NSString stringWithFormat:@"%li", (long)(tenthsOfCredits) / 10];
328
329 reentrancyLock = NO;
330
331 return result;
332}
#define EXPECT_NOT(x)
#define OOJSStopTimeLimiter()
#define OOJSStartTimeLimiter()
#define OOJSID(str)
Definition OOJSPropID.h:38
NSString * OOStringFromJSValue(JSContext *context, jsval value)
OOINLINE JSContext * OOJSAcquireContext(void)
OOINLINE jsval OOJSValueFromBOOL(int b) INLINE_CONST_FUNC
OOINLINE void OOJSRelinquishContext(JSContext *context)
typedef long(ZCALLBACK *tell_file_func) OF((voidpf opaque

References EXPECT_NOT, OOJavaScriptEngine::globalObject, nil, OOJSAcquireContext(), OOJSID, OOJSRelinquishContext(), OOJSStartTimeLimiter, OOJSStopTimeLimiter, OOJSValueFromBOOL(), OOStringFromJSValue(), and OOJavaScriptEngine::sharedEngine.

Referenced by OOCredits(), and OOStringFromIntCredits().

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

◆ OOStringFromIntCredits()

OOINLINE NSString * OOStringFromIntCredits ( OOCreditsQuantity integerCredits,
BOOL includeSymbol )

Definition at line 54 of file OOStringParsing.h.

55{
56 return OOStringFromDeciCredits(integerCredits * 10, NO, includeSymbol);
57}

References OOStringFromDeciCredits().

Referenced by OOIntCredits().

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

◆ PointFromString()

NSPoint PointFromString ( NSString * xyString)

Definition at line 209 of file OOStringParsing.m.

210{
211 NSArray *tokens = ScanTokensFromString(xyString);
212 NSPoint result = NSZeroPoint;
213
214 NSUInteger n_tokens = [tokens count];
215 if (n_tokens == 2)
216 {
217 result.x = [[tokens objectAtIndex:0] doubleValue];
218 result.y = [[tokens objectAtIndex:1] doubleValue];
219 }
220 return result;
221}
NSMutableArray * ScanTokensFromString(NSString *values)

References ScanTokensFromString().

+ Here is the call graph for this function:

◆ QuaternionFromString()

Quaternion QuaternionFromString ( NSString * wxyzString,
Quaternion defaultValue )

Definition at line 195 of file OOStringParsing.m.

196{
197 Quaternion result;
198 if (!ScanQuaternionFromString(wxyzString, &result)) result = defaultValue;
199 return result;
200}
BOOL ScanQuaternionFromString(NSString *wxyzString, Quaternion *outQuaternion)

References ScanQuaternionFromString().

+ Here is the call graph for this function:

◆ RandomSeedFromString()

Random_Seed RandomSeedFromString ( NSString * abcdefString)

Definition at line 224 of file OOStringParsing.m.

225{
226 Random_Seed result;
227 int abcdef[] = { 0, 0, 0, 0, 0, 0};
228 int i = 0;
229 NSString *error = nil;
230 NSScanner *scanner = [NSScanner scannerWithString:abcdefString];
231
232 while (![scanner isAtEnd] && i < 6 && !error)
233 {
234 if (![scanner scanInt:&abcdef[i++]]) error = @"could not scan a int value.";
235 }
236
237 if (!error && i < 6) error = @"found less than six int values.";
238
239 if (!error)
240 {
241 result.a = abcdef[0];
242 result.b = abcdef[1];
243 result.c = abcdef[2];
244 result.d = abcdef[3];
245 result.e = abcdef[4];
246 result.f = abcdef[5];
247 }
248 else
249 {
250 OOLogERR(kOOLogStringRandomSeedConversion, @"cannot make Random_Seed from '%@': %@", abcdefString, error);
251 result = kNilRandomSeed;
252 }
253
254 return result;
255}
#define OOLogERR(class, format,...)
Definition OOLogging.h:112
static NSString *const kOOLogStringRandomSeedConversion
const Random_Seed kNilRandomSeed

References Random_Seed::a, Random_Seed::b, Random_Seed::c, Random_Seed::d, Random_Seed::e, Random_Seed::f, kNilRandomSeed, kOOLogStringRandomSeedConversion, nil, and OOLogERR.

◆ ScanHPVectorFromString()

BOOL ScanHPVectorFromString ( NSString * xyzString,
HPVector * outVector )

Definition at line 103 of file OOStringParsing.m.

104{
105 Vector scanVector;
106 assert(outVector != NULL);
107 BOOL result = ScanVectorFromString(xyzString, &scanVector);
108 if (!result)
109 {
110 return NO;
111 }
112 *outVector = vectorToHPVector(scanVector);
113 return YES;
114}
BOOL ScanVectorFromString(NSString *xyzString, Vector *outVector)

References ScanVectorFromString().

Referenced by OOHPVectorFromObject().

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

◆ ScanQuaternionFromString()

BOOL ScanQuaternionFromString ( NSString * wxyzString,
Quaternion * outQuaternion )

Definition at line 116 of file OOStringParsing.m.

117{
118 GLfloat wxyz[] = {1.0, 0.0, 0.0, 0.0};
119 int i = 0;
120 NSString *error = nil;
121 NSScanner *scanner = nil;
122
123 assert(outQuaternion != NULL);
124 if (wxyzString == nil) return NO;
125
126 if (!error) scanner = [NSScanner scannerWithString:wxyzString];
127 while (![scanner isAtEnd] && i < 4 && !error)
128 {
129 if (![scanner scanFloat:&wxyz[i++]]) error = @"could not scan a float value.";
130 }
131
132 if (!error && i < 4) error = @"found less than four float values.";
133
134 if (!error)
135 {
136 outQuaternion->w = wxyz[0];
137 outQuaternion->x = wxyz[1];
138 outQuaternion->y = wxyz[2];
139 outQuaternion->z = wxyz[3];
140 quaternion_normalize(outQuaternion);
141 return YES;
142 }
143 else
144 {
145 OOLogERR(kOOLogStringQuaternionConversion, @"cannot make quaternion from '%@': %@", wxyzString, error);
146 return NO;
147 }
148}
static NSString *const kOOLogStringQuaternionConversion

References kOOLogStringQuaternionConversion, nil, and OOLogERR.

Referenced by OOQuaternionFromObject(), and QuaternionFromString().

+ Here is the caller graph for this function:

◆ ScanTokensFromString()

static NSMutableArray * ScanTokensFromString ( NSString * values)

Definition at line 335 of file GetMetadataForFile.m.

336{
337 NSMutableArray *result = nil;
338 NSScanner *scanner = nil;
339 NSString *token = nil;
340 static NSCharacterSet *space_set = nil;
341
342 if (values == nil) return [NSMutableArray array];
343 if (space_set == nil) space_set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
344
345 result = [NSMutableArray array];
346 scanner = [NSScanner scannerWithString:values];
347
348 while (![scanner isAtEnd])
349 {
350 [scanner ooliteScanCharactersFromSet:space_set intoString:NULL];
351 if ([scanner ooliteScanUpToCharactersFromSet:space_set intoString:&token])
352 {
353 [result addObject:token];
354 }
355 }
356
357 return result;
358}

References EXPECT_NOT, and nil.

Referenced by OORoleSet::anyRole, PointFromString(), SanitizeActionStatement(), and SanitizeCondition().

+ Here is the caller graph for this function:

◆ ScanVectorAndQuaternionFromString()

BOOL ScanVectorAndQuaternionFromString ( NSString * xyzwxyzString,
Vector * outVector,
Quaternion * outQuaternion )

Definition at line 151 of file OOStringParsing.m.

152{
153 GLfloat xyzwxyz[] = { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0};
154 int i = 0;
155 NSString *error = nil;
156 NSScanner *scanner = nil;
157
158 assert(outVector != NULL && outQuaternion != NULL);
159 if (xyzwxyzString == nil) return NO;
160
161 if (!error) scanner = [NSScanner scannerWithString:xyzwxyzString];
162 while (![scanner isAtEnd] && i < 7 && !error)
163 {
164 if (![scanner scanFloat:&xyzwxyz[i++]]) error = @"Could not scan a float value.";
165 }
166
167 if (!error && i < 7) error = @"Found less than seven float values.";
168
169 if (error)
170 {
171 OOLogERR(kOOLogStringQuaternionConversion, @"cannot make vector and quaternion from '%@': %@", xyzwxyzString, error);
172 return NO;
173 }
174
175 outVector->x = xyzwxyz[0];
176 outVector->y = xyzwxyz[1];
177 outVector->z = xyzwxyz[2];
178 outQuaternion->w = xyzwxyz[3];
179 outQuaternion->x = xyzwxyz[4];
180 outQuaternion->y = xyzwxyz[5];
181 outQuaternion->z = xyzwxyz[6];
182
183 return YES;
184}

References kOOLogStringQuaternionConversion, nil, and OOLogERR.

◆ ScanVectorFromString()

BOOL ScanVectorFromString ( NSString * xyzString,
Vector * outVector )

Definition at line 73 of file OOStringParsing.m.

74{
75 GLfloat xyz[] = {0.0, 0.0, 0.0};
76 int i = 0;
77 NSString *error = nil;
78 NSScanner *scanner = nil;
79
80 assert(outVector != NULL);
81 if (xyzString == nil) return NO;
82
83 if (!error) scanner = [NSScanner scannerWithString:xyzString];
84 while (![scanner isAtEnd] && i < 3 && !error)
85 {
86 if (![scanner scanFloat:&xyz[i++]]) error = @"could not scan a float value.";
87 }
88
89 if (!error && i < 3) error = @"found less than three float values.";
90
91 if (!error)
92 {
93 *outVector = make_vector(xyz[0], xyz[1], xyz[2]);
94 return YES;
95 }
96 else
97 {
98 OOLogERR(kOOLogStringVectorConversion, @"cannot make vector from '%@': %@", xyzString, error);
99 return NO;
100 }
101}
static NSString *const kOOLogStringVectorConversion

References kOOLogStringVectorConversion, nil, and OOLogERR.

Referenced by OOVectorFromObject(), ScanHPVectorFromString(), and VectorFromString().

+ Here is the caller graph for this function:

◆ StringFromPoint()

NSString * StringFromPoint ( NSPoint point)

Definition at line 203 of file OOStringParsing.m.

204{
205 return [NSString stringWithFormat:@"%f %f", point.x, point.y];
206}

◆ StringFromRandomSeed()

NSString * StringFromRandomSeed ( Random_Seed seed)

Definition at line 258 of file OOStringParsing.m.

259{
260 return [NSString stringWithFormat: @"%d %d %d %d %d %d", seed.a, seed.b, seed.c, seed.d, seed.e, seed.f];
261}

◆ VectorFromString()

Vector VectorFromString ( NSString * xyzString,
Vector defaultValue )

Definition at line 187 of file OOStringParsing.m.

188{
189 Vector result;
190 if (!ScanVectorFromString(xyzString, &result)) result = defaultValue;
191 return result;
192}

References ScanVectorFromString().

+ Here is the call graph for this function: