1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/corefoundation/strconv.cpp
3 // Purpose: Unicode conversion classes
4 // Author: David Elliott
8 // Copyright: (c) 2007 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
16 #include "wx/string.h"
19 #include "wx/strconv.h"
20 #include "wx/fontmap.h"
24 #include "wx/osx/core/private/strconv_cf.h"
25 #include "wx/osx/core/cfref.h"
28 // ============================================================================
29 // CoreFoundation conversion classes
30 // ============================================================================
32 /* Provide factory functions for unit tests. Not in any header. Do not
33 * assume ABI compatibility even within a given wxWidgets release.
37 WXDLLIMPEXP_BASE wxMBConv
* new_wxMBConv_cf( const char* name
)
39 wxMBConv_cf
*result
= new wxMBConv_cf(name
);
48 #endif // wxUSE_FONTMAP
50 WXDLLIMPEXP_BASE wxMBConv
* new_wxMBConv_cf(wxFontEncoding encoding
)
52 wxMBConv_cf
*result
= new wxMBConv_cf(encoding
);
62 // Provide a constant for the wchat_t encoding used by the host platform.
63 #ifdef WORDS_BIGENDIAN
64 static const CFStringEncoding wxCFStringEncodingWcharT
= kCFStringEncodingUTF32BE
;
66 static const CFStringEncoding wxCFStringEncodingWcharT
= kCFStringEncodingUTF32LE
;
69 size_t wxMBConv_cf::ToWChar(wchar_t * dst
, size_t dstSize
, const char * src
, size_t srcSize
) const
71 wxCHECK(src
, wxCONV_FAILED
);
73 /* NOTE: This is wrong if the source encoding has an element size
74 * other than char (e.g. it's kCFStringEncodingUnicode)
75 * If the user specifies it, it's presumably right though.
76 * Right now we don't support UTF-16 in anyway since wx can do a better job.
78 if(srcSize
== wxNO_LEN
)
79 srcSize
= strlen(src
) + 1;
81 // First create the temporary CFString
82 wxCFRef
<CFStringRef
> theString( CFStringCreateWithBytes (
87 false //no BOM/external representation
90 if ( theString
== NULL
)
93 /* NOTE: The string content includes the NULL element if the source string did
94 * That means we have to do nothing special because the destination will have
95 * the NULL element iff the source did and the NULL element will be included
96 * in the count iff it was included in the source count.
100 /* If we're compiling against Tiger headers we can support direct conversion
101 * to UTF32. If we are then run against a pre-Tiger system, the encoding
102 * won't be available so we'll defer to the string->UTF-16->UTF-32 conversion.
104 if(CFStringIsEncodingAvailable(wxCFStringEncodingWcharT
))
106 CFRange fullStringRange
= CFRangeMake(0, CFStringGetLength(theString
));
109 CFIndex charsConverted
= CFStringGetBytes(
112 wxCFStringEncodingWcharT
,
115 // if dstSize is 0 then pass NULL to get required length in usedBufLen
116 dstSize
!= 0?(UInt8
*)dst
:NULL
,
117 dstSize
* sizeof(wchar_t),
120 if(charsConverted
< CFStringGetLength(theString
))
121 return wxCONV_FAILED
;
123 /* usedBufLen is the number of bytes written, so we divide by
124 * sizeof(wchar_t) to get the number of elements written.
126 wxASSERT( (usedBufLen
% sizeof(wchar_t)) == 0 );
128 // CFStringGetBytes does exactly the right thing when buffer
129 // pointer is NULL and returns the number of bytes required
130 return usedBufLen
/ sizeof(wchar_t);
134 // NOTE: Includes NULL iff source did
135 /* NOTE: This is an approximation. The eventual UTF-32 will
136 * possibly have less elements but certainly not more.
138 size_t returnSize
= CFStringGetLength(theString
);
140 if (dstSize
== 0 || dst
== NULL
)
145 // Convert the entire string.. too hard to figure out how many UTF-16 we'd need
146 // for an undersized UTF-32 destination buffer.
147 CFRange fullStringRange
= CFRangeMake(0, CFStringGetLength(theString
));
148 UniChar
*szUniCharBuffer
= new UniChar
[fullStringRange
.length
];
150 CFStringGetCharacters(theString
, fullStringRange
, szUniCharBuffer
);
152 wxMBConvUTF16 converter
;
153 returnSize
= converter
.ToWChar( dst
, dstSize
, (const char*)szUniCharBuffer
, fullStringRange
.length
);
154 delete [] szUniCharBuffer
;
161 size_t wxMBConv_cf::FromWChar(char *dst
, size_t dstSize
, const wchar_t *src
, size_t srcSize
) const
163 wxCHECK(src
, wxCONV_FAILED
);
165 if(srcSize
== wxNO_LEN
)
166 srcSize
= wxStrlen(src
) + 1;
168 // Temporary CFString
169 wxCFRef
<CFStringRef
> theString
;
171 /* If we're compiling against Tiger headers we can support direct conversion
172 * from UTF32. If we are then run against a pre-Tiger system, the encoding
173 * won't be available so we'll defer to the UTF-32->UTF-16->string conversion.
175 if(CFStringIsEncodingAvailable(wxCFStringEncodingWcharT
))
177 theString
= wxCFRef
<CFStringRef
>(CFStringCreateWithBytes(
180 srcSize
* sizeof(wchar_t),
181 wxCFStringEncodingWcharT
,
186 wxMBConvUTF16 converter
;
187 size_t cbUniBuffer
= converter
.FromWChar( NULL
, 0, src
, srcSize
);
188 wxASSERT(cbUniBuffer
% sizeof(UniChar
));
190 // Will be free'd by kCFAllocatorMalloc when CFString is released
191 UniChar
*tmpUniBuffer
= (UniChar
*)malloc(cbUniBuffer
);
193 cbUniBuffer
= converter
.FromWChar( (char*) tmpUniBuffer
, cbUniBuffer
, src
, srcSize
);
194 wxASSERT(cbUniBuffer
% sizeof(UniChar
));
196 theString
= wxCFRef
<CFStringRef
>(CFStringCreateWithCharactersNoCopy(
199 cbUniBuffer
/ sizeof(UniChar
),
205 wxCHECK(theString
!= NULL
, wxCONV_FAILED
);
209 CFIndex charsConverted
= CFStringGetBytes(
211 CFRangeMake(0, CFStringGetLength(theString
)),
213 0, // FAIL on unconvertible characters
214 false, // not an external representation
220 // when dst is non-NULL, we check usedBufLen against dstSize as
221 // CFStringGetBytes sometimes treats dst as being NULL when dstSize==0
222 if( (charsConverted
< CFStringGetLength(theString
)) ||
223 (dst
&& (size_t) usedBufLen
> dstSize
) )
224 return wxCONV_FAILED
;