implemented explicit copy constructor and assignement operator
[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 license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_WXSTRCONVH__
13 #define _WX_WXSTRCONVH__
14
15 #ifdef __GNUG__
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 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
24 # undef __BSEXCPT__
25 #endif
26
27 #include <stdlib.h>
28
29 #if wxUSE_WCHAR_T
30
31 // ----------------------------------------------------------------------------
32 // wxMBConv (base class for conversions, using libc conversion itself)
33 // ----------------------------------------------------------------------------
34
35 class WXDLLEXPORT wxMBConv
36 {
37 public:
38 // the actual conversion takes place here
39 virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) const;
40 virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) const;
41
42 // No longer inline since BC++ complains.
43 const wxWCharBuffer cMB2WC(const char *psz) const;
44 const wxCharBuffer cWC2MB(const wchar_t *psz) const;
45
46 #if wxUSE_UNICODE
47 const wxWCharBuffer cMB2WX(const char *psz) const { return cMB2WC(psz); }
48 const wxCharBuffer cWX2MB(const wchar_t *psz) const { return cWC2MB(psz); }
49 const wchar_t* cWC2WX(const wchar_t *psz) const { return psz; }
50 const wchar_t* cWX2WC(const wchar_t *psz) const { return psz; }
51 #else // ANSI
52 const char* cMB2WX(const char *psz) const { return psz; }
53 const char* cWX2MB(const char *psz) const { return psz; }
54 const wxCharBuffer cWC2WX(const wchar_t *psz) const { return cWC2MB(psz); }
55 const wxWCharBuffer cWX2WC(const char *psz) const { return cMB2WC(psz); }
56 #endif // Unicode/ANSI
57 };
58
59 WXDLLEXPORT_DATA(extern wxMBConv) wxConvLibc;
60
61 // ----------------------------------------------------------------------------
62 // wxMBConvFile (for conversion to filenames)
63 // ----------------------------------------------------------------------------
64
65 class WXDLLEXPORT wxMBConvFile : public wxMBConv
66 {
67 public:
68 virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) const;
69 virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) const;
70 };
71
72 WXDLLEXPORT_DATA(extern wxMBConvFile) wxConvFile;
73
74 // ----------------------------------------------------------------------------
75 // wxMBConvUTF7 (for conversion using UTF7 encoding)
76 // ----------------------------------------------------------------------------
77
78 class WXDLLEXPORT wxMBConvUTF7 : public wxMBConv
79 {
80 public:
81 virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) const;
82 virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) const;
83 };
84
85 WXDLLEXPORT_DATA(extern wxMBConvUTF7) wxConvUTF7;
86
87 // ----------------------------------------------------------------------------
88 // wxMBConvUTF8 (for conversion using UTF8 encoding)
89 // ----------------------------------------------------------------------------
90
91 class WXDLLEXPORT wxMBConvUTF8 : public wxMBConv
92 {
93 public:
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;
96 };
97
98 WXDLLEXPORT_DATA(extern wxMBConvUTF8) wxConvUTF8;
99
100 #ifdef __WXGTK12__
101
102 // ----------------------------------------------------------------------------
103 // wxMBConvUTF8 (for conversion using GDK's internal converions)
104 // ----------------------------------------------------------------------------
105
106 class WXDLLEXPORT wxMBConvGdk : public wxMBConv
107 {
108 public:
109 virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) const;
110 virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) const;
111 };
112
113 WXDLLEXPORT_DATA(extern wxMBConvGdk) wxConvGdk;
114
115 #endif // wxGTK 1.2
116
117 // ----------------------------------------------------------------------------
118 // wxCSConv (for conversion based on loadable char sets)
119 // ----------------------------------------------------------------------------
120
121 class WXDLLEXPORT wxCharacterSet;
122
123 class WXDLLEXPORT wxCSConv : public wxMBConv
124 {
125 public:
126 wxCSConv(const wxChar *charset);
127 wxCSConv(const wxCSConv& conv);
128 virtual ~wxCSConv();
129
130 wxCSConv& operator=(const wxCSConv& conv);
131
132 void LoadNow();
133
134 virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) const;
135 virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) const;
136
137 void Clear() ;
138
139 private:
140 void SetName(const wxChar *charset);
141
142 // note that we can't use wxString here because of compilation
143 // dependencies: we're included from wx/string.h
144 wxChar *m_name;
145 wxCharacterSet *m_cset;
146 bool m_deferred;
147 };
148
149 WXDLLEXPORT_DATA(extern wxCSConv) wxConvLocal;
150 WXDLLEXPORT_DATA(extern wxMBConv *) wxConvCurrent;
151
152 // ----------------------------------------------------------------------------
153 // filename conversion macros
154 // ----------------------------------------------------------------------------
155
156 // filenames are multibyte on Unix and probably widechar on Windows?
157 #if defined(__UNIX__) || defined(__BORLANDC__)
158 #define wxMBFILES 1
159 #else
160 #define wxMBFILES 0
161 #endif
162
163 #if wxMBFILES && wxUSE_UNICODE
164 #define wxFNCONV(name) wxConvFile.cWX2MB(name)
165 #define wxFNSTRINGCAST wxMBSTRINGCAST
166 #else
167 #define wxFNCONV(name) name
168 #define wxFNSTRINGCAST WXSTRINGCAST
169 #endif
170
171 #else
172 // !wxUSE_WCHAR_T
173
174 // ----------------------------------------------------------------------------
175 // stand-ins in absence of wchar_t
176 // ----------------------------------------------------------------------------
177
178 class WXDLLEXPORT wxMBConv
179 {
180 public:
181 const char* cMB2WX(const char *psz) const { return psz; }
182 const char* cWX2MB(const char *psz) const { return psz; }
183 };
184
185 WXDLLEXPORT_DATA(extern wxMBConv) wxConvLibc, wxConvFile;
186 WXDLLEXPORT_DATA(extern wxMBConv *) wxConvCurrent;
187
188 #define wxFNCONV(name) name
189 #define wxFNSTRINGCAST WXSTRINGCAST
190
191 #endif
192 // wxUSE_WCHAR_T
193
194 // ----------------------------------------------------------------------------
195 // macros for the most common conversions
196 // ----------------------------------------------------------------------------
197
198 #if wxUSE_UNICODE
199 #define wxConvertWX2MB(s) wxConvCurrent->cWX2MB(s)
200 #define wxConvertMB2WX(s) wxConvCurrent->cMB2WX(s)
201 #else // ANSI
202 // no conversions to do
203 #define wxConvertWX2MB(s) (s)
204 #define wxConvertMB2WX(s) (s)
205 #endif // Unicode/ANSI
206
207 #endif
208 // _WX_WXSTRCONVH__
209