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.
36 WXDLLIMPEXP_BASE wxMBConv
* new_wxMBConv_cf( const char* name
)
38 wxMBConv_cf
*result
= new wxMBConv_cf(name
);
48 WXDLLIMPEXP_BASE wxMBConv
* new_wxMBConv_cf(wxFontEncoding encoding
)
50 wxMBConv_cf
*result
= new wxMBConv_cf(encoding
);
60 // Provide a constant for the wchat_t encoding used by the host platform.
61 #ifdef WORDS_BIGENDIAN
62 static const CFStringEncoding wxCFStringEncodingWcharT
= kCFStringEncodingUTF32BE
;
64 static const CFStringEncoding wxCFStringEncodingWcharT
= kCFStringEncodingUTF32LE
;
67 size_t wxMBConv_cf::ToWChar(wchar_t * dst
, size_t dstSize
, const char * src
, size_t srcSize
) const
69 wxCHECK(src
, wxCONV_FAILED
);
71 /* NOTE: This is wrong if the source encoding has an element size
72 * other than char (e.g. it's kCFStringEncodingUnicode)
73 * If the user specifies it, it's presumably right though.
74 * Right now we don't support UTF-16 in anyway since wx can do a better job.
76 if(srcSize
== wxNO_LEN
)
77 srcSize
= strlen(src
) + 1;
79 // First create the temporary CFString
80 wxCFRef
<CFStringRef
> theString( CFStringCreateWithBytes (
85 false //no BOM/external representation
88 if ( theString
== NULL
)
91 /* NOTE: The string content includes the NULL element if the source string did
92 * That means we have to do nothing special because the destination will have
93 * the NULL element iff the source did and the NULL element will be included
94 * in the count iff it was included in the source count.
98 /* If we're compiling against Tiger headers we can support direct conversion
99 * to UTF32. If we are then run against a pre-Tiger system, the encoding
100 * won't be available so we'll defer to the string->UTF-16->UTF-32 conversion.
102 if(CFStringIsEncodingAvailable(wxCFStringEncodingWcharT
))
104 CFRange fullStringRange
= CFRangeMake(0, CFStringGetLength(theString
));
107 CFIndex charsConverted
= CFStringGetBytes(
110 wxCFStringEncodingWcharT
,
113 // if dstSize is 0 then pass NULL to get required length in usedBufLen
114 dstSize
!= 0?(UInt8
*)dst
:NULL
,
115 dstSize
* sizeof(wchar_t),
118 if(charsConverted
< CFStringGetLength(theString
))
119 return wxCONV_FAILED
;
121 /* usedBufLen is the number of bytes written, so we divide by
122 * sizeof(wchar_t) to get the number of elements written.
124 wxASSERT( (usedBufLen
% sizeof(wchar_t)) == 0 );
126 // CFStringGetBytes does exactly the right thing when buffer
127 // pointer is NULL and returns the number of bytes required
128 return usedBufLen
/ sizeof(wchar_t);
132 // NOTE: Includes NULL iff source did
133 /* NOTE: This is an approximation. The eventual UTF-32 will
134 * possibly have less elements but certainly not more.
136 size_t returnSize
= CFStringGetLength(theString
);
138 if (dstSize
== 0 || dst
== NULL
)
143 // Convert the entire string.. too hard to figure out how many UTF-16 we'd need
144 // for an undersized UTF-32 destination buffer.
145 CFRange fullStringRange
= CFRangeMake(0, CFStringGetLength(theString
));
146 UniChar
*szUniCharBuffer
= new UniChar
[fullStringRange
.length
];
148 CFStringGetCharacters(theString
, fullStringRange
, szUniCharBuffer
);
150 wxMBConvUTF16 converter
;
151 returnSize
= converter
.ToWChar( dst
, dstSize
, (const char*)szUniCharBuffer
, fullStringRange
.length
);
152 delete [] szUniCharBuffer
;
159 size_t wxMBConv_cf::FromWChar(char *dst
, size_t dstSize
, const wchar_t *src
, size_t srcSize
) const
161 wxCHECK(src
, wxCONV_FAILED
);
163 if(srcSize
== wxNO_LEN
)
164 srcSize
= wxStrlen(src
) + 1;
166 // Temporary CFString
167 wxCFRef
<CFStringRef
> theString
;
169 /* If we're compiling against Tiger headers we can support direct conversion
170 * from UTF32. If we are then run against a pre-Tiger system, the encoding
171 * won't be available so we'll defer to the UTF-32->UTF-16->string conversion.
173 if(CFStringIsEncodingAvailable(wxCFStringEncodingWcharT
))
175 theString
= wxCFRef
<CFStringRef
>(CFStringCreateWithBytes(
178 srcSize
* sizeof(wchar_t),
179 wxCFStringEncodingWcharT
,
184 wxMBConvUTF16 converter
;
185 size_t cbUniBuffer
= converter
.FromWChar( NULL
, 0, src
, srcSize
);
186 wxASSERT(cbUniBuffer
% sizeof(UniChar
));
188 // Will be free'd by kCFAllocatorMalloc when CFString is released
189 UniChar
*tmpUniBuffer
= (UniChar
*)malloc(cbUniBuffer
);
191 cbUniBuffer
= converter
.FromWChar( (char*) tmpUniBuffer
, cbUniBuffer
, src
, srcSize
);
192 wxASSERT(cbUniBuffer
% sizeof(UniChar
));
194 theString
= wxCFRef
<CFStringRef
>(CFStringCreateWithCharactersNoCopy(
197 cbUniBuffer
/ sizeof(UniChar
),
203 wxCHECK(theString
!= NULL
, wxCONV_FAILED
);
207 CFIndex charsConverted
= CFStringGetBytes(
209 CFRangeMake(0, CFStringGetLength(theString
)),
211 0, // FAIL on unconvertible characters
212 false, // not an external representation
213 // if dstSize is 0 then pass NULL to get required length in usedBufLen
214 (dstSize
!= 0)?(UInt8
*)dst
:NULL
,
219 if(charsConverted
< CFStringGetLength(theString
) )
220 return wxCONV_FAILED
;