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 n is the size of the output buffer, not the length of input
45 // (which is always supposed to be NUL-terminated)
46 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const = 0;
47 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const = 0;
50 const wxWCharBuffer
cMB2WC(const char *psz
) const;
51 const wxCharBuffer
cWC2MB(const wchar_t *psz
) const;
53 // convenience functions for converting MB or WC to/from wxWin default
55 const wxWCharBuffer
cMB2WX(const char *psz
) const { return cMB2WC(psz
); }
56 const wxCharBuffer
cWX2MB(const wchar_t *psz
) const { return cWC2MB(psz
); }
57 const wchar_t* cWC2WX(const wchar_t *psz
) const { return psz
; }
58 const wchar_t* cWX2WC(const wchar_t *psz
) const { return psz
; }
60 const char* cMB2WX(const char *psz
) const { return psz
; }
61 const char* cWX2MB(const char *psz
) const { return psz
; }
62 const wxCharBuffer
cWC2WX(const wchar_t *psz
) const { return cWC2MB(psz
); }
63 const wxWCharBuffer
cWX2WC(const char *psz
) const { return cMB2WC(psz
); }
64 #endif // Unicode/ANSI
66 // virtual dtor for any base class
70 // ----------------------------------------------------------------------------
71 // wxMBConvLibc uses standard mbstowcs() and wcstombs() functions for
72 // conversion (hence it depends on the current locale)
73 // ----------------------------------------------------------------------------
75 class WXDLLIMPEXP_BASE wxMBConvLibc
: public wxMBConv
78 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const;
79 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const;
82 // not very accurately named because it is not necessarily of type wxMBConvLibc
83 // (but the name can't eb changed because of backwards compatibility) default
85 WXDLLIMPEXP_DATA_BASE(extern wxMBConv
&) wxConvLibc
;
87 // ----------------------------------------------------------------------------
88 // wxMBConvUTF7 (for conversion using UTF7 encoding)
89 // ----------------------------------------------------------------------------
91 class WXDLLIMPEXP_BASE wxMBConvUTF7
: public wxMBConv
94 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const;
95 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const;
98 WXDLLIMPEXP_DATA_BASE(extern wxMBConvUTF7
&) wxConvUTF7
;
100 // ----------------------------------------------------------------------------
101 // wxMBConvUTF8 (for conversion using UTF8 encoding)
102 // ----------------------------------------------------------------------------
104 class WXDLLIMPEXP_BASE wxMBConvUTF8
: public wxMBConv
107 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const;
108 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const;
111 WXDLLIMPEXP_DATA_BASE(extern wxMBConvUTF8
&) wxConvUTF8
;
113 // ----------------------------------------------------------------------------
114 // wxMBConvUTF16LE (for conversion using UTF16 Little Endian encoding)
115 // ----------------------------------------------------------------------------
117 class WXDLLIMPEXP_BASE wxMBConvUTF16LE
: public wxMBConv
120 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const;
121 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const;
124 // ----------------------------------------------------------------------------
125 // wxMBConvUTF16BE (for conversion using UTF16 Big Endian encoding)
126 // ----------------------------------------------------------------------------
128 class WXDLLIMPEXP_BASE wxMBConvUTF16BE
: public wxMBConv
131 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const;
132 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const;
135 // ----------------------------------------------------------------------------
136 // wxMBConvUCS4LE (for conversion using UTF32 Little Endian encoding)
137 // ----------------------------------------------------------------------------
139 class WXDLLIMPEXP_BASE wxMBConvUTF32LE
: public wxMBConv
142 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const;
143 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const;
146 // ----------------------------------------------------------------------------
147 // wxMBConvUCS4BE (for conversion using UTF32 Big Endian encoding)
148 // ----------------------------------------------------------------------------
150 class WXDLLIMPEXP_BASE wxMBConvUTF32BE
: public wxMBConv
153 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const;
154 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const;
157 // ----------------------------------------------------------------------------
158 // wxCSConv (for conversion based on loadable char sets)
159 // ----------------------------------------------------------------------------
161 #include "wx/fontenc.h"
163 class WXDLLIMPEXP_BASE wxCSConv
: public wxMBConv
166 // we can be created either from charset name or from an encoding constant
167 // but we can't have both at once
168 wxCSConv(const wxChar
*charset
);
169 wxCSConv(wxFontEncoding encoding
);
171 wxCSConv(const wxCSConv
& conv
);
174 wxCSConv
& operator=(const wxCSConv
& conv
);
176 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const;
177 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const;
182 // common part of all ctors
185 // creates m_convReal if necessary
186 void CreateConvIfNeeded() const;
188 // do create m_convReal (unconditionally)
189 wxMBConv
*DoCreate() const;
191 // set the name (may be only called when m_name == NULL), makes copy of
193 void SetName(const wxChar
*charset
);
196 // note that we can't use wxString here because of compilation
197 // dependencies: we're included from wx/string.h
199 wxFontEncoding m_encoding
;
201 // use CreateConvIfNeeded() before accessing m_convReal!
202 wxMBConv
*m_convReal
;
206 #define wxConvFile wxConvLocal
207 WXDLLIMPEXP_DATA_BASE(extern wxCSConv
&) wxConvLocal
;
208 WXDLLIMPEXP_DATA_BASE(extern wxCSConv
&) wxConvISO8859_1
;
209 WXDLLIMPEXP_DATA_BASE(extern wxMBConv
*) wxConvCurrent
;
211 // ----------------------------------------------------------------------------
212 // endianness-dependent conversions
213 // ----------------------------------------------------------------------------
215 #ifdef WORDS_BIGENDIAN
216 typedef wxMBConvUTF16BE wxMBConvUTF16
;
217 typedef wxMBConvUTF32BE wxMBConvUTF32
;
219 typedef wxMBConvUTF16LE wxMBConvUTF16
;
220 typedef wxMBConvUTF32LE wxMBConvUTF32
;
223 // ----------------------------------------------------------------------------
224 // filename conversion macros
225 // ----------------------------------------------------------------------------
227 // filenames are multibyte on Unix and probably widechar on Windows?
228 #if defined(__UNIX__) || defined(__BORLANDC__) || defined(__WXMAC__ )
234 #if wxMBFILES && wxUSE_UNICODE
235 #define wxFNCONV(name) wxConvFile.cWX2MB(name)
236 #define wxFNSTRINGCAST wxMBSTRINGCAST
238 #define wxFNCONV(name) name
239 #define wxFNSTRINGCAST WXSTRINGCAST
245 // ----------------------------------------------------------------------------
246 // stand-ins in absence of wchar_t
247 // ----------------------------------------------------------------------------
249 class WXDLLIMPEXP_BASE wxMBConv
252 const char* cMB2WX(const char *psz
) const { return psz
; }
253 const char* cWX2MB(const char *psz
) const { return psz
; }
256 #define wxConvFile wxConvLocal
258 WXDLLIMPEXP_DATA_BASE(extern wxMBConv
) wxConvLibc
,
262 WXDLLIMPEXP_DATA_BASE(extern wxMBConv
*) wxConvCurrent
;
264 #define wxFNCONV(name) name
265 #define wxFNSTRINGCAST WXSTRINGCAST
270 // ----------------------------------------------------------------------------
271 // macros for the most common conversions
272 // ----------------------------------------------------------------------------
275 #define wxConvertWX2MB(s) wxConvCurrent->cWX2MB(s)
276 #define wxConvertMB2WX(s) wxConvCurrent->cMB2WX(s)
278 // no conversions to do
279 #define wxConvertWX2MB(s) (s)
280 #define wxConvertMB2WX(s) (s)
281 #endif // Unicode/ANSI