1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: conversion routines for char sets any Unicode
4 // Author: Robert Roebling, Ove Kaaven
8 // Copyright: (c) 1998 Ove Kaaven, Robert Roebling, Vadim Zeitlin
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_WXSTRCONVH__
13 #define _WX_WXSTRCONVH__
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "strconv.h"
20 #include "wx/wxchar.h"
21 #include "wx/buffer.h"
23 #ifdef __DIGITALMARS__
27 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
35 // ----------------------------------------------------------------------------
36 // wxMBConv (abstract base class for conversions)
37 // ----------------------------------------------------------------------------
39 class WXDLLIMPEXP_BASE wxMBConv
42 // the actual conversion takes place here
44 // note that outputSize is the size of the output buffer, not the length of input
45 // (the latter is always supposed to be NUL-terminated)
46 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const = 0;
47 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const = 0;
50 const wxWCharBuffer
cMB2WC(const char *psz
) const;
51 const wxCharBuffer
cWC2MB(const wchar_t *psz
) const;
53 // MB <-> WC for strings with embedded null characters
55 // pszLen length of the input string
56 // pOutSize gets the final size of the converted string
57 const wxWCharBuffer
cMB2WC(const char *psz
, size_t pszLen
, size_t* pOutSize
) const;
58 const wxCharBuffer
cWC2MB(const wchar_t *psz
, size_t pszLen
, size_t* pOutSize
) const;
60 // convenience functions for converting MB or WC to/from wxWin default
62 const wxWCharBuffer
cMB2WX(const char *psz
) const { return cMB2WC(psz
); }
63 const wxCharBuffer
cWX2MB(const wchar_t *psz
) const { return cWC2MB(psz
); }
64 const wchar_t* cWC2WX(const wchar_t *psz
) const { return psz
; }
65 const wchar_t* cWX2WC(const wchar_t *psz
) const { return psz
; }
67 const char* cMB2WX(const char *psz
) const { return psz
; }
68 const char* cWX2MB(const char *psz
) const { return psz
; }
69 const wxCharBuffer
cWC2WX(const wchar_t *psz
) const { return cWC2MB(psz
); }
70 const wxWCharBuffer
cWX2WC(const char *psz
) const { return cMB2WC(psz
); }
71 #endif // Unicode/ANSI
73 // virtual dtor for any base class
77 // ----------------------------------------------------------------------------
78 // wxMBConvLibc uses standard mbstowcs() and wcstombs() functions for
79 // conversion (hence it depends on the current locale)
80 // ----------------------------------------------------------------------------
82 class WXDLLIMPEXP_BASE wxMBConvLibc
: public wxMBConv
85 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
86 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
89 // not very accurately named because it is not necessarily of type wxMBConvLibc
90 // (but the name can't eb changed because of backwards compatibility) default
92 WXDLLIMPEXP_DATA_BASE(extern wxMBConv
&) wxConvLibc
;
94 // ----------------------------------------------------------------------------
95 // wxMBConvUTF7 (for conversion using UTF7 encoding)
96 // ----------------------------------------------------------------------------
98 class WXDLLIMPEXP_BASE wxMBConvUTF7
: public wxMBConv
101 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
102 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
105 WXDLLIMPEXP_DATA_BASE(extern wxMBConvUTF7
&) wxConvUTF7
;
107 // ----------------------------------------------------------------------------
108 // wxMBConvUTF8 (for conversion using UTF8 encoding)
109 // ----------------------------------------------------------------------------
111 class WXDLLIMPEXP_BASE wxMBConvUTF8
: public wxMBConv
114 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
115 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
118 WXDLLIMPEXP_DATA_BASE(extern wxMBConvUTF8
&) wxConvUTF8
;
120 // ----------------------------------------------------------------------------
121 // wxMBConvUTF16LE (for conversion using UTF16 Little Endian encoding)
122 // ----------------------------------------------------------------------------
124 class WXDLLIMPEXP_BASE wxMBConvUTF16LE
: public wxMBConv
127 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
128 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
131 // ----------------------------------------------------------------------------
132 // wxMBConvUTF16BE (for conversion using UTF16 Big Endian encoding)
133 // ----------------------------------------------------------------------------
135 class WXDLLIMPEXP_BASE wxMBConvUTF16BE
: public wxMBConv
138 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
139 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
142 // ----------------------------------------------------------------------------
143 // wxMBConvUCS4LE (for conversion using UTF32 Little Endian encoding)
144 // ----------------------------------------------------------------------------
146 class WXDLLIMPEXP_BASE wxMBConvUTF32LE
: public wxMBConv
149 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
150 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
153 // ----------------------------------------------------------------------------
154 // wxMBConvUCS4BE (for conversion using UTF32 Big Endian encoding)
155 // ----------------------------------------------------------------------------
157 class WXDLLIMPEXP_BASE wxMBConvUTF32BE
: public wxMBConv
160 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
161 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
164 // ----------------------------------------------------------------------------
165 // wxCSConv (for conversion based on loadable char sets)
166 // ----------------------------------------------------------------------------
168 #include "wx/fontenc.h"
170 class WXDLLIMPEXP_BASE wxCSConv
: public wxMBConv
173 // we can be created either from charset name or from an encoding constant
174 // but we can't have both at once
175 wxCSConv(const wxChar
*charset
);
176 wxCSConv(wxFontEncoding encoding
);
178 wxCSConv(const wxCSConv
& conv
);
181 wxCSConv
& operator=(const wxCSConv
& conv
);
183 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
184 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
189 // common part of all ctors
192 // creates m_convReal if necessary
193 void CreateConvIfNeeded() const;
195 // do create m_convReal (unconditionally)
196 wxMBConv
*DoCreate() const;
198 // set the name (may be only called when m_name == NULL), makes copy of
200 void SetName(const wxChar
*charset
);
203 // note that we can't use wxString here because of compilation
204 // dependencies: we're included from wx/string.h
206 wxFontEncoding m_encoding
;
208 // use CreateConvIfNeeded() before accessing m_convReal!
209 wxMBConv
*m_convReal
;
214 #define wxConvFile wxConvUTF8
216 #define wxConvFile wxConvLocal
219 WXDLLIMPEXP_DATA_BASE(extern wxCSConv
&) wxConvLocal
;
220 WXDLLIMPEXP_DATA_BASE(extern wxCSConv
&) wxConvISO8859_1
;
221 WXDLLIMPEXP_DATA_BASE(extern wxMBConv
*) wxConvCurrent
;
223 // ----------------------------------------------------------------------------
224 // endianness-dependent conversions
225 // ----------------------------------------------------------------------------
227 #ifdef WORDS_BIGENDIAN
228 typedef wxMBConvUTF16BE wxMBConvUTF16
;
229 typedef wxMBConvUTF32BE wxMBConvUTF32
;
231 typedef wxMBConvUTF16LE wxMBConvUTF16
;
232 typedef wxMBConvUTF32LE wxMBConvUTF32
;
235 // ----------------------------------------------------------------------------
236 // filename conversion macros
237 // ----------------------------------------------------------------------------
239 // filenames are multibyte on Unix and probably widechar on Windows?
240 #if defined(__UNIX__) || defined(__BORLANDC__) || defined(__WXMAC__ )
246 #if wxMBFILES && wxUSE_UNICODE
247 #define wxFNCONV(name) wxConvFile.cWX2MB(name)
248 #define wxFNSTRINGCAST wxMBSTRINGCAST
250 #if defined( __WXOSX__ ) && wxMBFILES
251 #define wxFNCONV(name) wxConvFile.cWC2MB( wxConvLocal.cWX2WC(name) )
253 #define wxFNCONV(name) name
255 #define wxFNSTRINGCAST WXSTRINGCAST
261 // ----------------------------------------------------------------------------
262 // stand-ins in absence of wchar_t
263 // ----------------------------------------------------------------------------
265 class WXDLLIMPEXP_BASE wxMBConv
268 const char* cMB2WX(const char *psz
) const { return psz
; }
269 const char* cWX2MB(const char *psz
) const { return psz
; }
272 #define wxConvFile wxConvLocal
274 WXDLLIMPEXP_DATA_BASE(extern wxMBConv
) wxConvLibc
,
278 WXDLLIMPEXP_DATA_BASE(extern wxMBConv
*) wxConvCurrent
;
280 #define wxFNCONV(name) name
281 #define wxFNSTRINGCAST WXSTRINGCAST
286 // ----------------------------------------------------------------------------
287 // macros for the most common conversions
288 // ----------------------------------------------------------------------------
291 #define wxConvertWX2MB(s) wxConvCurrent->cWX2MB(s)
292 #define wxConvertMB2WX(s) wxConvCurrent->cMB2WX(s)
294 // no conversions to do
295 #define wxConvertWX2MB(s) (s)
296 #define wxConvertMB2WX(s) (s)
297 #endif // Unicode/ANSI