FileUtils.m
author Olivier Scherler <oscherler@femto-byte.com>
Tue May 12 14:38:30 2009 +0200 (2009-05-12)
changeset 30 2befbe36c746
parent 0 d84d25d6cdbb
permissions -rw-r--r--
Changed -[MYDirectoryEvent relativePath] to work on standardised paths, in case symlinks are used. Fixes issue #28 in Murky.
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
}
jens@11
    56
jens@11
    57
jens@11
    58
/*
jens@11
    59
 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
jens@11
    60
 
jens@11
    61
 Redistribution and use in source and binary forms, with or without modification, are permitted
jens@11
    62
 provided that the following conditions are met:
jens@11
    63
 
jens@11
    64
 * Redistributions of source code must retain the above copyright notice, this list of conditions
jens@11
    65
 and the following disclaimer.
jens@11
    66
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
jens@11
    67
 and the following disclaimer in the documentation and/or other materials provided with the
jens@11
    68
 distribution.
jens@11
    69
 
jens@11
    70
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
jens@11
    71
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
jens@11
    72
 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
jens@11
    73
 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
jens@11
    74
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
jens@11
    75
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
jens@11
    76
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
jens@11
    77
 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
jens@11
    78
 */