Re: How best to archive in CSV format
Re: How best to archive in CSV format
- Subject: Re: How best to archive in CSV format
- From: Mont Rothstein <email@hidden>
- Date: Tue, 17 May 2005 20:48:40 -0700
Ug. Sorry about that subject on the previous post. Not sure how I did that.
-Mont
On May 17, 2005, at 7:48 PM, Mont Rothstein wrote:
If you haven't already solved this here is some code that may help. It does this for tsv files, tweaking it for csv files shouldn't be that much work.
If you do mod this for csv, I'd love to get that code back, but it isn't required.
Enjoy,
-Mont
Sorry, no docs.
@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
@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
fileContents = nil;
[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
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
_______________________________________________
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
References: | |
| >Re: How best to archive in CSV format If you haven't already solved this here is some code that may help. It does this for tsv files, tweaking it for csv files shouldn't be that much work. If you do mod this for csv, I'd love to get that code back, but it isn't required. Enjoy, -Mont Sorry, no docs. @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 LineEndingTyp! eDOS : return [self componentsJoinedByString: @"\r\n"]; case LineEndingTypeMac : return [self componentsJoinedByString: @"\r"]; case LineEndingTypeUnix : return [self componentsJoinedBy String: @"\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 @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 fileContents = nil; [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] valu! eForKey: @"tabSeparatedComponents"]; } @end 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 { re! turn [[self lines] valueForKey: @"tabSeparatedComponents"]; } @end (From: Mont Rothstein <email@hidden>) |
- Prev by Date:
Re: Figuring out which values are selected in an NSTableView
- Next by Date:
Re: Figuring out which values are selected in an NSTableView
- Previous by thread:
Re: How best to archive in CSV format If you haven't already solved this here is some code that may help. It does this for tsv files, tweaking it for csv files shouldn't be that much work. If you do mod this for csv, I'd love to get that code back, but it isn't required. Enjoy, -Mont Sorry, no docs. @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 LineEndingTyp! eDOS : return [self componentsJoinedByString: @"\r\n"]; case LineEndingTypeMac : return [self componentsJoinedByString: @"\r"]; case LineEndingTypeUnix : return [self componentsJoinedBy String: @"\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 @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 fileContents = nil; [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] valu! eForKey: @"tabSeparatedComponents"]; } @end 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 { re! turn [[self lines] valueForKey: @"tabSeparatedComponents"]; } @end
- Next by thread:
Trying to solve a to-many relationship?
- Index(es):