Re: [Q] CFStringGetCStringPtr( ..., kCFStringEncodingUTF8)
Re: [Q] CFStringGetCStringPtr( ..., kCFStringEncodingUTF8)
- Subject: Re: [Q] CFStringGetCStringPtr( ..., kCFStringEncodingUTF8)
- From: JongAm Park <email@hidden>
- Date: Tue, 23 Jun 2009 13:07:06 -0700
Hello, Aki
Oh.. I see. "when it can do efficiently" is the key!
Thank you very much for pointing that out.
Aki Inoue wrote:
JongAm,
The keyword here is that the function returns non-NULL when it can do
so "efficiently".
It all depends on a particular CFString instance's internal
representation and the encoding being passed.
The best encoding for a particular string is
CFStringGetFastestEncoding().
Aki
On Jun 23, 2009, at 12:13 PM, JongAm Park wrote:
Oh.. one more thing...
I think you didn't catch what I wanted to say.
It will be OK to return NULL if it can't convert a given string to a
string in a given text encoding method.
( But, yes.. Apple's document mentioned that its behaviour can be
changed, so don't rely on it. )
What I wanted to say was "Why does it return a meaningful address
when the system encoding is Japanese while it doesn't when the system
encoding is English?".
(When UTF8 is used for a target. )
Can't MacRoman be converted to UTF8 while MacJapanese can be?
I assumed that MacRoman code page is almost identical to UTF8,
because UTF8 is compatible with ASCII and MacRoman is extended form
for ASCII.
Anyway.. I will go around the problem...
Thank you anyway.
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
_______________________________________________
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