jens@0: // jens@0: // FileUtils.m jens@0: // MYUtilities jens@0: // jens@0: // Created by Jens Alfke on 1/14/08. jens@0: // Copyright 2008 Jens Alfke. All rights reserved. jens@0: // jens@0: jens@0: #import "FileUtils.h" jens@0: jens@0: jens@0: OSStatus PathToFSRef( NSString *path, FSRef *fsRef ) jens@0: { jens@0: NSCParameterAssert(path); jens@0: return FSPathMakeRef((const UInt8 *)[path UTF8String],fsRef,NULL); jens@0: } jens@0: jens@0: OSStatus FSRefToPath( const FSRef *fsRef, NSString **outPath ) jens@0: { jens@0: NSURL *url = (id) CFURLCreateFromFSRef(NULL,fsRef); jens@0: if( ! url ) jens@0: return paramErr; jens@0: *outPath = [url path]; jens@0: [url release]; jens@0: return noErr; jens@0: } jens@0: jens@0: jens@0: BOOL CheckOSErr( OSStatus err, NSError **error ) jens@0: { jens@0: if( err ) { jens@0: if( error ) jens@0: *error = [NSError errorWithDomain: NSOSStatusErrorDomain code: err userInfo: nil]; jens@0: return NO; jens@0: } else { jens@0: return YES; jens@0: } jens@0: } jens@0: jens@0: jens@0: NSString* AppSupportDirectory() jens@0: { jens@0: static NSString *sPath; jens@0: if( ! sPath ) { jens@0: NSString *dir = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, jens@0: NSUserDomainMask, YES) jens@0: objectAtIndex: 0]; jens@0: dir = [dir stringByAppendingPathComponent: [[NSBundle mainBundle] bundleIdentifier]]; jens@0: if( ! [[NSFileManager defaultManager] fileExistsAtPath: dir] jens@0: && ! [[NSFileManager defaultManager] createDirectoryAtPath: dir attributes: nil] ) jens@0: [NSException raise: NSGenericException format: @"Unable to create app support dir %@",dir]; jens@0: sPath = [dir copy]; jens@0: } jens@0: return sPath; jens@0: } jens@11: jens@11: jens@11: /* jens@11: Copyright (c) 2008, Jens Alfke . All rights reserved. jens@11: jens@11: Redistribution and use in source and binary forms, with or without modification, are permitted jens@11: provided that the following conditions are met: jens@11: jens@11: * Redistributions of source code must retain the above copyright notice, this list of conditions jens@11: and the following disclaimer. jens@11: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions jens@11: and the following disclaimer in the documentation and/or other materials provided with the jens@11: distribution. jens@11: jens@11: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR jens@11: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND jens@11: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI- jens@11: BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES jens@11: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR jens@11: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN jens@11: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF jens@11: THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. jens@11: */