-
Notifications
You must be signed in to change notification settings - Fork 0
/
TLIGCFile.m
444 lines (367 loc) · 13.2 KB
/
TLIGCFile.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//
// TLIGCFile.m
// Tagalog
//
// Created by Nathan Vander Wilt on 10/10/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "TLIGCFile.h"
#import "TLLineParser.h"
/* http://www.fai.org/gliding/system/files/tech_spec_gnss.pdf */
@interface TLIGCFile () <TLLineParserDelegate>
@end
@interface NSData (TLAdditions)
- (const char*)tl_readRange:(NSRange)range;
@end
@interface NSString (TLAdditions)
+ (id)tl_stringWithData:(NSData*)data
range:(NSRange)range
encoding:(NSStringEncoding)encoding;
@end
@interface NSDate (TLAdditions)
+ (id)tl_baseDateFromDDMMYY:(const char[6])datePtr;
+ (NSNumber*)tl_timeIntervalFromHHMMSS:(const char[6])timePtr;
@end
@interface NSNumber (TLAdditions)
+ (id)tl_numberFromData:(NSData*)data
range:(NSRange)range;
@end
@implementation TLIGCFile
@synthesize header;
@synthesize headerSources;
@synthesize fixes;
- (id)initWithContentsOfURL:(NSURL*)url error:(NSError**)err {
self = [super init];
if (self) {
header = [NSMutableDictionary new];
//headerSources = [NSMutableDictionary new]; // NOTE: not currently used by clients
fixes = [NSMutableArray new];
TLLineParser* theParser = [[[TLLineParser alloc]
initWithContentsOfURL:url] autorelease];
[theParser setDelegate:self];
parser = theParser;
[theParser parse];
parser = nil;
if ([theParser parserError]) {
if (err) *err = [theParser parserError];
[self release];
return nil;
}
}
return self;
}
- (void)dealloc {
[header release];
[headerSources release];
[fixes release];
[super dealloc];
}
NSString* const TLIGCHeaderSourceRecorder = @"Flight recorder";
NSString* const TLIGCHeaderSourceObserver = @"Official observer";
NSString* const TLIGCHeaderSourcePilot = @"Pilot";
NSString* const TLIGCDateKey = @"Date";
// value is NSDate* representing 0Z on date
NSString* const TLIGCDatumKey = @"Geodetic datum";
NSString* const TLIGCDatumWGS84 = @"WGS-84";
NSString* const TLIGCDatumTextKey = @"Geodetic datum text";
// value is NSString* with datum text
static NSString* const TLIGCFixExtensionsKey = @"Fix extensions";
/* "Header prefix length" - part that foundHeader reads (HSCCC)
Kept cryptic to fit nicely in NSMakeRange() lines */
static const size_t hpl = 5;
- (NSDictionary*)readHeaderDTE:(NSData*)lineData {
NSMutableDictionary* info = [NSMutableDictionary dictionary];
const char* datePtr = [lineData tl_readRange:NSMakeRange(hpl, 6)];
if (datePtr) {
NSDate* flightDate = [NSDate tl_baseDateFromDDMMYY:datePtr];
if (flightDate) {
[info setObject:flightDate forKey:TLIGCDateKey];
}
}
return info;
}
- (NSDictionary*)readHeaderDTM:(NSData*)lineData {
NSMutableDictionary* info = [NSMutableDictionary dictionary];
NSNumber* datumCodeValue = [NSNumber tl_numberFromData:lineData range:NSMakeRange(hpl, 3)];
if (datumCodeValue) switch ([datumCodeValue intValue]) {
case 100:
[info setObject:TLIGCDatumWGS84 forKey:TLIGCDatumKey];
break;
default:
[info setObject:
[NSString stringWithFormat:@"Unknown (%i)", (int)[datumCodeValue intValue]]
forKey:TLIGCDatumKey];
}
const size_t datumTextIdx = hpl + 3 + 9; // 9 == strlen("GPSDATUM:");
if (datumTextIdx < [lineData length]) {
size_t datumTextLength = [lineData length] - datumTextIdx;
NSString* datumText = [NSString tl_stringWithData:lineData
range:NSMakeRange(datumTextIdx, datumTextLength)
encoding:NSASCIIStringEncoding];
[info setObject:datumText forKey:TLIGCDatumTextKey];
}
return info;
}
- (void)foundHeader:(NSData*)lineData {
NSString* source = nil;
const char* sourcePtr = [lineData tl_readRange:NSMakeRange(1, 1)];
if (sourcePtr) switch (*sourcePtr) {
case 'F':
source = TLIGCHeaderSourceRecorder;
break;
case 'O':
source = TLIGCHeaderSourceObserver;
break;
case 'P':
source = TLIGCHeaderSourcePilot;
break;
default:
source = [NSString stringWithFormat:@"Unknown (%c)", *sourcePtr];
}
NSDictionary* subInfo = nil;
const char* subtypePtr = [lineData tl_readRange:NSMakeRange(2, 3)];
if (subtypePtr) {
NSString* headerParserName = [NSString stringWithFormat:@"readHeader%c%c%c:",
subtypePtr[0], subtypePtr[1], subtypePtr[2]];
SEL headerParser = NSSelectorFromString(headerParserName);
if ([self respondsToSelector:headerParser]) {
subInfo = [self performSelector:headerParser withObject:lineData];
}
}
if (subInfo) {
[header addEntriesFromDictionary:subInfo];
if (source) {
for (NSString* key in subInfo) {
[headerSources setObject:source forKey:key];
}
}
}
//NSLog(@"header: %@", subInfo);
}
- (void)foundFixFormatting:(NSData*)lineData {
NSMutableDictionary* fixExtensions = [NSMutableDictionary dictionary];
NSNumber* countValue = [NSNumber tl_numberFromData:lineData
range:NSMakeRange(1, 2)];
size_t s = 3;
NSUInteger fieldsFound = 0;
do {
NSNumber* startIdxValue = [NSNumber tl_numberFromData:lineData range:NSMakeRange(s, 2)];
NSNumber* finishIdxValue = [NSNumber tl_numberFromData:lineData range:NSMakeRange(s+2, 2)];
NSString* code = [NSString tl_stringWithData:lineData range:NSMakeRange(s+4, 3)
encoding:NSASCIIStringEncoding];
if (!code) break;
if (startIdxValue && finishIdxValue) {
NSUInteger startIdx = [startIdxValue unsignedIntegerValue];
NSUInteger finishIdx = [finishIdxValue unsignedIntegerValue];
if (startIdx < finishIdx) {
NSRange extensionRange = NSMakeRange(startIdx, finishIdx - startIdx);
[fixExtensions setObject:[NSValue valueWithRange:extensionRange] forKey:code];
}
}
fieldsFound++;
s += 7;
} while (s < [lineData length]);
if (fieldsFound != [countValue unsignedIntegerValue]) {
// TODO: turn into stored NSError warning
NSLog(@"Incorrect number of fields found in fix format information (line %lu)",
[parser lineNumber]);
}
[header setObject:fixExtensions forKey:TLIGCFixExtensionsKey];
//NSLog(@"fixExtensions: %@", fixExtensions);
}
NSString* const TLIGCFixTimeKey = @"Timestamp";
NSString* const TLIGCFixLatitudeKey = @"Latitude";
NSString* const TLIGCFixLongitudeKey = @"Longitude";
NSString* const TLIGCFixPressureAltitudeKey = @"Pressure alitude";
NSString* const TLIGCFixValidityKey = @"Fix validity";
NSString* const TLIGCFixValidity2D = @"2D or invalid fix";
NSString* const TLIGCFixValidity3D = @"3D fix";
NSString* const TLIGCFixGeoidAltitudeKey = @"Height above geoid";
NSString* const TLIGCFixAccuracyKey = @"Accuracy (2-sigma EPE)";
NSString* const TLIGCFixEngineNoiseKey = @"Engine noise level";
- (void)foundFix:(NSData*)lineData {
NSMutableDictionary* fix = [NSMutableDictionary dictionary];
const char* timePtr = [lineData tl_readRange:NSMakeRange(1, 6)];
if (timePtr) {
NSNumber* timeValue = [NSDate tl_timeIntervalFromHHMMSS:timePtr];
if (timeValue) {
[fix setObject:timeValue forKey:TLIGCFixTimeKey];
}
}
NSNumber* latDegValue = [NSNumber tl_numberFromData:lineData range:NSMakeRange(7, 2)];
NSNumber* latMinValue = [NSNumber tl_numberFromData:lineData range:NSMakeRange(9, 5)];
const char* latDirectionValuePtr = [lineData tl_readRange:NSMakeRange(14, 1)];
if (latDegValue && latMinValue && latDirectionValuePtr) {
double latitude = [latDegValue doubleValue] + [latMinValue integerValue] / 1000.0 / 60.0;
if (*latDirectionValuePtr == 'S') {
latitude = -latitude;
}
[fix setObject:[NSNumber numberWithDouble:latitude] forKey:TLIGCFixLatitudeKey];
}
NSNumber* lonDegValue = [NSNumber tl_numberFromData:lineData range:NSMakeRange(15, 3)];
NSNumber* lonMinValue = [NSNumber tl_numberFromData:lineData range:NSMakeRange(18, 5)];
const char* lonDirectionValuePtr = [lineData tl_readRange:NSMakeRange(23, 1)];
if (lonDegValue && lonMinValue && lonDirectionValuePtr) {
double longitude = [lonDegValue doubleValue] + [lonMinValue integerValue] / 1000.0 / 60.0;
if (*lonDirectionValuePtr == 'W') {
longitude = -longitude;
}
[fix setObject:[NSNumber numberWithDouble:longitude] forKey:TLIGCFixLongitudeKey];
}
const char* fixValidityPtr = [lineData tl_readRange:NSMakeRange(24, 1)];
if (fixValidityPtr) {
NSString* fixValidity = nil;
switch (*fixValidityPtr) {
case 'A':
fixValidity = TLIGCFixValidity3D;
break;
case 'V':
fixValidity = TLIGCFixValidity2D;
break;
default:
fixValidity = [NSString stringWithFormat:@"Unknown (%c)", *fixValidityPtr];
}
[fix setObject:fixValidity forKey:TLIGCFixValidityKey];
}
NSNumber* pressureAltValue = [NSNumber tl_numberFromData:lineData range:NSMakeRange(25, 5)];
if (pressureAltValue) {
// http://en.wikipedia.org/wiki/International_Standard_Atmosphere
[fix setObject:pressureAltValue forKey:TLIGCFixGeoidAltitudeKey];
}
NSNumber* geoidAltValue = [NSNumber tl_numberFromData:lineData range:NSMakeRange(30, 5)];
if (geoidAltValue) {
[fix setObject:geoidAltValue forKey:TLIGCFixPressureAltitudeKey];
}
NSDictionary* fixExtensions = [header objectForKey:TLIGCFixExtensionsKey];
for (NSString* extension in fixExtensions) {
NSRange extensionRange = [[fixExtensions objectForKey:extension] rangeValue];
NSString* extensionKey = nil;
id extensionValue = nil;
if ([extension isEqualToString:@"FXA"]) {
extensionKey = TLIGCFixAccuracyKey;
extensionValue = [NSNumber tl_numberFromData:lineData range:extensionRange];
}
else if ([extension isEqualToString:@"ENL"]) {
extensionKey = TLIGCFixEngineNoiseKey;
extensionValue = [NSNumber tl_numberFromData:lineData range:extensionRange];
}
else {
extensionKey = extension;
extensionValue = [NSString tl_stringWithData:lineData
range:extensionRange
encoding:NSASCIIStringEncoding];
[fix setObject:extensionValue forKey:extension];
}
// LAD, LOD, TDS, VXA
if (extensionValue) {
[fix setObject:extensionValue forKey:extensionKey];
}
}
[fixes addObject:fix];
//NSLog(@"fix: %@", fix);
}
- (void)lineParser:(TLLineParser*)theParser foundLine:(NSData*)lineData {
(void)theParser;
const char* lineTypePtr = [lineData tl_readRange:NSMakeRange(0, 1)];
if (lineTypePtr) switch (*lineTypePtr) {
case 'H':
[self foundHeader:lineData];
break;
case 'I':
[self foundFixFormatting:lineData];
break;
case 'B':
[self foundFix:lineData];
break;
default:
; // ignore
}
}
@end
@implementation NSString (TLAdditions)
+ (id)tl_stringWithData:(NSData*)data
range:(NSRange)range
encoding:(NSStringEncoding)encoding
{
const char* stringPtr = [data tl_readRange:range];
if (!stringPtr) return nil;
NSString* string = [[NSString alloc] initWithBytes:stringPtr
length:range.length
encoding:encoding];
return [string autorelease];
}
@end
@implementation NSData (TLAdditions)
- (const char*)tl_readRange:(NSRange)range {
return ((range.location + range.length <= [self length]) ?
[self bytes] + range.location : NULL);
}
@end
@implementation NSDate (TLAdditions)
+ (id)tl_baseDateFromDDMMYY:(const char[6])datePtr {
if (!datePtr) return nil;
// text will be interpreted as if in this timezone
NSTimeZone* const theTimezone = [NSTimeZone timeZoneForSecondsFromGMT:0];
// timestamps will be this many years or less into the future
const char windowYears = 1;
unsigned char twoDigitYear = 0;
unsigned char month = 0;
unsigned char day = 0;
int numArgumentsFilled = sscanf(datePtr, "%2hhu%2hhu%2hhu",
&day, &month, &twoDigitYear);
if (numArgumentsFilled != 3) return nil;
NSDate* now = [NSDate date];
NSCalendar* gregorian = [[[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
[gregorian setTimeZone:theTimezone];
NSDateComponents* components = [gregorian components:kCFCalendarUnitYear fromDate:now];
NSInteger yearNow = [components year];
NSInteger hundredsNow = 100 * (yearNow / 100);
// window timestamp
NSInteger year = hundredsNow + twoDigitYear;
if (year > yearNow + windowYears) {
year -= 100;
}
NSDateComponents* dayComponents = [[NSDateComponents new] autorelease];
[dayComponents setYear:year];
[dayComponents setMonth:month];
[dayComponents setDay:day];
return [gregorian dateFromComponents:dayComponents];
}
+ (NSNumber*)tl_timeIntervalFromHHMMSS:(const char[6])timePtr {
unsigned char hours = 0;
unsigned char minutes = 0;
unsigned char seconds = 0;
int argumentsFilled = sscanf(timePtr, "%2hhu%2hhu%2hhu",
&hours, &minutes, &seconds);
NSNumber* timeInterval = nil;
if (argumentsFilled == 3) {
NSTimeInterval interval = (hours * 60.0 * 60.0) + (minutes * 60.0) + seconds;
timeInterval = [NSNumber numberWithDouble:interval];
}
return timeInterval;
}
@end
@implementation NSNumber (TLAdditions)
+ (id)tl_numberFromData:(NSData*)data
range:(NSRange)range
{
NSAssert1(range.length < 325,
@"Unsupported width (%lu) for number string.", (long unsigned)range.length);
const char* numberPtr = [data tl_readRange:range];
if (!numberPtr || !range.length) return nil;
// NOTE: currently only integer values supported
#define maxFormatLength 7 // NNN%li0
char formatBuffer[maxFormatLength] = {};
int len = snprintf(formatBuffer, maxFormatLength, "%%%luld", (long unsigned)range.length);
NSAssert(len < maxFormatLength, @"Couldn't create parse format string.");
#undef maxFormatLength
long number = 0;
int numArgumentsFilled = sscanf(numberPtr, formatBuffer, &number);
NSNumber* numberValue = nil;
if (numArgumentsFilled == 1) {
numberValue = [NSNumber numberWithLong:number];
}
return numberValue;
}
@end