forked from davedelong/CHCSVParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.m
100 lines (80 loc) · 2.85 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#import <Foundation/Foundation.h>
#import "CHCSV.h"
@interface Delegate : NSObject <CHCSVParserDelegate>
@end
@implementation Delegate
- (void) parser:(CHCSVParser *)parser didStartDocument:(NSString *)csvFile {
// NSLog(@"parser started: %@", csvFile);
}
- (void) parser:(CHCSVParser *)parser didStartLine:(NSUInteger)lineNumber {
// NSLog(@"Starting line: %lu", lineNumber);
}
- (void) parser:(CHCSVParser *)parser didReadField:(NSString *)field {
// NSLog(@" field: %@", field);
}
- (void) parser:(CHCSVParser *)parser didEndLine:(NSUInteger)lineNumber {
// NSLog(@"Ending line: %lu", lineNumber);
}
- (void) parser:(CHCSVParser *)parser didEndDocument:(NSString *)csvFile {
// NSLog(@"parser ended: %@", csvFile);
}
- (void) parser:(CHCSVParser *)parser didFailWithError:(NSError *)error {
NSLog(@"ERROR: %@", error);
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString * file = @"/Users/dave/Developer/Open Source/Git Projects/CHCSVParser/Test.csv";
/**
CHCSVWriter *big = [[CHCSVWriter alloc] initWithCSVFile:file atomic:NO];
for (int i = 0; i < 1000000; ++i) {
NSAutoreleasePool *inner = [[NSAutoreleasePool alloc] init];
for (int j = 0; j < 10; ++j) {
[big writeField:[NSString stringWithFormat:@"%d-%d", i, j]];
}
[big writeLine];
[inner drain];
}
[big closeFile];
[big release];
**/
/**
NSError * error = nil;
NSArray * rows = [[NSArray alloc] initWithContentsOfCSVFile:file usedEncoding:&encoding delimiter:@"\t" error:&error];
if ([rows count] == 0) {
NSLog(@"error: %@", error);
error = nil;
rows = [NSArray arrayWithContentsOfCSVFile:file encoding:NSUTF8StringEncoding error:&error];
}
NSLog(@"error: %@", error);
NSLog(@"%@", rows);
CHCSVWriter *w = [[CHCSVWriter alloc] initWithCSVFile:[NSTemporaryDirectory() stringByAppendingPathComponent:@"test.tsv"] atomic:NO];
[w setDelimiter:@"\t"];
for (NSArray *row in rows) {
[w writeLineWithFields:row];
}
[w closeFile];
[w release];
[rows release];
**/
NSLog(@"Beginning...");
NSStringEncoding encoding = 0;
NSInputStream *stream = [NSInputStream inputStreamWithFileAtPath:file];
NSError *error = nil;
CHCSVParser * p = [[CHCSVParser alloc] initWithStream:stream usedEncoding:&encoding error:&error];
NSLog(@"encoding: %@", CFStringGetNameOfEncoding(CFStringConvertNSStringEncodingToEncoding(encoding)));
Delegate * d = [[Delegate alloc] init];
[p setParserDelegate:d];
NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
[p parse];
NSTimeInterval end = [NSDate timeIntervalSinceReferenceDate];
NSLog(@"raw difference: %f", (end-start));
[d release];
NSArray *a = [NSArray arrayWithContentsOfCSVFile:file encoding:encoding error:nil];
NSLog(@"%@", a);
NSString *s = [a CSVString];
NSLog(@"%@", s);
[p release];
[pool drain];
return 0;
}