Created
September 8, 2010 18:42
-
-
Save AlanQuatermain/570597 to your computer and use it in GitHub Desktop.
A simple class wrapping a movable file/folder location using Aliases.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import <CoreServices/CoreServices.h> | |
@interface Location | |
{ | |
FSRef fsRef; | |
HFSUniStr255 name; | |
} | |
@property (readonly) FSRef * fileRef; | |
@property (readonly) NSString * path; | |
@property (readonly) NSData * aliasData; | |
+ (Location *) locationWithFSRef: (FSRef *) ref; | |
+ (Location *) locationWithAliasData: (NSData *) aliasData; | |
- (id) initWithFSRef: (FSRef *) ref; | |
- (id) initWithAliasData: (NSData *) aliasData; | |
@end | |
@implementation Location | |
- (id) initWithFSRef: (FSRef *) ref | |
{ | |
if ( [super init] == nil ) | |
return ( nil ); | |
memcpy( &fsRef, ref, sizeof(FSRef) ); | |
return ( self ); | |
} | |
- (id) initWithAliasData: (NSData *) aliasData | |
{ | |
if ( [super init] == nil ) | |
return ( nil ); | |
AliasHandle aliasHandle = (AliasHandle) NewHandle( (Size) [aliasData length] ); | |
PtrToHand( [aliasData bytes], aliasHandle, [aliasData length] ); | |
Boolean wasChanged = FALSE; | |
OSErr err = FSResolveAlias( NULL, aliasHandle, &fsRef, &wasChanged ); | |
DisposeHandle( (Handle) aliasHandle ); | |
if ( err != noErr ) | |
{ | |
[self release]; | |
return ( nil ); | |
} | |
return ( self ); | |
} | |
- (id) initWithCoder: (NSCoder *) coder | |
{ | |
if ( [super init] == nil ) | |
return ( nil ); | |
NSData * aliasData = nil; | |
if ( [coder allowsKeyedCoding] ) | |
aliasData = [coder decodeObjectForKey: @"aliasData"]; | |
else | |
aliasData = [coder decodeObject]; | |
AliasHandle aliasHandle = (AliasHandle) NewHandle( (Size) [aliasData length] ); | |
PtrToHand( [aliasData bytes], aliasHandle, [aliasData length] ); | |
Boolean wasChanged = FALSE; | |
OSErr err = FSResolveAlias( NULL, aliasHandle, &fsRef, &wasChanged ); | |
DisposeHandle( (Handle) aliasHandle ); | |
if ( err != noErr ) | |
{ | |
[self autorelease]; | |
return ( nil ); | |
} | |
return ( self ); | |
} | |
- (void) encodeWithCoder: (NSCoder *) coder | |
{ | |
if ( [coder allowsKeyedCoding] ) | |
[coder encodeObject: self.aliasData forKey: @"aliasData"]; | |
else | |
[coder encodeObject: self.aliasData]; | |
} | |
- (id) copyWithZone: (NSZone *) zone | |
{ | |
// only need to copy the FSRef | |
return ( [[MFLocation alloc] initWithFSRef: &fsRef] ); | |
} | |
- (void) isEqual: (MFLocation *) other | |
{ | |
return ( FSCompareFSRefs(&fsRef, &other->fsRef) == noErr ); | |
} | |
- (FSRef *) fileRef | |
{ | |
return ( &fsRef ); | |
} | |
- (NSString *) path | |
{ | |
char buffer[PATH_MAX+1]; | |
if ( FSRefMakePath(&fsRef, (UInt8 *) buffer, PATH_MAX+1) != noErr ) | |
return ( nil ); | |
return ( [NSString stringWithUTF8String: buffer] ); | |
} | |
- (NSData *) aliasData | |
{ | |
AliasHandle alias = NULL; | |
if ( FSNewAliasMinimal(&fsRef, &alias) != noErr ) | |
return ( nil ); | |
HLock( (Handle) alias ); | |
NSData * result = [NSData dataWithBytes: (void *)(*alias) length: GetAliasSize(alias)]; | |
HUnlock( (Handle) alias ); | |
DisposeHandle( (Handle) alias ); | |
return ( result ); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment