tgunr
3/2/2011 - 3:53 AM

processCSV

processCSV

- (NSMutableArray *) processCSV: (NSString *)csvData
{
    // Character delimiter sets
	NSCharacterSet *lineCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"\r\n"];
	NSCharacterSet *fieldCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@","];
	NSCharacterSet *quotedCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"\""];
	
	// Temporary line and field holders
    // The scanner will alloc and init this variables as needed
	NSString *line;
	NSString *field;
    
    NSMutableArray *results = [NSMutableArray arrayWithCapacity:1];
    
	NSScanner *mainScanner = [NSScanner scannerWithString:csvData];
	while(![mainScanner isAtEnd]) {
        NSMutableArray *elements = [NSMutableArray arrayWithCapacity:1];
		[mainScanner scanUpToCharactersFromSet:lineCharacterSet intoString:&line];
        
		PMMSG(1, @"location:%d line:%@\n", [mainScanner scanLocation], line);
        
		NSScanner *fieldScanner = [NSScanner scannerWithString:line];
		while(![fieldScanner isAtEnd]) {
            
			[fieldScanner scanUpToCharactersFromSet:fieldCharacterSet intoString:&field];
			
			if([field hasPrefix:@"\""] == YES && [field hasSuffix:@"\""] != YES) {
                
				// No trailing speech mark, go back and take the token all the way up to the next " (ignoring commas)
				NSString *restOfQuotedString = [[NSString alloc] init];
				[fieldScanner scanUpToCharactersFromSet:quotedCharacterSet intoString:&restOfQuotedString];
				
				NSMutableString *newField = [NSMutableString stringWithString:[field stringByTrimmingCharactersInSet:quotedCharacterSet]];
				[newField appendString:restOfQuotedString];
                
				[fieldScanner scanCharactersFromSet:quotedCharacterSet intoString:nil];
                [fieldScanner scanCharactersFromSet:fieldCharacterSet intoString:nil];
				
				NSLog(@"TO QUOTED TOKEN %@ (%lu)\n", newField, [fieldScanner scanLocation]);
                [elements addObject:newField];
				[restOfQuotedString release];
			} else {
				
				[fieldScanner scanCharactersFromSet:fieldCharacterSet intoString:nil];
				NSLog(@"TO TOKEN %@ (%lu)\n", field, [fieldScanner scanLocation]);
                [elements addObject:field];

                
			}
			
		}
        [results addObject:elements];
	}
    return results;
}

- (void) processCSVFiles: (NSArray *) files  {
    int i;
    NSError *error;

    // Loop through all the files and process them.
    for( i = 0; i < [files count]; i++ )
    {
        NSString* fileName = [files objectAtIndex:i];
        csvFileContent = [[NSString alloc] initWithContentsOfFile: fileName encoding:NSUTF8StringEncoding error: &error];
        NSArray *newCards = [self processCSV: csvFileContent];
        cards = (NSMutableArray *)[cards arrayByAddingObjectsFromArray: newCards];
		[csvFileContent release];
		[newCards release];
    }
    NSRange r;
    r.location = nextcardnumber;
    r.length = 1;
    NSIndexSet *index = [NSIndexSet indexSetWithIndexesInRange: r];
    [cardsTableView selectRowIndexes: index byExtendingSelection: NO];
    [cardsTableView reloadData];
    [startButton setEnabled:YES];
}