-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSVBloodPressureLog.m
170 lines (130 loc) · 4.15 KB
/
CSVBloodPressureLog.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
//
// CSVBloodPressureLog.m
// BPTracker
//
// Created by Robert Saccone on 12/2/11.
// Copyright (c) 2017 Robert Saccone. All rights reserved.
//
#import "CSVBloodPressureLog.h"
static NSString *delimeter = @",";
static NSString *eol = @"\r\n";
@interface CSVBloodPressureLog ()
- (void)writeColumnValue:(NSString *)value;
- (void)writeDelimeter;
- (void)writeEOL;
@property(nonatomic, strong) NSCharacterSet *illegalChars;
@property(nonatomic, readwrite, strong) NSMutableString *contents;
@end
@implementation CSVBloodPressureLog
{
@private
NSCharacterSet *illegalChars_;
}
@synthesize illegalChars = illegalChars_;
@synthesize contents=contents_;
static NSString *const columnHeaderKeys[] =
{
@"CSV_DATE_FIELD_TITLE",
@"CSV_SYSTOLIC_FIELD_TITLE",
@"CSV_DIASTOLIC_FIELD_TITLE",
@"CSV_PULSE_FIELD_TITLE",
@"CSV_WEIGHT_FIELD_TITLE",
@"CSV_NOTE_FIELD_TITLE",
nil
};
static void writeHeader(NSMutableString *logContents)
{
NSCAssert(logContents != nil, @"logContents == nil");
NSString * const *currColHdrKey = columnHeaderKeys;
while (*currColHdrKey != nil)
{
NSString * const *nextColHdrKey = currColHdrKey + 1;
NSString *colHdrStr = NSLocalizedString(*currColHdrKey, nil);
if (*nextColHdrKey != nil)
{
[logContents appendFormat:@"%@,", colHdrStr];
}
else
{
[logContents appendFormat:@"%@\r\n", colHdrStr];
}
currColHdrKey = nextColHdrKey;
}
}
- (id)init
{
self = [super init];
if (self)
{
NSMutableString *contents = [[NSMutableString alloc] init];
if (contents != nil)
{
NSMutableCharacterSet *bad = [NSMutableCharacterSet newlineCharacterSet];
[bad addCharactersInString:@"\"\\"];
[bad addCharactersInString:delimeter];
illegalChars_ = [bad copy];
writeHeader(contents);
contents_ = contents;
}
else
{
// Failure to allocate.
self = nil;
}
}
return self;
}
- (void)writeColumnValue:(NSString *)value
{
if ([value rangeOfCharacterFromSet:self.illegalChars].location != NSNotFound || [value hasPrefix:@"#"])
{
NSMutableString *writeable = [value mutableCopy];
[writeable replaceOccurrencesOfString:@"\"" withString:@"\"\"" options:NSLiteralSearch range:NSMakeRange(0, [writeable length])];
[writeable replaceOccurrencesOfString:@"\\" withString:@"\\\\" options:NSLiteralSearch range:NSMakeRange(0, [writeable length])];
[writeable insertString:@"\"" atIndex:0];
[writeable appendString:@"\""];
[self.contents appendString:writeable];
}
else
{
[self.contents appendString:value];
}
}
- (void)writeDelimeter
{
[self.contents appendString:delimeter];
}
- (void)writeEOL
{
[self.contents appendString:eol];
}
- (void)addReading:(BloodPressureReading *)bpReading;
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
NSString *colValue = [dateFormatter stringFromDate:bpReading.readingDate];
[self writeColumnValue:colValue];
[self writeDelimeter];
colValue = [NSString stringWithFormat:@"%hu", bpReading.systolic.shortValue];
[self writeColumnValue:colValue];
[self writeDelimeter];
colValue = [NSString stringWithFormat:@"%hu", bpReading.diastolic.shortValue];
[self writeColumnValue:colValue];
[self writeDelimeter];
colValue = [NSString stringWithFormat:@"%hu", bpReading.pulse.shortValue];
[self writeColumnValue:colValue];
[self writeDelimeter];
colValue = [NSString stringWithFormat:@"%hu", bpReading.weight.shortValue];
[self writeColumnValue:colValue];
[self writeDelimeter];
[self writeColumnValue:bpReading.note];
[self writeEOL];
}
- (NSString *)consumeContents
{
NSString * result = [[NSString alloc] initWithString:self.contents];
[self.contents setString:@""];
return result;
}
@end