//
//  MPT_ResolveAlias.m
//  AUCoderDemo
//
//  Created by Andrew Salamon on 6/6/06.
//  Copyright (c) 2006 Machine Perception Laboratory 
//  University of California San Diego.//

#import "MPT_ResolveAlias.h"


@implementation NSString(MPT_ResolveAlias)
- (NSString *)stringByResolvingAliases
{
	NSString *resolvedPath = nil;
	CFURLRef url;
	
	url = CFURLCreateWithFileSystemPath(NULL /*allocator*/, (CFStringRef)self, kCFURLPOSIXPathStyle, NO /*isDirectory*/);
	if (url != NULL)
	{
		FSRef fsRef;
		if (CFURLGetFSRef(url, &fsRef))
		{
			Boolean targetIsFolder, wasAliased;
			if (FSResolveAliasFile (&fsRef, true /*resolveAliasChains*/, 
									&targetIsFolder, &wasAliased) == noErr && wasAliased)
			{
				CFURLRef resolvedUrl = CFURLCreateFromFSRef(NULL, &fsRef);
				if (resolvedUrl != NULL)
				{
					resolvedPath = (NSString*)CFURLCopyFileSystemPath( resolvedUrl, kCFURLPOSIXPathStyle );
					CFRelease(resolvedUrl);
				}
			}
		}
		CFRelease(url);
	}
	
	if( resolvedPath == nil )
	{
		resolvedPath = [[NSString alloc] initWithString:self];
	}
	
	return [resolvedPath autorelease];
}
@end
