+// ============================================================================
+// Mac conversion classes
+// ============================================================================
+
+#if defined(__WXMAC__) && defined(TARGET_CARBON)
+
+class wxMBConv_mac : public wxMBConv
+{
+public:
+ wxMBConv_mac()
+ {
+ Init(CFStringGetSystemEncoding()) ;
+ }
+
+ wxMBConv_mac(const wxChar* name)
+ {
+ Init( wxMacGetSystemEncFromFontEnc(wxFontMapper::Get()->CharsetToEncoding(name, FALSE) ) ) ;
+ }
+
+ wxMBConv_mac(wxFontEncoding encoding)
+ {
+ Init( wxMacGetSystemEncFromFontEnc(encoding) );
+ }
+
+ ~wxMBConv_mac()
+ {
+ OSStatus status = noErr ;
+ status = TECDisposeConverter(m_MB2WC_converter);
+ status = TECDisposeConverter(m_WC2MB_converter);
+ }
+
+
+ void Init( TextEncodingBase encoding)
+ {
+ OSStatus status = noErr ;
+ m_char_encoding = encoding ;
+#if SIZEOF_WCHAR_T == 4
+ m_unicode_encoding = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode32BitFormat) ;
+#else
+ m_unicode_encoding = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode16BitFormat) ;
+#endif
+ status = TECCreateConverter(&m_MB2WC_converter,
+ m_char_encoding,
+ m_unicode_encoding);
+ status = TECCreateConverter(&m_WC2MB_converter,
+ m_unicode_encoding,
+ m_char_encoding);
+ }
+
+ size_t MB2WC(wchar_t *buf, const char *psz, size_t n) const
+ {
+ OSStatus status = noErr ;
+ ByteCount byteOutLen ;
+ ByteCount byteInLen = strlen(psz) ;
+ wchar_t *tbuf = NULL ;
+
+ if (buf == NULL)
+ {
+ n = byteInLen ;
+ tbuf = (wchar_t*) malloc( n * SIZEOF_WCHAR_T) ;
+ }
+
+ ByteCount byteBufferLen = n * SIZEOF_WCHAR_T ;
+ status = TECConvertText(m_MB2WC_converter, (ConstTextPtr) psz , byteInLen, &byteInLen,
+ (TextPtr) (buf ? buf : tbuf) , byteBufferLen, &byteOutLen);
+
+ if ( buf == NULL )
+ free(tbuf) ;
+
+ size_t res = byteOutLen / SIZEOF_WCHAR_T ;
+ if ( buf && res < n)
+ buf[res] = 0;
+
+ return res ;
+ }
+
+ size_t WC2MB(char *buf, const wchar_t *psz, size_t n) const
+ {
+ OSStatus status = noErr ;
+ ByteCount byteOutLen ;
+ ByteCount byteInLen = wxWcslen(psz) * SIZEOF_WCHAR_T ;
+
+ char *tbuf = NULL ;
+
+ if (buf == NULL)
+ {
+ // worst case
+ n = byteInLen * 2 ;
+ tbuf = (char*) malloc( n ) ;
+ }
+
+ ByteCount byteBufferLen = n ;
+ status = TECConvertText(m_WC2MB_converter, (ConstTextPtr) psz , byteInLen, &byteInLen,
+ (TextPtr) ( buf ? buf : tbuf ) , byteBufferLen, &byteOutLen);
+
+ if ( buf == NULL )
+ free(tbuf) ;
+
+ size_t res = byteOutLen ;
+ if ( buf && res < n)
+ buf[res] = 0;
+
+ return res ;
+ }
+
+ bool IsOk() const
+ { return m_MB2WC_converter != NULL && m_WC2MB_converter != NULL ; }
+
+private:
+ TECObjectRef m_MB2WC_converter ;
+ TECObjectRef m_WC2MB_converter ;
+
+ TextEncodingBase m_char_encoding ;
+ TextEncodingBase m_unicode_encoding ;
+};
+
+#endif // defined(__WXMAC__) && defined(TARGET_CARBON)