diff --git a/CHCSVParser/CHCSVParser/CHCSVParser.h b/CHCSVParser/CHCSVParser/CHCSVParser.h index 5d6d6f7..3c2eb13 100644 --- a/CHCSVParser/CHCSVParser/CHCSVParser.h +++ b/CHCSVParser/CHCSVParser/CHCSVParser.h @@ -436,6 +436,19 @@ typedef NS_OPTIONS(NSUInteger, CHCSVParserOptions) { */ + (instancetype)arrayWithContentsOfDelimitedURL:(NSURL *)fileURL options:(CHCSVParserOptions)options delimiter:(unichar)delimiter error:(NSError *__autoreleasing *)error; +/** + * A convenience constructor to parse a delimited file + * + * @param fileURL The @c NSURL to the delimited file + * @param options A bitwise-OR of @c CHCSVParserOptions to control how parsing should occur + * @param usedEncoding A pointer to an @c NSStringEncoding. A nil value tells CHCSVParser to guess the encoding. + * @param delimiter The delimiter used in the file + * @param error A pointer to an @c NSError*, which will be filled in if parsing fails + * + * @return An @c NSArray of @c NSArrays of @c NSStrings, if parsing succeeds; @c nil otherwise. + */ ++ (instancetype)arrayWithContentsOfDelimitedURL:(NSURL *)fileURL options:(CHCSVParserOptions)options usedEncoding:(inout NSStringEncoding *)usedEncoding delimiter:(unichar)delimiter error:(NSError *__autoreleasing *)error; + /** * If the receiver is an @c NSArray of @c NSArrays of objects, this will turn it into a comma-delimited string * Returns the string of CSV, if writing succeeds; @c nil otherwise. diff --git a/CHCSVParser/CHCSVParser/CHCSVParser.m b/CHCSVParser/CHCSVParser/CHCSVParser.m index 7c8cf34..6ba80e5 100644 --- a/CHCSVParser/CHCSVParser/CHCSVParser.m +++ b/CHCSVParser/CHCSVParser/CHCSVParser.m @@ -774,9 +774,9 @@ - (void)parser:(CHCSVParser *)parser didEndLine:(NSUInteger)recordNumber { @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]; +NSArray *_CHCSVParserParse(NSInputStream *inputStream, CHCSVParserOptions options, NSStringEncoding *usedEncoding, unichar delimiter, NSError *__autoreleasing *error); +NSArray *_CHCSVParserParse(NSInputStream *inputStream, CHCSVParserOptions options, NSStringEncoding *usedEncoding, unichar delimiter, NSError *__autoreleasing *error) { + CHCSVParser *parser = [[CHCSVParser alloc] initWithInputStream:inputStream usedEncoding:usedEncoding delimiter:delimiter]; BOOL usesFirstLineAsKeys = !!(options & CHCSVParserOptionsUsesFirstLineAsKeys); _CHCSVAggregator *aggregator = usesFirstLineAsKeys ? [[_CHCSVKeyedAggregator alloc] init] : [[_CHCSVAggregator alloc] init]; @@ -822,7 +822,14 @@ + (instancetype)arrayWithContentsOfDelimitedURL:(NSURL *)fileURL options:(CHCSVP NSParameterAssert(fileURL); NSInputStream *stream = [NSInputStream inputStreamWithURL:fileURL]; - return _CHCSVParserParse(stream, options, delimiter, error); + return _CHCSVParserParse(stream, options, nil, delimiter, error); +} + ++ (instancetype)arrayWithContentsOfDelimitedURL:(NSURL *)fileURL options:(CHCSVParserOptions)options usedEncoding:(inout NSStringEncoding *)usedEncoding delimiter:(unichar)delimiter error:(NSError *__autoreleasing *)error { + NSParameterAssert(fileURL); + NSInputStream *stream = [NSInputStream inputStreamWithURL:fileURL]; + + return _CHCSVParserParse(stream, options, usedEncoding, delimiter, error); } - (NSString *)CSVString { @@ -863,7 +870,7 @@ - (NSArray *)componentsSeparatedByDelimiter:(unichar)delimiter options:(CHCSVPar NSData *csvData = [self dataUsingEncoding:NSUTF8StringEncoding]; NSInputStream *stream = [NSInputStream inputStreamWithData:csvData]; - return _CHCSVParserParse(stream, options, delimiter, error); + return _CHCSVParserParse(stream, options, nil, delimiter, error); } @end