-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCHCSVParser.m
More file actions
1034 lines (825 loc) · 33.4 KB
/
CHCSVParser.m
File metadata and controls
1034 lines (825 loc) · 33.4 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// CHCSVParser.m
// CHCSVParser
/**
Copyright (c) 2014 Dave DeLong
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
**/
#import "CHCSVParser.h"
#if !__has_feature(objc_arc)
#error CHCSVParser requires ARC. If the rest of your project is non-ARC, add the "-fobjc-arc" compiler flag for this file.
#endif
NSString *const CHCSVErrorDomain = @"com.davedelong.csv";
#define CHUNK_SIZE 512
#define DOUBLE_QUOTE '"'
#define COMMA ','
#define OCTOTHORPE '#'
#define EQUAL '='
#define BACKSLASH '\\'
#define NULLCHAR '\0'
@interface CHCSVParser ()
@property (assign) NSUInteger totalBytesRead;
@end
@implementation CHCSVParser {
NSInputStream *_stream;
NSStringEncoding _streamEncoding;
NSMutableData *_stringBuffer;
NSMutableString *_string;
NSCharacterSet *_validFieldCharacters;
NSUInteger _nextIndex;
NSInteger _fieldIndex;
NSRange _fieldRange;
NSMutableString *_sanitizedField;
unichar _delimiter;
NSError *_error;
NSUInteger _currentRecord;
BOOL _cancelled;
}
- (id)initWithCSVString:(NSString *)csv {
return [self initWithDelimitedString:csv delimiter:COMMA];
}
- (instancetype)initWithDelimitedString:(NSString *)string delimiter:(unichar)delimiter {
NSInputStream *stream = [NSInputStream inputStreamWithData:[string dataUsingEncoding:NSUTF8StringEncoding]];
return [self initWithInputStream:stream usedEncoding:NULL delimiter:delimiter];
}
- (instancetype)initWithContentsOfCSVURL:(NSURL *)csvURL {
return [self initWithContentsOfDelimitedURL:csvURL delimiter:COMMA];
}
- (instancetype)initWithContentsOfDelimitedURL:(NSURL *)URL delimiter:(unichar)delimiter {
NSInputStream *stream = [NSInputStream inputStreamWithURL:URL];
return [self initWithInputStream:stream usedEncoding:NULL delimiter:delimiter];
}
- (id)initWithInputStream:(NSInputStream *)stream usedEncoding:(NSStringEncoding *)encoding delimiter:(unichar)delimiter {
NSParameterAssert(stream);
NSParameterAssert(delimiter);
NSAssert([[NSCharacterSet newlineCharacterSet] characterIsMember:delimiter] == NO, @"The field delimiter may not be a newline");
NSAssert(delimiter != DOUBLE_QUOTE, @"The field delimiter may not be a double quote");
self = [super init];
if (self) {
_stream = stream;
[_stream open];
_stringBuffer = [[NSMutableData alloc] init];
_string = [[NSMutableString alloc] init];
_delimiter = delimiter;
_nextIndex = 0;
_recognizesComments = NO;
_recognizesBackslashesAsEscapes = NO;
_sanitizesFields = NO;
_sanitizedField = [[NSMutableString alloc] init];
_trimsWhitespace = NO;
_recognizesLeadingEqualSign = NO;
NSMutableCharacterSet *m = [[NSCharacterSet newlineCharacterSet] mutableCopy];
NSString *invalid = [NSString stringWithFormat:@"%c%C", DOUBLE_QUOTE, _delimiter];
[m addCharactersInString:invalid];
_validFieldCharacters = [m invertedSet];
if (encoding == NULL || *encoding == 0) {
// we need to determine the encoding
[self _sniffEncoding];
if (encoding) {
*encoding = _streamEncoding;
}
} else {
_streamEncoding = *encoding;
}
}
return self;
}
- (void)dealloc {
[_stream close];
}
#pragma mark -
- (void)setRecognizesBackslashesAsEscapes:(BOOL)recognizesBackslashesAsEscapes {
_recognizesBackslashesAsEscapes = recognizesBackslashesAsEscapes;
if (_delimiter == BACKSLASH && _recognizesBackslashesAsEscapes) {
[NSException raise:NSInternalInconsistencyException format:@"Cannot recognize backslashes as escapes when using '\\' as the delimiter"];
}
}
- (void)setRecognizesComments:(BOOL)recognizesComments {
_recognizesComments = recognizesComments;
if (_delimiter == OCTOTHORPE && _recognizesComments) {
[NSException raise:NSInternalInconsistencyException format:@"Cannot recognize comments when using '#' as the delimiter"];
}
}
- (void)setRecognizesLeadingEqualSign:(BOOL)recognizesLeadingEqualSign {
_recognizesLeadingEqualSign = recognizesLeadingEqualSign;
if (_delimiter == EQUAL && _recognizesLeadingEqualSign) {
[NSException raise:NSInternalInconsistencyException format:@"Cannot recognize leading equal sign when using '=' as the delimiter"];
}
}
#pragma mark -
- (void)_sniffEncoding {
NSStringEncoding encoding = NSUTF8StringEncoding;
uint8_t bytes[CHUNK_SIZE];
NSInteger readLength = [_stream read:bytes maxLength:CHUNK_SIZE];
if (readLength > 0 && readLength <= CHUNK_SIZE) {
[_stringBuffer appendBytes:bytes length:readLength];
[self setTotalBytesRead:[self totalBytesRead] + readLength];
NSInteger bomLength = 0;
if (readLength > 3 && bytes[0] == 0x00 && bytes[1] == 0x00 && bytes[2] == 0xFE && bytes[3] == 0xFF) {
encoding = NSUTF32BigEndianStringEncoding;
bomLength = 4;
} else if (readLength > 3 && bytes[0] == 0xFF && bytes[1] == 0xFE && bytes[2] == 0x00 && bytes[3] == 0x00) {
encoding = NSUTF32LittleEndianStringEncoding;
bomLength = 4;
} else if (readLength > 3 && bytes[0] == 0x1B && bytes[1] == 0x24 && bytes[2] == 0x29 && bytes[3] == 0x43) {
encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingISO_2022_KR);
bomLength = 4;
} else if (readLength > 1 && bytes[0] == 0xFE && bytes[1] == 0xFF) {
encoding = NSUTF16BigEndianStringEncoding;
bomLength = 2;
} else if (readLength > 1 && bytes[0] == 0xFF && bytes[1] == 0xFE) {
encoding = NSUTF16LittleEndianStringEncoding;
bomLength = 2;
} else if (readLength > 2 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) {
encoding = NSUTF8StringEncoding;
bomLength = 3;
} else {
NSString *bufferAsUTF8 = nil;
for (NSInteger triedLength = 0; triedLength < 4; ++triedLength) {
bufferAsUTF8 = [[NSString alloc] initWithBytes:bytes length:readLength-triedLength encoding:NSUTF8StringEncoding];
if (bufferAsUTF8 != nil) {
break;
}
}
if (bufferAsUTF8 != nil) {
encoding = NSUTF8StringEncoding;
} else {
NSLog(@"unable to determine stream encoding; assuming MacOSRoman");
encoding = NSMacOSRomanStringEncoding;
}
}
if (bomLength > 0) {
[_stringBuffer replaceBytesInRange:NSMakeRange(0, bomLength) withBytes:NULL length:0];
}
}
_streamEncoding = encoding;
}
- (void)_loadMoreIfNecessary {
NSUInteger stringLength = [_string length];
NSUInteger reloadPortion = stringLength / 3;
if (reloadPortion < 10) { reloadPortion = 10; }
if (_nextIndex+reloadPortion >= stringLength && [_stream hasBytesAvailable]) {
// read more from the stream
uint8_t buffer[CHUNK_SIZE];
NSInteger readBytes = [_stream read:buffer maxLength:CHUNK_SIZE];
if (readBytes > 0) {
// append it to the buffer
[_stringBuffer appendBytes:buffer length:readBytes];
[self setTotalBytesRead:[self totalBytesRead] + readBytes];
}
}
if ([_stringBuffer length] > 0) {
// try to turn the next portion of the buffer into a string
NSUInteger readLength = [_stringBuffer length];
while (readLength > 0) {
NSString *readString = [[NSString alloc] initWithBytes:[_stringBuffer bytes] length:readLength encoding:_streamEncoding];
if (readString == nil) {
readLength--;
} else {
[_string appendString:readString];
break;
}
};
[_stringBuffer replaceBytesInRange:NSMakeRange(0, readLength) withBytes:NULL length:0];
}
}
- (void)_advance {
[self _loadMoreIfNecessary];
_nextIndex++;
}
- (unichar)_peekCharacter {
[self _loadMoreIfNecessary];
if (_nextIndex >= [_string length]) { return NULLCHAR; }
return [_string characterAtIndex:_nextIndex];
}
- (unichar)_peekPeekCharacter {
[self _loadMoreIfNecessary];
NSUInteger nextNextIndex = _nextIndex+1;
if (nextNextIndex >= [_string length]) { return NULLCHAR; }
return [_string characterAtIndex:nextNextIndex];
}
#pragma mark -
- (void)parse {
@autoreleasepool {
[self _beginDocument];
_currentRecord = 0;
while ([self _parseRecord]) {
; // yep;
}
if (_error != nil) {
[self _error];
} else {
[self _endDocument];
}
}
}
- (void)cancelParsing {
_cancelled = YES;
}
- (BOOL)_parseRecord {
while ([self _peekCharacter] == OCTOTHORPE && _recognizesComments) {
[self _parseComment];
}
if ([self _peekCharacter] != NULLCHAR) {
@autoreleasepool {
[self _beginRecord];
while (1) {
if (![self _parseField]) {
break;
}
if (![self _parseDelimiter]) {
break;
}
}
[self _endRecord];
}
}
BOOL followedByNewline = [self _parseNewline];
return (followedByNewline && _error == nil && [self _peekCharacter] != NULLCHAR);
}
- (BOOL)_parseNewline {
if (_cancelled) { return NO; }
NSUInteger charCount = 0;
unichar peek = [self _peekCharacter];
unichar peekPeek = [self _peekPeekCharacter];
if (peek == '\r' && peekPeek == '\n') {
// assume \r\n is a single delimiter
charCount += 2;
[self _advance];
[self _advance];
} else if ([[NSCharacterSet newlineCharacterSet] characterIsMember:peek]) {
charCount++;
[self _advance];
}
return (charCount > 0);
}
- (BOOL)_parseComment {
[self _advance]; // consume the octothorpe
NSCharacterSet *newlines = [NSCharacterSet newlineCharacterSet];
[self _beginComment];
BOOL isBackslashEscaped = NO;
while (1) {
if (isBackslashEscaped == NO) {
unichar next = [self _peekCharacter];
if (next == BACKSLASH && _recognizesBackslashesAsEscapes) {
isBackslashEscaped = YES;
[self _advance];
} else if ([newlines characterIsMember:next] == NO && next != NULLCHAR) {
[self _advance];
} else {
// it's a newline
break;
}
} else {
isBackslashEscaped = YES;
[self _advance];
}
}
[self _endComment];
return [self _parseNewline];
}
- (void)_parseFieldWhitespace {
NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet];
while ([self _peekCharacter] != NULLCHAR &&
[whitespace characterIsMember:[self _peekCharacter]] &&
[self _peekCharacter] != _delimiter) {
if (_trimsWhitespace == NO) {
[_sanitizedField appendFormat:@"%C", [self _peekCharacter]];
// if we're sanitizing fields, then these characters would be stripped (because they're not appended to _sanitizedField)
}
[self _advance];
}
}
- (BOOL)_parseField {
if (_cancelled) { return NO; }
BOOL parsedField = NO;
[self _beginField];
// consume leading whitespace
[self _parseFieldWhitespace];
if ([self _peekCharacter] == DOUBLE_QUOTE) {
parsedField = [self _parseEscapedField];
} else if (_recognizesLeadingEqualSign && [self _peekCharacter] == EQUAL && [self _peekPeekCharacter] == DOUBLE_QUOTE) {
[self _advance]; // consume the equal sign
parsedField = [self _parseEscapedField];
} else {
parsedField = [self _parseUnescapedField];
if (_trimsWhitespace) {
NSString *trimmedString = [_sanitizedField stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[_sanitizedField setString:trimmedString];
}
}
if (parsedField) {
// consume trailing whitespace
[self _parseFieldWhitespace];
[self _endField];
}
return parsedField;
}
- (BOOL)_parseEscapedField {
[self _advance]; // consume the opening double quote
NSCharacterSet *newlines = [NSCharacterSet newlineCharacterSet];
BOOL isBackslashEscaped = NO;
while (1) {
unichar next = [self _peekCharacter];
if (next == NULLCHAR) { break; }
if (isBackslashEscaped == NO) {
if (next == BACKSLASH && _recognizesBackslashesAsEscapes) {
isBackslashEscaped = YES;
[self _advance]; // consume the backslash
} else if ([_validFieldCharacters characterIsMember:next] ||
[newlines characterIsMember:next] ||
next == _delimiter) {
[_sanitizedField appendFormat:@"%C", next];
[self _advance];
} else if (next == DOUBLE_QUOTE && [self _peekPeekCharacter] == DOUBLE_QUOTE) {
[_sanitizedField appendFormat:@"%C", next];
[self _advance];
[self _advance];
} else {
// not valid, or it's not a doubled double quote
break;
}
} else {
[_sanitizedField appendFormat:@"%C", next];
isBackslashEscaped = NO;
[self _advance];
}
}
if ([self _peekCharacter] == DOUBLE_QUOTE) {
[self _advance];
return YES;
}
return NO;
}
- (BOOL)_parseUnescapedField {
NSCharacterSet *newlines = [NSCharacterSet newlineCharacterSet];
BOOL isBackslashEscaped = NO;
while (1) {
unichar next = [self _peekCharacter];
if (next == NULLCHAR) { break; }
if (isBackslashEscaped == NO) {
if (next == BACKSLASH && _recognizesBackslashesAsEscapes) {
isBackslashEscaped = YES;
[self _advance];
} else if ([newlines characterIsMember:next] == YES || next == _delimiter) {
break;
} else {
[_sanitizedField appendFormat:@"%C", next];
[self _advance];
}
} else {
isBackslashEscaped = NO;
[_sanitizedField appendFormat:@"%C", next];
[self _advance];
}
}
return YES;
}
- (BOOL)_parseDelimiter {
unichar next = [self _peekCharacter];
if (next == _delimiter) {
[self _advance];
return YES;
}
if (next != NULLCHAR && [[NSCharacterSet newlineCharacterSet] characterIsMember:next] == NO) {
NSString *description = [NSString stringWithFormat:@"Unexpected delimiter. Expected '%C' (0x%X), but got '%C' (0x%X)", _delimiter, _delimiter, [self _peekCharacter], [self _peekCharacter]];
_error = [[NSError alloc] initWithDomain:CHCSVErrorDomain code:CHCSVErrorCodeInvalidFormat userInfo:@{NSLocalizedDescriptionKey : description}];
}
return NO;
}
- (void)_beginDocument {
if ([_delegate respondsToSelector:@selector(parserDidBeginDocument:)]) {
[_delegate parserDidBeginDocument:self];
}
}
- (void)_endDocument {
if ([_delegate respondsToSelector:@selector(parserDidEndDocument:)]) {
[_delegate parserDidEndDocument:self];
}
}
- (void)_beginRecord {
if (_cancelled) { return; }
_fieldIndex = 0;
_currentRecord++;
if ([_delegate respondsToSelector:@selector(parser:didBeginLine:)]) {
[_delegate parser:self didBeginLine:_currentRecord];
}
}
- (void)_endRecord {
if (_cancelled) { return; }
if ([_delegate respondsToSelector:@selector(parser:didEndLine:)]) {
[_delegate parser:self didEndLine:_currentRecord];
}
}
- (void)_beginField {
if (_cancelled) { return; }
[_sanitizedField setString:@""];
_fieldRange.location = _nextIndex;
}
- (void)_endField {
if (_cancelled) { return; }
_fieldRange.length = (_nextIndex - _fieldRange.location);
NSString *field = nil;
if (_sanitizesFields) {
field = [_sanitizedField copy];
} else {
field = [_string substringWithRange:_fieldRange];
if (_trimsWhitespace) {
field = [field stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
}
if ([_delegate respondsToSelector:@selector(parser:didReadField:atIndex:)]) {
[_delegate parser:self didReadField:field atIndex:_fieldIndex];
}
[_string replaceCharactersInRange:NSMakeRange(0, NSMaxRange(_fieldRange)) withString:@""];
_nextIndex = 0;
_fieldIndex++;
}
- (void)_beginComment {
if (_cancelled) { return; }
_fieldRange.location = _nextIndex;
}
- (void)_endComment {
if (_cancelled) { return; }
_fieldRange.length = (_nextIndex - _fieldRange.location);
if ([_delegate respondsToSelector:@selector(parser:didReadComment:)]) {
NSString *comment = [_string substringWithRange:_fieldRange];
[_delegate parser:self didReadComment:comment];
}
[_string replaceCharactersInRange:NSMakeRange(0, NSMaxRange(_fieldRange)) withString:@""];
_nextIndex = 0;
}
- (void)_error {
if (_cancelled) { return; }
if ([_delegate respondsToSelector:@selector(parser:didFailWithError:)]) {
[_delegate parser:self didFailWithError:_error];
}
}
@end
@implementation CHCSVWriter {
NSOutputStream *_stream;
NSStringEncoding _streamEncoding;
NSData *_delimiter;
NSData *_bom;
NSCharacterSet *_illegalCharacters;
NSUInteger _currentLine;
NSUInteger _currentField;
NSMutableArray *_firstLineKeys;
}
- (instancetype)initForWritingToCSVFile:(NSString *)path {
NSOutputStream *stream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
return [self initWithOutputStream:stream encoding:NSUTF8StringEncoding delimiter:COMMA];
}
- (instancetype)initWithOutputStream:(NSOutputStream *)stream encoding:(NSStringEncoding)encoding delimiter:(unichar)delimiter {
self = [super init];
if (self) {
_stream = stream;
_streamEncoding = encoding;
if ([_stream streamStatus] == NSStreamStatusNotOpen) {
[_stream open];
}
NSData *a = [@"a" dataUsingEncoding:_streamEncoding];
NSData *aa = [@"aa" dataUsingEncoding:_streamEncoding];
if ([a length] * 2 != [aa length]) {
NSUInteger characterLength = [aa length] - [a length];
_bom = [a subdataWithRange:NSMakeRange(0, [a length] - characterLength)];
[self _writeData:_bom];
}
NSString *delimiterString = [NSString stringWithFormat:@"%C", delimiter];
NSData *delimiterData = [delimiterString dataUsingEncoding:_streamEncoding];
if ([_bom length] > 0) {
_delimiter = [delimiterData subdataWithRange:NSMakeRange([_bom length], [delimiterData length] - [_bom length])];
} else {
_delimiter = delimiterData;
}
NSMutableCharacterSet *illegalCharacters = [[NSCharacterSet newlineCharacterSet] mutableCopy];
[illegalCharacters addCharactersInString:delimiterString];
[illegalCharacters addCharactersInString:@"\""];
_illegalCharacters = [illegalCharacters copy];
_firstLineKeys = [NSMutableArray array];
}
return self;
}
- (void)dealloc {
[self closeStream];
}
- (void)_writeData:(NSData *)data {
if ([data length] > 0) {
const void *bytes = [data bytes];
[_stream write:bytes maxLength:[data length]];
}
}
- (void)_writeString:(NSString *)string {
NSData *stringData = [string dataUsingEncoding:_streamEncoding];
NSRange subRange = NSMakeRange(0, stringData.length);
if ([_bom length] > 0) {
subRange.location = _bom.length;
subRange.length -= _bom.length;
}
if (stringData.length <= 0) { return; }
const int8_t *bytes = stringData.bytes;
if (bytes[subRange.location + subRange.length - 1] == NULLCHAR) {
subRange.length -= 1;
}
if (stringData.length <= 0) { return; }
if (subRange.length != stringData.length) {
stringData = [stringData subdataWithRange:subRange];
}
[self _writeData:stringData];
}
- (void)_writeDelimiter {
[self _writeData:_delimiter];
}
- (void)writeField:(id)field {
if (_currentField > 0) {
[self _writeDelimiter];
}
if (_currentLine == 0) {
[_firstLineKeys addObject:field];
}
NSString *string = field ? [field description] : @"";
if ([string rangeOfCharacterFromSet:_illegalCharacters].location != NSNotFound) {
// replace double quotes with double double quotes
string = [string stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""];
// surround in double quotes
string = [NSString stringWithFormat:@"\"%@\"", string];
}
[self _writeString:string];
_currentField++;
}
- (void)finishLine {
[self _writeString:@"\n"];
_currentField = 0;
_currentLine++;
}
- (void)_finishLineIfNecessary {
if (_currentField != 0) {
[self finishLine];
}
}
- (void)writeLineOfFields:(id<NSFastEnumeration>)fields {
[self _finishLineIfNecessary];
for (id field in fields) {
[self writeField:field];
}
[self finishLine];
}
- (void)writeLineWithDictionary:(NSDictionary *)dictionary {
if (_currentLine == 0) {
[NSException raise:NSInternalInconsistencyException format:@"Cannot write a dictionary unless a line of keys has already been given"];
}
[self _finishLineIfNecessary];
for (id key in _firstLineKeys) {
id value = [dictionary objectForKey:key];
[self writeField:value];
}
[self finishLine];
}
- (void)writeComment:(NSString *)comment {
[self _finishLineIfNecessary];
NSArray *lines = [comment componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
for (NSString *line in lines) {
NSString *commented = [NSString stringWithFormat:@"#%@\n", line];
[self _writeString:commented];
}
}
- (void)closeStream {
[_stream close];
_stream = nil;
}
@end
#pragma mark - Convenience Categories
@interface _CHCSVAggregator : NSObject <CHCSVParserDelegate>
@property (strong) NSMutableArray *lines;
@property (strong) NSError *error;
@property (strong) NSMutableArray *currentLine;
@end
@implementation _CHCSVAggregator
- (void)parserDidBeginDocument:(CHCSVParser *)parser {
self.lines = [[NSMutableArray alloc] init];
}
- (void)parser:(CHCSVParser *)parser didBeginLine:(NSUInteger)recordNumber {
self.currentLine = [[NSMutableArray alloc] init];
}
- (void)parser:(CHCSVParser *)parser didEndLine:(NSUInteger)recordNumber {
[self.lines addObject:self.currentLine];
self.currentLine = nil;
}
- (void)parser:(CHCSVParser *)parser didReadField:(NSString *)field atIndex:(NSInteger)fieldIndex {
[self.currentLine addObject:field];
}
- (void)parser:(CHCSVParser *)parser didFailWithError:(NSError *)error {
self.error = error;
self.lines = nil;
}
@end
@interface _CHCSVKeyedAggregator : _CHCSVAggregator
@property (strong) NSArray *firstLine;
@end
@implementation _CHCSVKeyedAggregator
- (void)parser:(CHCSVParser *)parser didEndLine:(NSUInteger)recordNumber {
if (self.firstLine == nil) {
self.firstLine = self.currentLine;
} else if (self.currentLine.count == self.firstLine.count) {
CHCSVOrderedDictionary *line = [[CHCSVOrderedDictionary alloc] initWithObjects:self.currentLine
forKeys:self.firstLine];
[self.lines addObject:line];
} else {
[parser cancelParsing];
self.error = [NSError errorWithDomain:CHCSVErrorDomain code:CHCSVErrorCodeIncorrectNumberOfFields userInfo:nil];
}
self.currentLine = nil;
}
@end
NSArray *_CHCSVParserParse(NSInputStream *inputStream, CHCSVParserOptions options, unichar delimiter, NSError *__autoreleasing *error);
NSArray *_CHCSVParserParse(NSInputStream *inputStream, CHCSVParserOptions options, unichar delimiter, NSError *__autoreleasing *error) {
CHCSVParser *parser = [[CHCSVParser alloc] initWithInputStream:inputStream usedEncoding:nil delimiter:delimiter];
BOOL usesFirstLineAsKeys = !!(options & CHCSVParserOptionsUsesFirstLineAsKeys);
_CHCSVAggregator *aggregator = usesFirstLineAsKeys ? [[_CHCSVKeyedAggregator alloc] init] : [[_CHCSVAggregator alloc] init];
parser.delegate = aggregator;
parser.recognizesBackslashesAsEscapes = !!(options & CHCSVParserOptionsRecognizesBackslashesAsEscapes);
parser.sanitizesFields = !!(options & CHCSVParserOptionsSanitizesFields);
parser.recognizesComments = !!(options & CHCSVParserOptionsRecognizesComments);
parser.trimsWhitespace = !!(options & CHCSVParserOptionsTrimsWhitespace);
parser.recognizesLeadingEqualSign = !!(options & CHCSVParserOptionsRecognizesLeadingEqualSign);
[parser parse];
if (aggregator.error != nil) {
if (error) {
*error = aggregator.error;
}
return nil;
} else {
return aggregator.lines;
}
}
@implementation NSArray (CHCSVAdditions)
+ (instancetype)arrayWithContentsOfCSVURL:(NSURL *)fileURL {
return [self arrayWithContentsOfDelimitedURL:fileURL options:0 delimiter:COMMA error:nil];
}
+ (instancetype)arrayWithContentsOfDelimitedURL:(NSURL *)fileURL delimiter:(unichar)delimiter {
return [self arrayWithContentsOfDelimitedURL:fileURL options:0 delimiter:delimiter error:nil];
}
+ (instancetype)arrayWithContentsOfCSVURL:(NSURL *)fileURL options:(CHCSVParserOptions)options {
return [self arrayWithContentsOfDelimitedURL:fileURL options:options delimiter:COMMA error:nil];
}
+ (instancetype)arrayWithContentsOfDelimitedURL:(NSURL *)fileURL options:(CHCSVParserOptions)options delimiter:(unichar)delimiter {
return [self arrayWithContentsOfDelimitedURL:fileURL options:options delimiter:delimiter error:nil];
}
+ (instancetype)arrayWithContentsOfDelimitedURL:(NSURL *)fileURL options:(CHCSVParserOptions)options delimiter:(unichar)delimiter error:(NSError *__autoreleasing *)error {
NSParameterAssert(fileURL);
NSInputStream *stream = [NSInputStream inputStreamWithURL:fileURL];
return _CHCSVParserParse(stream, options, delimiter, error);
}
- (NSString *)CSVString {
NSOutputStream *output = [NSOutputStream outputStreamToMemory];
CHCSVWriter *writer = [[CHCSVWriter alloc] initWithOutputStream:output encoding:NSUTF8StringEncoding delimiter:COMMA];
for (id object in self) {
if ([object conformsToProtocol:@protocol(NSFastEnumeration)]) {
[writer writeLineOfFields:object];
}
}
[writer closeStream];
NSData *buffer = [output propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
return [[NSString alloc] initWithData:buffer encoding:NSUTF8StringEncoding];
}
@end
@implementation NSString (CHCSVAdditions)
- (NSArray *)CSVComponents {
return [self componentsSeparatedByDelimiter:COMMA options:0 error:nil];
}
- (NSArray *)CSVComponentsWithOptions:(CHCSVParserOptions)options {
return [self componentsSeparatedByDelimiter:COMMA options:options error:nil];
}
- (NSArray *)componentsSeparatedByDelimiter:(unichar)delimiter {
return [self componentsSeparatedByDelimiter:delimiter options:0 error:nil];
}
- (NSArray *)componentsSeparatedByDelimiter:(unichar)delimiter options:(CHCSVParserOptions)options {
return [self componentsSeparatedByDelimiter:delimiter options:options error:nil];
}
- (NSArray *)componentsSeparatedByDelimiter:(unichar)delimiter options:(CHCSVParserOptions)options error:(NSError *__autoreleasing *)error {
NSData *csvData = [self dataUsingEncoding:NSUTF8StringEncoding];
NSInputStream *stream = [NSInputStream inputStreamWithData:csvData];
return _CHCSVParserParse(stream, options, delimiter, error);
}
@end
@implementation CHCSVOrderedDictionary {
NSArray *_keys;
NSArray *_values;
NSDictionary *_dictionary;
}
- (instancetype)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys {
self = [super init];
if (self) {
_keys = keys.copy;
_values = objects.copy;
_dictionary = [NSDictionary dictionaryWithObjects:_values forKeys:_keys];
}
return self;
}
- (instancetype)initWithObjects:(const id [])objects forKeys:(const id<NSCopying> [])keys count:(NSUInteger)cnt {
return [self initWithObjects:[NSArray arrayWithObjects:objects count:cnt]
forKeys:[NSArray arrayWithObjects:keys count:cnt]];
}
- (instancetype)init {
return [self initWithObjects:@[] forKeys:@[]];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
return [super initWithCoder:aDecoder];
}
- (NSArray *)allKeys {
return _keys;
}
- (NSArray *)allValues {
return _values;
}
- (NSUInteger)count {
return _dictionary.count;
}
- (id)objectForKey:(id)aKey {
return [_dictionary objectForKey:aKey];
}
- (NSEnumerator *)keyEnumerator {
return _keys.objectEnumerator;
}
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(__unsafe_unretained id [])buffer count:(NSUInteger)len {
return [_keys countByEnumeratingWithState:state objects:buffer count:len];
}
- (id)objectAtIndex:(NSUInteger)idx {
id key = [_keys objectAtIndex:idx];
return [self objectForKey:key];
}
- (id)objectAtIndexedSubscript:(NSUInteger)idx {
return [self objectAtIndex:idx];
}
- (NSUInteger)hash {
return _dictionary.hash;
}
- (BOOL)isEqual:(CHCSVOrderedDictionary *)object {
if ([super isEqual:object] && [object isKindOfClass:[CHCSVOrderedDictionary class]]) {
// we've determined that from a dictionary POV, they're equal
// now we need to test for key ordering
return [object->_keys isEqual:_keys];
}
return NO;
}
@end
#pragma mark - Deprecated methods
@implementation CHCSVParser (Deprecated)
- (id)initWithCSVString:(NSString *)csv delimiter:(unichar)delimiter {
return [self initWithDelimitedString:csv delimiter:delimiter];
}
- (id)initWithContentsOfCSVFile:(NSString *)csvFilePath {
return [self initWithContentsOfCSVURL:[NSURL fileURLWithPath:csvFilePath]];
}
- (id)initWithContentsOfCSVFile:(NSString *)csvFilePath delimiter:(unichar)delimiter {
return [self initWithContentsOfDelimitedURL:[NSURL fileURLWithPath:csvFilePath] delimiter:delimiter];
}
- (void)setStripsLeadingAndTrailingWhitespace:(BOOL)stripsLeadingAndTrailingWhitespace {
self.trimsWhitespace = stripsLeadingAndTrailingWhitespace;
}
- (BOOL)stripsLeadingAndTrailingWhitespace {
return self.trimsWhitespace;
}
@end
@implementation NSArray (CHCSVAdditions_Deprecated)
+ (instancetype)arrayWithContentsOfCSVFile:(NSString *)csvFilePath {
return [self arrayWithContentsOfDelimitedURL:[NSURL fileURLWithPath:csvFilePath] options:0 delimiter:COMMA error:nil];
}