Permalink
Browse files

Rewrote Writer + helper categories

  • Loading branch information...
davedelong committed Dec 20, 2012
1 parent b5fb608 commit f5dc084ac0b5935fd2c284c757c8eebe7d7d755f
View
40 CHCSV.h
@@ -1,40 +0,0 @@
-//
-// CHCSV.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.
- **/
-
-#import "CHCSVParser.h"
-#import "CHCSVWriter.h"
-#import "NSArray+CHCSVAdditions.h"
-#import "NSString+CHCSVAdditions.h"
-
-extern NSString *const CHCSVErrorDomain;
-
-enum {
- CHCSVErrorCodeGeneric = 0,
- CHCSVErrorCodeInvalidStream,
- CHCSVErrorCodeInvalidFormat,
- CHCSVErrorCodeParsingCancelled
-};
-
-typedef NSInteger CHCSVErrorCode;
View
@@ -1,42 +0,0 @@
-//
-// CHCSVParser.h
-// CHCSVParser
-//
-// Created by Dave DeLong on 9/22/12.
-//
-//
-
-#import <Foundation/Foundation.h>
-
-@class CHCSVParser;
-@protocol CHCSVParserDelegate <NSObject>
-
-@optional
-- (void)parserDidBeginDocument:(CHCSVParser *)parser;
-- (void)parserDidEndDocument:(CHCSVParser *)parser;
-
-- (void)parser:(CHCSVParser *)parser didBeginLine:(NSUInteger)recordNumber;
-- (void)parser:(CHCSVParser *)parser didEndLine:(NSUInteger)recordNumber;
-
-- (void)parser:(CHCSVParser *)parser didReadField:(NSString *)field;
-
-- (void)parser:(CHCSVParser *)parser didFailWithError:(NSError *)error;
-
-@end
-
-@interface CHCSVParser : NSObject
-
-@property (assign) id<CHCSVParserDelegate> delegate;
-@property (assign) BOOL recognizesBackslashesAsEscapes; // default is YES
-@property (assign) BOOL sanitizesFields; // default is NO
-
-// designated initializer
-- (id)initWithInputStream:(NSInputStream *)stream usedEncoding:(NSStringEncoding *)encoding delimiter:(unichar)delimiter;
-
-- (id)initWithCSVString:(NSString *)csv;
-- (id)initWithContentsOfCSVFile:(NSString *)csvFilePath;
-
-- (void)parse;
-- (void)cancelParsing;
-
-@end
@@ -7,17 +7,12 @@
objects = {
/* Begin PBXBuildFile section */
- 5516BCB512578CFC0025F235 /* NSString+CHCSVAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 5516BCB412578CFC0025F235 /* NSString+CHCSVAdditions.m */; };
- 5516BCBB12578EA90025F235 /* NSString+CHCSVAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 5516BCB412578CFC0025F235 /* NSString+CHCSVAdditions.m */; };
5538B52D1344F0A1004930DD /* test.tsv in Resources */ = {isa = PBXBuildFile; fileRef = 5538B52C1344F0A1004930DD /* test.tsv */; };
557FCEB61203F938009FCDBA /* CoreServices.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 557FCEB51203F938009FCDBA /* CoreServices.framework */; };
- 557FD0431204A45D009FCDBA /* NSArray+CHCSVAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 557FD0421204A45D009FCDBA /* NSArray+CHCSVAdditions.m */; };
- 557FD0591204A71C009FCDBA /* NSArray+CHCSVAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 557FD0421204A45D009FCDBA /* NSArray+CHCSVAdditions.m */; };
557FD05D1204A731009FCDBA /* UnitTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 557FD05B1204A72B009FCDBA /* UnitTests.m */; };
557FD05E1204A736009FCDBA /* Test.csv in Resources */ = {isa = PBXBuildFile; fileRef = 551981EE1203800300FBE033 /* Test.csv */; };
55B99321160E376100D6548A /* CHCSVParser.m in Sources */ = {isa = PBXBuildFile; fileRef = 55B99320160E376100D6548A /* CHCSVParser.m */; };
- 55BB40E612A367A500A1BB33 /* CHCSVWriter.m in Sources */ = {isa = PBXBuildFile; fileRef = 55EFC7B81210608C0070B303 /* CHCSVWriter.m */; };
- 55EFC7B91210608C0070B303 /* CHCSVWriter.m in Sources */ = {isa = PBXBuildFile; fileRef = 55EFC7B81210608C0070B303 /* CHCSVWriter.m */; };
+ 55F79EE01682D6B60095E9D5 /* CHCSVParser.m in Sources */ = {isa = PBXBuildFile; fileRef = 55B99320160E376100D6548A /* CHCSVParser.m */; };
8DD76F9A0486AA7600D96B5E /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 08FB7796FE84155DC02AAC07 /* main.m */; settings = {ATTRIBUTES = (); }; };
8DD76F9C0486AA7600D96B5E /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 08FB779EFE84155DC02AAC07 /* Foundation.framework */; };
8DD76F9F0486AA7600D96B5E /* CHCSVParser.1 in CopyFiles */ = {isa = PBXBuildFile; fileRef = C6859EA3029092ED04C91782 /* CHCSVParser.1 */; };
@@ -40,22 +35,15 @@
08FB7796FE84155DC02AAC07 /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
08FB779EFE84155DC02AAC07 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = "<absolute>"; };
32A70AAB03705E1F00C91783 /* CHCSVParser_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CHCSVParser_Prefix.pch; sourceTree = "<group>"; };
- 5516BCB312578CFC0025F235 /* NSString+CHCSVAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSString+CHCSVAdditions.h"; sourceTree = "<group>"; };
- 5516BCB412578CFC0025F235 /* NSString+CHCSVAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSString+CHCSVAdditions.m"; sourceTree = "<group>"; };
- 5516BCB612578D480025F235 /* CHCSV.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CHCSV.h; sourceTree = "<group>"; };
551981EE1203800300FBE033 /* Test.csv */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = Test.csv; sourceTree = "<group>"; };
5538B52C1344F0A1004930DD /* test.tsv */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = test.tsv; sourceTree = "<group>"; };
557FCEB51203F938009FCDBA /* CoreServices.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreServices.framework; path = System/Library/Frameworks/CoreServices.framework; sourceTree = SDKROOT; };
- 557FD0411204A45D009FCDBA /* NSArray+CHCSVAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSArray+CHCSVAdditions.h"; sourceTree = "<group>"; };
- 557FD0421204A45D009FCDBA /* NSArray+CHCSVAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSArray+CHCSVAdditions.m"; sourceTree = "<group>"; };
557FD0531204A70D009FCDBA /* Unit Tests.octest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Unit Tests.octest"; sourceTree = BUILT_PRODUCTS_DIR; };
557FD0541204A70D009FCDBA /* Unit Tests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "Unit Tests-Info.plist"; sourceTree = "<group>"; };
557FD05A1204A72B009FCDBA /* UnitTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UnitTests.h; sourceTree = "<group>"; };
557FD05B1204A72B009FCDBA /* UnitTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UnitTests.m; sourceTree = "<group>"; };
- 55B9931F160E376100D6548A /* CHCSVParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CHCSVParser.h; sourceTree = "<group>"; };
- 55B99320160E376100D6548A /* CHCSVParser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CHCSVParser.m; sourceTree = "<group>"; };
- 55EFC7B71210608C0070B303 /* CHCSVWriter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CHCSVWriter.h; sourceTree = "<group>"; };
- 55EFC7B81210608C0070B303 /* CHCSVWriter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CHCSVWriter.m; sourceTree = "<group>"; };
+ 55B9931F160E376100D6548A /* CHCSVParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CHCSVParser.h; path = CHCSVParser/CHCSVParser.h; sourceTree = "<group>"; };
+ 55B99320160E376100D6548A /* CHCSVParser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CHCSVParser.m; path = CHCSVParser/CHCSVParser.m; sourceTree = "<group>"; };
8DD76FA10486AA7600D96B5E /* CHCSVParser */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = CHCSVParser; sourceTree = BUILT_PRODUCTS_DIR; };
C6859EA3029092ED04C91782 /* CHCSVParser.1 */ = {isa = PBXFileReference; lastKnownFileType = text.man; path = CHCSVParser.1; sourceTree = "<group>"; };
/* End PBXFileReference section */
@@ -127,15 +115,8 @@
5516BCBA12578DA90025F235 /* CHCSVParser */ = {
isa = PBXGroup;
children = (
- 5516BCB612578D480025F235 /* CHCSV.h */,
55B9931F160E376100D6548A /* CHCSVParser.h */,
55B99320160E376100D6548A /* CHCSVParser.m */,
- 55EFC7B71210608C0070B303 /* CHCSVWriter.h */,
- 55EFC7B81210608C0070B303 /* CHCSVWriter.m */,
- 557FD0411204A45D009FCDBA /* NSArray+CHCSVAdditions.h */,
- 557FD0421204A45D009FCDBA /* NSArray+CHCSVAdditions.m */,
- 5516BCB312578CFC0025F235 /* NSString+CHCSVAdditions.h */,
- 5516BCB412578CFC0025F235 /* NSString+CHCSVAdditions.m */,
);
name = CHCSVParser;
sourceTree = "<group>";
@@ -248,10 +229,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
- 557FD0591204A71C009FCDBA /* NSArray+CHCSVAdditions.m in Sources */,
557FD05D1204A731009FCDBA /* UnitTests.m in Sources */,
- 55EFC7B91210608C0070B303 /* CHCSVWriter.m in Sources */,
- 5516BCB512578CFC0025F235 /* NSString+CHCSVAdditions.m in Sources */,
+ 55F79EE01682D6B60095E9D5 /* CHCSVParser.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -260,9 +239,6 @@
buildActionMask = 2147483647;
files = (
8DD76F9A0486AA7600D96B5E /* main.m in Sources */,
- 557FD0431204A45D009FCDBA /* NSArray+CHCSVAdditions.m in Sources */,
- 5516BCBB12578EA90025F235 /* NSString+CHCSVAdditions.m in Sources */,
- 55BB40E612A367A500A1BB33 /* CHCSVWriter.m in Sources */,
55B99321160E376100D6548A /* CHCSVParser.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
View
@@ -0,0 +1,100 @@
+//
+// 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.
+ **/
+
+#import <Foundation/Foundation.h>
+
+extern NSString * const CHCSVErrorDomain;
+
+enum {
+ CHCSVErrorCodeInvalidFormat = 1,
+};
+
+typedef NSInteger CHCSVErrorCode;
+
+@class CHCSVParser;
+@protocol CHCSVParserDelegate <NSObject>
+
+@optional
+- (void)parserDidBeginDocument:(CHCSVParser *)parser;
+- (void)parserDidEndDocument:(CHCSVParser *)parser;
+
+- (void)parser:(CHCSVParser *)parser didBeginLine:(NSUInteger)recordNumber;
+- (void)parser:(CHCSVParser *)parser didEndLine:(NSUInteger)recordNumber;
+
+- (void)parser:(CHCSVParser *)parser didReadField:(NSString *)field;
+- (void)parser:(CHCSVParser *)parser didReadComment:(NSString *)comment;
+
+- (void)parser:(CHCSVParser *)parser didFailWithError:(NSError *)error;
+
+@end
+
+@interface CHCSVParser : NSObject
+
+@property (assign) id<CHCSVParserDelegate> delegate;
+@property (assign) BOOL recognizesBackslashesAsEscapes; // default is NO
+@property (assign) BOOL sanitizesFields; // default is NO
+@property (assign) BOOL recognizesComments; // default is NO
+
+// designated initializer
+- (id)initWithInputStream:(NSInputStream *)stream usedEncoding:(NSStringEncoding *)encoding delimiter:(unichar)delimiter;
+
+- (id)initWithCSVString:(NSString *)csv;
+- (id)initWithContentsOfCSVFile:(NSString *)csvFilePath;
+
+- (void)parse;
+- (void)cancelParsing;
+
+@end
+
+@interface CHCSVWriter : NSObject
+
+- (instancetype)initForWritingToCSVFile:(NSString *)path;
+- (instancetype)initWithOutputStream:(NSOutputStream *)stream encoding:(NSStringEncoding)encoding delimiter:(unichar)delimiter;
+
+- (void)writeField:(NSString *)field;
+- (void)finishLine;
+
+- (void)writeLineOfFields:(NSArray *)fields;
+
+- (void)writeComment:(NSString *)comment;
+
+- (void)closeStream;
+
+@end
+
+#pragma mark - Convenience Categories
+
+@interface NSArray (CHCSVAdditions)
+
++ (instancetype)arrayWithContentsOfCSVFile:(NSString *)csvFilePath;
+- (NSString *)CSVString;
+
+@end
+
+@interface NSString (CHCSVAdditions)
+
+- (NSArray *)CSVComponents;
+
+@end
Oops, something went wrong.

0 comments on commit f5dc084

Please sign in to comment.