Created
November 16, 2008 21:26
-
-
Save atg/25555 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Controller.m | |
// CSVTest | |
// | |
// Created by Alex Gordon on 16/11/2008. | |
// Copyright 2008 Fileability. All rights reserved. | |
// | |
#import "Controller.h" | |
@implementation Controller | |
- (IBAction)parse:(id)sender | |
{ | |
[self parseString:[textView string] options:[NSDictionary dictionaryWithObjectsAndKeys:@"Test", @"Table Name", @"\t", @"Delimiter", [NSArray arrayWithObjects:@"A", @"B", @"C", nil], @"Column Names", nil]]; | |
} | |
- (void)parseString:(NSString *)str options:(NSDictionary *)options | |
{ | |
NSMutableArray *data = [[NSMutableArray alloc] initWithCapacity:10]; | |
NSMutableString *curString = [NSMutableString stringWithString:@""]; | |
char delimiter = ','; | |
if ([[options objectForKey:@"Delimiter"] length]) | |
{ | |
const char *delString = [[options objectForKey:@"Delimiter"] cStringUsingEncoding:NSASCIIStringEncoding]; | |
delimiter = delString[0]; | |
} | |
xuint curRow = 0, curCol = 0; | |
BOOL inStringMode = NO, lastWasQuote = NO; | |
xuint length = (xuint)[str length]; | |
xuint i; | |
for (i = 0; i < length; i++) | |
{ | |
unichar c = [str characterAtIndex:i]; | |
if (c == '"') | |
{ | |
inStringMode = !inStringMode; | |
if (lastWasQuote) | |
{ | |
[curString appendString:@"\""]; | |
lastWasQuote = NO; | |
} | |
else | |
lastWasQuote = YES; | |
} | |
else | |
{ | |
lastWasQuote = NO; | |
if ((inStringMode && (c == delimiter || c == '\n' || c == '\r')) || (c != delimiter && c != '\n' && c != '\r')) | |
{ | |
[curString appendString:[NSString stringWithCharacters:&c length:1]]; | |
} | |
} | |
if (!inStringMode && c == delimiter) | |
{ | |
[data addObject:curString]; | |
curCol++; | |
curString = [@"" mutableCopy]; | |
} | |
if (!inStringMode && (c == '\n' || c == '\r') && !([curString length] == 0 && curCol == 0)) | |
{ | |
[data addObject:curString]; | |
[self doInsertDataArray:[data copy] row:curRow options:options]; | |
[data removeAllObjects]; | |
curRow++; | |
curCol = 0; | |
curString = [@"" mutableCopy]; | |
} | |
} | |
[data addObject:curString]; | |
[self doInsertDataArray:[data copy] row:curRow options:options]; | |
} | |
- (void)doInsertDataArray:(NSArray *)data row:(xuint)rowIndex options:(NSDictionary *)options | |
{ | |
if (![data count]) | |
return; | |
id database = [options valueForKey:@"Database"]; | |
Class dbClass = [database class]; | |
NSMutableString *insertQuery = [NSMutableString stringWithString:@"INSERT INTO `"]; | |
[insertQuery appendString:[options valueForKey:@"Table Name"]]; | |
[insertQuery appendString:@"` ("]; | |
NSArray *columnNames = [options valueForKey:@"Column Names"]; | |
xuint i; | |
for (i = 0; i < [columnNames count]; i++) | |
{ | |
NSString *colName = [columnNames objectAtIndex:i]; | |
[insertQuery appendFormat:@"`%@`", colName]; | |
if (i < [columnNames count] - 1) | |
[insertQuery appendString:@", "]; | |
} | |
[insertQuery appendString:@") VALUES ("]; | |
BOOL allBlank = YES; | |
for (i = 0; i < [columnNames count]; i++) | |
{ | |
NSString *dataValue = @""; | |
if (i < [data count]) | |
dataValue = [data objectAtIndex:i]; | |
if ([dataValue length]) | |
allBlank = NO; | |
[insertQuery appendFormat:@"'%@'", dataValue];//[dbClass prepareStringForQuery:dataValue]]; | |
if (i < [columnNames count] - 1) | |
[insertQuery appendString:@", "]; | |
} | |
if (allBlank) | |
return; | |
[insertQuery appendString:@")"]; | |
NSLog(@"%@", insertQuery); | |
//[database executeQuery:insertQuery]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment