• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: [Q] CFStringGetCStringPtr( ..., kCFStringEncodingUTF8)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Q] CFStringGetCStringPtr( ..., kCFStringEncodingUTF8)


  • Subject: Re: [Q] CFStringGetCStringPtr( ..., kCFStringEncodingUTF8)
  • From: Aki Inoue <email@hidden>
  • Date: Tue, 23 Jun 2009 12:25:33 -0700

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


  • Follow-Ups:
    • Re: [Q] CFStringGetCStringPtr( ..., kCFStringEncodingUTF8)
      • From: JongAm Park <email@hidden>
References: 
 >[Q] CFStringGetCStringPtr( ..., kCFStringEncodingUTF8) (From: JongAm Park <email@hidden>)
 >Re: [Q] CFStringGetCStringPtr( ..., kCFStringEncodingUTF8) (From: Laurent Cerveau <email@hidden>)
 >Re: [Q] CFStringGetCStringPtr( ..., kCFStringEncodingUTF8) (From: JongAm Park <email@hidden>)

  • Prev by Date: bluetooth on the iPhone
  • Next by Date: Re: Problem: how to get glyph descent and ascent
  • Previous by thread: Re: [Q] CFStringGetCStringPtr( ..., kCFStringEncodingUTF8)
  • Next by thread: Re: [Q] CFStringGetCStringPtr( ..., kCFStringEncodingUTF8)
  • Index(es):
    • Date
    • Thread