Skip to content

Commit e6fd7c5

Browse files
committed
Convert to ARC and modern Objective-C syntax. Now requires OS X 10.6.
1 parent 0e536ee commit e6fd7c5

File tree

7 files changed

+347
-375
lines changed

7 files changed

+347
-375
lines changed

CSVDocument.h

+11-16
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,20 @@
1010
#import <Cocoa/Cocoa.h>
1111

1212

13-
@interface CSVDocument : NSObject {
14-
NSString *separator;
15-
NSArray *rows;
16-
NSArray *columnKeys;
17-
18-
BOOL autoDetectSeparator;
19-
}
20-
21-
@property (nonatomic, retain) NSString *separator;
22-
@property (nonatomic, retain) NSArray *rows;
23-
@property (nonatomic, retain) NSArray *columnKeys;
13+
/**
14+
* An object representing data in a CSV file.
15+
*/
16+
@interface CSVDocument : NSObject
2417

25-
@property (nonatomic, assign) BOOL autoDetectSeparator;
18+
@property (copy, nonatomic) NSString *separator;
19+
@property (copy, nonatomic) NSArray *rows;
20+
@property (copy, nonatomic) NSArray *columnKeys;
2621

27-
+ (CSVDocument *) csvDoc;
28-
- (NSUInteger) numRowsFromCSVString:(NSString *)string error:(NSError **)error;
29-
- (NSUInteger) numRowsFromCSVString:(NSString *)string maxRows:(NSUInteger)maxRows error:(NSError **)error;
22+
@property (nonatomic, assign) BOOL autoDetectSeparator;
3023

31-
- (BOOL) isFirstColumn:(NSString *)columnKey;
24+
- (NSUInteger)numRowsFromCSVString:(NSString *)string error:(NSError **)error;
25+
- (NSUInteger)numRowsFromCSVString:(NSString *)string maxRows:(NSUInteger)maxRows error:(NSError **)error;
3226

27+
- (BOOL)isFirstColumn:(NSString *)columnKey;
3328

3429
@end

CSVDocument.m

+30-43
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,31 @@
1515

1616
@implementation CSVDocument
1717

18-
@synthesize separator, rows, columnKeys, autoDetectSeparator;
1918

