Re: [Q] CFStringGetCStringPtr( ..., kCFStringEncodingUTF8)
Re: [Q] CFStringGetCStringPtr( ..., kCFStringEncodingUTF8)
- Subject: Re: [Q] CFStringGetCStringPtr( ..., kCFStringEncodingUTF8)
- From: JongAm Park <email@hidden>
- Date: Tue, 23 Jun 2009 12:06:14 -0700
Thanks for your reply.
However, even with CFStringGetCString() with the 2nd parameter
"kCFStringEncodingUTF8" doesn't work correctly.
Laurent Cerveau wrote:
According to the doc it is completely OK for the CFString....Ptr to
return NULL.
CFStringGetCStringPtr()
CFStringGetCharactersPtr()
These functions are provided for optimization only. They will either
return the desired
pointer quickly, in constant time, or they return NULL. They might
choose to return NULL
for many reasons; for instance it's possible that for users running in
different
languages these sometimes return NULL; or in a future OS release the
first two might
switch to always returning NULL. Never observing NULL returns in your
usages of these
functions does not mean they won't ever return NULL. (But note the
CFStringGetCharactersPtr()
exception mentioned further below.)
What you do with falling back to CFStringGetCString is right
laurent
On Jun 23, 2009, at 7:26 PM, JongAm Park wrote:
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 (email@hidden
<mailto:email@hidden>)
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:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden