Skip to content

Commit

Permalink
Rewrote parser
Browse files Browse the repository at this point in the history
  • Loading branch information
davedelong committed Dec 17, 2012
1 parent 825dcf1 commit b5fb608
Show file tree
Hide file tree
Showing 4 changed files with 385 additions and 550 deletions.
94 changes: 23 additions & 71 deletions CHCSVParser.h
Original file line number Diff line number Diff line change
@@ -1,90 +1,42 @@
//
// CHCSVParser.h
// CHCSVParser
/**
Copyright (c) 2010 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.
**/
//
// Created by Dave DeLong on 9/22/12.
//
//

#import <Foundation/Foundation.h>

@protocol CHCSVParserDelegate;

@interface CHCSVParser : NSObject {

@private
__weak id<CHCSVParserDelegate> parserDelegate;
NSInputStream *csvReadStream;
BOOL endOfStreamReached;
NSStringEncoding fileEncoding;

NSString *csvFile;

BOOL hasStarted;
NSString *delimiter;
unichar delimiterCharacter;

NSMutableData *currentChunk;
NSMutableString *currentChunkString;
NSUInteger stringIndex;

BOOL balancedQuotes;
BOOL balancedEscapes;

NSMutableString *currentField;
NSUInteger currentLine;

NSUInteger state;
NSError *error;
}

@property (assign) __weak id<CHCSVParserDelegate> parserDelegate;
@property (readonly) NSError * error;
@property (readonly) NSString * csvFile;
@property (nonatomic, copy) NSString *delimiter;
@property (nonatomic) NSUInteger chunkSize;
@class CHCSVParser;
@protocol CHCSVParserDelegate <NSObject>

- (id) initWithStream:(NSInputStream *)readStream usedEncoding:(NSStringEncoding *)usedEncoding error:(NSError **)anError; //designated initializer
- (id) initWithStream:(NSInputStream *)readStream encoding:(NSStringEncoding)encoding error:(NSError **)anError;
@optional
- (void)parserDidBeginDocument:(CHCSVParser *)parser;
- (void)parserDidEndDocument:(CHCSVParser *)parser;

- (id) initWithContentsOfCSVFile:(NSString *)aCSVFile encoding:(NSStringEncoding)encoding error:(NSError **)anError;
- (id) initWithContentsOfCSVFile:(NSString *)aCSVFile usedEncoding:(NSStringEncoding *)usedEncoding error:(NSError **)anError;
- (void)parser:(CHCSVParser *)parser didBeginLine:(NSUInteger)recordNumber;
- (void)parser:(CHCSVParser *)parser didEndLine:(NSUInteger)recordNumber;

- (id) initWithCSVString:(NSString *)csvString encoding:(NSStringEncoding)encoding error:(NSError **)anError;
- (void)parser:(CHCSVParser *)parser didReadField:(NSString *)field;

- (void) parse;
- (void) cancelParsing;
- (void)parser:(CHCSVParser *)parser didFailWithError:(NSError *)error;

@end

@protocol CHCSVParserDelegate <NSObject>

- (void) parser:(CHCSVParser *)parser didStartDocument:(NSString *)csvFile;
- (void) parser:(CHCSVParser *)parser didStartLine:(NSUInteger)lineNumber;
@interface CHCSVParser : NSObject

- (void) parser:(CHCSVParser *)parser didEndLine:(NSUInteger)lineNumber;
@property (assign) id<CHCSVParserDelegate> delegate;
@property (assign) BOOL recognizesBackslashesAsEscapes; // default is YES
@property (assign) BOOL sanitizesFields; // default is NO

- (void) parser:(CHCSVParser *)parser didReadField:(NSString *)field;
// designated initializer
- (id)initWithInputStream:(NSInputStream *)stream usedEncoding:(NSStringEncoding *)encoding delimiter:(unichar)delimiter;

- (void) parser:(CHCSVParser *)parser didEndDocument:(NSString *)csvFile;
- (id)initWithCSVString:(NSString *)csv;
- (id)initWithContentsOfCSVFile:(NSString *)csvFilePath;

- (void) parser:(CHCSVParser *)parser didFailWithError:(NSError *)error;
- (void)parse;
- (void)cancelParsing;

@end
Loading

0 comments on commit b5fb608

Please sign in to comment.