forked from davedelong/CHCSVParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.m
76 lines (60 loc) · 2.09 KB
/
main.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
#import <Foundation/Foundation.h>
#import "CHCSVParser.h"
@interface Delegate : NSObject <CHCSVParserDelegate>
@property (readonly) NSArray *lines;
@end
@implementation Delegate {
NSMutableArray *_lines;
NSMutableArray *_currentLine;
}
- (void)dealloc {
[_lines release];
[super dealloc];
}
- (void)parserDidBeginDocument:(CHCSVParser *)parser {
[_lines release];
_lines = [[NSMutableArray alloc] init];
}
- (void)parser:(CHCSVParser *)parser didBeginLine:(NSUInteger)recordNumber {
_currentLine = [[NSMutableArray alloc] init];
}
- (void)parser:(CHCSVParser *)parser didReadField:(NSString *)field atIndex:(NSInteger)fieldIndex {
NSLog(@"%@", field);
[_currentLine addObject:field];
}
- (void)parser:(CHCSVParser *)parser didEndLine:(NSUInteger)recordNumber {
[_lines addObject:_currentLine];
[_currentLine release];
_currentLine = nil;
}
- (void)parserDidEndDocument:(CHCSVParser *)parser {
// NSLog(@"parser ended: %@", csvFile);
}
- (void)parser:(CHCSVParser *)parser didFailWithError:(NSError *)error {
NSLog(@"ERROR: %@", error);
_lines = nil;
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *file = @(__FILE__);
file = [[file stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Test.scsv"];
NSLog(@"Beginning...");
NSStringEncoding encoding = 0;
NSInputStream *stream = [NSInputStream inputStreamWithFileAtPath:file];
CHCSVParser * p = [[CHCSVParser alloc] initWithInputStream:stream usedEncoding:&encoding delimiter:';'];
[p setRecognizesBackslashesAsEscapes:YES];
[p setSanitizesFields:YES];
NSLog(@"encoding: %@", CFStringGetNameOfEncoding(CFStringConvertNSStringEncodingToEncoding(encoding)));
Delegate * d = [[Delegate alloc] init];
[p setDelegate:d];
NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
[p parse];
NSTimeInterval end = [NSDate timeIntervalSinceReferenceDate];
NSLog(@"raw difference: %f", (end-start));
NSLog(@"%@", [d lines]);
[d release];
[p release];
[pool drain];
return 0;
}