[fpc-pascal] specifying the relative path to a file that is located in the same folder as the application bundle?

Jonas Maebe jonas.maebe at elis.ugent.be
Thu Feb 5 21:51:19 CET 2009


On 05 Feb 2009, at 21:29, Ken G. Brown wrote:

> So far I have the following external definitions:

All external definitions you need are already in the MacOSAll unit  
shipped with FPC.

> Function CFStringGetFileSystemRepresentation(myPathString :  
> CFStringRef; buffer : ppchar; maxBufLen : CFIndex) : Boolean; cdecl;  
> External;
> Function _CFStringGetFileSystemRepresentation(myPathString :  
> CFStringRef; buffer : ppchar; maxBufLen : CFIndex) : Boolean; C;  
> External;
>
> Function  
> CFStringGetMaximumSizeOfFileSystemRepresentation(myPathStr2 :  
> CFStringRef) : CFIndex; cdecl; External;
> Function  
> _CFStringGetMaximumSizeOfFileSystemRepresentation(myPathStr2 :  
> CFStringRef) : CFIndex; C; External;	
>
> Function CFBundleGetMainBundle() : CFBundleRef; cdecl; External;
> Function _CFBundleGetMainBundle() : CFBundleRef; C; External;

The ones with the extra underscore don't exist.

> and the following snippet
>
> var
> 		success : boolean = false;
> 		mainBundle : CFBundleRef;
> 		bundlePath : CFURLRef;
> 		bundlePathStr : CFStringRef;
> 		myBundlePathStr : ppchar;

myBundlePathStr has to be a pchar;

> begin
>
>
> // Get the main bundle for the app
> 	mainBundle := CFBundleGetMainBundle();
> 	bundlePath := CFBundleCopyBundleURL(mainBundle);
> 	
> 	bundlePathStr := CFURLCopyFileSystemPath (bundlePath,  
> kCFURLPOSIXPathStyle);
> 	
> 	
> success := CFStringGetFileSystemRepresentation (bundlePathStr,  
> myBundlePathStr,  
> CFStringGetMaximumSizeOfFileSystemRepresentation(bundlePathStr));
> 	
> I'm not quite sure if I am doing the 2nd arg correctly, it's  
> supposed to be char *buffer.
>
> Does this look alright? If not, what should I be using for the  
> myBundlePathString type?

You have to allocate memory for it. Something like

myBundlePathStr 
:= 
getmem(CFStringGetMaximumSizeOfFileSystemRepresentation(bundlePathStr));
success := CFStringGetFileSystemRepresentation (bundlePathStr,  
myBundlePathStr,  
CFStringGetMaximumSizeOfFileSystemRepresentation(bundlePathStr));

When interfacing with C routines, you have to do the same things as if  
you were using C.

> And I notice that CFStringGetFileSystemRepresentation is only  
> available for 10.4 and newer. What do i need to do instead if  
> wanting to deploy to 10.3.9?

Call CFStringGetCStringPtr and/or CFStringGetCStringPtr and tell it to  
convert to utf-8 (kCFStringEncodingUTF8).


Jonas



More information about the fpc-pascal mailing list