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 // ----------------------------------------------------------------------------
90 // wxMBConvUTF7 (for conversion using UTF7 encoding)
91 // ----------------------------------------------------------------------------
93 class WXDLLIMPEXP_BASE wxMBConvUTF7
: public wxMBConv
96 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
97 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
100 // ----------------------------------------------------------------------------
101 // wxMBConvUTF8 (for conversion using UTF8 encoding)
102 // ----------------------------------------------------------------------------
104 class WXDLLIMPEXP_BASE wxMBConvUTF8
: public wxMBConv
107 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
108 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
111 // ----------------------------------------------------------------------------
112 // wxMBConvUTF16LE (for conversion using UTF16 Little Endian encoding)
113 // ----------------------------------------------------------------------------
115 class WXDLLIMPEXP_BASE wxMBConvUTF16LE
: public wxMBConv
118 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
119 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
122 // ----------------------------------------------------------------------------
123 // wxMBConvUTF16BE (for conversion using UTF16 Big Endian encoding)
124 // ----------------------------------------------------------------------------
126 class WXDLLIMPEXP_BASE wxMBConvUTF16BE
: public wxMBConv
129 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
130 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
133 // ----------------------------------------------------------------------------
134 // wxMBConvUCS4LE (for conversion using UTF32 Little Endian encoding)
135 // ----------------------------------------------------------------------------
137 class WXDLLIMPEXP_BASE wxMBConvUTF32LE
: public wxMBConv
140 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
141 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
144 // ----------------------------------------------------------------------------
145 // wxMBConvUCS4BE (for conversion using UTF32 Big Endian encoding)
146 // ----------------------------------------------------------------------------
148 class WXDLLIMPEXP_BASE wxMBConvUTF32BE
: public wxMBConv
151 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
152 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
155 // ----------------------------------------------------------------------------
156 // wxCSConv (for conversion based on loadable char sets)
157 // ----------------------------------------------------------------------------
159 #include "wx/fontenc.h"
161 class WXDLLIMPEXP_BASE wxCSConv
: public wxMBConv
164 // we can be created either from charset name or from an encoding constant
165 // but we can't have both at once
166 wxCSConv(const wxChar
*charset
);
167 wxCSConv(wxFontEncoding encoding
);
169 wxCSConv(const wxCSConv
& conv
);
172 wxCSConv
& operator=(const wxCSConv
& conv
);
174 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
175 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
180 // common part of all ctors
183 // creates m_convReal if necessary
184 void CreateConvIfNeeded() const;
186 // do create m_convReal (unconditionally)
187 wxMBConv
*DoCreate() const;
189 // set the name (may be only called when m_name == NULL), makes copy of
191 void SetName(const wxChar
*charset
);
194 // note that we can't use wxString here because of compilation
195 // dependencies: we're included from wx/string.h
197 wxFontEncoding m_encoding
;
199 // use CreateConvIfNeeded() before accessing m_convReal!
200 wxMBConv
*m_convReal
;
204 // ----------------------------------------------------------------------------
205 // declare predefined conversion objects
206 // ----------------------------------------------------------------------------
208 // conversion to be used with all standard functions affected by locale, e.g.
209 // strtol(), strftime(), ...
210 extern WXDLLIMPEXP_DATA_BASE(wxMBConv
&) wxConvLibc
;
212 // conversion ISO-8859-1/UTF-7/UTF-8 <-> wchar_t
213 extern WXDLLIMPEXP_DATA_BASE(wxCSConv
&) wxConvISO8859_1
;
214 extern WXDLLIMPEXP_DATA_BASE(wxMBConvUTF7
&) wxConvUTF7
;
215 extern WXDLLIMPEXP_DATA_BASE(wxMBConvUTF8
&) wxConvUTF8
;
217 // conversion used for the file names on the systems where they're not Unicode
218 // (basically anything except Windows)
220 // this is used by all file functions, can be changed by the application
222 // by default UTF-8 under Mac OS X and wxConvLibc elsewhere (but it's not used
223 // under Windows normally)
224 extern WXDLLIMPEXP_DATA_BASE(wxMBConv
*) wxConvFileName
;
226 // backwards compatible define
227 #define wxConvFile (*wxConvFileName)
229 // the current conversion object, may be set to any conversion, is used by
230 // default in a couple of places inside wx (initially same as wxConvLibc)
231 extern WXDLLIMPEXP_DATA_BASE(wxMBConv
*) wxConvCurrent
;
234 extern WXDLLIMPEXP_DATA_BASE(wxCSConv
&) wxConvLocal
;
237 // ----------------------------------------------------------------------------
238 // endianness-dependent conversions
239 // ----------------------------------------------------------------------------
241 #ifdef WORDS_BIGENDIAN
242 typedef wxMBConvUTF16BE wxMBConvUTF16
;
243 typedef wxMBConvUTF32BE wxMBConvUTF32
;
245 typedef wxMBConvUTF16LE wxMBConvUTF16
;
246 typedef wxMBConvUTF32LE wxMBConvUTF32
;
249 // ----------------------------------------------------------------------------
250 // filename conversion macros
251 // ----------------------------------------------------------------------------
253 // filenames are multibyte on Unix and probably widechar on Windows?
254 #if defined(__UNIX__) || defined(__BORLANDC__) || defined(__WXMAC__ )
260 #if wxMBFILES && wxUSE_UNICODE
261 #define wxFNCONV(name) wxConvFileName->cWX2MB(name)
262 #define wxFNSTRINGCAST wxMBSTRINGCAST
264 #if defined( __WXOSX__ ) && wxMBFILES
265 #define wxFNCONV(name) wxConvFileName->cWC2MB( wxConvLocal.cWX2WC(name) )
267 #define wxFNCONV(name) name
269 #define wxFNSTRINGCAST WXSTRINGCAST
272 #else // !wxUSE_WCHAR_T
274 // ----------------------------------------------------------------------------
275 // stand-ins in absence of wchar_t
276 // ----------------------------------------------------------------------------
278 class WXDLLIMPEXP_BASE wxMBConv
281 const char* cMB2WX(const char *psz
) const { return psz
; }
282 const char* cWX2MB(const char *psz
) const { return psz
; }
285 #define wxConvFile wxConvLocal
287 extern WXDLLIMPEXP_DATA_BASE(wxMBConv
) wxConvLibc
,
291 extern WXDLLIMPEXP_DATA_BASE(wxMBConv
*) wxConvCurrent
;
293 #define wxFNCONV(name) name
294 #define wxFNSTRINGCAST WXSTRINGCAST
299 // ----------------------------------------------------------------------------
300 // macros for the most common conversions
301 // ----------------------------------------------------------------------------
304 #define wxConvertWX2MB(s) wxConvCurrent->cWX2MB(s)
305 #define wxConvertMB2WX(s) wxConvCurrent->cMB2WX(s)
307 // no conversions to do
308 #define wxConvertWX2MB(s) (s)
309 #define wxConvertMB2WX(s) (s)
310 #endif // Unicode/ANSI