FMCPlanner2/FMC Planner 2/NavDataParser.m

169 lines
6.0 KiB
Objective-C

//
// NavDataParser.m
// FMC Planner 2
//
// Created by Kilian Hofmann on 13.04.16.
// Copyright © 2016 Kilian Hofmann. All rights reserved.
//
#import "NavDataParser.h"
@implementation NavDataParser
NSString *const Airways = @"NAVDATA/awys.txt";
NSString *const Intersections = @"NAVDATA/ints.txt";
NSString *const Navaids = @"NAVDATA/navs.txt";
- (id)init
{
self.parserReady = false;
// Load Navaids
NSArray *temp = [[NSString
stringWithContentsOfFile:[SharedDeclerations savePathForFile:Navaids]
encoding:NSASCIIStringEncoding
error:nil] componentsSeparatedByString:@"\r\n"];
self.navaids = [[NSMutableDictionary alloc] init];
for (NSString *str in temp) {
if (str.length > 0 &&
![[str substringToIndex:1] isEqualToString:@";"]) {
if ([self.navaids objectForKey:[str substringToIndex:5]] != nil) {
// Item exists, make array and set that.
NSMutableArray *temp2 = [[NSMutableArray alloc] init];
[temp2 addObject:[str substringFromIndex:10]];
if ([[self.navaids objectForKey:[str substringToIndex:5]]
isKindOfClass:[NSArray class]]) {
[temp2 addObjectsFromArray:
[self.navaids
objectForKey:[str substringToIndex:5]]];
}
else {
[temp2
addObject:[self.navaids
objectForKey:[str substringToIndex:5]]];
}
[self.navaids setValue:temp2 forKey:[str substringToIndex:5]];
}
else {
[self.navaids setValue:[str substringFromIndex:10]
forKey:[str substringToIndex:5]];
}
}
}
// Load Intersections
temp = [[NSString
stringWithContentsOfFile:[SharedDeclerations
savePathForFile:Intersections]
encoding:NSASCIIStringEncoding
error:nil] componentsSeparatedByString:@"\r\n"];
self.intersections = [[NSMutableDictionary alloc] init];
for (NSString *str in temp) {
if (str.length > 0 &&
![[str substringToIndex:1] isEqualToString:@";"]) {
if ([self.intersections objectForKey:[str substringToIndex:5]] !=
nil) {
// Item exists, make array and set that.
NSMutableArray *temp2 = [[NSMutableArray alloc] init];
[temp2 addObject:[str substringFromIndex:10]];
if ([[self.intersections objectForKey:[str substringToIndex:5]]
isKindOfClass:[NSArray class]]) {
[temp2 addObjectsFromArray:
[self.intersections
objectForKey:[str substringToIndex:5]]];
}
else {
[temp2
addObject:[self.intersections
objectForKey:[str substringToIndex:5]]];
}
[self.intersections setValue:temp2
forKey:[str substringToIndex:5]];
}
else {
[self.intersections setValue:[str substringFromIndex:10]
forKey:[str substringToIndex:5]];
}
}
}
// Load Airways
temp = [[NSString
stringWithContentsOfFile:[SharedDeclerations savePathForFile:Airways]
encoding:NSASCIIStringEncoding
error:nil] componentsSeparatedByString:@"\r\n"];
self.airways = [[NSMutableDictionary alloc] init];
for (NSString *str in temp) {
if (str.length > 0 &&
![[str substringToIndex:1] isEqualToString:@";"]) {
if ([self.airways objectForKey:[str substringToIndex:5]] != nil) {
// Item exists, make array and set that.
NSMutableArray *temp2 = [[NSMutableArray alloc] init];
[temp2 addObject:[str substringFromIndex:10]];
if ([[self.airways objectForKey:[str substringToIndex:5]]
isKindOfClass:[NSArray class]]) {
[temp2 addObjectsFromArray:
[self.airways
objectForKey:[str substringToIndex:5]]];
}
else {
[temp2
addObject:[self.airways
objectForKey:[str substringToIndex:5]]];
}
[self.airways setValue:temp2 forKey:[str substringToIndex:5]];
}
else {
[self.airways setValue:[str substringFromIndex:10]
forKey:[str substringToIndex:5]];
}
}
}
self.parserReady = true;
return self;
}
#pragma mark - Search for elements in normal route entry
- (NSArray *)findAirway:(NSString *)airway
{
return nil;
}
/**
* Find the given waypoint in both files, returns Array so user can select
* @param waypoint
*/
- (NSArray *)findWaypoint:(NSString *)waypoint
{
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int i = (int)waypoint.length; i < 5; i++) {
waypoint = [waypoint stringByAppendingString:@" "];
}
id navaids = [_navaids objectForKey:waypoint];
id intersections = [_intersections objectForKey:waypoint];
if (navaids != nil) {
if ([navaids isKindOfClass:[NSArray class]]) {
[array addObjectsFromArray:navaids];
}
else {
[array addObject:navaids];
}
}
if (intersections != nil) {
if ([intersections isKindOfClass:[NSArray class]]) {
[array addObjectsFromArray:intersections];
}
else {
[array addObject:intersections];
}
}
return array;
}
@end