Carbon File Code

Posts: 45
Joined: 2002.04
Post: #1
Can someone give me a very simple function that finds a file called "World Map" inside of the folder "Senario Files".

the names don't matter much, but I need some code that creates an FSSpec to the file. The Directory is in the same one as the App.

I just need the bare minimum..
I was told in a previous message I sent that in OS X the file system works differently and when you specify a file by something like "\p:Senario Files:World Map" it doesn't initally look in the apps directory but else where.

I have looked at documentation on the apple site and haven't yet found what I am looking for. I will continue to search until I get a response here.


PS: I am working with the DataFork.

/* Drunk...... fix later.... */
Quote this message in a reply
Post: #2
You first have to find out the directory your application is in. Then create a string with the full pathname of the file and last convert it to an FSSpec.
Here's the code I use for this:

int main(int argc, char **argv)
int a, b;
char programDirectory[1024];
FSRef fsr;
FSSpec fsp;
char fileName[1024];

//First crack out the program directory from the argv parameter
sprintf(programDirectory, "%s", *argv);
for (a=strlen(programDirectory); a> 0; a--)
if (programDirectory[a]=='/'){b++;}
if (b==4){a=0;}
strcat(programDirectory, "/");

//Now create the file name of the file
sprintf(fileName, "%sScenario Files/World Map", programDirectory);

FSPathMakeRef(fileName, &fsr, NULL);//Convert pathname to FSRef
FSGetCatalogInfo (&fsr, NULL, NULL, NULL, &fsp, NULL);//Convert it to FSSpec

//Now you work with your FSSpec (fsp here). Use something like FSpOpenDF() to open it up. Good luck

return 0;
Quote this message in a reply
Posts: 104
Joined: 2002.04
Post: #3
Though, if yopu can swing it, working with FSRefs is much better. FSSpec stuff on MacOS X is pretty hacky, though it can mostly work. There are FSRef calls that are corrollary to most FSSpec file system calls. The only issue is in using Quicktime ro some other API that hasn't moved to FSRefs.
Quote this message in a reply
Posts: 116
Joined: 2002.04
Post: #4
Here's what I use. Note that my purpose in this code is to get the top level of the application package directory. Thus, once I get the path to the executable, I chop off the last directory in the path.

CFBundleRef bundle;
    CFURLRef bundle_url;
    CFStringRef    sr;
    char  path[1024];

    bundle = CFBundleGetMainBundle();
    if ( ! bundle )
        goto mac_fail;
    bundle_url=CFBundleCopyBundleURL( bundle );
    if ( ! bundle_url )
        goto mac_fail;
    sr=CFURLCopyFileSystemPath(bundle_url, kCFURLPOSIXPathStyle);
    if ( ! sr )
        goto mac_fail;
    if ( CFStringGetCString(sr, path, 1024, kCFStringEncodingASCII) )
        end=strrchr(path, '/');
        if ( end )
            goto mac_fail;
        if ( chdir(path) )
            goto mac_fail;  //chdir returns -1 if unsucessful

    CFRelease( bundle_url );
    CFRelease( sr );

Quote this message in a reply
Post: #5
Both methods should work well. I'm not experienced with all those new CFString..., CFBundle... calls so I'm using a more "traditional" code Smile . The advantage of wadesworld's solution is that you don't need any parameters for your main function and can simply create an "int main(void)".

Quote this message in a reply
Post: #6
Just this afternoon I decided I should learn how to use the CFBundle stuff for creating file paths. I thought I'd just check idev to see if there was anything useful and bingo.

Thanks wade, that code is exactly what I was after.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Recursively Packing Source Code Into One File? daveh84 15 12,376 Feb 3, 2010 08:36 PM
Last Post: AndyKorth
  Bundles with Carbon File Manager... BinarySpike 2 4,665 Apr 25, 2005 03:36 PM
Last Post: BinarySpike
  File Code Problems Muffinking 16 12,168 Jun 17, 2002 06:54 PM
Last Post: WFleming