Permalink
Browse files

Fixed crash when resizing table smaller than width

  • Loading branch information...
1 parent 10def0d commit 6d8d1e9522164e9b9a2197c5fd76ff52b8c3225c svnuser committed Aug 31, 2009
@@ -24,8 +24,8 @@
NSNumber *numRows;
CSVRowController *rowController;
- NSArray *columns;
- NSDictionary *columnDict;
+ NSArray *columns; // needs to be an array to preserve column order
+ NSDictionary *columnDict; // readonly to allow fast access to columns by key
BOOL parseSuccessful;
BOOL autoDetectSeparator; // if YES will check for other separators (";" and TAB) than the comma
@@ -58,20 +58,23 @@
+ (id) csvDocument;
- (void) setNumRowsWithInt:(NSInteger)num_rows;
- (NSUInteger) numRowsToExpect:(NSString *)string;
-- (void) parseCSVString:(NSString *)string error:(NSError **)error;
-- (void) parseCSVString:(NSString *)string maxRows:(NSUInteger)maxRows error:(NSError **)error;
+- (BOOL) parseCSVString:(NSString *)string error:(NSError **)error;
+- (BOOL) parseCSVString:(NSString *)string maxRows:(NSUInteger)maxRows error:(NSError **)error;
- (NSString *) stringInFormat:(PPStringFormat *)format withColumns:(NSArray *)columnArray forRowIndexes:(NSIndexSet *)rowIndexes writeHeader:(BOOL)headerFlag;
-// Retrieving data
+// column handling
- (void) changeHeaderRow:(CSVRow *)newHeaderRow;
+- (void) setNewColumns:(NSArray *)newColumns;
- (BOOL) isFirstColumnKey:(NSString *)columnKey;
- (BOOL) hasColumnKey:(NSString *)columnKey;
+- (void) setColumnOrderByKeys:(NSArray *)newOrderKeys;
- (NSString *) nameForColumn:(NSString *)columnKey;
- (NSString *) nameForColumn:(NSString *)columnKey quoted:(BOOL)quoted;
- (void) setHeaderName:(NSString *)newName forColumnKey:(NSString *)columnKey;
- (void) setHeaderActive:(BOOL)active forColumnKey:(NSString *)columnKey;
+// row handling
- (CSVRow *) rowAtIndex:(NSUInteger)rowIndex;
@@ -25,15 +25,16 @@ - (void) setColumnDict:(NSDictionary *)newColumnDict;
@implementation CSVDocument
@synthesize delegate, separator, rows, numRows, columnDict, rowController, parseSuccessful, autoDetectSeparator, headerRow, mustAbortImport, didAbortImport;
-@dynamic columns, firstRowIsHeaderRow;
+@synthesize columns;
+@dynamic firstRowIsHeaderRow;
- (id) init
{
self = [super init];
if (nil != self) {
self.separator = @",";
- self.columns = [NSArray arrayWithObject:[CSVColumn columnWithKey:@"col_0"]];
+ [self setNewColumns:[NSArray arrayWithObject:[CSVColumn columnWithKey:@"col_0"]]];
self.rows = [NSMutableArray array];
self.rowController = [[[CSVRowController alloc] initWithContent:rows] autorelease];
rowController.document = self;
@@ -63,27 +64,6 @@ - (void) dealloc
#pragma mark KVC
-- (NSArray *) columns
-{
- return columns;
-}
-- (void) setColumns:(NSArray *)newColumns
-{
- if (newColumns != columns) {
- [columns release];
- columns = [newColumns retain];
-
- // we also represent the columns in a hash, mostly due to faster access and bindability
- if (nil != columns) {
- NSMutableDictionary *columnHash = [NSMutableDictionary dictionaryWithCapacity:[columns count]];
- for (CSVColumn *column in columns) {
- [columnHash setObject:column forKey:column.key];
- }
- [self setColumnDict:columnHash];
- }
- }
-}
-
- (void) setColumnDict:(NSDictionary *)newColumnDict
{
if (newColumnDict != columnDict) {
@@ -137,12 +117,12 @@ - (NSUInteger) numRowsToExpect:(NSString *)string
return [[string componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] count];
}
-- (void) parseCSVString:(NSString *)string error:(NSError **)error
+- (BOOL) parseCSVString:(NSString *)string error:(NSError **)error
{
- [self parseCSVString:string maxRows:0 error:error];
+ return [self parseCSVString:string maxRows:0 error:error];
}
-- (void) parseCSVString:(NSString *)string maxRows:(NSUInteger)maxRows error:(NSError **)error
+- (BOOL) parseCSVString:(NSString *)string maxRows:(NSUInteger)maxRows error:(NSError **)error
{
NSUInteger num_rows = 0;
BOOL success = YES;
@@ -313,14 +293,14 @@ - (void) parseCSVString:(NSString *)string maxRows:(NSUInteger)maxRows error:(NS
[innerPool release];
// finished scanning our string; make first row the headerRow
- self.columns = newColumns;
+ [self setNewColumns:newColumns];
if ([rows count] > 0) {
[self changeHeaderRow:[rows objectAtIndex:0]];
}
}
// empty string
- else if (nil != error) {
+ else {
NSDictionary *errorDict = [NSDictionary dictionaryWithObject:@"Cannot parse a nil string" forKey:@"userInfo"];
*error = [NSError errorWithDomain:NSCocoaErrorDomain code:1 userInfo:errorDict];
success = NO;
@@ -337,6 +317,8 @@ - (void) parseCSVString:(NSString *)string maxRows:(NSUInteger)maxRows error:(NS
if (nil != delegate) {
[delegate csvDocumentDidParseString:self];
}
+
+ return success;
}
#pragma mark -
@@ -403,6 +385,22 @@ - (void) changeHeaderRow:(CSVRow *)newHeaderRow
}
}
+- (void) setNewColumns:(NSArray *)newColumns
+{
+ if (columns != newColumns) {
+ self.columns = newColumns;
+
+ // we also represent the columns in a hash, mostly due to faster access and bindability
+ if (nil != newColumns) {
+ NSMutableDictionary *columnHash = [NSMutableDictionary dictionaryWithCapacity:[columns count]];
+ for (CSVColumn *column in columns) {
+ [columnHash setObject:column forKey:column.key];
+ }
+ [self setColumnDict:columnHash];
+ }
+ }
+}
+
- (BOOL) isFirstColumnKey:(NSString *)columnKey
{
if ((nil != columns) && ([columns count] > 0)) {
@@ -418,6 +416,21 @@ - (BOOL) hasColumnKey:(NSString *)columnKey
return (nil != [columnDict objectForKey:columnKey]);
}
+- (void) setColumnOrderByKeys:(NSArray *)newOrderKeys
+{
+ NSMutableArray *arr = [NSMutableArray arrayWithCapacity:[newOrderKeys count]];
+ for (NSString *columnKey in newOrderKeys) {
+ CSVColumn *column = [columnDict objectForKey:columnKey];
+ if (nil != column) {
+ [arr addObject:column];
+ }
+ }
+
+ self.columns = arr;
+ NSLog(@"columns: %@", columns);
+}
+
+
- (NSString *) nameForColumn:(NSString *)columnKey
{
return [self nameForColumn:columnKey quoted:NO];
Oops, something went wrong.

0 comments on commit 6d8d1e9

Please sign in to comment.