-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReader.m
149 lines (108 loc) · 5.19 KB
/
Reader.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
#import <Foundation/Foundation.h>
#import <stdio.h>
#import "Reader.h"
@implementation Reader
NSString * filePath; //Represents the location of the file to be read from.
@synthesize filePath;
-(id) init: (NSString*) tempPath
{
filePath = tempPath; //Initialize with desired file path.
return self;
}
-(NSArray*) read
{
//Get file contents from file path as array of lines.
NSString * fileContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
NSArray * allLinedStrings = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
//Filter out blank lines and comments.
NSPredicate * pred = [NSPredicate predicateWithFormat:@"(NOT (SELF LIKE %@)) AND (NOT (SELF BEGINSWITH %@))", @"", @"#"];
NSArray * filtered = [allLinedStrings filteredArrayUsingPredicate:pred];
return filtered;
}
-(int) timeExtractor
{
NSArray * filtered = [self read];
//Grab time value - always first line of filtered, always from fourth character onwards.
NSString * tValue = [[filtered objectAtIndex:0] substringFromIndex:4];
int t = [tValue intValue];
return t;
}
-(NSDictionary*) ratioExtractor
{
NSArray * filtered = [self read];
//To get ratio, first filter out time, amount, and formula lines.
NSPredicate * pred = [NSPredicate predicateWithFormat:@"(NOT (SELF BEGINSWITH %@)) AND (SELF MATCHES %@)", @"t", @"^[a-z] =.*"];
NSArray * tempRatios = [filtered filteredArrayUsingPredicate:pred];
NSMutableArray * ratioMolecules = [NSMutableArray new];
NSMutableArray * ratioValues = [NSMutableArray new];
//Iterate over ratios, extracting molecules and values and storing them in mutable arrays.
int i;
for (i = 0; i < tempRatios.count; i++) {
NSString * molecule = [tempRatios objectAtIndex:i];
NSNumber * value = [NSNumber numberWithFloat:[[molecule substringFromIndex:4] floatValue]];
[ratioMolecules insertObject:[molecule substringToIndex:1] atIndex:i];
[ratioValues insertObject:value atIndex:i];
}
//Finally, build dictionary from molecule/ratio arrays, with molecule as key and ratio as value.
NSDictionary * ratios = [NSDictionary dictionaryWithObjects:ratioValues forKeys:ratioMolecules];
[ratioMolecules release];
[ratioValues release];
return ratios;
}
-(NSMutableDictionary*) amountExtractor
{
NSArray * filtered = [self read];
//Filter out time, ratios, and formula lines.
NSPredicate * pred = [NSPredicate predicateWithFormat:@"(NOT (SELF BEGINSWITH %@)) AND (SELF MATCHES %@)", @"t", @"^[A-Z] =.*"];
NSArray * tempAmounts = [filtered filteredArrayUsingPredicate:pred];
NSMutableArray * amountMolecules = [NSMutableArray new];
NSMutableArray * amountValues = [NSMutableArray new];
//Iterate over amounts, extracting molecules and values and storing them in mutable arrays.
int i;
for (i = 0; i < tempAmounts.count; i++) {
NSString * molecule = [tempAmounts objectAtIndex:i];
NSNumber * value = [NSNumber numberWithFloat:[[molecule substringFromIndex:4] floatValue]];
[amountMolecules insertObject:[molecule substringToIndex:1] atIndex:i];
[amountValues insertObject:value atIndex:i];
}
//Finally, build dictionary from molecule/amount arrays, with molecule as key and amount as value.
NSMutableDictionary * amounts = [NSMutableDictionary dictionaryWithObjects:amountValues forKeys:amountMolecules];
[amountMolecules release];
[amountValues release];
return amounts;
}
-(NSArray*) formulaeExtractor
{
NSArray * filtered = [self read];
//Filter out time, ratio, and amounts.
NSPredicate * pred = [NSPredicate predicateWithFormat:@"(NOT (SELF BEGINSWITH %@)) AND (SELF MATCHES %@)", @"t", @"^[a-z] :.*"];
NSArray * tempFormulae = [filtered filteredArrayUsingPredicate:pred];
NSMutableArray * formulae = [NSMutableArray new];
//Iterate over formula, extracting molecules and formulae and storing them in mutable array. Individual formula are represented as
//an array containing a character (for kinetic ratio), and two arrays - one for reactants and one for products, both represented by
//their molecules.
int i;
for (i = 0; i < tempFormulae.count; i++) {
NSString * molecule = [tempFormulae objectAtIndex:i];
NSString * ratio = [molecule substringToIndex:1];
molecule = [molecule substringFromIndex:4];
NSCharacterSet * charSet = [NSCharacterSet characterSetWithCharactersInString:@"-"];
NSArray * seperated = [molecule componentsSeparatedByCharactersInSet:charSet];
NSMutableArray *formula = [NSMutableArray new];
[formula addObject:ratio];
charSet = [NSCharacterSet characterSetWithCharactersInString:@"+>: "];
NSString * firstHalf = [[seperated objectAtIndex:0] stringByTrimmingCharactersInSet:charSet];
NSString * secondHalf = [[seperated objectAtIndex:1] stringByTrimmingCharactersInSet:charSet];
charSet = [NSCharacterSet characterSetWithCharactersInString:@" + "];
NSArray * seperated1 = [firstHalf componentsSeparatedByCharactersInSet:charSet];
NSArray * seperated2 = [secondHalf componentsSeparatedByCharactersInSet:charSet];
NSPredicate * pred = [NSPredicate predicateWithFormat:@"NOT (SELF LIKE %@)", @""];
NSArray * cleanSeperated1 = [seperated1 filteredArrayUsingPredicate:pred];
NSArray * cleanSeperated2 = [seperated2 filteredArrayUsingPredicate:pred];
[formula addObject:cleanSeperated1];
[formula addObject:cleanSeperated2];
[formulae insertObject:formula atIndex:i];
}
return formulae;
}
@end