/*=============================================================================
	CAFilePathUtils.cpp
	
	$Log: CAFilePathUtils.cpp,v $
	Revision 1.1  2006/03/15 20:26:55  dwyatt
	moved from Source/Tests/AudioFileTools/Utility
	
	Revision 1.4  2005/03/19 01:25:12  jcm10
	use the right encoding on Windows
	
	Revision 1.3  2005/03/10 02:15:58  ealdrich
	Make it build on Windows
	
	Revision 1.2  2005/02/25 01:54:25  dwyatt
	PosixPathToParentFSRefAndName now correctly transforms colons to slashes in the file name
	
	Revision 1.1  2004/10/15 17:09:04  dwyatt
	initial checkin
	
	created 10/15/04, Doug Wyatt
	Copyright (c) 2004 Apple Computer, Inc.  All Rights Reserved

	$NoKeywords: $
=============================================================================*/

#include "CAFilePathUtils.h"
#include <string.h>

OSStatus	PosixPathToParentFSRefAndName(const char *path, FSRef &outParentDir, CFStringRef &outFileName)
{
	// convert C string to CFString
#if !TARGET_OS_WIN32
	CFStringRef cfFullPath = CFStringCreateWithCString(NULL, path, kCFStringEncodingUTF8);
#else
	CFStringRef cfFullPath = CFStringCreateWithCString(NULL, path, kCFStringEncodingWindowsLatin1);
#endif
	// convert CF string to URL
	CFURLRef fullurl = CFURLCreateWithFileSystemPath(NULL, cfFullPath, TARGET_OS_WIN32 ? kCFURLWindowsPathStyle : kCFURLPOSIXPathStyle, false);
	CFRelease(cfFullPath);
	// get the directory portion of the URL
	CFURLRef dirurl = CFURLCreateCopyDeletingLastPathComponent(NULL, fullurl);
	// get the directory's FSSpec
	OSStatus err = CFURLGetFSRef(dirurl, &outParentDir) ? OSStatus(noErr) : OSStatus(fnfErr);
	CFRelease(dirurl);
	
	CFStringRef lastPathComponent = CFURLCopyLastPathComponent(fullurl);
	CFRelease(fullurl);
	CFMutableStringRef filename = CFStringCreateMutableCopy(NULL, 0, lastPathComponent);
	CFRelease(lastPathComponent);
	// convert colons (legal in POSIX paths, illegal in File Manager) to slashes
	CFStringFindAndReplace(filename, CFSTR(":"), CFSTR("/"), CFRangeMake(0, CFStringGetLength(filename)), 0);
	
	outFileName = filename;
	
	return err;
}


#if TARGET_OS_WIN32

char*	dirname(const char* inPath)
{
	static char sAnswer[1024];
	
	char* theAnswer = NULL;
	SInt32 thePathLength = strlen(inPath);
	if(thePathLength < 1023)
	{
		//	make a working copy
		strcpy(sAnswer, inPath);
		
		//	start at the end of the string
		SInt32 theIndex = thePathLength - 1;
		
		//	walk back over the '\' characters
		while((theIndex > 0) && (sAnswer[theIndex] == '\\'))
		{
			--theIndex;
		}
		
		//	now keep walking back until we get to a '\'
		while((theIndex > 0) && (sAnswer[theIndex] != '\\'))
		{
			--theIndex;
		}
		
		//	where we are now is either the first character of the path or the '\' that marks the end of the directory name
		if(theIndex > 0)
		{
			//	we have a name so put a '\0' in place of the '\'
			sAnswer[theIndex] = 0;
		}
		else
		{
			//	no name, so the answer is "."
			sAnswer[0] = '.';
			sAnswer[1] = 0;
		}
		
		//	set the return value
		theAnswer = sAnswer;
	}
	
	return theAnswer;
}

char*	basename(const char* inPath)
{
	static char sAnswer[1024];
	
	char* theAnswer = NULL;
	SInt32 thePathLength = strlen(inPath);
	if(thePathLength < 1023)
	{
		//	make a working copy
		strcpy(sAnswer, inPath);
		
		//	start at the end of the string
		SInt32 theLastIndex = thePathLength - 1;
		
		//	walk back over the '\' characters
		while((theLastIndex > 0) && (sAnswer[theLastIndex] == '\\'))
		{
			--theLastIndex;
		}
		
		//	check to see if we're at the beginning now
		if(theLastIndex > 0)
		{
			//	there's a name in there now, so start where we are and go back to the next '\'
			UInt32 theFirstIndex = theLastIndex;
			while((theFirstIndex > 0) && (sAnswer[theFirstIndex] != '\\'))
			{
				--theFirstIndex;
			}
			
			//	we now have a string, so put a '\0' after the last character
			sAnswer[theLastIndex + 1] = 0;
			
			//	and set the return value
			theAnswer = &sAnswer[theFirstIndex];
		}
		else
		{
			//	the path was entirely '\' characters, so the return value is "\"
			sAnswer[0] = '\\';
			sAnswer[1] = 0;
		
			//	set the return value
			theAnswer = sAnswer;
		}
	}
	
	return theAnswer;
}

#endif
