Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > relative path to FSRef

relative path to FSRef
Thread Tools
Fresh-Faced Recruit
Join Date: Oct 2002
Location: Vancouver, Canada
Status: Offline
Reply With Quote
Jul 25, 2003, 05:25 AM
 
i have an interesting question. When I feed a path that starts with ~/ to FSPathMakeRef() i get a -43 error....file not found.

The program I'm writing needs to access the current user's library folder so I cant think of any other way of doing this other than using the ~/library/ path. Does anyone have any suggestion on work arounds.

thanx
     
Forum Regular
Join Date: Oct 2001
Location: Sweden
Status: Offline
Reply With Quote
Jul 25, 2003, 06:51 AM
 
If you're using cocoa you can check out the NSString method stringByExpandingTildeInPath: or the Foundation function NSHomeDirectory()
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Jul 25, 2003, 09:47 AM
 
Form a file URL for your path using either NSURL or CFURlRef (they're toll-free bridgable to one another, so you can just cast from one to the other). Use CFURLGetFSRef to get your FSRef. There is no step three.
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
oexel  (op)
Fresh-Faced Recruit
Join Date: Oct 2002
Location: Vancouver, Canada
Status: Offline
Reply With Quote
Aug 16, 2003, 04:56 AM
 
i dont see how using CFURL helps me. It wont aceept a path that starts with ~/. What I need is a carbon version of that cocoa tilde expansion function. Can anyone help?
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Aug 16, 2003, 07:32 AM
 
Code:
#include <CoreFoundation/CoreFoundation.h> #include <unistd.h> #include <pwd.h> CFURLRef FDHomeDirectory( void ); CFURLRef FDHomeDirectoryForUserID( uid_t uid ); CFStringRef FDStringByReplacingTildeInPath( CFStringRef theString ); CFURLRef FDHomeDirectory( void ) { return FDHomeDirectoryForUserID( geteuid() ); } CFURLRef FDHomeDirectoryForUserID( uid_t uid ) { struct passwd *pw = getpwuid( uid ); if (pw != NULL) return CFURLCreateFromFileSystemRepresentation( NULL, pw->pw_dir, strlen(pw->pw_dir), true ); else return NULL; } CFStringRef FDStringByReplacingTildeInPath( CFStringRef theString ) { if ( CFStringHasPrefix( theString, CFSTR("~/") ) ) { CFMutableStringRef mutableString = CFStringCreateMutableCopy( NULL, PATH_MAX, theString ); CFStringReplace( mutableString, CFRangeMake( 0, 1 ), CFURLCopyFileSystemPath( FDHomeDirectory(), kCFURLPOSIXPathStyle) ); return mutableString; } else { return theString; } } int main (int argc, const char * argv[]) { // insert code here... CFStringRef string = CFSTR("~/Desktop"); CFShow( FDStringByReplacingTildeInPath( string ) ); return 0; }
Fixing the leaks is left as an exercise for the reader.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:14 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2