Skip to content

Commit

Permalink
Options for convenience methods so you don't have to lash up a full d…
Browse files Browse the repository at this point in the history
…elegate object. Fixes issue #26.
  • Loading branch information
davedelong committed Mar 6, 2013
1 parent 19f84d1 commit b5b4c89
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHCSVParser/CHCSVParser.h
Expand Up @@ -86,15 +86,23 @@ typedef NSInteger CHCSVErrorCode;

#pragma mark - Convenience Categories

typedef NS_OPTIONS(NSUInteger, CHCSVParserOptions) {
CHCSVParserOptionsRecognizesBackslashesAsEscapes = 1 << 0,
CHCSVParserOptionsSanitizesFields = 1 << 1,
CHCSVParserOptionsRecognizesComments = 1 << 2
};

@interface NSArray (CHCSVAdditions)

+ (instancetype)arrayWithContentsOfCSVFile:(NSString *)csvFilePath;
+ (instancetype)arrayWithContentsOfCSVFile:(NSString *)csvFilePath options:(CHCSVParserOptions)options;
- (NSString *)CSVString;

@end

@interface NSString (CHCSVAdditions)

- (NSArray *)CSVComponents;
- (NSArray *)CSVComponentsWithOptions:(CHCSVParserOptions)options;

@end
22 changes: 20 additions & 2 deletions CHCSVParser/CHCSVParser.m
Expand Up @@ -101,8 +101,8 @@ - (id)initWithInputStream:(NSInputStream *)stream usedEncoding:(NSStringEncoding

_nextIndex = 0;
_recognizesComments = NO;
_recognizesBackslashesAsEscapes = YES;
_sanitizesFields = YES;
_recognizesBackslashesAsEscapes = NO;
_sanitizesFields = NO;
_sanitizedField = [[NSMutableString alloc] init];

NSMutableCharacterSet *m = [[NSCharacterSet newlineCharacterSet] mutableCopy];
Expand Down Expand Up @@ -705,10 +705,19 @@ - (void)parser:(CHCSVParser *)parser didFailWithError:(NSError *)error {
@implementation NSArray (CHCSVAdditions)

+ (instancetype)arrayWithContentsOfCSVFile:(NSString *)csvFilePath {
return [self arrayWithContentsOfCSVFile:csvFilePath options:0];
}

+ (instancetype)arrayWithContentsOfCSVFile:(NSString *)csvFilePath options:(CHCSVParserOptions)options{
NSParameterAssert(csvFilePath);
_CHCSVAggregator *aggregator = [[_CHCSVAggregator alloc] init];
CHCSVParser *parser = [[CHCSVParser alloc] initWithContentsOfCSVFile:csvFilePath];
[parser setDelegate:aggregator];

[parser setRecognizesBackslashesAsEscapes:!!(options & CHCSVParserOptionsRecognizesBackslashesAsEscapes)];
[parser setSanitizesFields:!!(options & CHCSVParserOptionsSanitizesFields)];
[parser setRecognizesComments:!!(options & CHCSVParserOptionsRecognizesComments)];

[parser parse];
CHCSV_RELEASE(parser);

Expand Down Expand Up @@ -739,9 +748,18 @@ - (NSString *)CSVString {
@implementation NSString (CHCSVAdditions)

- (NSArray *)CSVComponents {
return [self CSVComponentsWithOptions:0];
}

- (NSArray *)CSVComponentsWithOptions:(CHCSVParserOptions)options {
_CHCSVAggregator *aggregator = [[_CHCSVAggregator alloc] init];
CHCSVParser *parser = [[CHCSVParser alloc] initWithCSVString:self];
[parser setDelegate:aggregator];

[parser setRecognizesBackslashesAsEscapes:!!(options & CHCSVParserOptionsRecognizesBackslashesAsEscapes)];
[parser setSanitizesFields:!!(options & CHCSVParserOptionsSanitizesFields)];
[parser setRecognizesComments:!!(options & CHCSVParserOptionsRecognizesComments)];

[parser parse];
CHCSV_RELEASE(parser);

Expand Down
5 changes: 3 additions & 2 deletions main.m
Expand Up @@ -34,8 +34,9 @@ - (void) parser:(CHCSVParser *)parser didFailWithError:(NSError *)error {

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString * file = @"/Users/dave/Developer/Open Source/Git Projects/CHCSVParser/Test.csv";
NSArray *a = [NSArray arrayWithContentsOfCSVFile:file];
NSString *file = @(__FILE__);
file = [[file stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Test.csv"];
NSArray *a = [NSArray arrayWithContentsOfCSVFile:file options:CHCSVParserOptionsRecognizesBackslashesAsEscapes | CHCSVParserOptionsSanitizesFields | CHCSVParserOptionsRecognizesComments];
NSLog(@"%@", a);
CHCSVParser *newP = [[CHCSVParser alloc] initWithContentsOfCSVFile:file];
// [newP setDelegate:[[[Delegate alloc] init] autorelease]];
Expand Down

0 comments on commit b5b4c89

Please sign in to comment.