From 985ab27e1dbeb018bf4af6f5e30118138fe874ab Mon Sep 17 00:00:00 2001 From: vspader Date: Wed, 14 Mar 2007 02:29:16 +0000 Subject: [PATCH] Added formatters and NSFileHandle+CreateFile category for easy file creation when writing. --- Playlist/IndexFormatter.h | 16 +++++ Playlist/IndexFormatter.m | 49 ++++++++++++++ Playlist/SecondsFormatter.h | 27 ++++++++ Playlist/SecondsFormatter.m | 109 ++++++++++++++++++++++++++++++++ Utils/NSFileHandle+CreateFile.h | 16 +++++ Utils/NSFileHandle+CreateFile.m | 24 +++++++ 6 files changed, 241 insertions(+) create mode 100644 Playlist/IndexFormatter.h create mode 100644 Playlist/IndexFormatter.m create mode 100644 Playlist/SecondsFormatter.h create mode 100644 Playlist/SecondsFormatter.m create mode 100644 Utils/NSFileHandle+CreateFile.h create mode 100644 Utils/NSFileHandle+CreateFile.m diff --git a/Playlist/IndexFormatter.h b/Playlist/IndexFormatter.h new file mode 100644 index 000000000..73777fc0c --- /dev/null +++ b/Playlist/IndexFormatter.h @@ -0,0 +1,16 @@ +// +// IndexFormatter.h +// Cog +// +// Created by Zaphod Beeblebrox on 3/13/07. +// Copyright 2007 __MyCompanyName__. All rights reserved. +// + +#import + + +@interface IndexFormatter : NSObject { + +} + +@end diff --git a/Playlist/IndexFormatter.m b/Playlist/IndexFormatter.m new file mode 100644 index 000000000..dd536232f --- /dev/null +++ b/Playlist/IndexFormatter.m @@ -0,0 +1,49 @@ +// +// IndexFormatter.m +// Cog +// +// Created by Zaphod Beeblebrox on 3/13/07. +// Copyright 2007 __MyCompanyName__. All rights reserved. +// + +#import "IndexFormatter.h" + + +@implementation IndexFormatter + +- (NSString *) stringForObjectValue:(id)object +{ + NSString *result = nil; + int value; + + if(nil == object || NO == [object isKindOfClass:[NSNumber class]]) { + return nil; + } + + value = ([object intValue] + 1); + + result = [NSString stringWithFormat:@"%i", value]; + + return [[result retain] autorelease]; +} + +- (BOOL) getObjectValue:(id *)object forString:(NSString *)string errorDescription:(NSString **)error +{ + if(NULL != object) { + *object = [NSNumber numberWithInt:[string intValue]]; + + return YES; + } + + return NO; +} + +- (NSAttributedString *) attributedStringForObjectValue:(id)object withDefaultAttributes:(NSDictionary *)attributes +{ + NSAttributedString *result = nil; + + result = [[NSAttributedString alloc] initWithString:[self stringForObjectValue:object] attributes:attributes]; + return [result autorelease]; +} + +@end diff --git a/Playlist/SecondsFormatter.h b/Playlist/SecondsFormatter.h new file mode 100644 index 000000000..48a959170 --- /dev/null +++ b/Playlist/SecondsFormatter.h @@ -0,0 +1,27 @@ +/* + * $Id: SecondsFormatter.h 227 2007-01-21 06:22:13Z stephen_booth $ + * + * Copyright (C) 2006 - 2007 Stephen F. Booth + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#import + +@interface SecondsFormatter : NSFormatter +{ +} + +@end diff --git a/Playlist/SecondsFormatter.m b/Playlist/SecondsFormatter.m new file mode 100644 index 000000000..d04e69f7b --- /dev/null +++ b/Playlist/SecondsFormatter.m @@ -0,0 +1,109 @@ +/* + * $Id: SecondsFormatter.m 227 2007-01-21 06:22:13Z stephen_booth $ + * + * Copyright (C) 2006 - 2007 Stephen F. Booth + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#import "SecondsFormatter.h" + +@implementation SecondsFormatter + +- (NSString *) stringForObjectValue:(id)object +{ + NSString *result = nil; + unsigned value; + unsigned days = 0; + unsigned hours = 0; + unsigned minutes = 0; + unsigned seconds = 0; + + if(nil == object || NO == [object isKindOfClass:[NSNumber class]]) { + return nil; + } + + value = (unsigned)([object doubleValue] / 1000.0); + + seconds = value % 60; + minutes = value / 60; + + while(60 <= minutes) { + minutes -= 60; + ++hours; + } + + while(24 <= hours) { + hours -= 24; + ++days; + } + + if(0 < days) { + result = [NSString stringWithFormat:@"%u:%.2u:%.2u:%.2u", days, hours, minutes, seconds]; + } + else if(0 < hours) { + result = [NSString stringWithFormat:@"%u:%.2u:%.2u", hours, minutes, seconds]; + } + else if(0 < minutes) { + result = [NSString stringWithFormat:@"%u:%.2u", minutes, seconds]; + } + else { + result = [NSString stringWithFormat:@"0:%.2u", seconds]; + } + + return [[result retain] autorelease]; +} + +- (BOOL) getObjectValue:(id *)object forString:(NSString *)string errorDescription:(NSString **)error +{ + NSScanner *scanner = nil; + BOOL result = NO; + int value = 0; + unsigned seconds; + + scanner = [NSScanner scannerWithString:string]; + + while(NO == [scanner isAtEnd]) { + + // Grab a value + if([scanner scanInt:&value]) { + seconds *= 60; + seconds += value; + result = YES; + } + + // Grab the separator, if present + [scanner scanString:@":" intoString:NULL]; + } + + if(result && NULL != object) { + *object = [NSNumber numberWithUnsignedInt:seconds]; + } + else if(NULL != error) { + *error = @"Couldn't convert value to seconds"; + } + + return result; +} + +- (NSAttributedString *) attributedStringForObjectValue:(id)object withDefaultAttributes:(NSDictionary *)attributes +{ + NSAttributedString *result = nil; + + result = [[NSAttributedString alloc] initWithString:[self stringForObjectValue:object] attributes:attributes]; + return [result autorelease]; +} + +@end diff --git a/Utils/NSFileHandle+CreateFile.h b/Utils/NSFileHandle+CreateFile.h new file mode 100644 index 000000000..2640c7fcb --- /dev/null +++ b/Utils/NSFileHandle+CreateFile.h @@ -0,0 +1,16 @@ +// +// NSFileHandle+CreateFile.h +// Cog +// +// Created by Zaphod Beeblebrox on 3/13/07. +// Copyright 2007 __MyCompanyName__. All rights reserved. +// + +#import + + +@interface NSFileHandle (CreateFile) + ++ (id)fileHandleForWritingAtPath:(NSString *)path createFile:(BOOL)create; + +@end diff --git a/Utils/NSFileHandle+CreateFile.m b/Utils/NSFileHandle+CreateFile.m new file mode 100644 index 000000000..aebd261c3 --- /dev/null +++ b/Utils/NSFileHandle+CreateFile.m @@ -0,0 +1,24 @@ +// +// NSFileHandle+CreateFile.m +// Cog +// +// Created by Zaphod Beeblebrox on 3/13/07. +// Copyright 2007 __MyCompanyName__. All rights reserved. +// + +#import "NSFileHandle+CreateFile.h" + + +@implementation NSFileHandle (CreateFile) + ++ (id)fileHandleForWritingAtPath:(NSString *)path createFile:(BOOL)create +{ + NSFileManager *manager = [NSFileManager defaultManager]; + + if (![manager fileExistsAtPath:path]) + [manager createFileAtPath:path contents:nil attributes:nil]; + + return [NSFileHandle fileHandleForWritingAtPath:path]; +} + +@end