Hello.

I didn't find any CoreFoundation mailing list, so I post my question about CFStringEtCStringPtr() here in Cocoa mailing list.

My problem is that CFStringGetCStringPtr( ..., kCFStringEncodingUTF8) doesn't convert a string passed through its first parameter and returns 0 on English system. However, on Japanese system, it returns a pointer to converted string correctly.

The first parameter points to a string which is a path in English. There is no Japanese characters. Because the 2nd parameter is one of 8 bit encoding, it didn't work before on Japanese system, where it was called like :

CFStringGetCStringPtr( ...,  CFStringGetSystemEncoding() )

The encoding method on a Japanese system is

kCFStringEncodingMacJapanese 
<http://developer.apple.com/documentation/CoreFoundation/Reference/CFStringRef/Reference/reference.html#//apple_ref/doc/c_ref/kCFStringEncodingMacJapanese>

So, I forced the 2nd parameter to kCFStringEncodingUTF8 and thought that it would work on English system also.
However, on an English system, of which system encoding is

kCFStringEncodingMacRoman 
<http://developer.apple.com/documentation/CoreFoundation/Reference/CFStringRef/Reference/reference.html#//apple_ref/doc/c_ref/kCFStringEncodingMacRoman>


It doesn't convert its 1st parameter and returned 0.
So, I had to call another CFStringGetCStringPtr() with CFStringGetSystemEncoding() as its 2nd parameter.

Is it a bug? Usually English encoding is the most trouble-free encoding method which can be converted to UTF8. So, I expected that it would work with kCFStringEncodingUTF8.
Or.. can anyone tell me why it doesn't work?

My whole source code which contains this fix is like

   char *fullPath;
   char outPath[512];;
Boolean conversionResult;
   CFStringEncoding encodingMethod;
// This is for ensuring safer operation. When CFStringGetCStringPtr() fails,
   // it tries CFStringGetCString().
encodingMethod = CFStringGetSystemEncoding(); // 1st try for English system
   fullPath = (char*)CFStringGetCStringPtr(mstr, encodingMethod);
   if( fullPath == NULL )
   {
       // 2nd try for Japanese system
       encodingMethod = kCFStringEncodingUTF8;
       fullPath = (char*)CFStringGetCStringPtr(mstr, encodingMethod);
   }
// for safer operation.
   if( fullPath == NULL )
   {
       CFIndex length = CFStringGetLength(mstr);
       fullPath = (char *)malloc( length + 1 );
conversionResult = CFStringGetCString(mstr, fullPath, length, kCFStringEncodingUTF8 ); strcpy( outPath, fullPath ); free( fullPath );
   }
   else
       strcpy( outPath, fullPath );

Thank you.

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to