Re: Unicode filename and FSPathMakeRef
Re: Unicode filename and FSPathMakeRef
- Subject: Re: Unicode filename and FSPathMakeRef
- From: Chris Hanson <email@hidden>
- Date: Thu, 3 Jan 2002 17:02:00 -0600
At 10:51 PM +0800 1/3/02, Johnny CN Lee wrote:
I get a path from FSRef using FSRefMakePath. How should I covert the path to
NSString?
You shouldn't use FSRefMakePath -- there's no way to tell in advance
how long the path will be, so you can't know how large a buffer to
pass it. If you just randomly pick a size -- 1024 bytes, or 2048, or
4096, or whatever -- it *will* be wrong for some user's system
(particularly when using multibyte character sets) and you'll have
needlessly built some fragile software.
Instead, use CFURLCreateWithFSRef to get a CFURLRef corresponding to
an FSRef, then call CFURLCopyFileSystemPath (with the constant
kCFURLPOSIXPathStyle) to get a CFStringRef with a POSIX file system
path. This CFStringRef can be used as if it were an NSString* thanks
to toll-free bridging between CoreFoundation and the Foundation Kit.
The entire thing should look something like this:
#include <CoreFoundation/CFURL.h>
CFStringRef CopyPathForFSRef(FSRef *inRef)
{
CFURLRef url = NULL;
CFStringRef path = NULL;
assert(inRef != NULL);
url = CFURLCreateWithFSRef(kCFAllocatorDefault, inRef);
if (url != NULL) {
path = CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle);
CFRelease(url);
}
return path;
}
(Not compiled or tested, use at your own risk, but it should give you
the idea.)
-- Chris
--
Chris Hanson | Email: email@hidden
bDistributed.com, Inc. | Phone: +1-847-372-3955
Making Business Distributed | Fax: +1-847-589-3738
http://bdistributed.com/ | Personal Email: email@hidden