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