Re: [Q] CFStringGetCStringPtr( ..., kCFStringEncodingUTF8)
Re: [Q] CFStringGetCStringPtr( ..., kCFStringEncodingUTF8)
- Subject: Re: [Q] CFStringGetCStringPtr( ..., kCFStringEncodingUTF8)
- From: Greg Guerin <email@hidden>
- Date: Tue, 23 Jun 2009 11:20:30 -0700
JongAm Park wrote:
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.
It seems like you don't understand exactly what CFStringGetCStringPtr
() does.
First, CFStringGetCStringPtr does not perform a conversion.
Refer to CFStringGetCStringPtr's docs, which state:
"This function either returns the requested pointer immediately, with
no memory allocations and no copying, in constant time, or returns
NULL."
The phrases "no memory allocations" and "no copying" should be
clear. The phrase "in constant time" implies that no actual
conversion between encodings is performed, either. Therefore, it's
reasonable to conclude that if the passed-in string-ref is not
already in the requested encoding, CFStringGetCStringPtr() will
return NULL.
Unless you have a specific need for the extra speed that
CFStringGetCStringPtr() might offer, then you probably shouldn't use
CFStringGetCStringPtr() at all. Just use CFStringGetCString(),
because it will perform conversions. If you changed your posted code
to only use CFStringGetCString(), it would eliminate about 2/3 of the
code, maybe even more. Then you could add code that does the buffer-
length check to avoid overrunning the fixed-length outBuf, whose
length is currently never checked nor used as a limit (i.e. you have
a latent buffer overrun bug).
Also see the docs for CFStringGetCString, which say:
"You also typically call it as a “backup” when a prior call to the
CFStringGetCStringPtr function fails."
So clearly, the behavior you described in your post is not a bug. In
fact, it is the expected behavior, and the docs describe what to do
when it happens.
fullPath = (char*)CFStringGetCStringPtr(mstr, encodingMethod);
What is the type of 'mstr'?
How was 'mstr' produced?
-- GG
_______________________________________________
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