FileUtils.m
author Jens Alfke <jens@mooseyard.com>
Sat May 24 13:24:33 2008 -0700 (2008-05-24)
changeset 11 e5976864dfe9
parent 0 d84d25d6cdbb
permissions -rw-r--r--
* Added BSD license to more .m files.
* Updated some copyright notices.
* Added some #imports to make the MYNetwork classes build without using the MYUtilities.pch prefix header.
     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 }
    56 
    57 
    58 /*
    59  Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
    60  
    61  Redistribution and use in source and binary forms, with or without modification, are permitted
    62  provided that the following conditions are met:
    63  
    64  * Redistributions of source code must retain the above copyright notice, this list of conditions
    65  and the following disclaimer.
    66  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
    67  and the following disclaimer in the documentation and/or other materials provided with the
    68  distribution.
    69  
    70  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
    71  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
    72  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
    73  BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    74  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
    75   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
    76  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
    77  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    78  */