views:

78

answers:

2

Hi, I am in Mac.

How to get NSString (Unix style path) from FSSpec

Regards, Dhana.

A: 

I think below code will work fine..

If wrong please modify..

char * UnixPathFromFSSpec(FSSpec *sfile)
{
    char unixPath[2048];
    unixPath[0] = '\0';
    FSRef ref;

    if(FSpMakeFSRef(sfile, &ref) == noErr )
    {
        FSRefMakePath(&ref, unixPath, sizeof(unixPath));
    }

    return unixPath;
}
Dhanaraj
Use `PATH_MAX` rather than hard-coding a number of bytes. You may also want to return `NULL` if either function fails, instead of returning an empty string; moreover, you've declared the array as a local variable, which means that it will stop existing when the function exits. You really should return an NSString instead of a C string.
Peter Hosey
+2  A: 
  1. Create an FSRef for the FSSpec.
  2. Create a CFURL for the FSRef.
  3. Copy or get an NSString of the URL's file-system path, using either the CFURLCopyFileSystemPath function or the -[NSURL path] method.

Don't forget to release the CFURL, since you Created it. The same goes for the path, if you Copied it.

Peter Hosey