cog/Plugins/CueSheet/CueSheet.m

259 lines
6.1 KiB
Matlab
Raw Normal View History

2007-10-09 02:25:40 +00:00
//
// CueSheet.m
// CueSheet
//
// Created by Zaphod Beeblebrox on 10/8/07.
// Copyright 2007 __MyCompanyName__. All rights reserved.
//
#import "CueSheet.h"
#import "CueSheetTrack.h"
@implementation CueSheet
+ (id)cueSheetWithFile:(NSString *)filename
{
return [[[CueSheet alloc] initWithFile:filename] autorelease];
}
- (NSURL *)urlForPath:(NSString *)path relativeTo:(NSString *)baseFilename
{
NSRange protocolRange = [path rangeOfString:@"://"];
if (protocolRange.location != NSNotFound)
{
return [NSURL URLWithString:path];
}
NSMutableString *unixPath = [path mutableCopy];
2007-10-15 22:57:30 +00:00
NSString *fragment = @"";
2007-10-19 02:23:10 +00:00
NSRange fragmentRange = [path rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"#0123456789"] options:(NSAnchoredSearch | NSBackwardsSearch)];
2007-10-15 22:57:30 +00:00
if (fragmentRange.location != NSNotFound)
{
fragment = [unixPath substringWithRange:fragmentRange];
[unixPath deleteCharactersInRange:fragmentRange];
}
2007-10-15 22:57:30 +00:00
if (![unixPath hasPrefix:@"/"]) {
//Only relative paths would have windows backslashes.
[unixPath replaceOccurrencesOfString:@"\\" withString:@"/" options:0 range:NSMakeRange(0, [unixPath length])];
NSString *basePath = [[[baseFilename stringByStandardizingPath] stringByDeletingLastPathComponent] stringByAppendingString:@"/"];
2007-10-15 22:57:30 +00:00
[unixPath insertString:basePath atIndex:0];
}
2007-10-15 22:57:30 +00:00
//Append the fragment
return [NSURL URLWithString:[[[NSURL fileURLWithPath:unixPath] absoluteString] stringByAppendingString: fragment]];
}
2007-10-19 02:23:10 +00:00
- (void)parseFile:(NSString *)filename
{
NSError *error = nil;
NSString *contents = [NSString stringWithContentsOfFile:filename encoding:NSUTF8StringEncoding error:&error];
if (error || !contents) {
NSLog(@"Could not open file...%@ %@ %@", filename, contents, error);
return;
}
NSMutableArray *entries = [[NSMutableArray alloc] init];
NSString *track = nil;
NSString *path = nil;
2007-10-13 01:07:19 +00:00
NSString *artist = nil;
NSString *album = nil;
NSString *title = nil;
NSString *genre = nil;
NSString *year = nil;
BOOL trackAdded = NO;
NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSString *line;
NSScanner *scanner = nil;
NSEnumerator *e = [[contents componentsSeparatedByString:@"\n"] objectEnumerator];
while (line = [e nextObject])
{
[scanner release];
scanner = [[NSScanner alloc] initWithString:line];
NSString *command;
if (![scanner scanUpToCharactersFromSet:whitespace intoString:&command]) {
continue;
}
//FILE "filename.shn" WAVE
if ([command isEqualToString:@"FILE"]) {
trackAdded = NO;
2007-10-13 01:07:19 +00:00
if (![scanner scanString:@"\"" intoString:nil]) {
continue;
}
//Read in the path
if (![scanner scanUpToString:@"\"" intoString:&path]) {
continue;
}
}
//TRACK 01 AUDIO
else if ([command isEqualToString:@"TRACK"]) {
trackAdded = NO;
if (![scanner scanUpToCharactersFromSet:whitespace intoString:&track]) {
continue;
}
NSString *type = nil;
if (![scanner scanUpToCharactersFromSet:whitespace intoString:&type]
|| ![type isEqualToString:@"AUDIO"]) {
continue;
}
}
//INDEX 01 00:00:10
//Note that time is written in Minutes:Seconds:Frames, where frames are 1/75 of a second
else if ([command isEqualToString:@"INDEX"]) {
if (trackAdded) {
continue;
}
if (!path) {
continue;
}
NSString *index = nil;
2007-10-12 01:15:11 +00:00
if (![scanner scanUpToCharactersFromSet:whitespace intoString:&index] || [index intValue] != 1) {
continue;
}
2007-10-12 01:15:11 +00:00
[scanner scanCharactersFromSet:whitespace intoString:nil];
NSString *time = nil;
if (![scanner scanUpToCharactersFromSet:whitespace intoString:&time]) {
continue;
}
NSArray *msf = [time componentsSeparatedByString:@":"];
if ([msf count] != 3) {
continue;
}
double seconds = (60*[[msf objectAtIndex:0] intValue]) + [[msf objectAtIndex:1] intValue] + ([[msf objectAtIndex:2] floatValue]/75);
if (track == nil) {
track = @"01";
}
//Need to add basePath, and convert to URL
[entries addObject:
[CueSheetTrack trackWithURL:[self urlForPath:path relativeTo:filename]
track: track
2007-10-13 01:07:19 +00:00
time: seconds
artist:artist
album:album
title:title
genre:genre
year:year]];
trackAdded = YES;
}
2007-10-13 01:07:19 +00:00
else if ([command isEqualToString:@"PERFORMER"])
{
if (![scanner scanString:@"\"" intoString:nil]) {
continue;
}
//Read in the path
if (![scanner scanUpToString:@"\"" intoString:&artist]) {
continue;
}
}
else if ([command isEqualToString:@"TITLE"])
{
NSString **titleDest;
if (!path) //Have not come across a file yet.
titleDest = &album;
else
titleDest = &title;
if (![scanner scanString:@"\"" intoString:nil]) {
continue;
}
//Read in the path
if (![scanner scanUpToString:@"\"" intoString:titleDest]) {
continue;
}
}
else if ([command isEqualToString:@"REM"]) //Additional metadata sometimes stored in comments
{
NSString *type;
2007-10-13 16:31:18 +00:00
if ( ![scanner scanUpToCharactersFromSet:whitespace intoString:&type]) {
continue;
}
2007-10-13 01:07:19 +00:00
if ([type isEqualToString:@"GENRE"])
{
2007-10-13 16:31:18 +00:00
NSLog(@"GENRE!");
if ([scanner scanString:@"\"" intoString:nil]) {
2007-10-13 16:31:18 +00:00
NSLog(@"QUOTED");
if (![scanner scanUpToString:@"\"" intoString:&genre]) {
2007-10-13 16:31:18 +00:00
NSLog(@"FAILED TO SCAN");
continue;
}
}
else {
2007-10-13 16:31:18 +00:00
NSLog(@"UNQUOTED");
if ( ![scanner scanUpToCharactersFromSet:whitespace intoString:&genre]) {
continue;
}
}
2007-10-13 01:07:19 +00:00
}
else if ([type isEqualToString:@"DATE"])
{
2007-10-13 16:31:18 +00:00
NSLog(@"DATE!");
if ( ![scanner scanUpToCharactersFromSet:whitespace intoString:&year]) {
continue;
}
2007-10-13 01:07:19 +00:00
}
}
}
[scanner release];
tracks = [entries copy];
[entries release];
}
2007-10-09 02:25:40 +00:00
- (id)initWithFile:(NSString *)filename
{
self = [super init];
if (self) {
[self parseFile:filename];
2007-10-09 02:25:40 +00:00
}
return self;
}
- (NSArray *)tracks
{
return tracks;
}
- (CueSheetTrack *)track:(NSString *)fragment
{
CueSheetTrack *t;
NSEnumerator *e = [tracks objectEnumerator];
while (t = [e nextObject]) {
if ([[t track] isEqualToString:fragment]) {
return t;
}
}
return nil;
}
@end