]> git.saurik.com Git - wxWidgets.git/blob - include/wx/strconv.h
Moved print dialog data conversion code
[wxWidgets.git] / include / wx / strconv.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: strconv.h
3 // Purpose: conversion routines for char sets any Unicode
4 // Author: Robert Roebling, Ove Kaaven
5 // Modified by:
6 // Created: 29/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Ove Kaaven, Robert Roebling, Vadim Zeitlin
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_WXSTRCONVH__
13 #define _WX_WXSTRCONVH__
14
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "strconv.h"
17 #endif
18
19 #include "wx/defs.h"
20 #include "wx/wxchar.h"
21 #include "wx/buffer.h"
22
23 #ifdef __DIGITALMARS__
24 #include "typeinfo.h"
25 #endif
26
27 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
28 # undef __BSEXCPT__
29 #endif
30
31 #include <stdlib.h>
32
33 #if wxUSE_WCHAR_T
34
35 // ----------------------------------------------------------------------------
36 // wxMBConv (abstract base class for conversions)
37 // ----------------------------------------------------------------------------
38
39 class WXDLLIMPEXP_BASE wxMBConv
40 {
41 public:
42 // the actual conversion takes place here
43 //
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;
48
49 // actual conversion for strings with embedded null characters
50 //
51 // outputSize is the size of the output buffer
52 // pszLen is the length of the input string (including all but last null character)
53 size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize, size_t pszLen) const;
54 size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize, size_t pszLen) const;
55
56 // MB <-> WC
57 const wxWCharBuffer cMB2WC(const char *psz) const;
58 const wxCharBuffer cWC2MB(const wchar_t *psz) const;
59
60 // convenience functions for converting MB or WC to/from wxWin default
61 #if wxUSE_UNICODE
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; }
66 #else // ANSI
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
72
73 // virtual dtor for any base class
74 virtual ~wxMBConv();
75 };
76
77 // ----------------------------------------------------------------------------
78 // wxMBConvLibc uses standard mbstowcs() and wcstombs() functions for
79 // conversion (hence it depends on the current locale)
80 // ----------------------------------------------------------------------------
81
82 class WXDLLIMPEXP_BASE wxMBConvLibc : public wxMBConv
83 {
84 public:
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;
87 };
88
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
91 // conversion
92 WXDLLIMPEXP_DATA_BASE(extern wxMBConv&) wxConvLibc;
93
94 // ----------------------------------------------------------------------------
95 // wxMBConvUTF7 (for conversion using UTF7 encoding)
96 // ----------------------------------------------------------------------------
97
98 class WXDLLIMPEXP_BASE wxMBConvUTF7 : public wxMBConv
99 {
100 public:
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;
103 };
104
105 WXDLLIMPEXP_DATA_BASE(extern wxMBConvUTF7&) wxConvUTF7;
106
107 // ----------------------------------------------------------------------------
108 // wxMBConvUTF8 (for conversion using UTF8 encoding)
109 // ----------------------------------------------------------------------------
110
111 class WXDLLIMPEXP_BASE wxMBConvUTF8 : public wxMBConv
112 {
113 public:
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;
116 };
117
118 WXDLLIMPEXP_DATA_BASE(extern wxMBConvUTF8&) wxConvUTF8;
119
120 // ----------------------------------------------------------------------------
121 // wxMBConvUTF16LE (for conversion using UTF16 Little Endian encoding)
122 // ----------------------------------------------------------------------------
123
124 class WXDLLIMPEXP_BASE wxMBConvUTF16LE : public wxMBConv
125 {
126 public:
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;
129 };
130
131 // ----------------------------------------------------------------------------
132 // wxMBConvUTF16BE (for conversion using UTF16 Big Endian encoding)
133 // ----------------------------------------------------------------------------
134
135 class WXDLLIMPEXP_BASE wxMBConvUTF16BE : public wxMBConv
136 {
137 public:
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;
140 };
141
142 // ----------------------------------------------------------------------------
143 // wxMBConvUCS4LE (for conversion using UTF32 Little Endian encoding)
144 // ----------------------------------------------------------------------------
145
146 class WXDLLIMPEXP_BASE wxMBConvUTF32LE : public wxMBConv
147 {
148 public:
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;
151 };
152
153 // ----------------------------------------------------------------------------
154 // wxMBConvUCS4BE (for conversion using UTF32 Big Endian encoding)
155 // ----------------------------------------------------------------------------
156
157 class WXDLLIMPEXP_BASE wxMBConvUTF32BE : public wxMBConv
158 {
159 public:
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;
162 };
163
164 // ----------------------------------------------------------------------------
165 // wxCSConv (for conversion based on loadable char sets)
166 // ----------------------------------------------------------------------------
167
168 #include "wx/fontenc.h"
169
170 class WXDLLIMPEXP_BASE wxCSConv : public wxMBConv
171 {
172 public:
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);
177
178 wxCSConv(const wxCSConv& conv);
179 virtual ~wxCSConv();
180
181 wxCSConv& operator=(const wxCSConv& conv);
182
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;
185
186 void Clear() ;
187
188 private:
189 // common part of all ctors
190 void Init();
191
192 // creates m_convReal if necessary
193 void CreateConvIfNeeded() const;
194
195 // do create m_convReal (unconditionally)
196 wxMBConv *DoCreate() const;
197
198 // set the name (may be only called when m_name == NULL), makes copy of
199 // charset string
200 void SetName(const wxChar *charset);
201
202
203 // note that we can't use wxString here because of compilation
204 // dependencies: we're included from wx/string.h
205 wxChar *m_name;
206 wxFontEncoding m_encoding;
207
208 // use CreateConvIfNeeded() before accessing m_convReal!
209 wxMBConv *m_convReal;
210 bool m_deferred;
211 };
212
213 #define wxConvFile wxConvLocal
214 WXDLLIMPEXP_DATA_BASE(extern wxCSConv&) wxConvLocal;
215 WXDLLIMPEXP_DATA_BASE(extern wxCSConv&) wxConvISO8859_1;
216 WXDLLIMPEXP_DATA_BASE(extern wxMBConv *) wxConvCurrent;
217
218 // ----------------------------------------------------------------------------
219 // endianness-dependent conversions
220 // ----------------------------------------------------------------------------
221
222 #ifdef WORDS_BIGENDIAN
223 typedef wxMBConvUTF16BE wxMBConvUTF16;
224 typedef wxMBConvUTF32BE wxMBConvUTF32;
225 #else
226 typedef wxMBConvUTF16LE wxMBConvUTF16;
227 typedef wxMBConvUTF32LE wxMBConvUTF32;
228 #endif
229
230 // ----------------------------------------------------------------------------
231 // filename conversion macros
232 // ----------------------------------------------------------------------------
233
234 // filenames are multibyte on Unix and probably widechar on Windows?
235 #if defined(__UNIX__) || defined(__BORLANDC__) || defined(__WXMAC__ )
236 #define wxMBFILES 1
237 #else
238 #define wxMBFILES 0
239 #endif
240
241 #if wxMBFILES && wxUSE_UNICODE
242 #define wxFNCONV(name) wxConvFile.cWX2MB(name)
243 #define wxFNSTRINGCAST wxMBSTRINGCAST
244 #else
245 #define wxFNCONV(name) name
246 #define wxFNSTRINGCAST WXSTRINGCAST
247 #endif
248
249 #else
250 // !wxUSE_WCHAR_T
251
252 // ----------------------------------------------------------------------------
253 // stand-ins in absence of wchar_t
254 // ----------------------------------------------------------------------------
255
256 class WXDLLIMPEXP_BASE wxMBConv
257 {
258 public:
259 const char* cMB2WX(const char *psz) const { return psz; }
260 const char* cWX2MB(const char *psz) const { return psz; }
261 };
262
263 #define wxConvFile wxConvLocal
264
265 WXDLLIMPEXP_DATA_BASE(extern wxMBConv) wxConvLibc,
266 wxConvLocal,
267 wxConvISO8859_1,
268 wxConvUTF8;
269 WXDLLIMPEXP_DATA_BASE(extern wxMBConv *) wxConvCurrent;
270
271 #define wxFNCONV(name) name
272 #define wxFNSTRINGCAST WXSTRINGCAST
273
274 #endif
275 // wxUSE_WCHAR_T
276
277 // ----------------------------------------------------------------------------
278 // macros for the most common conversions
279 // ----------------------------------------------------------------------------
280
281 #if wxUSE_UNICODE
282 #define wxConvertWX2MB(s) wxConvCurrent->cWX2MB(s)
283 #define wxConvertMB2WX(s) wxConvCurrent->cMB2WX(s)
284 #else // ANSI
285 // no conversions to do
286 #define wxConvertWX2MB(s) (s)
287 #define wxConvertMB2WX(s) (s)
288 #endif // Unicode/ANSI
289
290 #endif
291 // _WX_WXSTRCONVH__
292