-
Notifications
You must be signed in to change notification settings - Fork 1
/
DBCSVReader.m
325 lines (287 loc) · 8.57 KB
/
DBCSVReader.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
/* -*- mode: objc -*-
Project: DataBasin
Copyright (C) 2009-2015 Free Software Foundation
Author: Riccardo Mottola
Created: 2009-06-24 22:34:06 +0200 by Riccardo Mottola
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02111 USA.
*/
#import "DBCSVReader.h"
#import "DBLoggerProtocol.h"
@implementation DBCSVReader
- (id)initWithPath:(NSString *)filePath withLogger:(id<DBLoggerProtocol>)l
{
if ((self = [self initWithPath:filePath byParsingHeaders:YES withLogger:l]))
{
}
return self;
}
- (id)initWithPath:(NSString *)filePath byParsingHeaders:(BOOL)parseHeader withLogger:(id<DBLoggerProtocol>)l
{
if ((self = [super init]))
{
NSString *fileContentString;
NSRange firstNLRange;
NSRange firstCRRange;
logger = l;
newLine = @"\n";
currentLine = 0;
fileContentString = [NSString stringWithContentsOfFile:filePath];
[logger log:LogStandard :@"[DBCVSReader initWithPath] analyzing file\n"];
if (fileContentString)
{
firstNLRange = [fileContentString rangeOfString:@"\n"];
firstCRRange = [fileContentString rangeOfString:@"\r"];
if (firstCRRange.location == NSNotFound)
{
/* it can be NL only */
if (firstNLRange.location > 0)
{
[logger log:LogStandard :@"[DBCVSReader initWithPath] standard unix-style\n"];
newLine = @"\n";
}
else
NSLog(@"could not determine line ending style");
}
else
{
/* it could be CR or CR+NL */
if (firstNLRange.location != NSNotFound && firstNLRange.location > 0)
{
if (firstNLRange.location - firstCRRange.location == 1)
{
[logger log:LogStandard :@"[DBCVSReader initWithPath] standard DOS-style\n"];
newLine=@"\r\n";
}
else
[logger log:LogStandard :@"[DBCVSReader initWithPath] ambiguous, using unix-style\n"];
}
else if (firstCRRange.location > 0)
{
[logger log:LogStandard :@"[DBCVSReader initWithPath] old mac-style\n"];
newLine = @"\r";
}
else
[logger log:LogStandard :@"[DBCVSReader initWithPath] could not determine line ending style\n"];
}
linesArray = [fileContentString componentsSeparatedByString:newLine];
if (linesArray && [linesArray count])
[linesArray retain];
else
linesArray = nil;
[self setQualifier:@"\""];
[self setSeparator:@","];
if (linesArray && parseHeader)
{
[self parseHeaders];
currentLine++;
}
}
}
return self;
}
- (void)dealloc
{
[qualifier release];
[separator release];
[linesArray release];
[fieldNames release];
[super dealloc];
}
- (void)setLogger:(id<DBLoggerProtocol>)l
{
logger = l;
}
- (void)setQualifier: (NSString *)q
{
if (qualifier != q)
{
isQualified = NO;
[qualifier release];
qualifier = [q retain];
if (linesArray && [linesArray count])
{
NSString *l;
l = [linesArray objectAtIndex:0];
if ([l length] > 0)
{
if ([[l substringToIndex:[qualifier length]] isEqualToString: qualifier])
isQualified = YES;
}
else
[logger log:LogStandard :@"[DBCVSReader setQualifier] Header Line empty?\n"];
}
[logger log:LogStandard :@"[DBCVSReader setQualifier] Is file qualified? %d\n", isQualified];
}
}
- (void)setSeparator: (NSString *)sep
{
if (separator != sep)
{
[separator release];
separator = [sep retain];
}
}
/**
Returns an array with the field names. parseHeaders needs to be called once before.
*/
- (NSArray *)fieldNames
{
return fieldNames;
}
/**
Extracts the field names from the first line. Needs to be (re)called after field separator
and qualifier characters are set.
*/
- (void)parseHeaders
{
[fieldNames release];
fieldNames = [[NSArray arrayWithArray:[self decodeOneLine:[linesArray objectAtIndex:0]]] retain];
[logger log:LogDebug :@"[DBCVSReader fieldNames] %@\n", fieldNames];
}
- (NSArray *)readDataSet
{
NSMutableArray *set;
NSString *line;
set = [NSMutableArray arrayWithCapacity:1];
while ((line = [self readLine]) != nil)
{
NSArray *record;
currentLine++;
record = [self decodeOneLine:line];
// NSLog(@"record %@", record);
if (record != nil)
[set addObject:record];
}
return set;
}
- (NSArray *)decodeOneLine:(NSString *)line
{
NSScanner *scanner;
NSMutableArray *record;
NSString *field;
if ([line length] == 0)
return nil;
scanner = [NSScanner scannerWithString:line];
[scanner setCharactersToBeSkipped:nil];
record = [NSMutableArray arrayWithCapacity:1];
if (isQualified)
{
NSString *token;
BOOL inField;
BOOL inQualifier;
field = @"";
//NSLog(@"loc %lu, total length: %lu", [scanner scanLocation], [line length]);
inField = NO;
inQualifier = NO;
while (![scanner isAtEnd])
{
NSUInteger loc;
token = nil;
[scanner scanUpToString:qualifier intoString:&token];
loc = [scanner scanLocation];
if ([token hasPrefix:separator])
{
if (inField)
{
field = [field stringByAppendingString: token];
}
else
{
[record addObject:field];
/* some CSV's do not put qualifiers for empty strings, leading to ,, here */
if ([token length] == 2 && [token hasSuffix:separator])
[record addObject:@""];
field = @"";
}
[scanner scanString:separator intoString:(NSString **)nil];
}
else if (loc > 0 && [line characterAtIndex:(loc-1)] == [qualifier characterAtIndex:0])
{
if (token)
field = [field stringByAppendingString: token];
if (!inField && !inQualifier)
{
inQualifier = YES;
inField = YES;
}
else if (inQualifier) /* inField : don't care */
{
/* it was a qualified qualifier */
field = [field stringByAppendingString: qualifier];
inQualifier = NO;
inField = YES;
}
else /* inField && !inQualifier */
{
inQualifier = YES;
inField = NO;
}
[scanner scanString:qualifier intoString:(NSString **)nil];
}
else if (token)
{
field = [field stringByAppendingString: token];
inField = YES;
}
else
{
/* let's skip this qualifier */
[scanner scanString:qualifier intoString:(NSString **)nil];
inQualifier = inField;
inField = !inField;
}
} // while scanLocation
if (field)
[record addObject:field];
}
else
{
while ([scanner scanLocation] < [line length])
{
NSUInteger scanLocation;
NSUInteger scanLocation2;
if ([scanner scanUpToString:separator intoString:&field] == YES)
{
if (field == nil)
field = @"";
}
else
{
field = @"";
}
[record addObject:field];
scanLocation = [scanner scanLocation];
[scanner scanString:separator intoString:(NSString **)nil];
scanLocation2 = [scanner scanLocation];
if ((scanLocation2 == [line length]) && (scanLocation != scanLocation2))
{
/* the last is empty and was skipped, we add it */
[record addObject:@""];
}
}
}
if ([fieldNames count] > 0 && ([fieldNames count] != [record count]))
[logger log:LogStandard :@"[DBCVSReader decodeOneLine] decoded fields (%d) do not match header count (%d): %@\n", [record count], [fieldNames count], record];
[logger log:LogDebug :@"[DBCVSReader decodeOneLine] decoded record: %@\n", record];
return record;
}
- (NSString *)readLine
{
if (currentLine < [linesArray count])
{
return [linesArray objectAtIndex:currentLine];
}
return nil;
}
@end