FileUtils.m
author Jens Alfke <jens@mooseyard.com>
Fri May 02 12:49:43 2008 -0700 (2008-05-02)
changeset 7 59addced5e2a
child 11 e5976864dfe9
permissions -rw-r--r--
Added URLUtils. Rewrote Target.
jens@0
     1
//
jens@0
     2
//  FileUtils.m
jens@0
     3
//  MYUtilities
jens@0
     4
//
jens@0
     5
//  Created by Jens Alfke on 1/14/08.
jens@0
     6
//  Copyright 2008 Jens Alfke. All rights reserved.
jens@0
     7
//
jens@0
     8
jens@0
     9
#import "FileUtils.h"
jens@0
    10
jens@0
    11
jens@0
    12
OSStatus PathToFSRef( NSString *path, FSRef *fsRef )
jens@0
    13
{
jens@0
    14
    NSCParameterAssert(path);
jens@0
    15
    return FSPathMakeRef((const UInt8 *)[path UTF8String],fsRef,NULL);
jens@0
    16
}
jens@0
    17
jens@0
    18
OSStatus FSRefToPath( const FSRef *fsRef, NSString **outPath )
jens@0
    19
{
jens@0
    20
    NSURL *url = (id) CFURLCreateFromFSRef(NULL,fsRef);
jens@0
    21
    if( ! url )
jens@0
    22
        return paramErr;
jens@0
    23
    *outPath = [url path];
jens@0
    24
    [url release];
jens@0
    25
    return noErr;
jens@0
    26
}
jens@0
    27
jens@0
    28
jens@0
    29
BOOL CheckOSErr( OSStatus err, NSError **error )
jens@0
    30
{
jens@0
    31
    if( err ) {
jens@0
    32
        if( error )
jens@0
    33
            *error = [NSError errorWithDomain: NSOSStatusErrorDomain code: err userInfo: nil];
jens@0
    34
        return NO;
jens@0
    35
    } else {
jens@0
    36
        return YES;
jens@0
    37
    }
jens@0
    38
}
jens@0
    39
jens@0
    40
jens@0
    41
NSString* AppSupportDirectory()
jens@0
    42
{
jens@0
    43
    static NSString *sPath;
jens@0
    44
    if( ! sPath ) {
jens@0
    45
        NSString *dir = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,
jens@0
    46
                                                             NSUserDomainMask, YES)
jens@0
    47
                         objectAtIndex: 0];
jens@0
    48
        dir = [dir stringByAppendingPathComponent: [[NSBundle mainBundle] bundleIdentifier]];
jens@0
    49
        if( ! [[NSFileManager defaultManager] fileExistsAtPath: dir]
jens@0
    50
                && ! [[NSFileManager defaultManager] createDirectoryAtPath: dir attributes: nil] )
jens@0
    51
            [NSException raise: NSGenericException format: @"Unable to create app support dir %@",dir];
jens@0
    52
        sPath = [dir copy];
jens@0
    53
    }
jens@0
    54
    return sPath;
jens@0
    55
}