Re: importing tab-delimited table (newbie)
Re: importing tab-delimited table (newbie)
- Subject: Re: importing tab-delimited table (newbie)
- From: Mont Rothstein <email@hidden>
- Date: Sun, 6 Feb 2005 22:39:38 -0800
I just recently threw together some code to make this easier. This consists of categories on NSArray, NSData, and NSString. Sorry, no docs or comments. I'll throw in a couple of examples below.
Should anyone make any fixes or enhancements to this please send them back if you can.
Enjoy,
-Mont
Example 1 - Import from NSData or NSString:
Given and NSData or NSString
[data rowsAndColumns] // returns an NSArray of NSArrays of NSStrings.
[string rowsAndColumns] // also returns an NSArray of NSArrays of NSStrings.
Example 2 - Export to tab delimited file
Given an NSArray of NSArrays of NSStrings and a LineEndingType (from the NSString cateogry)
[array dataWithLineEndingType: lineEndingType] // returns a NSData of tab delimited data
// NSArray Category
@interface NSArray (BNExtensions)
- (NSArray *)tabJoinedComponents;
- (NSString *)joinAsLinesOfEndingType:(LineEndingType)type;
- (NSData *)dataWithLineEndingType:(LineEndingType)lineEndingType;
@end
@implementation NSArray (BNExtensions)
- (NSArray *)tabJoinedComponents
{
NSEnumerator *components;
NSMutableArray *rows;
NSArray *row;
components = [self objectEnumerator];
rows = [NSMutableArray arrayWithCapacity: [self count]];
while (row = [components nextObject])
{
[rows addObject: [row componentsJoinedByString: @"\t"]];
}
return rows;
}
- (NSString *)joinAsLinesOfEndingType:(LineEndingType)lineEndingType
{
switch (lineEndingType)
{
case LineEndingTypeDOS : return [self componentsJoinedByString: @"\r\n"];
case LineEndingTypeMac : return [self componentsJoinedByString: @"\r"];
case LineEndingTypeUnix : return [self componentsJoinedByString: @"\n"];
default : return [self componentsJoinedByString: @""];
}
}
- (NSData *)dataWithLineEndingType:(LineEndingType)lineEndingType;
{
NSArray *rows;
NSString *dataString;
rows = [self tabJoinedComponents];
dataString = [rows joinAsLinesOfEndingType: lineEndingType];
return [dataString dataUsingEncoding: NSASCIIStringEncoding];
}
@end
// NSData Category
@interface NSData (BNExtension)
- (NSArray *)rowsAndColumns;
@end
@implementation NSData (BNExtension)
- (NSArray *)rowsAndColumns
{
NSString *fileContents;
NS_DURING
fileContents = [[[NSString alloc] initWithData: self encoding: NSASCIIStringEncoding] autorelease];
NS_HANDLER
[NSException raise: @"Import Error"
format: @"There was a problem reading your file. Please make sure that it is a Tab delimited file. Additional information:\n\nException: %@\nReason: %@\nDetail: %@", [localException name], [localException reason], [localException userInfo]];
NS_ENDHANDLER
return [[fileContents lines] valueForKey: @"tabSeparatedComponents"];
}
@end
// NSString Category
typedef enum
{
LineEndingTypeNone = 0,
LineEndingTypeDOS = 1,
LineEndingTypeMac = 2,
LineEndingTypeUnix = 3,
} LineEndingType;
@interface NSString (BNExtensions)
- (NSArray *)lines;
- (NSArray *)tabSeparatedComponents;
- (NSArray *)rowsAndColumns;
@end
@implementation NSString (BNExtensions)
- (LineEndingType)lineEndingType
{
NSRange lineEndRange;
// Is this alineEndRangeDOS format?
lineEndRange = [self rangeOfString:@"\r\n"];
if (lineEndRange.location != NSNotFound) return LineEndingTypeDOS;
// Not DOS; is this the Mac format?
lineEndRange = [self rangeOfString:@"\r"];
if (lineEndRange.location != NSNotFound) return LineEndingTypeMac;
// Not DOS or Mac, is this Unix format?
lineEndRange = [self rangeOfString:@"\n"];
if (lineEndRange.location != NSNotFound) return LineEndingTypeUnix;
// This string has a single line
return LineEndingTypeNone;
}
- (NSArray *)lines
{
LineEndingType lineEndingType;
lineEndingType = [self lineEndingType];
switch (lineEndingType)
{
case LineEndingTypeDOS : return [self componentsSeparatedByString: @"\r\n"];
case LineEndingTypeMac : return [self componentsSeparatedByString: @"\r"];
case LineEndingTypeUnix : return [self componentsSeparatedByString: @"\n"];
default : return [NSArray arrayWithObject: self];
}
}
- (NSArray *)tabSeparatedComponents
{
return [self componentsSeparatedByString: @"\t"];
}
- (NSArray *)rowsAndColumns
{
return [[self lines] valueForKey: @"tabSeparatedComponents"];
}
@end _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden