]> git.saurik.com Git - wxWidgets.git/blame - include/wx/strconv.h
properly NUL-terminate the output in wxMBConvUTF16swap::WC2MB()
[wxWidgets.git] / include / wx / strconv.h
CommitLineData
6001e347
RR
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
65571936 9// Licence: wxWindows licence
6001e347
RR
10///////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_WXSTRCONVH__
13#define _WX_WXSTRCONVH__
14
6001e347
RR
15#include "wx/defs.h"
16#include "wx/wxchar.h"
17#include "wx/buffer.h"
18
7db39dd6
CE
19#ifdef __DIGITALMARS__
20#include "typeinfo.h"
21#endif
22
9dea36ef
DW
23#if defined(__VISAGECPP__) && __IBMCPP__ >= 400
24# undef __BSEXCPT__
25#endif
dccce9ea 26
6001e347
RR
27#include <stdlib.h>
28
29#if wxUSE_WCHAR_T
30
483b0434
VZ
31// the error value returned by wxMBConv methods
32#define wxCONV_FAILED ((size_t)-1)
33
e90c1d2a 34// ----------------------------------------------------------------------------
bde4baac 35// wxMBConv (abstract base class for conversions)
e90c1d2a 36// ----------------------------------------------------------------------------
6001e347 37
509da451
VZ
38// When deriving a new class from wxMBConv you must reimplement ToWChar() and
39// FromWChar() methods which are not pure virtual only for historical reasons,
40// don't let the fact that the existing classes implement MB2WC/WC2MB() instead
41// confuse you.
42//
43// And you might need to override GetMBNulLen() as well.
bddd7a8d 44class WXDLLIMPEXP_BASE wxMBConv
6001e347
RR
45{
46public:
483b0434
VZ
47 // The functions doing actual conversion from/to narrow to/from wide
48 // character strings.
bde4baac 49 //
483b0434
VZ
50 // On success, the return value is the length (i.e. the number of
51 // characters, not bytes) of the converted string including any trailing
52 // L'\0' or (possibly multiple) '\0'(s). If the conversion fails or if
53 // there is not enough space for everything, including the trailing NUL
54 // character(s), in the output buffer, (size_t)-1 is returned.
55 //
56 // In the special case when dstLen is 0 (outputBuf may be NULL then) the
57 // return value is the length of the needed buffer but nothing happens
58 // otherwise. If srcLen is -1, the entire string, up to and including the
59 // trailing NUL(s), is converted, otherwise exactly srcLen bytes are.
60 //
61 // Typical usage:
62 //
63 // size_t dstLen = conv.ToWChar(NULL, 0, src);
64 // if ( dstLen != wxCONV_FAILED )
65 // ... handle error ...
66 // wchar_t *wbuf = new wchar_t[dstLen];
67 // conv.ToWChar(wbuf, dstLen, src);
68 //
69 virtual size_t ToWChar(wchar_t *dst, size_t dstLen,
70 const char *src, size_t srcLen = -1) const;
71
72 virtual size_t FromWChar(char *dst, size_t dstLen,
73 const wchar_t *src, size_t srcLen = -1) const;
74
e90c1d2a 75
483b0434
VZ
76 // Convenience functions for translating NUL-terminated strings: returns
77 // the buffer containing the converted string or NULL pointer if the
78 // conversion failed.
eec47cc6
VZ
79 const wxWCharBuffer cMB2WC(const char *in) const;
80 const wxCharBuffer cWC2MB(const wchar_t *in) const;
6001e347 81
483b0434
VZ
82 // Convenience functions for converting strings which may contain embedded
83 // NULs and don't have to be NUL-terminated.
f5fb6871 84 //
eec47cc6
VZ
85 // inLen is the length of the buffer including trailing NUL if any: if the
86 // last 4 bytes of the buffer are all NULs, these functions are more
87 // efficient as they avoid copying the string, but otherwise a copy is made
88 // internally which could be quite bad for (very) long strings.
89 //
90 // outLen receives, if not NULL, the length of the converted string or 0 if
91 // the conversion failed (returning 0 and not -1 in this case makes it
92 // difficult to distinguish between failed conversion and empty input but
93 // this is done for backwards compatibility)
94 const wxWCharBuffer
95 cMB2WC(const char *in, size_t inLen, size_t *outLen) const;
96 const wxCharBuffer
97 cWC2MB(const wchar_t *in, size_t inLen, size_t *outLen) const;
f5fb6871 98
bde4baac 99 // convenience functions for converting MB or WC to/from wxWin default
6001e347 100#if wxUSE_UNICODE
e90c1d2a
VZ
101 const wxWCharBuffer cMB2WX(const char *psz) const { return cMB2WC(psz); }
102 const wxCharBuffer cWX2MB(const wchar_t *psz) const { return cWC2MB(psz); }
103 const wchar_t* cWC2WX(const wchar_t *psz) const { return psz; }
f6bcfd97 104 const wchar_t* cWX2WC(const wchar_t *psz) const { return psz; }
e90c1d2a
VZ
105#else // ANSI
106 const char* cMB2WX(const char *psz) const { return psz; }
107 const char* cWX2MB(const char *psz) const { return psz; }
108 const wxCharBuffer cWC2WX(const wchar_t *psz) const { return cWC2MB(psz); }
109 const wxWCharBuffer cWX2WC(const char *psz) const { return cMB2WC(psz); }
110#endif // Unicode/ANSI
2b5f62a0 111
c1464d9d
VZ
112 // this function is used in the implementation of cMB2WC() to distinguish
113 // between the following cases:
eec47cc6 114 //
c1464d9d
VZ
115 // a) var width encoding with strings terminated by a single NUL
116 // (usual multibyte encodings): return 1 in this case
117 // b) fixed width encoding with 2 bytes/char and so terminated by
118 // 2 NULs (UTF-16/UCS-2 and variants): return 2 in this case
119 // c) fixed width encoding with 4 bytes/char and so terminated by
120 // 4 NULs (UTF-32/UCS-4 and variants): return 4 in this case
121 //
122 // anything else is not supported currently and -1 should be returned
7ef3ab50
VZ
123 virtual size_t GetMBNulLen() const { return 1; }
124
483b0434
VZ
125 // return the maximal value currently returned by GetMBNulLen() for any
126 // encoding
127 static size_t GetMaxMBNulLen() { return 4 /* for UTF-32 */; }
128
129
130 // The old conversion functions. The existing classes currently mostly
131 // implement these ones but we're in transition to using To/FromWChar()
132 // instead and any new classes should implement just the new functions.
133 // For now, however, we provide default implementation of To/FromWChar() in
134 // this base class in terms of MB2WC/WC2MB() to avoid having to rewrite all
135 // the conversions at once.
136 //
137 // On success, the return value is the length (i.e. the number of
138 // characters, not bytes) not counting the trailing NUL(s) of the converted
139 // string. On failure, (size_t)-1 is returned. In the special case when
140 // outputBuf is NULL the return value is the same one but nothing is
141 // written to the buffer.
142 //
143 // Note that outLen is the length of the output buffer, not the length of
144 // the input (which is always supposed to be terminated by one or more
145 // NULs, as appropriate for the encoding)!
509da451
VZ
146 virtual size_t MB2WC(wchar_t *out, const char *in, size_t outLen) const;
147 virtual size_t WC2MB(char *out, const wchar_t *in, size_t outLen) const;
483b0434
VZ
148
149
7ef3ab50
VZ
150 // virtual dtor for any base class
151 virtual ~wxMBConv();
6001e347
RR
152};
153
bde4baac
VZ
154// ----------------------------------------------------------------------------
155// wxMBConvLibc uses standard mbstowcs() and wcstombs() functions for
156// conversion (hence it depends on the current locale)
157// ----------------------------------------------------------------------------
158
159class WXDLLIMPEXP_BASE wxMBConvLibc : public wxMBConv
160{
161public:
75736a9c
DS
162 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
163 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
bde4baac
VZ
164};
165
5576edf8
RR
166#ifdef __UNIX__
167
168// ----------------------------------------------------------------------------
169// wxConvBrokenFileNames is made for Unix in Unicode mode when
170// files are accidentally written in an encoding which is not
171// the system encoding. Typically, the system encoding will be
172// UTF8 but there might be files stored in ISO8859-1 on disk.
173// ----------------------------------------------------------------------------
174
175class WXDLLIMPEXP_BASE wxConvBrokenFileNames : public wxMBConv
176{
177public:
845905d5 178 wxConvBrokenFileNames(const wxChar *charset);
5576edf8
RR
179 virtual ~wxConvBrokenFileNames() { delete m_conv; }
180
eec47cc6
VZ
181 virtual size_t MB2WC(wchar_t *out, const char *in, size_t outLen) const
182 {
183 return m_conv->MB2WC(out, in, outLen);
184 }
185
186 virtual size_t WC2MB(char *out, const wchar_t *in, size_t outLen) const
187 {
188 return m_conv->WC2MB(out, in, outLen);
189 }
5576edf8 190
7ef3ab50 191 virtual size_t GetMBNulLen() const
eec47cc6 192 {
22886fb3 193 // cast needed to call a private function
7ef3ab50 194 return m_conv->GetMBNulLen();
eec47cc6
VZ
195 }
196
7ef3ab50 197private:
5576edf8
RR
198 // the conversion object we forward to
199 wxMBConv *m_conv;
200};
201
eec47cc6 202#endif // __UNIX__
5576edf8 203
e90c1d2a 204// ----------------------------------------------------------------------------
6001e347 205// wxMBConvUTF7 (for conversion using UTF7 encoding)
e90c1d2a 206// ----------------------------------------------------------------------------
6001e347 207
bddd7a8d 208class WXDLLIMPEXP_BASE wxMBConvUTF7 : public wxMBConv
6001e347
RR
209{
210public:
75736a9c
DS
211 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
212 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
6001e347
RR
213};
214
e90c1d2a 215// ----------------------------------------------------------------------------
6001e347 216// wxMBConvUTF8 (for conversion using UTF8 encoding)
e90c1d2a 217// ----------------------------------------------------------------------------
6001e347 218
bddd7a8d 219class WXDLLIMPEXP_BASE wxMBConvUTF8 : public wxMBConv
6001e347
RR
220{
221public:
eec47cc6 222 enum {
ea8ce907
RR
223 MAP_INVALID_UTF8_NOT = 0,
224 MAP_INVALID_UTF8_TO_PUA = 1,
225 MAP_INVALID_UTF8_TO_OCTAL = 2
226 };
227
228 wxMBConvUTF8(int options = MAP_INVALID_UTF8_NOT) : m_options(options) { }
75736a9c
DS
229 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
230 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
eec47cc6 231
ea8ce907
RR
232private:
233 int m_options;
6001e347
RR
234};
235
eec47cc6
VZ
236// ----------------------------------------------------------------------------
237// wxMBConvUTF16Base: for both LE and BE variants
238// ----------------------------------------------------------------------------
239
240class WXDLLIMPEXP_BASE wxMBConvUTF16Base : public wxMBConv
241{
7ef3ab50
VZ
242public:
243 virtual size_t GetMBNulLen() const { return 2; }
eec47cc6
VZ
244};
245
e90c1d2a 246// ----------------------------------------------------------------------------
c91830cb
VZ
247// wxMBConvUTF16LE (for conversion using UTF16 Little Endian encoding)
248// ----------------------------------------------------------------------------
249
eec47cc6 250class WXDLLIMPEXP_BASE wxMBConvUTF16LE : public wxMBConvUTF16Base
c91830cb
VZ
251{
252public:
75736a9c
DS
253 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
254 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
c91830cb
VZ
255};
256
257// ----------------------------------------------------------------------------
258// wxMBConvUTF16BE (for conversion using UTF16 Big Endian encoding)
259// ----------------------------------------------------------------------------
260
eec47cc6 261class WXDLLIMPEXP_BASE wxMBConvUTF16BE : public wxMBConvUTF16Base
c91830cb
VZ
262{
263public:
75736a9c
DS
264 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
265 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
c91830cb
VZ
266};
267
eec47cc6
VZ
268// ----------------------------------------------------------------------------
269// wxMBConvUTF32Base: base class for both LE and BE variants
270// ----------------------------------------------------------------------------
271
272class WXDLLIMPEXP_BASE wxMBConvUTF32Base : public wxMBConv
273{
7ef3ab50
VZ
274public:
275 virtual size_t GetMBNulLen() const { return 4; }
eec47cc6
VZ
276};
277
c91830cb 278// ----------------------------------------------------------------------------
8b9e1f43 279// wxMBConvUTF32LE (for conversion using UTF32 Little Endian encoding)
c91830cb
VZ
280// ----------------------------------------------------------------------------
281
eec47cc6 282class WXDLLIMPEXP_BASE wxMBConvUTF32LE : public wxMBConvUTF32Base
c91830cb
VZ
283{
284public:
75736a9c
DS
285 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
286 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
c91830cb
VZ
287};
288
289// ----------------------------------------------------------------------------
8b9e1f43 290// wxMBConvUTF32BE (for conversion using UTF32 Big Endian encoding)
c91830cb
VZ
291// ----------------------------------------------------------------------------
292
eec47cc6 293class WXDLLIMPEXP_BASE wxMBConvUTF32BE : public wxMBConvUTF32Base
c91830cb
VZ
294{
295public:
75736a9c
DS
296 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
297 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
c91830cb
VZ
298};
299
300// ----------------------------------------------------------------------------
e90c1d2a
VZ
301// wxCSConv (for conversion based on loadable char sets)
302// ----------------------------------------------------------------------------
6001e347 303
8b04d4c4
VZ
304#include "wx/fontenc.h"
305
bddd7a8d 306class WXDLLIMPEXP_BASE wxCSConv : public wxMBConv
6001e347 307{
6001e347 308public:
e95354ec
VZ
309 // we can be created either from charset name or from an encoding constant
310 // but we can't have both at once
e90c1d2a 311 wxCSConv(const wxChar *charset);
8b04d4c4 312 wxCSConv(wxFontEncoding encoding);
e95354ec 313
54380f29 314 wxCSConv(const wxCSConv& conv);
e90c1d2a
VZ
315 virtual ~wxCSConv();
316
54380f29 317 wxCSConv& operator=(const wxCSConv& conv);
2b5f62a0 318
75736a9c
DS
319 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
320 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
7ef3ab50 321 virtual size_t GetMBNulLen() const;
e90c1d2a 322
65e50848
JS
323 void Clear() ;
324
e90c1d2a 325private:
8b04d4c4
VZ
326 // common part of all ctors
327 void Init();
328
e95354ec
VZ
329 // creates m_convReal if necessary
330 void CreateConvIfNeeded() const;
331
332 // do create m_convReal (unconditionally)
333 wxMBConv *DoCreate() const;
334
bda3d86a
VZ
335 // set the name (may be only called when m_name == NULL), makes copy of
336 // charset string
e90c1d2a
VZ
337 void SetName(const wxChar *charset);
338
e95354ec 339
dccce9ea
VZ
340 // note that we can't use wxString here because of compilation
341 // dependencies: we're included from wx/string.h
e90c1d2a 342 wxChar *m_name;
8b04d4c4 343 wxFontEncoding m_encoding;
e95354ec
VZ
344
345 // use CreateConvIfNeeded() before accessing m_convReal!
346 wxMBConv *m_convReal;
e90c1d2a 347 bool m_deferred;
6001e347
RR
348};
349
c3c1a9a9 350
f5a1953b
VZ
351// ----------------------------------------------------------------------------
352// declare predefined conversion objects
353// ----------------------------------------------------------------------------
d5c8817c 354
f5a1953b
VZ
355// conversion to be used with all standard functions affected by locale, e.g.
356// strtol(), strftime(), ...
357extern WXDLLIMPEXP_DATA_BASE(wxMBConv&) wxConvLibc;
358
359// conversion ISO-8859-1/UTF-7/UTF-8 <-> wchar_t
16cba29d 360extern WXDLLIMPEXP_DATA_BASE(wxCSConv&) wxConvISO8859_1;
f5a1953b
VZ
361extern WXDLLIMPEXP_DATA_BASE(wxMBConvUTF7&) wxConvUTF7;
362extern WXDLLIMPEXP_DATA_BASE(wxMBConvUTF8&) wxConvUTF8;
363
364// conversion used for the file names on the systems where they're not Unicode
365// (basically anything except Windows)
366//
367// this is used by all file functions, can be changed by the application
368//
369// by default UTF-8 under Mac OS X and wxConvLibc elsewhere (but it's not used
370// under Windows normally)
371extern WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvFileName;
372
373// backwards compatible define
374#define wxConvFile (*wxConvFileName)
375
376// the current conversion object, may be set to any conversion, is used by
377// default in a couple of places inside wx (initially same as wxConvLibc)
16cba29d 378extern WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvCurrent;
6001e347 379
f5a1953b
VZ
380// ???
381extern WXDLLIMPEXP_DATA_BASE(wxCSConv&) wxConvLocal;
382
383
e95354ec
VZ
384// ----------------------------------------------------------------------------
385// endianness-dependent conversions
386// ----------------------------------------------------------------------------
387
388#ifdef WORDS_BIGENDIAN
389 typedef wxMBConvUTF16BE wxMBConvUTF16;
390 typedef wxMBConvUTF32BE wxMBConvUTF32;
391#else
392 typedef wxMBConvUTF16LE wxMBConvUTF16;
393 typedef wxMBConvUTF32LE wxMBConvUTF32;
394#endif
395
e90c1d2a 396// ----------------------------------------------------------------------------
6001e347 397// filename conversion macros
e90c1d2a 398// ----------------------------------------------------------------------------
6001e347
RR
399
400// filenames are multibyte on Unix and probably widechar on Windows?
c4e41ce3 401#if defined(__UNIX__) || defined(__BORLANDC__) || defined(__WXMAC__ )
e90c1d2a 402 #define wxMBFILES 1
6001e347 403#else
e90c1d2a 404 #define wxMBFILES 0
6001e347
RR
405#endif
406
80df4d31 407#if wxMBFILES && wxUSE_UNICODE
f5a1953b 408 #define wxFNCONV(name) wxConvFileName->cWX2MB(name)
e90c1d2a 409 #define wxFNSTRINGCAST wxMBSTRINGCAST
d5c8817c
SC
410#else
411#if defined( __WXOSX__ ) && wxMBFILES
f5a1953b 412 #define wxFNCONV(name) wxConvFileName->cWC2MB( wxConvLocal.cWX2WC(name) )
6001e347 413#else
e90c1d2a 414 #define wxFNCONV(name) name
d5c8817c 415#endif
e90c1d2a 416 #define wxFNSTRINGCAST WXSTRINGCAST
6001e347
RR
417#endif
418
f5a1953b 419#else // !wxUSE_WCHAR_T
6001e347 420
e90c1d2a 421// ----------------------------------------------------------------------------
6001e347 422// stand-ins in absence of wchar_t
e90c1d2a 423// ----------------------------------------------------------------------------
6001e347 424
bddd7a8d 425class WXDLLIMPEXP_BASE wxMBConv
6001e347
RR
426{
427public:
e90c1d2a
VZ
428 const char* cMB2WX(const char *psz) const { return psz; }
429 const char* cWX2MB(const char *psz) const { return psz; }
6001e347 430};
e90c1d2a 431
bde4baac
VZ
432#define wxConvFile wxConvLocal
433
16cba29d 434extern WXDLLIMPEXP_DATA_BASE(wxMBConv) wxConvLibc,
8b04d4c4
VZ
435 wxConvLocal,
436 wxConvISO8859_1,
437 wxConvUTF8;
16cba29d 438extern WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvCurrent;
6001e347
RR
439
440#define wxFNCONV(name) name
e90c1d2a 441#define wxFNSTRINGCAST WXSTRINGCAST
6001e347
RR
442
443#endif
444 // wxUSE_WCHAR_T
445
e90c1d2a
VZ
446// ----------------------------------------------------------------------------
447// macros for the most common conversions
448// ----------------------------------------------------------------------------
449
450#if wxUSE_UNICODE
451 #define wxConvertWX2MB(s) wxConvCurrent->cWX2MB(s)
452 #define wxConvertMB2WX(s) wxConvCurrent->cMB2WX(s)
453#else // ANSI
454 // no conversions to do
455 #define wxConvertWX2MB(s) (s)
456 #define wxConvertMB2WX(s) (s)
457#endif // Unicode/ANSI
458
459#endif
6001e347
RR
460 // _WX_WXSTRCONVH__
461