]> git.saurik.com Git - wxWidgets.git/blame - include/wx/strconv.h
clear up wxStringBuffer docs a bit
[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
12028905 15#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
6001e347
RR
16 #pragma interface "strconv.h"
17#endif
18
19#include "wx/defs.h"
20#include "wx/wxchar.h"
21#include "wx/buffer.h"
22
7db39dd6
CE
23#ifdef __DIGITALMARS__
24#include "typeinfo.h"
25#endif
26
9dea36ef
DW
27#if defined(__VISAGECPP__) && __IBMCPP__ >= 400
28# undef __BSEXCPT__
29#endif
dccce9ea 30
6001e347
RR
31#include <stdlib.h>
32
33#if wxUSE_WCHAR_T
34
e90c1d2a 35// ----------------------------------------------------------------------------
bde4baac 36// wxMBConv (abstract base class for conversions)
e90c1d2a 37// ----------------------------------------------------------------------------
6001e347 38
bddd7a8d 39class WXDLLIMPEXP_BASE wxMBConv
6001e347
RR
40{
41public:
e90c1d2a 42 // the actual conversion takes place here
bde4baac 43 //
e4e3bbb4 44 // note that outputSize is the size of the output buffer, not the length of input
75736a9c
DS
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;
e90c1d2a 48
bde4baac 49 // MB <-> WC
e90c1d2a
VZ
50 const wxWCharBuffer cMB2WC(const char *psz) const;
51 const wxCharBuffer cWC2MB(const wchar_t *psz) const;
6001e347 52
f5fb6871
RN
53 // MB <-> WC for strings with embedded null characters
54 //
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;
59
bde4baac 60 // convenience functions for converting MB or WC to/from wxWin default
6001e347 61#if wxUSE_UNICODE
e90c1d2a
VZ
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; }
f6bcfd97 65 const wchar_t* cWX2WC(const wchar_t *psz) const { return psz; }
e90c1d2a
VZ
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
2b5f62a0
VZ
72
73 // virtual dtor for any base class
e4a4a50b 74 virtual ~wxMBConv();
6001e347
RR
75};
76
bde4baac
VZ
77// ----------------------------------------------------------------------------
78// wxMBConvLibc uses standard mbstowcs() and wcstombs() functions for
79// conversion (hence it depends on the current locale)
80// ----------------------------------------------------------------------------
81
82class WXDLLIMPEXP_BASE wxMBConvLibc : public wxMBConv
83{
84public:
75736a9c
DS
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;
bde4baac
VZ
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
92WXDLLIMPEXP_DATA_BASE(extern wxMBConv&) wxConvLibc;
6001e347 93
e90c1d2a 94// ----------------------------------------------------------------------------
6001e347 95// wxMBConvUTF7 (for conversion using UTF7 encoding)
e90c1d2a 96// ----------------------------------------------------------------------------
6001e347 97
bddd7a8d 98class WXDLLIMPEXP_BASE wxMBConvUTF7 : public wxMBConv
6001e347
RR
99{
100public:
75736a9c
DS
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;
6001e347
RR
103};
104
bde4baac 105WXDLLIMPEXP_DATA_BASE(extern wxMBConvUTF7&) wxConvUTF7;
6001e347 106
e90c1d2a 107// ----------------------------------------------------------------------------
6001e347 108// wxMBConvUTF8 (for conversion using UTF8 encoding)
e90c1d2a 109// ----------------------------------------------------------------------------
6001e347 110
bddd7a8d 111class WXDLLIMPEXP_BASE wxMBConvUTF8 : public wxMBConv
6001e347
RR
112{
113public:
75736a9c
DS
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;
6001e347
RR
116};
117
bde4baac 118WXDLLIMPEXP_DATA_BASE(extern wxMBConvUTF8&) wxConvUTF8;
6001e347 119
e90c1d2a 120// ----------------------------------------------------------------------------
c91830cb
VZ
121// wxMBConvUTF16LE (for conversion using UTF16 Little Endian encoding)
122// ----------------------------------------------------------------------------
123
124class WXDLLIMPEXP_BASE wxMBConvUTF16LE : public wxMBConv
125{
126public:
75736a9c
DS
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;
c91830cb
VZ
129};
130
131// ----------------------------------------------------------------------------
132// wxMBConvUTF16BE (for conversion using UTF16 Big Endian encoding)
133// ----------------------------------------------------------------------------
134
135class WXDLLIMPEXP_BASE wxMBConvUTF16BE : public wxMBConv
136{
137public:
75736a9c
DS
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;
c91830cb
VZ
140};
141
142// ----------------------------------------------------------------------------
143// wxMBConvUCS4LE (for conversion using UTF32 Little Endian encoding)
144// ----------------------------------------------------------------------------
145
146class WXDLLIMPEXP_BASE wxMBConvUTF32LE : public wxMBConv
147{
148public:
75736a9c
DS
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;
c91830cb
VZ
151};
152
153// ----------------------------------------------------------------------------
154// wxMBConvUCS4BE (for conversion using UTF32 Big Endian encoding)
155// ----------------------------------------------------------------------------
156
157class WXDLLIMPEXP_BASE wxMBConvUTF32BE : public wxMBConv
158{
159public:
75736a9c
DS
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;
c91830cb
VZ
162};
163
164// ----------------------------------------------------------------------------
e90c1d2a
VZ
165// wxCSConv (for conversion based on loadable char sets)
166// ----------------------------------------------------------------------------
6001e347 167
8b04d4c4
VZ
168#include "wx/fontenc.h"
169
bddd7a8d 170class WXDLLIMPEXP_BASE wxCSConv : public wxMBConv
6001e347 171{
6001e347 172public:
e95354ec
VZ
173 // we can be created either from charset name or from an encoding constant
174 // but we can't have both at once
e90c1d2a 175 wxCSConv(const wxChar *charset);
8b04d4c4 176 wxCSConv(wxFontEncoding encoding);
e95354ec 177
54380f29 178 wxCSConv(const wxCSConv& conv);
e90c1d2a
VZ
179 virtual ~wxCSConv();
180
54380f29 181 wxCSConv& operator=(const wxCSConv& conv);
2b5f62a0 182
75736a9c
DS
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;
e90c1d2a 185
65e50848
JS
186 void Clear() ;
187
e90c1d2a 188private:
8b04d4c4
VZ
189 // common part of all ctors
190 void Init();
191
e95354ec
VZ
192 // creates m_convReal if necessary
193 void CreateConvIfNeeded() const;
194
195 // do create m_convReal (unconditionally)
196 wxMBConv *DoCreate() const;
197
bda3d86a
VZ
198 // set the name (may be only called when m_name == NULL), makes copy of
199 // charset string
e90c1d2a
VZ
200 void SetName(const wxChar *charset);
201
e95354ec 202
dccce9ea
VZ
203 // note that we can't use wxString here because of compilation
204 // dependencies: we're included from wx/string.h
e90c1d2a 205 wxChar *m_name;
8b04d4c4 206 wxFontEncoding m_encoding;
e95354ec
VZ
207
208 // use CreateConvIfNeeded() before accessing m_convReal!
209 wxMBConv *m_convReal;
e90c1d2a 210 bool m_deferred;
6001e347
RR
211};
212
b1ac3b56 213#define wxConvFile wxConvLocal
bde4baac
VZ
214WXDLLIMPEXP_DATA_BASE(extern wxCSConv&) wxConvLocal;
215WXDLLIMPEXP_DATA_BASE(extern wxCSConv&) wxConvISO8859_1;
bddd7a8d 216WXDLLIMPEXP_DATA_BASE(extern wxMBConv *) wxConvCurrent;
6001e347 217
e95354ec
VZ
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
e90c1d2a 230// ----------------------------------------------------------------------------
6001e347 231// filename conversion macros
e90c1d2a 232// ----------------------------------------------------------------------------
6001e347
RR
233
234// filenames are multibyte on Unix and probably widechar on Windows?
c4e41ce3 235#if defined(__UNIX__) || defined(__BORLANDC__) || defined(__WXMAC__ )
e90c1d2a 236 #define wxMBFILES 1
6001e347 237#else
e90c1d2a 238 #define wxMBFILES 0
6001e347
RR
239#endif
240
80df4d31 241#if wxMBFILES && wxUSE_UNICODE
e90c1d2a
VZ
242 #define wxFNCONV(name) wxConvFile.cWX2MB(name)
243 #define wxFNSTRINGCAST wxMBSTRINGCAST
6001e347 244#else
e90c1d2a
VZ
245 #define wxFNCONV(name) name
246 #define wxFNSTRINGCAST WXSTRINGCAST
6001e347
RR
247#endif
248
249#else
250 // !wxUSE_WCHAR_T
251
e90c1d2a 252// ----------------------------------------------------------------------------
6001e347 253// stand-ins in absence of wchar_t
e90c1d2a 254// ----------------------------------------------------------------------------
6001e347 255
bddd7a8d 256class WXDLLIMPEXP_BASE wxMBConv
6001e347
RR
257{
258public:
e90c1d2a
VZ
259 const char* cMB2WX(const char *psz) const { return psz; }
260 const char* cWX2MB(const char *psz) const { return psz; }
6001e347 261};
e90c1d2a 262
bde4baac
VZ
263#define wxConvFile wxConvLocal
264
8b04d4c4 265WXDLLIMPEXP_DATA_BASE(extern wxMBConv) wxConvLibc,
8b04d4c4
VZ
266 wxConvLocal,
267 wxConvISO8859_1,
268 wxConvUTF8;
bddd7a8d 269WXDLLIMPEXP_DATA_BASE(extern wxMBConv *) wxConvCurrent;
6001e347
RR
270
271#define wxFNCONV(name) name
e90c1d2a 272#define wxFNSTRINGCAST WXSTRINGCAST
6001e347
RR
273
274#endif
275 // wxUSE_WCHAR_T
276
e90c1d2a
VZ
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
6001e347
RR
291 // _WX_WXSTRCONVH__
292