FileUtils.m
author Jens Alfke <jens@mooseyard.com>
Wed May 07 16:47:44 2008 -0700 (2008-05-07)
changeset 8 5588347dfcbd
child 11 e5976864dfe9
permissions -rw-r--r--
* Added $apply and some other collection utils.
* Moved logging code to a separate code segment.
* Fixed uninitialized variable in Target.
     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 }