c++cmacosmacos-carboncodewarrior

How to convert CFString to HFSUniStr255?


First things first, this relates to ancient technology. The program port I'm working on is maintained in Metrowerks Codewarrior 9, target is PPC.

For use in MSL C's FSRefParentAndFilename_fopen function

FILE * FSRefParentAndFilename_fopen(
const FSRefPtr theParentRef,
ConstHFSUniStr255Param theName,
const char *open_mode);

I need a ConstHFSUniStr255Param which is a pointer to a HFSUniStr255. I have a CFString containing my filename, my question is how do I convert to HFSUniStr255?

struct HFSUniStr255 {
UInt16 length;
UniChar unicode[255];
};

So far I have:

HFSUniStr255 HFSString;
FSGetDataForkName(&HFSString);
HFSString.length=(uint16)CFStringGetLength(fileName);
HFSString.unicode=?

Solution

  • You can use the following snippet:

    HFSString.length=(uint16)CFStringGetLength(fileName);
    CFStringGetCharacters(filename, CFRangeMake(0, CFStringGetLength(filename)), HFSString.unicode);
    

    Be sure however that your filename is valid, and in particular that its length is less than 255 Unicode characters. You will have to face the consequences of a buffer overflow otherwise.