//
//  FileUtils.m
//  MYUtilities
//
//  Created by Jens Alfke on 1/14/08.
//  Copyright 2008 Jens Alfke. All rights reserved.
//

#import "FileUtils.h"


OSStatus PathToFSRef( NSString *path, FSRef *fsRef )
{
    NSCParameterAssert(path);
    return FSPathMakeRef((const UInt8 *)[path UTF8String],fsRef,NULL);
}

OSStatus FSRefToPath( const FSRef *fsRef, NSString **outPath )
{
    NSURL *url = (id) CFURLCreateFromFSRef(NULL,fsRef);
    if( ! url )
        return paramErr;
    *outPath = [url path];
    [url release];
    return noErr;
}


BOOL CheckOSErr( OSStatus err, NSError **error )
{
    if( err ) {
        if( error )
            *error = [NSError errorWithDomain: NSOSStatusErrorDomain code: err userInfo: nil];
        return NO;
    } else {
        return YES;
    }
}


NSString* AppSupportDirectory()
{
    static NSString *sPath;
    if( ! sPath ) {
        NSString *dir = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,
                                                             NSUserDomainMask, YES)
                         objectAtIndex: 0];
        dir = [dir stringByAppendingPathComponent: [[NSBundle mainBundle] bundleIdentifier]];
        if( ! [[NSFileManager defaultManager] fileExistsAtPath: dir]
                && ! [[NSFileManager defaultManager] createDirectoryAtPath: dir attributes: nil] )
            [NSException raise: NSGenericException format: @"Unable to create app support dir %@",dir];
        sPath = [dir copy];
    }
    return sPath;
}
