FileUtils.m
changeset 0 d84d25d6cdbb
child 11 e5976864dfe9
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/FileUtils.m	Sat Mar 08 21:04:41 2008 -0800
     1.3 @@ -0,0 +1,55 @@
     1.4 +//
     1.5 +//  FileUtils.m
     1.6 +//  MYUtilities
     1.7 +//
     1.8 +//  Created by Jens Alfke on 1/14/08.
     1.9 +//  Copyright 2008 Jens Alfke. All rights reserved.
    1.10 +//
    1.11 +
    1.12 +#import "FileUtils.h"
    1.13 +
    1.14 +
    1.15 +OSStatus PathToFSRef( NSString *path, FSRef *fsRef )
    1.16 +{
    1.17 +    NSCParameterAssert(path);
    1.18 +    return FSPathMakeRef((const UInt8 *)[path UTF8String],fsRef,NULL);
    1.19 +}
    1.20 +
    1.21 +OSStatus FSRefToPath( const FSRef *fsRef, NSString **outPath )
    1.22 +{
    1.23 +    NSURL *url = (id) CFURLCreateFromFSRef(NULL,fsRef);
    1.24 +    if( ! url )
    1.25 +        return paramErr;
    1.26 +    *outPath = [url path];
    1.27 +    [url release];
    1.28 +    return noErr;
    1.29 +}
    1.30 +
    1.31 +
    1.32 +BOOL CheckOSErr( OSStatus err, NSError **error )
    1.33 +{
    1.34 +    if( err ) {
    1.35 +        if( error )
    1.36 +            *error = [NSError errorWithDomain: NSOSStatusErrorDomain code: err userInfo: nil];
    1.37 +        return NO;
    1.38 +    } else {
    1.39 +        return YES;
    1.40 +    }
    1.41 +}
    1.42 +
    1.43 +
    1.44 +NSString* AppSupportDirectory()
    1.45 +{
    1.46 +    static NSString *sPath;
    1.47 +    if( ! sPath ) {
    1.48 +        NSString *dir = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,
    1.49 +                                                             NSUserDomainMask, YES)
    1.50 +                         objectAtIndex: 0];
    1.51 +        dir = [dir stringByAppendingPathComponent: [[NSBundle mainBundle] bundleIdentifier]];
    1.52 +        if( ! [[NSFileManager defaultManager] fileExistsAtPath: dir]
    1.53 +                && ! [[NSFileManager defaultManager] createDirectoryAtPath: dir attributes: nil] )
    1.54 +            [NSException raise: NSGenericException format: @"Unable to create app support dir %@",dir];
    1.55 +        sPath = [dir copy];
    1.56 +    }
    1.57 +    return sPath;
    1.58 +}