20-
21-
- (id) init
19+
- (id)init
2220
{
23-
self = [super init];
24-
if (nil != self) {
21+
if ((self = [super init])) {
2522
self.separator = @",";
2623
}
2724

2825
return self;
2926
}
3027

31-
+ (CSVDocument *) csvDoc
32-
{
33-
return [[[CSVDocument alloc] init] autorelease];
34-
}
35-
36-
- (void) dealloc
37-
{
38-
self.separator = nil;
39-
self.rows = nil;
40-
self.columnKeys = nil;
41-
42-
[super dealloc];
43-
}
44-
#pragma mark -
45-
4628

4729

4830
#pragma mark Parsing from String
49-
- (NSUInteger) numRowsFromCSVString:(NSString *)string error:(NSError **)error
31+
/**
32+
* Parse the given string into CSV rows.
33+
*/
34+
- (NSUInteger)numRowsFromCSVString:(NSString *)string error:(NSError **)error
5035
{
5136
return [self numRowsFromCSVString:string maxRows:0 error:error];
5237
}
5338

54-
- (NSUInteger) numRowsFromCSVString:(NSString *)string maxRows:(NSUInteger)maxRows error:(NSError **)error
39+
/**
40+
* Parse the given string into CSV rows, up to a given number of rows if "maxRows" is greater than 0.
41+
*/
42+
- (NSUInteger)numRowsFromCSVString:(NSString *)string maxRows:(NSUInteger)maxRows error:(NSError **)error
5543
{
5644
NSUInteger numRows = 0;
5745

@@ -61,26 +49,26 @@ - (NSUInteger) numRowsFromCSVString:(NSString *)string maxRows:(NSUInteger)maxRo
6149
NSMutableArray *thisColumnKeys = [NSMutableArray array];
6250

6351
// Check whether the file uses ";" or TAB as separator by comparing relative occurrences in the first AUTODETECT_NUM_FIRST_CHARS chars
64-
if (autoDetectSeparator) {
52+
if (_autoDetectSeparator) {
6553
self.separator = @",";
6654

6755
NSUInteger testStringLength = ([string length] > AUTODETECT_NUM_FIRST_CHARS) ? AUTODETECT_NUM_FIRST_CHARS : [string length];
6856
NSString *testString = [string substringToIndex:testStringLength];
69-
NSArray *possSeparators = [NSArray arrayWithObjects:@";", @" ", @"|", nil];
57+
NSArray *possSeparators = @[@";", @" ", @"|"];
7058

7159
for (NSString *s in possSeparators) {
72-
if ([[testString componentsSeparatedByString:s] count] > [[testString componentsSeparatedByString:separator] count]) {
60+
if ([[testString componentsSeparatedByString:s] count] > [[testString componentsSeparatedByString:_separator] count]) {
7361
self.separator = s;
7462
}
7563
}
7664
}
7765

7866
// Get newline character set
79-
NSMutableCharacterSet *newlineCharacterSet = (id)[NSMutableCharacterSet whitespaceAndNewlineCharacterSet];
67+
NSMutableCharacterSet *newlineCharacterSet = [NSMutableCharacterSet whitespaceAndNewlineCharacterSet];
8068
[newlineCharacterSet formIntersectionWithCharacterSet:[[NSCharacterSet whitespaceCharacterSet] invertedSet]];
8169

8270
// Characters where the parser should stop
83-
NSMutableCharacterSet *importantCharactersSet = (id)[NSMutableCharacterSet characterSetWithCharactersInString:[NSString stringWithFormat:@"%@\"", separator]];
71+
NSMutableCharacterSet *importantCharactersSet = [NSMutableCharacterSet characterSetWithCharactersInString:[NSString stringWithFormat:@"%@\"", _separator]];
8472
[importantCharactersSet formUnionWithCharacterSet:newlineCharacterSet];
8573

8674

@@ -89,14 +77,14 @@ - (NSUInteger) numRowsFromCSVString:(NSString *)string maxRows:(NSUInteger)maxRo
8977
BOOL insideQuotes = NO; // needed to determine whether we're inside doublequotes
9078
BOOL finishedRow = NO; // used for the inner while loop
9179
BOOL isNewColumn = NO;
92-
BOOL skipWhitespace = (NSNotFound == [separator rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]].location);
80+
BOOL skipWhitespace = (NSNotFound == [_separator rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]].location);
9381
NSMutableDictionary *columns = nil;
9482
NSMutableString *currentCellString = [NSMutableString string];
9583
NSUInteger colIndex = 0;
9684

9785
NSScanner *scanner = [NSScanner scannerWithString:string];
9886
[scanner setCharactersToBeSkipped:nil];
99-
while(![scanner isAtEnd]) {
87+
while (![scanner isAtEnd]) {
10088

10189
// we'll end up here after every row
10290
insideQuotes = NO;
@@ -106,11 +94,11 @@ - (NSUInteger) numRowsFromCSVString:(NSString *)string maxRows:(NSUInteger)maxRo
10694
colIndex = 0;
10795

10896
// Scan row up to the next terminator
109-
while(!finishedRow) {
97+
while (!finishedRow) {
11098
NSString *tempString;
11199
NSString *colKey;
112100
if ([thisColumnKeys count] > colIndex) {
113-
colKey = [thisColumnKeys objectAtIndex:colIndex];
101+
colKey = thisColumnKeys[colIndex];
114102
isNewColumn = NO;
115103
}
116104
else {
@@ -126,12 +114,12 @@ - (NSUInteger) numRowsFromCSVString:(NSString *)string maxRows:(NSUInteger)maxRo
126114

127115

128116
// found the separator
129-
if ([scanner scanString:separator intoString:NULL]) {
117+
if ([scanner scanString:_separator intoString:NULL]) {
130118
if (insideQuotes) { // Separator character inside double quotes
131-
[currentCellString appendString:separator];
119+
[currentCellString appendString:_separator];
132120
}
133121
else { // This is a column separating comma
134-
[columns setObject:[[currentCellString copy] autorelease] forKey:colKey];
122+
columns[colKey] = [currentCellString copy];
135123
if (isNewColumn) {
136124
[thisColumnKeys addObject:colKey];
137125
}
@@ -163,7 +151,7 @@ - (NSUInteger) numRowsFromCSVString:(NSString *)string maxRows:(NSUInteger)maxRo
163151
[currentCellString appendString:tempString];
164152
}
165153
else { // End of row
166-
[columns setObject:[[currentCellString copy] autorelease] forKey:colKey];
154+
columns[colKey] = [currentCellString copy];
167155
if (isNewColumn) {
168156
[thisColumnKeys addObject:colKey];
169157
}
@@ -175,7 +163,7 @@ - (NSUInteger) numRowsFromCSVString:(NSString *)string maxRows:(NSUInteger)maxRo
175163

176164
// found the end
177165
else if ([scanner isAtEnd]) {
178-
[columns setObject:[[currentCellString copy] autorelease] forKey:colKey];
166+
columns[colKey] = [currentCellString copy];
179167
if (isNewColumn) {
180168
[thisColumnKeys addObject:colKey];
181169
}
@@ -187,7 +175,7 @@ - (NSUInteger) numRowsFromCSVString:(NSString *)string maxRows:(NSUInteger)maxRo
187175

188176
// one row scanned - add to the lines array
189177
if ([columns count] > 0) {
190-
CSVRowObject *newRow = [CSVRowObject rowFromDict:columns];
178+
CSVRowObject *newRow = [CSVRowObject newWithDictionary:columns];
191179
[thisRows addObject:newRow];
192180
}
193181

@@ -204,21 +192,20 @@ - (NSUInteger) numRowsFromCSVString:(NSString *)string maxRows:(NSUInteger)maxRo
204192

205193
// empty string
206194
else if (nil != error) {
207-
NSDictionary *errorDict = [NSDictionary dictionaryWithObject:@"Cannot parse an empty string" forKey:@"userInfo"];
195+
NSDictionary *errorDict = @{@"userInfo": @"Cannot parse an empty string"};
208196
*error = [NSError errorWithDomain:NSCocoaErrorDomain code:1 userInfo:errorDict];
209197
}
210198

211199
return numRows;
212200
}
213-
#pragma mark -
214201

215202

216203

217-
#pragma mark Document Properties
218-
- (BOOL) isFirstColumn:(NSString *)columnKey
204+
#pragma mark - Document Properties
205+
- (BOOL)isFirstColumn:(NSString *)columnKey
219206
{
220-
if ((nil != columnKeys) && ([columnKeys count] > 0)) {
221-
return [columnKey isEqualToString:[columnKeys objectAtIndex:0]];
207+
if ((nil != _columnKeys) && ([_columnKeys count] > 0)) {
208+
return [columnKey isEqualToString:_columnKeys[0]];
222209
}
223210

224211
return NO;

CSVRowObject.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
#import <Cocoa/Cocoa.h>
1111

1212

13-
@interface CSVRowObject : NSObject {
14-
NSDictionary *columns;
15-
}
13+
/**
14+
* Data contained in one row of CSV data.
15+
*/
16+
@interface CSVRowObject : NSObject
1617

17-
@property (nonatomic, retain) NSDictionary *columns;
18+
@property (copy, nonatomic) NSDictionary *columns;
1819

19-
+ (CSVRowObject *) row;
20-
+ (CSVRowObject *) rowFromDict:(NSMutableDictionary *)dict;
20+
+ (CSVRowObject *)newWithDictionary:(NSMutableDictionary *)dict;
2121

22-
- (NSString *) columns:(NSArray *)columnKeys combinedByString:(NSString *)sepString;
23-
- (NSString *) columnForKey:(NSString *)columnKey;
22+
- (NSString *)columns:(NSArray *)columnKeys combinedByString:(NSString *)sepString;
23+
- (NSString *)columnForKey:(NSString *)columnKey;
2424

2525
@end

CSVRowObject.m

+11-24
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,13 @@
1212

1313
@implementation CSVRowObject
1414

15-
@synthesize columns;
1615

17-
+ (CSVRowObject *) row
16+
/**
17+
* Instantiates an object with the given row-dictionary.
18+
*/
19+
+ (CSVRowObject *)newWithDictionary:(NSMutableDictionary *)dict
1820
{
19-
CSVRowObject *row = [[[CSVRowObject alloc] init] autorelease];
20-
return row;
21-
}
22-
23-
24-
+ (CSVRowObject *) rowFromDict:(NSMutableDictionary *)dict
25-
{
26-
CSVRowObject *row = [CSVRowObject row];
21+
CSVRowObject *row = [CSVRowObject new];
2722

2823
if (dict) {
2924
row.columns = dict;
@@ -32,34 +27,26 @@ + (CSVRowObject *) rowFromDict:(NSMutableDictionary *)dict
3227
return row;
3328
}
3429

35-
- (void) dealloc
36-
{
37-
self.columns = nil;
38-
39-
[super dealloc];
40-
}
41-
#pragma mark -
42-
4330

4431

4532
#pragma mark Returning Columns
46-
- (NSString *) columns:(NSArray *)columnKeys combinedByString:(NSString *)sepString
33+
- (NSString *)columns:(NSArray *)columnKeys combinedByString:(NSString *)sepString
4734
{
4835
NSString *rowString = nil;
4936

50-
if ((nil != columnKeys) && (nil != columns)) {
51-
rowString = [[columns objectsForKeys:columnKeys notFoundMarker:@""] componentsJoinedByString:sepString];
37+
if ((nil != columnKeys) && (nil != _columns)) {
38+
rowString = [[_columns objectsForKeys:columnKeys notFoundMarker:@""] componentsJoinedByString:sepString];
5239
}
5340

5441
return rowString;
5542
}
5643

57-
- (NSString *) columnForKey:(NSString *)columnKey
44+
- (NSString *)columnForKey:(NSString *)columnKey
5845
{
5946
NSString *cellString = nil;
6047

61-
if ((nil != columnKey) && (nil != columns)) {
62-
cellString = [columns objectForKey:columnKey];
48+
if ((nil != columnKey) && (nil != _columns)) {
49+
cellString = _columns[columnKey];
6350
}
6451

6552
return cellString;

0 commit comments

Comments
 (0)