Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Tilde expansion in paths
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Tilde expansion in paths



Hey Dair, Jerry, Jim, Larry, Eric,

Thanks for the replies.


On 2006-10-31, at 07:35:31, Jim Correia wrote:

The environment is fungible. If I remove or change HOME in the environment before launching your app, is that what you want? Or do you want the real home directory associated with the user?

Very good point. Looks like getenv() is not a good choice for this situation.



On 2006-10-31, at 07:37:18, Laurence Harris wrote:

Just thought I'd point out that this leaks the string returned by CFURLCopyFileSystemPath.

There used to be this saying about Vegas house musicians reportedly having to able to: "read the spots off a leopard goin' 90 MPH in the dark after three scotches, in the dark…". I think you got the gig. :)



On 2006-10-31, at 07:40:12, Eric Schlegel wrote:

In addition to these, I think you can also use glob (man 3 glob). You'd just need to extract the file-system representation of your CFURL as a UTF8 string, pass it to glob, then create a new CFURL.

I've now tried it as well. Two new funcs are listed below. As I'm writing this, I see I've put an extra step in the glob method. Maybe I can shave a bit off it's time but it seems to me it requires a certain overhead anyway, so it might not get much faster. The pwhome and gehome measured include fixes for the leak Larry spotted.



# time ./fshome /Users/pma/Desktop

real    0m0.061s
user    0m0.038s
sys     0m0.013s

# time ./globhome
/Users/pma/Desktop

real    0m0.056s
user    0m0.037s
sys     0m0.013s

# time ./pwhome
/Users/pma/Desktop

real    0m0.008s
user    0m0.001s
sys     0m0.004s

# time ./gehome
/Users/pma/Desktop

real    0m0.007s
user    0m0.001s
sys     0m0.004s




#include <CoreFoundation/CoreFoundation.h> #include <glob.h>

CFStringRef CF_ReplaceTildeByGlob( CFStringRef theString );

CFStringRef CF_ReplaceTildeByGlob( CFStringRef theString ) {

CFMutableStringRef mute = NULL;

if( theString != NULL ) {
if( CFStringHasPrefix( theString, CFSTR( "~/" ) ) ) {

glob_t pglob;

if( ! glob( "~/", GLOB_TILDE, NULL, &pglob ) ) {

CFStringRef home = CFStringCreateWithCString ( kCFAllocatorDefault, pglob.gl_pathv[0], kCFStringEncodingUTF8 );

if( home != NULL ) {
mute = CFStringCreateMutableCopy( kCFAllocatorDefault, PATH_MAX, theString );
if( mute != NULL ) {
CFStringReplace( mute, CFRangeMake( 0, 2 ), home );
}
CFRelease( home );
}
globfree( &pglob );
}
}
else {
return( theString );
}
}

return( mute );
}



int main( int argc, char *argv[] ) { CFStringRef cfst = CFSTR( "~/Desktop" ); CFShow( CF_ReplaceTildeByGlob( cfst ) ); return 0; }

####

#include <CoreServices/CoreServices.h>

CFStringRef FS_ExpandTilde( CFStringRef theString );

CFStringRef FS_ExpandTilde( CFStringRef theString ) {

OSStatus err = noErr;
CFMutableStringRef mute = NULL;

if( theString != NULL ) {
if( CFStringHasPrefix( theString, CFSTR( "~/" ) ) ) {

FSRef fsrf = {{0}};
err = FSFindFolder( kOnAppropriateDisk, kCurrentUserFolderType, kDontCreateFolder, &fsrf );

if( err == noErr ) {

CFURLRef url = CFURLCreateFromFSRef( kCFAllocatorDefault, &fsrf );

if( url != NULL ) {

CFStringRef home = CFURLCopyFileSystemPath( url, kCFURLPOSIXPathStyle );

if( home != NULL ) {
mute = CFStringCreateMutableCopy( kCFAllocatorDefault, PATH_MAX, theString );
if( mute != NULL ) {
CFStringReplace( mute, CFRangeMake( 0, 1 ), CFURLCopyFileSystemPath( url, kCFURLPOSIXPathStyle ) );
}
CFRelease( home );
}
CFRelease( url );
}
}
}
else {
return( theString );
}
}

return( mute );
}



int main( int argc, char *argv[] ) { CFStringRef cfst = CFSTR( "~/Desktop" ); CFShow( FS_ExpandTilde( cfst ) ); return 0; }




Philip Aker email@hidden


_______________________________________________ Do not post admin requests to the list. They will be ignored. Carbon-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Re: Tilde expansion in paths (From: Dair Grant <email@hidden>)
 >Re: Tilde expansion in paths (From: Philip Aker <email@hidden>)
 >Re: Tilde expansion in paths (From: Jerry <email@hidden>)
 >Re: Tilde expansion in paths (From: Philip Aker <email@hidden>)
 >Re: Tilde expansion in paths (From: Laurence Harris <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.