]> git.saurik.com Git - wxWidgets.git/blob - src/osx/core/strconv_cf.cpp
failed conversion shouldn't trigger an assert
[wxWidgets.git] / src / osx / core / strconv_cf.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/corefoundation/strconv.cpp
3 // Purpose: Unicode conversion classes
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2007-07-06
7 // RCS-ID: $Id$
8 // Copyright: (c) 2007 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/string.h"
17 #endif
18
19 #include "wx/strconv.h"
20 #include "wx/fontmap.h"
21
22 #ifdef __DARWIN__
23
24 #include "wx/osx/core/private/strconv_cf.h"
25 #include "wx/osx/core/cfref.h"
26
27
28 // ============================================================================
29 // CoreFoundation conversion classes
30 // ============================================================================
31
32 /* Provide factory functions for unit tests. Not in any header. Do not
33 * assume ABI compatibility even within a given wxWidgets release.
34 */
35
36 WXDLLIMPEXP_BASE wxMBConv* new_wxMBConv_cf( const char* name)
37 {
38 wxMBConv_cf *result = new wxMBConv_cf(name);
39 if(!result->IsOk())
40 {
41 delete result;
42 return NULL;
43 }
44 else
45 return result;
46 }
47
48 WXDLLIMPEXP_BASE wxMBConv* new_wxMBConv_cf(wxFontEncoding encoding)
49 {
50 wxMBConv_cf *result = new wxMBConv_cf(encoding);
51 if(!result->IsOk())
52 {
53 delete result;
54 return NULL;
55 }
56 else
57 return result;
58 }
59
60 // Provide a constant for the wchat_t encoding used by the host platform.
61 #ifdef WORDS_BIGENDIAN
62 static const CFStringEncoding wxCFStringEncodingWcharT = kCFStringEncodingUTF32BE;
63 #else
64 static const CFStringEncoding wxCFStringEncodingWcharT = kCFStringEncodingUTF32LE;
65 #endif
66
67 size_t wxMBConv_cf::ToWChar(wchar_t * dst, size_t dstSize, const char * src, size_t srcSize) const
68 {
69 wxCHECK(src, wxCONV_FAILED);
70
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.
75 */
76 if(srcSize == wxNO_LEN)
77 srcSize = strlen(src) + 1;
78
79 // First create the temporary CFString
80 wxCFRef<CFStringRef> theString( CFStringCreateWithBytes (
81 NULL, //the allocator
82 (const UInt8*)src,
83 srcSize,
84 m_encoding,
85 false //no BOM/external representation
86 ));
87
88 if ( theString == NULL )
89 return wxCONV_FAILED;
90
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.
95 */
96
97
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.
101 */
102 if(CFStringIsEncodingAvailable(wxCFStringEncodingWcharT))
103 {
104 CFRange fullStringRange = CFRangeMake(0, CFStringGetLength(theString));
105 CFIndex usedBufLen;
106
107 CFIndex charsConverted = CFStringGetBytes(
108 theString,
109 fullStringRange,
110 wxCFStringEncodingWcharT,
111 0,
112 false,
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),
116 &usedBufLen);
117
118 // charsConverted is > 0 iff conversion succeeded
119 if(charsConverted <= 0)
120 return wxCONV_FAILED;
121
122 /* usedBufLen is the number of bytes written, so we divide by
123 * sizeof(wchar_t) to get the number of elements written.
124 */
125 wxASSERT( (usedBufLen % sizeof(wchar_t)) == 0 );
126
127 // CFStringGetBytes does exactly the right thing when buffer
128 // pointer is NULL and returns the number of bytes required
129 return usedBufLen / sizeof(wchar_t);
130 }
131 else
132 {
133 // NOTE: Includes NULL iff source did
134 /* NOTE: This is an approximation. The eventual UTF-32 will
135 * possibly have less elements but certainly not more.
136 */
137 size_t returnSize = CFStringGetLength(theString);
138
139 if (dstSize == 0 || dst == NULL)
140 {
141 return returnSize;
142 }
143
144 // Convert the entire string.. too hard to figure out how many UTF-16 we'd need
145 // for an undersized UTF-32 destination buffer.
146 CFRange fullStringRange = CFRangeMake(0, CFStringGetLength(theString));
147 UniChar *szUniCharBuffer = new UniChar[fullStringRange.length];
148
149 CFStringGetCharacters(theString, fullStringRange, szUniCharBuffer);
150
151 wxMBConvUTF16 converter;
152 returnSize = converter.ToWChar( dst, dstSize, (const char*)szUniCharBuffer, fullStringRange.length );
153 delete [] szUniCharBuffer;
154
155 return returnSize;
156 }
157 // NOTREACHED
158 }
159
160 size_t wxMBConv_cf::FromWChar(char *dst, size_t dstSize, const wchar_t *src, size_t srcSize) const
161 {
162 wxCHECK(src, wxCONV_FAILED);
163
164 if(srcSize == wxNO_LEN)
165 srcSize = wxStrlen(src) + 1;
166
167 // Temporary CFString
168 wxCFRef<CFStringRef> theString;
169
170 /* If we're compiling against Tiger headers we can support direct conversion
171 * from UTF32. If we are then run against a pre-Tiger system, the encoding
172 * won't be available so we'll defer to the UTF-32->UTF-16->string conversion.
173 */
174 if(CFStringIsEncodingAvailable(wxCFStringEncodingWcharT))
175 {
176 theString = wxCFRef<CFStringRef>(CFStringCreateWithBytes(
177 kCFAllocatorDefault,
178 (UInt8*)src,
179 srcSize * sizeof(wchar_t),
180 wxCFStringEncodingWcharT,
181 false));
182 }
183 else
184 {
185 wxMBConvUTF16 converter;
186 size_t cbUniBuffer = converter.FromWChar( NULL, 0, src, srcSize );
187 wxASSERT(cbUniBuffer % sizeof(UniChar));
188
189 // Will be free'd by kCFAllocatorMalloc when CFString is released
190 UniChar *tmpUniBuffer = (UniChar*)malloc(cbUniBuffer);
191
192 cbUniBuffer = converter.FromWChar( (char*) tmpUniBuffer, cbUniBuffer, src, srcSize );
193 wxASSERT(cbUniBuffer % sizeof(UniChar));
194
195 theString = wxCFRef<CFStringRef>(CFStringCreateWithCharactersNoCopy(
196 kCFAllocatorDefault,
197 tmpUniBuffer,
198 cbUniBuffer / sizeof(UniChar),
199 kCFAllocatorMalloc
200 ));
201
202 }
203
204 wxCHECK(theString != NULL, wxCONV_FAILED);
205
206 CFIndex usedBufLen;
207
208 CFIndex charsConverted = CFStringGetBytes(
209 theString,
210 CFRangeMake(0, CFStringGetLength(theString)),
211 m_encoding,
212 0, // FAIL on unconvertible characters
213 false, // not an external representation
214 // if dstSize is 0 then pass NULL to get required length in usedBufLen
215 (dstSize != 0)?(UInt8*)dst:NULL,
216 dstSize,
217 &usedBufLen
218 );
219
220 // charsConverted is > 0 iff conversion succeeded
221 if(charsConverted <= 0)
222 return wxCONV_FAILED;
223
224 return usedBufLen;
225 }
226
227 #endif // __DARWIN__
228
229